Experiment to rebuild Diffuse using web applets.
1<script>
2 import type { Tasks } from "@scripts/engine/queue/worker";
3 import type { Track } from "@applets/core/types";
4 import type { State } from "./types.d.ts";
5
6 import { register } from "@scripts/applet/common";
7 import { endpoint, SharedWorker, sync, transfer } from "@scripts/common";
8 import manifest from "./_manifest.json";
9
10 ////////////////////////////////////////////
11 // SETUP
12 ////////////////////////////////////////////
13 const port = new SharedWorker(new URL("../../../scripts/engine/queue/worker", import.meta.url), {
14 type: "module",
15 name: manifest.name,
16 }).port;
17
18 const worker = endpoint<Tasks>(port);
19
20 // Register applet
21 const context = register<State>({ mode: "shared-worker", worker });
22 const groupId = context.groupId || "main";
23
24 // Initial state
25 context.data = {
26 future: [],
27 now: null,
28 past: [],
29 };
30
31 context.data = await worker.data(groupId);
32
33 // Keep applet data with worker data in sync
34 sync(context, port, { groupId });
35
36 ////////////////////////////////////////////
37 // ACTIONS
38 ////////////////////////////////////////////
39 context.setActionHandler("add", add);
40 context.setActionHandler("pool", pool);
41 context.setActionHandler("shift", shift);
42 context.setActionHandler("unshift", unshift);
43
44 async function add(items: Track[]) {
45 await worker.add({ groupId, items });
46 }
47
48 async function pool(tracks: Track[]) {
49 await worker.pool({ groupId, tracks });
50 }
51
52 async function shift() {
53 await worker.shift({ groupId });
54 }
55
56 async function unshift() {
57 await worker.unshift({ groupId });
58 }
59</script>