forked from
tokono.ma/diffuse
A music player that connects to your cloud/distributed storage.
1import { debounce } from "throttle-debounce";
2
3import * as Output from "~/common/output.js";
4import foundation from "~/common/foundation.js";
5import { effect } from "~/common/signal.js";
6
7const KEY = "facets/data/process-tracks/timestamp";
8const MAX_TIME_DIFF = 10 * 60 * 1000;
9
10////////////////////////////////////////////
11// ON LOAD
12////////////////////////////////////////////
13
14const output = await foundation.orchestrator.output();
15await Output.data(output.tracks);
16
17const lastTimestamp = localStorage.getItem(KEY);
18const now = Date.now();
19const diff = lastTimestamp ? now - JSON.parse(lastTimestamp) : MAX_TIME_DIFF;
20
21const orchestrator = await foundation.orchestrator.processTracks({
22 disableWhenReady: true,
23});
24
25if (diff >= MAX_TIME_DIFF) {
26 // Wait until we're actually done processing, only then set the timestamp
27 await orchestrator.process();
28 localStorage.setItem(KEY, JSON.stringify(now));
29}
30
31////////////////////////////////////////////
32// WHEN SOURCES CHANGE
33////////////////////////////////////////////
34
35const { sources } = await foundation.orchestrator.sources();
36let initialised = false;
37
38const debounced = debounce(
39 2500,
40 /** @param {import("~/components/input/types.d.ts").Source} _sources */ async (
41 _sources,
42 ) => {
43 if (initialised) orchestrator.process();
44 else initialised = true;
45 },
46 { atBegin: false },
47);
48
49effect(() => {
50 debounced(sources());
51});