Experiment to rebuild Diffuse using web applets.
1import type { Track } from "@applets/core/types.js";
2import type { State } from "./types";
3import { expose } from "@scripts/common.ts";
4
5////////////////////////////////////////////
6// ACTIONS
7////////////////////////////////////////////
8const actions = expose({
9 add,
10 fill,
11 shift,
12 unshift,
13});
14
15export type Actions = typeof actions;
16
17// Actions
18
19function add(state: State, items: Track[]): State {
20 return { ...state, future: [...state.future, ...items] };
21}
22
23// TODO: Shuffle, limit track amount, etc.
24function fill(state: State, availableItems: Track[]): State {
25 state = add(state, availableItems);
26
27 // Automatically insert track if there isn't any
28 if (!state.now) return shift(state);
29 return state;
30}
31
32function shift(state: State): State {
33 const now = state.future[0] || null;
34 const future = state.future.slice(1);
35 const past = state.now ? [...state.past, state.now] : state.past;
36
37 return { past, now, future };
38}
39
40function unshift(state: State): State {
41 if (state.past.length === 0) return state;
42
43 const past = [...state.past];
44 const [last] = past.splice(past.length - 1, 1);
45 const now = last ?? null;
46 const future = state.now ? [state.now, ...state.future] : state.future;
47
48 return { past, now, future };
49}