Skip to content

Commit

Permalink
Simplified actions
Browse files Browse the repository at this point in the history
  • Loading branch information
chronoDave committed Jul 18, 2024
1 parent a0ae77c commit 31b2611
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 141 deletions.
49 changes: 25 additions & 24 deletions src/app/lib/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,29 @@ export default class Library extends EventEmitter<LibraryEvents> {

private _all() {
return {
songs: this._db.song.select({}),
albums: this._db.album.select({}),
songs: this._db.song.select({})
.sort((a, b) => {
if (!a.label || !b.label) return 0;
if (a.label !== b.label) return a.label.localeCompare(b.label);
if (!a.year || !b.year) return 0;
if (a.year !== b.year) return a.year - b.year;
if (!a.album || !b.album) return 0;
if (a.album !== b.album) return a.album.localeCompare(b.album);
if (!a.track.no || !b.track.no) return 0;
return a.track.no - b.track.no;
}),
albums: this._db.album.select({})
.sort((a, b) => {
if (!a.label || !b.label) return 0;
if (a.label !== b.label) return a.label.localeCompare(b.label);
if (!a.year || !b.year) return 0;
return a.year - b.year;
}),
labels: this._db.label.select({})
.sort((a, b) => {
if (!a.label || !b.label) return 0;
return a.label.localeCompare(b.label);
})
};
}

Expand All @@ -59,14 +79,7 @@ export default class Library extends EventEmitter<LibraryEvents> {
.map(x => ({
_id: LeafDB.id(),
image: x[0].image,
songs: x
.sort((a, b) => {
if (a.disc.no && b.disc.no && a.disc.no !== b.disc.no) return a.disc.no - b.disc.no;
if (a.track.no && b.track.no && a.track.no !== b.track.no) return a.track.no - b.track.no;

return 1;
})
.map(song => song._id),
songs: x.map(song => song._id),
duration: x.reduce((acc, cur) => acc + (cur.duration ?? 0), 0),
album: x[0].album,
albumartist: x[0].albumartist,
Expand All @@ -85,20 +98,8 @@ export default class Library extends EventEmitter<LibraryEvents> {
const labels = this._db.label.insert(Array.from(group(albums, 'label').values())
.map(x => ({
_id: LeafDB.id(),
albums: x
.sort((a, b) => {
if (a.label && b.label && a.label !== b.label) return a.label.localeCompare(b.label);
if (a.albumartist && b.albumartist && a.albumartist !== b.albumartist) return a.albumartist.localeCompare(b.albumartist);
if (a.year && b.year && a.year !== b.year) return a.year - b.year;
if (a.album && b.album && a.album !== b.album) return a.album.localeCompare(b.album);

return 1;
})
.map(album => album._id),
songs: x
.sort((a, b) => (b.year ?? Number.MAX_SAFE_INTEGER) - (a.year ?? Number.MAX_SAFE_INTEGER))
.map(album => album.songs)
.flat(),
albums: x.map(album => album._id),
songs: x.map(album => album.songs).flat(),
label: x[0].label!,
duration: x.reduce((acc, cur) => acc + (cur.duration ?? 0), 0),
romaji: {
Expand Down
8 changes: 0 additions & 8 deletions src/renderer/windows/app/lib/sort/sortAlbums.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/renderer/windows/app/lib/sort/sortLabels.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/renderer/windows/app/lib/sort/sortSongs.ts

This file was deleted.

22 changes: 0 additions & 22 deletions src/renderer/windows/app/modules/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@ import * as forgo from 'forgo';

import Window from '@doombox/components/window/window';

import { fetchPath } from '../../state/actions/app.actions';
import { fetchCache } from '../../state/actions/cache.actions';
import { fetchLibrary } from '../../state/actions/library.actions';
import { fetchPlaylists } from '../../state/actions/playlist.actions';
import { setRouteApp } from '../../state/actions/route.actions';
import { fetchTheme } from '../../state/actions/theme.actions';
import { fetchUser } from '../../state/actions/user.actions';
import * as Route from '../../state/route';

import subscribe from './app.state';
import AppRouter from './appRouter/appRouter';

Expand All @@ -31,19 +22,6 @@ const App: Component<AppProps> = () => {
}
});

component.mount(async () => {
await Promise.all([
fetchLibrary(),
fetchTheme(),
fetchUser(),
fetchCache(),
fetchPlaylists(),
fetchPath()
]);

setRouteApp(Route.App.Home);
});

return component;
};

Expand Down
11 changes: 0 additions & 11 deletions src/renderer/windows/app/state/actions/app.actions.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/renderer/windows/app/state/actions/cache.actions.ts

This file was deleted.

20 changes: 3 additions & 17 deletions src/renderer/windows/app/state/actions/library.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,18 @@ import difference from '@doombox/lib/list/difference';
import shuffle from '@doombox/lib/list/shuffle';
import unique from '@doombox/lib/list/unique';

import sortAlbums from '../../lib/sort/sortAlbums';
import sortLabels from '../../lib/sort/sortLabels';
import sortSongs from '../../lib/sort/sortSongs';
import * as Route from '../route';
import { imageSelector } from '../selectors';
import store from '../store';

import { play } from './player.actions';

const dispatch = (library: Library) => store.set(produce(draft => {
draft.entities.song = new Map(library.songs
.sort(sortSongs)
.map(song => [song._id, song]));
draft.entities.album = new Map(library.albums
.sort(sortAlbums)
.map(album => [album._id, album]));
draft.entities.label = new Map(library.labels
.sort(sortLabels)
.map(label => [label._id, label]));
draft.entities.song = new Map(library.songs.map(song => [song._id, song]));
draft.entities.album = new Map(library.albums.map(album => [album._id, album]));
draft.entities.label = new Map(library.labels.map(label => [label._id, label]));
}));

export const fetchLibrary = async () => {
const library = await window.ipc.library.select();
dispatch(library);
};

export const reindexLibrary = async () => {
store.set(produce(draft => {
draft.route.app = Route.App.Scan;
Expand Down
9 changes: 0 additions & 9 deletions src/renderer/windows/app/state/actions/playlist.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@ import produce from 'immer';

import store from '../store';

const dispatchPlaylists = (playlists: Playlist[]) => store.set(produce(draft => {
draft.entities.playlist = new Map(playlists.map(playlist => [playlist._id, playlist]));
}));

export const fetchPlaylists = async () => {
const playlists = await window.ipc.playlist.get();
dispatchPlaylists(playlists);
};

export const createPlaylist = async (songs?: string[]) => {
if (!songs) return;
const playlist = await window.ipc.playlist.add(songs);
Expand Down
9 changes: 2 additions & 7 deletions src/renderer/windows/app/state/actions/queue.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import produce from 'immer';

import random from '@doombox/lib/math/random';

import sortSongs from '../../lib/sort/sortSongs';
import { hasAutoplay, populateSongs } from '../selectors';
import store from '../store';

Expand Down Expand Up @@ -51,9 +50,7 @@ export const playLabel = (id: string) => {
const label = draft.entities.label.get(id);

if (label) {
const songs = populateSongs(draft)(label.songs)
.sort(sortSongs)
.map(song => song._id);
const songs = populateSongs(draft)(label.songs).map(song => song._id);

draft.queue.songs = songs;
draft.queue.index = 0;
Expand All @@ -69,9 +66,7 @@ export const playAlbum = (id: string) => {
const album = draft.entities.album.get(id);

if (album) {
const songs = populateSongs(draft)(album.songs)
.sort(sortSongs)
.map(song => song._id);
const songs = populateSongs(draft)(album.songs).map(song => song._id);

draft.queue.songs = songs;
draft.queue.index = 0;
Expand Down
7 changes: 0 additions & 7 deletions src/renderer/windows/app/state/actions/theme.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ import produce from 'immer';

import store from '../store';

export const fetchTheme = async () => {
const theme = await window.ipc.theme.get();
store.set(produce(draft => {
draft.theme = theme;
}));
};

export const setTheme = async (reducer: (state: ThemeShape) => ThemeShape) => {
store.set(produce(draft => {
draft.theme = reducer(draft.theme);
Expand Down
10 changes: 0 additions & 10 deletions src/renderer/windows/app/state/actions/user.actions.ts

This file was deleted.

40 changes: 40 additions & 0 deletions src/renderer/windows/app/state/ipc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { State } from './state';
import type Store from '@doombox/lib/store/store';

import produce from 'immer';

import { App } from './route';

const createEntity = <T extends object, K extends keyof T>(arr: T[], k: K): Map<T[K], T> =>
new Map(arr.map(x => [x[k], x]));

export default async (store: Store<State>) => {
const [
thumbs,
user,
cache,
theme,
playlists,
library
] = await Promise.all([
window.ipc.os.image(),
window.ipc.user.get(),
window.ipc.cache.get(),
window.ipc.theme.get(),
window.ipc.playlist.get(),
window.ipc.library.select()
]);

store.set(produce(draft => {
draft.dir.thumbs = thumbs;
draft.user = user;
draft.player.muted = cache.player.muted;
draft.player.volume = cache.player.volume;
draft.theme = theme;
draft.entities.playlist = createEntity(playlists, '_id');
draft.entities.song = createEntity(library.songs, '_id');
draft.entities.album = createEntity(library.albums, '_id');
draft.entities.label = createEntity(library.labels, '_id');
draft.route.app = App.Home;
}));
};
3 changes: 3 additions & 0 deletions src/renderer/windows/app/state/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import userShape from '@doombox/types/shapes/user.shape';

import { AudioStatus } from '../../../lib/audio/audio';

import fetch from './ipc';
import * as Route from './route';

const store = new Store<State>({
Expand Down Expand Up @@ -50,6 +51,8 @@ const store = new Store<State>({
user: userShape
});

fetch(store);

window.ipc.on.parser.song(() => {
store.set(produce(draft => {
draft.route.app = Route.App.Scan;
Expand Down

0 comments on commit 31b2611

Please sign in to comment.