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