diff --git a/src/components/engine/queue/element.js b/src/components/engine/queue/element.js index d2197829..94ae8cb8 100644 --- a/src/components/engine/queue/element.js +++ b/src/components/engine/queue/element.js @@ -26,8 +26,8 @@ class QueueEngine extends DiffuseElement { this.add = this.proxy.add; this.fill = this.proxy.fill; - this.pool = this.proxy.pool; this.shift = this.proxy.shift; + this.supply = this.proxy.supply; this.unshift = this.proxy.unshift; } @@ -36,14 +36,14 @@ class QueueEngine extends DiffuseElement { #future = signal(/** @type {Array} */ ([])); #now = signal(/** @type {Item | null} */ (null)); #past = signal(/** @type {Array} */ ([])); - #poolHash = signal(/** @type {string | undefined} */ (undefined)); + #supplyFingerprint = signal(/** @type {string | undefined} */ (undefined)); // STATE future = this.#future.get; now = this.#now.get; past = this.#past.get; - poolHash = this.#poolHash.get; + supplyFingerprint = this.#supplyFingerprint.get; // LIFECYCLE @@ -60,13 +60,13 @@ class QueueEngine extends DiffuseElement { listen("future", this.#future.set, link); listen("now", this.#now.set, link); listen("past", this.#past.set, link); - listen("poolHash", this.#poolHash.set, link); + listen("supplyFingerprint", this.#supplyFingerprint.set, link); // Fetch current data state this.proxy.future().then(this.#future.set); this.proxy.now().then(this.#now.set); this.proxy.past().then(this.#past.set); - this.proxy.poolHash().then(this.#poolHash.set); + this.proxy.supplyFingerprint().then(this.#supplyFingerprint.set); } } diff --git a/src/components/engine/queue/types.d.ts b/src/components/engine/queue/types.d.ts index 298e4f78..8a633883 100644 --- a/src/components/engine/queue/types.d.ts +++ b/src/components/engine/queue/types.d.ts @@ -11,8 +11,8 @@ export type Actions = { shuffled: boolean; }, ) => void; - pool: (tracks: Track[]) => void; shift: () => void; + supply: (args: { tracks: Track[] }) => void; unshift: () => void; }; @@ -24,5 +24,9 @@ export type State = { future: SignalReader; now: SignalReader; past: SignalReader; - poolHash: SignalReader; + + /** + * Initially this is set to `undefined`, but whenever the cache is changed afterwards this will be the hash of the items in the supply. + */ + supplyFingerprint: SignalReader; }; diff --git a/src/components/engine/queue/worker.js b/src/components/engine/queue/worker.js index 3a89eb22..1290668c 100644 --- a/src/components/engine/queue/worker.js +++ b/src/components/engine/queue/worker.js @@ -17,7 +17,9 @@ export const $lake = signal(/** @type {Track[]} */ ([])); export const $future = signal(/** @type {Item[]} */ ([])); export const $now = signal(/** @type {Item | null} */ (null)); export const $past = signal(/** @type {Item[]} */ ([])); -export const $poolHash = signal(/** @type {string | undefined} */ (undefined)); +export const $supplyFingerprint = signal( + /** @type {string | undefined} */ (undefined), +); //////////////////////////////////////////// // ACTIONS @@ -51,18 +53,18 @@ export function fill({ augment, amount, shuffled }) { } /** - * @type {Actions['pool']} + * @type {Actions['shift']} */ -export function pool(tracks) { - $lake.value = tracks; - $poolHash.value = tracks.length ? hash(tracks) : undefined; +export function shift() { + return _shift(); } /** - * @type {Actions['shift']} + * @type {Actions['supply']} */ -export function shift() { - return _shift(); +export function supply({ tracks }) { + $lake.value = tracks; + $supplyFingerprint.value = tracks.length ? hash(tracks) : undefined; } /** @@ -89,15 +91,15 @@ ostiary((context, _firstConnection, _connectionId) => { rpc(context, { add, fill, - pool, shift, + supply, unshift, // State future: $future.get, now: $now.get, past: $past.get, - poolHash: $poolHash.get, + supplyFingerprint: $supplyFingerprint.get, }); // Effects @@ -106,7 +108,9 @@ ostiary((context, _firstConnection, _connectionId) => { effect(() => announce("future", $future.value, context)); effect(() => announce("now", $now.value, context)); effect(() => announce("past", $past.value, context)); - effect(() => announce("poolHash", $poolHash.value, context)); + effect(() => + announce("supplyFingerprint", $supplyFingerprint.value, context) + ); }); //////////////////////////////////////////// diff --git a/src/components/orchestrator/queue-tracks/worker.js b/src/components/orchestrator/queue-tracks/worker.js index c1d54caf..664e73f5 100644 --- a/src/components/orchestrator/queue-tracks/worker.js +++ b/src/components/orchestrator/queue-tracks/worker.js @@ -39,7 +39,7 @@ export async function poolAvailable({ data, ports }) { }, []); // Set pool - await queue.pool(availableTracks); + await queue.supply({ tracks: availableTracks }); } //////////////////////////////////////////// diff --git a/src/components/orchestrator/repeat-shuffle/element.js b/src/components/orchestrator/repeat-shuffle/element.js index 45e7264d..6f1eca31 100644 --- a/src/components/orchestrator/repeat-shuffle/element.js +++ b/src/components/orchestrator/repeat-shuffle/element.js @@ -53,7 +53,7 @@ class RepeatShuffleOrchestrator extends BroadcastableDiffuseElement { // Effects this.effect(() => { const trigger = queue.now(); - const _other_trigger = queue.poolHash(); + const _other_trigger = queue.supplyFingerprint(); this.isLeader().then((isLeader) => { if (!isLeader) return; diff --git a/src/components/processor/search/element.js b/src/components/processor/search/element.js index 237d545b..d73abb7f 100644 --- a/src/components/processor/search/element.js +++ b/src/components/processor/search/element.js @@ -30,11 +30,11 @@ class SearchProcessor extends DiffuseElement { // SIGNALS - #cacheId = signal(/** @type {string | undefined} */ (undefined)); + #supplyFingerprint = signal(/** @type {string | undefined} */ (undefined)); // STATE - cacheId = this.#cacheId.get; + supplyFingerprint = this.#supplyFingerprint.get; // LIFECYCLE @@ -48,10 +48,10 @@ class SearchProcessor extends DiffuseElement { const link = this.workerLink(); // Listen for remote data changes - listen("cacheId", this.#cacheId.set, link); + listen("supplyFingerprint", this.#supplyFingerprint.set, link); // Fetch current data state - this.proxy.cacheId().then(this.#cacheId.set); + this.proxy.supplyFingerprint().then(this.#supplyFingerprint.set); } } diff --git a/src/components/processor/search/types.d.ts b/src/components/processor/search/types.d.ts index 97a981b4..ec9579aa 100644 --- a/src/components/processor/search/types.d.ts +++ b/src/components/processor/search/types.d.ts @@ -15,5 +15,8 @@ export type Actions = { export type Schema = Orama; export type State = { - cacheId: SignalReader; + /** + * Initially this is set to `undefined`, but whenever the cache is changed afterwards this will be the hash of the items in the supply. + */ + supplyFingerprint: SignalReader; }; diff --git a/src/components/processor/search/worker.js b/src/components/processor/search/worker.js index 22260fe9..12a48e7e 100644 --- a/src/components/processor/search/worker.js +++ b/src/components/processor/search/worker.js @@ -22,7 +22,9 @@ export const $inserted = signal(/** @type {Set} */ (new Set()), { }); // Communicated state -export const $cacheId = signal(/** @type {string | undefined} */ (undefined)); +export const $supplyFingerprint = signal( + /** @type {string | undefined} */ (undefined), +); //////////////////////////////////////////// // DATABASE @@ -93,9 +95,7 @@ export async function supply({ tracks }) { await Orama.removeMultiple(db, Array.from(removedIds)); await Orama.insertMultiple(db, newTracks); - $cacheId.value = ids.length === 0 - ? undefined - : xxh32(ids.sort().join("")).toString(); + $supplyFingerprint.value = xxh32(ids.sort().join("")).toString(); } //////////////////////////////////////////// @@ -108,13 +108,15 @@ ostiary((context) => { supply, // State - cacheId: $cacheId.get, + supplyFingerprint: $supplyFingerprint.get, }); // Effects // Communicate state - effect(() => announce("cacheId", $cacheId.value, context)); + effect(() => + announce("supplyFingerprint", $supplyFingerprint.value, context) + ); }); //////////////////////////////////////////// diff --git a/src/themes/blur/index.js b/src/themes/blur/index.js index 16ec1866..a92a7c15 100644 --- a/src/themes/blur/index.js +++ b/src/themes/blur/index.js @@ -34,7 +34,7 @@ effect(() => { }); effect(() => { - console.log("Queue pool hash:", queue.poolHash()); + console.log("Queue pool hash:", queue.supplyFingerprint()); }); /** @@ -42,7 +42,7 @@ effect(() => { */ effect(() => { const trigger = queue.now(); - const _other_trigger = queue.poolHash(); + const _other_trigger = queue.supplyFingerprint(); isLeader().then((bool) => { if (bool) { diff --git a/src/themes/webamp/browser/element.js b/src/themes/webamp/browser/element.js index 093fa405..ce4ea941 100644 --- a/src/themes/webamp/browser/element.js +++ b/src/themes/webamp/browser/element.js @@ -66,7 +66,7 @@ class Browser extends DiffuseElement { // Wait for the above dependencies to be defined, then render again. whenElementsDefined({ input, output, search }).then(() => { this.effect(() => { - const _cacheId = search.cacheId(); + const _ = search.supplyFingerprint(); this.performSearch(); }); @@ -118,7 +118,7 @@ class Browser extends DiffuseElement { render({ html }) { const isLoading = this.$output.value?.tracks?.state() !== "loaded" || (this.#collectionSize.value > 0 && - this.$search.value?.cacheId() === undefined); + this.$search.value?.supplyFingerprint() === undefined); const tracks = this.#searchResults.value; @@ -174,6 +174,7 @@ class Browser extends DiffuseElement { } table tbody tr { + cursor: pointer; content-visibility: auto; } diff --git a/src/themes/webamp/index.js b/src/themes/webamp/index.js index dc6c9be4..1f3e8a1b 100644 --- a/src/themes/webamp/index.js +++ b/src/themes/webamp/index.js @@ -85,18 +85,17 @@ amp.media.loadFromUrl = loadOverride.bind(amp.media); * Whenever the queue changes update the playlist. */ effect(() => { - const now = untracked(queue.now); - const past = untracked(queue.past); const future = queue.future(); const playlist = [ - ...past, - ...(now ? [now] : []), ...future, ]; const lengthLastPlaylist = untracked($playlist.get).length; - const tracksToAdd = playlist.slice(lengthLastPlaylist); + const tracksToAdd = playlist.slice( + 0, + Math.max(0, playlist.length - lengthLastPlaylist), + ); $playlist.value = playlist; @@ -116,11 +115,11 @@ effect(() => { const state = output.tracks.state(); if (state !== "loaded") return; - const cacheId = search.cacheId(); - if (cacheId === undefined) return; + const fingerprintSearch = search.supplyFingerprint(); + if (fingerprintSearch === undefined) return; - const poolHash = queue.poolHash(); - if (poolHash === undefined) return; + const fingerprintQueue = queue.supplyFingerprint(); + if (fingerprintQueue === undefined) return; tracksPromise.resolve("loaded"); }); @@ -172,9 +171,6 @@ amp.onClose(() => winampIsShown = false); async function addBatch() { await queue.fill({ augment: true, amount: 50, shuffled: true }); - - // Automatically insert track if there isn't any - if (!queue.now()) await queue.shift(); } function windowManager() { diff --git a/tests/components/engine/queue/test.ts b/tests/components/engine/queue/test.ts index c1dea02b..6669f53a 100644 --- a/tests/components/engine/queue/test.ts +++ b/tests/components/engine/queue/test.ts @@ -4,8 +4,6 @@ import { expect } from "@std/expect"; import { testWeb } from "@tests/common/index.ts"; import { tracks } from "@src/testing/sample/tracks.js"; -import type { Item } from "@components/engine/queue/types.d.ts"; - describe("components/engine/queue", () => { it("adds tracks", async () => { const items = await testWeb(async () => { @@ -35,7 +33,7 @@ describe("components/engine/queue", () => { const { tracks } = await import("@src/testing/sample/tracks.js"); - await engine.pool(tracks); + await engine.supply({ tracks }); await engine.fill({ amount: 1, shuffled: false }); await engine.shift();