@avoid.quest/audio 0.1.0 → 0.1.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -2
- package/dist/effects/index.d.ts +21 -72
- package/dist/effects/index.js +212 -505
- package/dist/effects/index.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/midi/index.d.ts +0 -24
- package/dist/midi/index.js +222 -258
- package/dist/midi/index.js.map +1 -1
- package/dist/native-Bern4_PB.js +172 -0
- package/dist/native-Bern4_PB.js.map +1 -0
- package/dist/pcm-buffer-DSzGHbon.js +436 -0
- package/dist/pcm-buffer-DSzGHbon.js.map +1 -0
- package/dist/playback/index.d.ts +63 -115
- package/dist/playback/index.js +906 -1062
- package/dist/playback/index.js.map +1 -1
- package/dist/queue/index.d.ts +8 -19
- package/dist/queue/index.js +461 -960
- package/dist/queue/index.js.map +1 -1
- package/dist/recognize/index.d.ts +37 -74
- package/dist/recognize/index.js +172 -2715
- package/dist/recognize/index.js.map +1 -1
- package/dist/routing/index.d.ts +3 -34
- package/dist/routing/index.js +413 -536
- package/dist/routing/index.js.map +1 -1
- package/dist/shazam-gBrX1NEY.d.ts +27 -0
- package/dist/sync/index.d.ts +9 -77
- package/dist/sync/index.js +320 -542
- package/dist/sync/index.js.map +1 -1
- package/dist/{worklet-loader-BvEW0oVO.d.ts → worklet-loader-BabwPALw.d.ts} +4 -7
- package/dist/{worklet-loader-S7R8ZAmP.js → worklet-loader-f741D1tk.js} +37 -52
- package/dist/worklet-loader-f741D1tk.js.map +1 -0
- package/dist/worklets/couch-live.js +110 -192
- package/dist/worklets/couch-live.js.map +1 -1
- package/dist/worklets/meter.js +14 -20
- package/dist/worklets/meter.js.map +1 -1
- package/dist/worklets/pcm-capture.d.ts +6 -0
- package/dist/worklets/pcm-capture.js +52 -0
- package/dist/worklets/pcm-capture.js.map +1 -0
- package/package.json +18 -4
- package/types/hls-unavailable.d.ts +4 -0
- package/dist/worklet-loader-S7R8ZAmP.js.map +0 -1
package/dist/queue/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import { o as AudioQueueError } from "../errors-BT9HxDLn.js";
|
|
1
2
|
import { Context, Effect, Layer, Ref, Schema } from "effect";
|
|
2
3
|
import * as Atom from "effect/reactivity/Atom";
|
|
3
4
|
import * as AtomRef from "effect/reactivity/AtomRef";
|
|
4
|
-
import { o as AudioQueueError } from "../errors-BT9HxDLn.js";
|
|
5
5
|
// src/queue/shuffle.ts
|
|
6
6
|
/**
|
|
7
7
|
* Mulberry32 deterministic 32-bit PRNG returning values in `[0, 1)`.
|
|
@@ -54,15 +54,10 @@ const DEFAULT_MAX_QUEUE_ENTRIES = 500;
|
|
|
54
54
|
const DEFAULT_UNDO_REMOVAL_TTL_MS = 6e3;
|
|
55
55
|
const DEFAULT_UNDO_STOP_TTL_MS = 11e3;
|
|
56
56
|
const DEFAULT_URL_RENEW_AHEAD_MS = 6e4;
|
|
57
|
-
// src/queue/state.ts
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
const
|
|
61
|
-
const EXPIRE_DIGITS_REGEX = /^\d{9,11}$/u;
|
|
62
|
-
/**
|
|
63
|
-
* Extracts a Unix epoch millisecond expiry timestamp from signed media URLs
|
|
64
|
-
* (`?expire=`, `?expires=`, `?ts=`, `/expire/<sec>/`, or JSON `"expires": <sec>`).
|
|
65
|
-
*/
|
|
57
|
+
// src/queue/state-helpers.ts
|
|
58
|
+
const EXPIRE_PATH_RE = /\/expire\/(?<epoch>\d{9,11})(?:\/|$)/iu;
|
|
59
|
+
const EXPIRE_JSON_RE = /"expires"\s*:\s*(?<epoch>\d{9,11})/u;
|
|
60
|
+
const EXPIRE_DIGITS_RE = /^\d{9,11}$/u;
|
|
66
61
|
function extractLinkExpiryMs(url) {
|
|
67
62
|
let parsed;
|
|
68
63
|
try {
|
|
@@ -70,989 +65,495 @@ function extractLinkExpiryMs(url) {
|
|
|
70
65
|
} catch {
|
|
71
66
|
return;
|
|
72
67
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
const pathMatch = EXPIRE_PATH_REGEX.exec(parsed.pathname);
|
|
78
|
-
if (pathMatch?.groups?.epoch !== void 0) return Number(pathMatch.groups.epoch) * 1e3;
|
|
79
|
-
for (const value of parsed.searchParams.values()) {
|
|
80
|
-
const jsonMatch = EXPIRE_JSON_REGEX.exec(value);
|
|
81
|
-
if (jsonMatch?.groups?.epoch !== void 0) return Number(jsonMatch.groups.epoch) * 1e3;
|
|
68
|
+
let epoch = ["expire", "expires"].map((k) => parsed.searchParams.get(k)).find((v) => v !== null && EXPIRE_DIGITS_RE.test(v)) ?? EXPIRE_PATH_RE.exec(parsed.pathname)?.groups?.epoch;
|
|
69
|
+
if (epoch === void 0) for (const v of parsed.searchParams.values()) {
|
|
70
|
+
epoch = EXPIRE_JSON_RE.exec(v)?.groups?.epoch;
|
|
71
|
+
if (epoch !== void 0) break;
|
|
82
72
|
}
|
|
73
|
+
return epoch === void 0 ? void 0 : Number(epoch) * 1e3;
|
|
83
74
|
}
|
|
84
|
-
|
|
85
|
-
* Checks whether a queue entry's URL is expired or within `renewAheadMs` of expiry.
|
|
86
|
-
*/
|
|
87
|
-
function isUrlExpiringSoon(entry, nowMs = Date.now(), renewAheadMs = DEFAULT_URL_RENEW_AHEAD_MS) {
|
|
75
|
+
const isUrlExpiringSoon = (entry, nowMs = Date.now(), renewAheadMs = DEFAULT_URL_RENEW_AHEAD_MS) => {
|
|
88
76
|
const expiry = entry.expiresAtMs ?? extractLinkExpiryMs(entry.url);
|
|
89
|
-
|
|
90
|
-
|
|
77
|
+
return expiry !== void 0 && expiry - nowMs < renewAheadMs;
|
|
78
|
+
};
|
|
79
|
+
const createInitialQueueState = (initialEntries = []) => ({
|
|
80
|
+
continuation: void 0,
|
|
81
|
+
cursor: initialEntries.length > 0 ? 0 : -1,
|
|
82
|
+
entries: [...initialEntries],
|
|
83
|
+
history: [],
|
|
84
|
+
originalOrder: initialEntries.map((e) => e.id),
|
|
85
|
+
repeat: "off",
|
|
86
|
+
shuffle: false,
|
|
87
|
+
shuffleSeed: void 0,
|
|
88
|
+
undoRemoval: void 0,
|
|
89
|
+
undoStop: void 0
|
|
90
|
+
});
|
|
91
|
+
const qErr = (code, message) => new AudioQueueError({
|
|
92
|
+
code,
|
|
93
|
+
message
|
|
94
|
+
});
|
|
95
|
+
const curOf = (s) => s.cursor >= 0 ? s.entries[s.cursor] : void 0;
|
|
96
|
+
const refOrId = (list, t) => {
|
|
97
|
+
const i = list.indexOf(t);
|
|
98
|
+
return i === -1 ? list.findIndex((e) => e.id === t.id) : i;
|
|
99
|
+
};
|
|
100
|
+
const pushHist = (h, e, max) => e ? [...h, e].slice(-max) : h;
|
|
101
|
+
const markMut = (s) => !s.undoStop || s.undoStop.mutated ? s : {
|
|
102
|
+
...s,
|
|
103
|
+
undoStop: {
|
|
104
|
+
...s.undoStop,
|
|
105
|
+
mutated: true
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
const defaultTokenGen = () => {
|
|
109
|
+
let n = 0;
|
|
110
|
+
return () => {
|
|
111
|
+
if (typeof globalThis.crypto?.randomUUID === "function") return globalThis.crypto.randomUUID();
|
|
112
|
+
n += 1;
|
|
113
|
+
return `undo-${Date.now()}-${n}`;
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
function removeOccurrences(order, removed) {
|
|
117
|
+
if (removed.length === 0) return [...order];
|
|
118
|
+
const counts = /* @__PURE__ */ new Map();
|
|
119
|
+
for (const { id } of removed) counts.set(id, (counts.get(id) ?? 0) + 1);
|
|
120
|
+
const kept = [];
|
|
121
|
+
for (let i = order.length - 1; i >= 0; i -= 1) {
|
|
122
|
+
const id = order[i];
|
|
123
|
+
if (id === void 0) continue;
|
|
124
|
+
const left = counts.get(id) ?? 0;
|
|
125
|
+
if (left > 0) counts.set(id, left - 1);
|
|
126
|
+
else kept.push(id);
|
|
127
|
+
}
|
|
128
|
+
return kept.toReversed();
|
|
91
129
|
}
|
|
92
|
-
function
|
|
130
|
+
function cursorInOrder(s) {
|
|
131
|
+
const cur = curOf(s);
|
|
132
|
+
if (!cur) return -1;
|
|
133
|
+
let rank = 0;
|
|
134
|
+
for (let i = 0; i < s.cursor; i += 1) if (s.entries[i]?.id === cur.id) rank += 1;
|
|
135
|
+
for (const [i, id] of s.originalOrder.entries()) if (id === cur.id) {
|
|
136
|
+
if (rank === 0) return i;
|
|
137
|
+
rank -= 1;
|
|
138
|
+
}
|
|
139
|
+
return s.originalOrder.indexOf(cur.id);
|
|
140
|
+
}
|
|
141
|
+
function advanceCont(s, cont, cur, max) {
|
|
142
|
+
const [nextEntry, ...rest] = cont.entries;
|
|
143
|
+
if (!nextEntry) return;
|
|
144
|
+
const drop = Math.max(0, s.entries.length - (max - 1));
|
|
145
|
+
const dropped = drop > 0 ? s.entries.slice(0, drop) : [];
|
|
146
|
+
const trimmed = drop > 0 ? s.entries.slice(drop) : s.entries;
|
|
147
|
+
let { cursor } = s;
|
|
148
|
+
if (drop > 0) {
|
|
149
|
+
if (max <= 1) cursor = -1;
|
|
150
|
+
else cursor = s.cursor > 0 ? Math.max(-1, s.cursor - drop) : trimmed.length - 1;
|
|
151
|
+
}
|
|
152
|
+
return [nextEntry, {
|
|
153
|
+
...s,
|
|
154
|
+
continuation: rest.length > 0 ? {
|
|
155
|
+
entries: rest,
|
|
156
|
+
title: cont.title
|
|
157
|
+
} : void 0,
|
|
158
|
+
cursor: cursor + 1,
|
|
159
|
+
entries: [...trimmed, nextEntry],
|
|
160
|
+
history: pushHist(s.history, cur, max),
|
|
161
|
+
originalOrder: [...removeOccurrences(s.originalOrder, dropped), nextEntry.id].slice(-max),
|
|
162
|
+
undoStop: void 0
|
|
163
|
+
}];
|
|
164
|
+
}
|
|
165
|
+
function unshuffle(s, rank) {
|
|
166
|
+
const cur = curOf(s);
|
|
167
|
+
const buckets = /* @__PURE__ */ new Map();
|
|
168
|
+
for (const e of s.entries) {
|
|
169
|
+
const list = buckets.get(e.id) ?? [];
|
|
170
|
+
list.push(e);
|
|
171
|
+
buckets.set(e.id, list);
|
|
172
|
+
}
|
|
173
|
+
for (const list of buckets.values()) if (list.length > 1) list.sort((a, b) => (rank.get(a) ?? Number.MAX_SAFE_INTEGER) - (rank.get(b) ?? Number.MAX_SAFE_INTEGER));
|
|
174
|
+
const entries = [];
|
|
175
|
+
for (const id of s.originalOrder) {
|
|
176
|
+
const item = buckets.get(id)?.shift();
|
|
177
|
+
if (item !== void 0) entries.push(item);
|
|
178
|
+
}
|
|
179
|
+
for (const list of buckets.values()) entries.push(...list);
|
|
93
180
|
return {
|
|
181
|
+
cursor: cur ? refOrId(entries, cur) : s.cursor,
|
|
182
|
+
entries
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
function reorderIds(order, id, beforeId) {
|
|
186
|
+
const from = order.indexOf(id);
|
|
187
|
+
if (from === -1) return order;
|
|
188
|
+
const without = order.toSpliced(from, 1);
|
|
189
|
+
const to = beforeId === void 0 ? -1 : without.indexOf(beforeId);
|
|
190
|
+
return without.toSpliced(to === -1 ? without.length : to, 0, id);
|
|
191
|
+
}
|
|
192
|
+
// src/queue/transitions.ts
|
|
193
|
+
function enqueueTransition(raw, entries, placement, max) {
|
|
194
|
+
const s = markMut(raw);
|
|
195
|
+
if (entries.length === 0) return s;
|
|
196
|
+
if (s.entries.length + entries.length > max) return qErr("QUEUE_FULL", `Capacity of ${max} exceeded.`);
|
|
197
|
+
const at = placement === "next" && s.cursor >= 0 ? s.cursor + 1 : s.entries.length;
|
|
198
|
+
const nextEntries = s.entries.toSpliced(at, 0, ...entries);
|
|
199
|
+
const newIds = entries.map((e) => e.id);
|
|
200
|
+
let originalOrder;
|
|
201
|
+
if (!s.shuffle) originalOrder = nextEntries.map((e) => e.id);
|
|
202
|
+
else if (placement === "next" && s.cursor >= 0) {
|
|
203
|
+
const anchor = cursorInOrder(s);
|
|
204
|
+
originalOrder = s.originalOrder.toSpliced(anchor === -1 ? Math.min(at, s.originalOrder.length) : anchor + 1, 0, ...newIds);
|
|
205
|
+
} else originalOrder = [...s.originalOrder, ...newIds];
|
|
206
|
+
return {
|
|
207
|
+
...s,
|
|
208
|
+
cursor: s.cursor === -1 ? 0 : s.cursor,
|
|
209
|
+
entries: nextEntries,
|
|
210
|
+
originalOrder,
|
|
211
|
+
undoRemoval: void 0
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
function loadTransition(prev, entries, mode, max) {
|
|
215
|
+
if (entries.length > max) return qErr("QUEUE_FULL", `Load exceeds capacity (${max}).`);
|
|
216
|
+
return {
|
|
217
|
+
...prev,
|
|
94
218
|
continuation: void 0,
|
|
95
|
-
cursor:
|
|
96
|
-
entries: [...
|
|
97
|
-
history:
|
|
98
|
-
originalOrder:
|
|
99
|
-
repeat: "off",
|
|
219
|
+
cursor: entries.length > 0 ? 0 : -1,
|
|
220
|
+
entries: [...entries],
|
|
221
|
+
history: mode === "play" ? pushHist(prev.history, curOf(prev), max) : prev.history,
|
|
222
|
+
originalOrder: entries.map((e) => e.id),
|
|
100
223
|
shuffle: false,
|
|
101
224
|
shuffleSeed: void 0,
|
|
102
225
|
undoRemoval: void 0,
|
|
103
226
|
undoStop: void 0
|
|
104
227
|
};
|
|
105
228
|
}
|
|
106
|
-
function
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
229
|
+
function moveTransition(raw, entryId, beforeEntryId) {
|
|
230
|
+
const s = markMut(raw);
|
|
231
|
+
const from = s.entries.findIndex((e) => e.id === entryId);
|
|
232
|
+
const moving = from === -1 ? void 0 : s.entries[from];
|
|
233
|
+
if (!moving) return qErr("ENTRY_NOT_FOUND", `Entry "${entryId}" not found.`);
|
|
234
|
+
if (entryId === beforeEntryId) return s;
|
|
235
|
+
const without = s.entries.toSpliced(from, 1);
|
|
236
|
+
const to = beforeEntryId === void 0 ? without.length : without.findIndex((e) => e.id === beforeEntryId);
|
|
237
|
+
if (to === -1) return qErr("ENTRY_NOT_FOUND", `Target "${beforeEntryId}" not found.`);
|
|
238
|
+
const nextEntries = without.toSpliced(to, 0, moving);
|
|
239
|
+
const cur = curOf(s);
|
|
240
|
+
return {
|
|
241
|
+
...s,
|
|
242
|
+
cursor: cur ? refOrId(nextEntries, cur) : s.cursor,
|
|
243
|
+
entries: nextEntries,
|
|
244
|
+
originalOrder: s.shuffle ? reorderIds(s.originalOrder, entryId, beforeEntryId) : nextEntries.map((e) => e.id),
|
|
245
|
+
undoRemoval: void 0
|
|
112
246
|
};
|
|
113
247
|
}
|
|
114
|
-
function
|
|
115
|
-
|
|
116
|
-
const
|
|
117
|
-
|
|
248
|
+
function jumpToTransition(raw, entryId, max) {
|
|
249
|
+
const s = markMut(raw);
|
|
250
|
+
const idx = s.entries.findIndex((e) => e.id === entryId);
|
|
251
|
+
const target = idx === -1 ? void 0 : s.entries[idx];
|
|
252
|
+
if (!target) return [qErr("ENTRY_NOT_FOUND", `Entry "${entryId}" not found.`), raw];
|
|
253
|
+
const cur = curOf(s);
|
|
254
|
+
return [target, {
|
|
255
|
+
...s,
|
|
256
|
+
cursor: idx,
|
|
257
|
+
history: cur && cur.id !== target.id ? pushHist(s.history, cur, max) : s.history,
|
|
258
|
+
undoStop: void 0
|
|
259
|
+
}];
|
|
260
|
+
}
|
|
261
|
+
function previousTransition(s) {
|
|
262
|
+
if (s.entries.length === 0) return [void 0, s];
|
|
263
|
+
let idx = -1;
|
|
264
|
+
if (s.cursor > 0) idx = s.cursor - 1;
|
|
265
|
+
else if (s.repeat === "all") idx = s.entries.length - 1;
|
|
266
|
+
return idx < 0 ? [s.entries[0], s] : [s.entries[idx], {
|
|
267
|
+
...s,
|
|
268
|
+
cursor: idx,
|
|
269
|
+
undoStop: void 0
|
|
270
|
+
}];
|
|
118
271
|
}
|
|
119
|
-
function
|
|
120
|
-
if (
|
|
272
|
+
function setShuffleTransition(s, enabled, seed, now, preShuffleRank) {
|
|
273
|
+
if (enabled === s.shuffle && (seed === void 0 || seed === s.shuffleSeed)) return s;
|
|
274
|
+
if (!enabled) {
|
|
275
|
+
const { cursor, entries } = unshuffle(s, preShuffleRank);
|
|
276
|
+
return {
|
|
277
|
+
...s,
|
|
278
|
+
cursor,
|
|
279
|
+
entries,
|
|
280
|
+
shuffle: false,
|
|
281
|
+
shuffleSeed: void 0
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
const chosenSeed = seed ?? Math.trunc(now()) % 4294967296;
|
|
285
|
+
if (!s.shuffle) for (const [idx, e] of s.entries.entries()) preShuffleRank.set(e, idx);
|
|
286
|
+
const entries = s.cursor >= 0 && curOf(s) !== void 0 ? [...s.entries.slice(0, s.cursor + 1), ...fisherYatesShuffle(s.entries.slice(s.cursor + 1), chosenSeed)] : fisherYatesShuffle(s.entries, chosenSeed);
|
|
121
287
|
return {
|
|
122
|
-
...
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
288
|
+
...s,
|
|
289
|
+
entries,
|
|
290
|
+
originalOrder: s.shuffle ? s.originalOrder : s.entries.map((e) => e.id),
|
|
291
|
+
shuffle: true,
|
|
292
|
+
shuffleSeed: chosenSeed
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
// src/queue/undo.ts
|
|
296
|
+
function finishRemoval(cfg, s, remaining, cursor, removed, positions, originalOrder) {
|
|
297
|
+
const token = cfg.nextToken();
|
|
298
|
+
const next = {
|
|
299
|
+
...s,
|
|
300
|
+
cursor,
|
|
301
|
+
entries: remaining,
|
|
302
|
+
originalOrder,
|
|
303
|
+
undoRemoval: {
|
|
304
|
+
currentEntryId: cursor >= 0 ? remaining[cursor]?.id : void 0,
|
|
305
|
+
entries: removed,
|
|
306
|
+
expiresAtMs: cfg.now() + cfg.undoRemTtl,
|
|
307
|
+
index: positions[0]?.index ?? -1,
|
|
308
|
+
positions,
|
|
309
|
+
priorOriginalOrder: s.originalOrder,
|
|
310
|
+
queueIds: remaining.map((e) => e.id),
|
|
311
|
+
token
|
|
126
312
|
}
|
|
127
313
|
};
|
|
314
|
+
return [{
|
|
315
|
+
removed,
|
|
316
|
+
state: next,
|
|
317
|
+
undoToken: token
|
|
318
|
+
}, next];
|
|
128
319
|
}
|
|
129
|
-
function
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
320
|
+
function removeTransition(cfg, raw, entryIds) {
|
|
321
|
+
const s = markMut(raw);
|
|
322
|
+
if (entryIds.length === 0) return [{
|
|
323
|
+
removed: [],
|
|
324
|
+
state: s,
|
|
325
|
+
undoToken: void 0
|
|
326
|
+
}, s];
|
|
327
|
+
const idSet = new Set(entryIds);
|
|
328
|
+
const removed = [];
|
|
329
|
+
const positions = [];
|
|
330
|
+
const remaining = [];
|
|
331
|
+
for (const [index, entry] of s.entries.entries()) if (idSet.has(entry.id)) {
|
|
332
|
+
removed.push(entry);
|
|
333
|
+
positions.push({
|
|
334
|
+
entry,
|
|
335
|
+
index
|
|
336
|
+
});
|
|
337
|
+
} else remaining.push(entry);
|
|
338
|
+
if (removed.length === 0) return [qErr("ENTRY_NOT_FOUND", "No specified entries found."), raw];
|
|
339
|
+
const cur = curOf(s);
|
|
340
|
+
let cursor = Math.min(s.cursor, remaining.length - 1);
|
|
341
|
+
if (remaining.length === 0) cursor = -1;
|
|
342
|
+
else if (cur && !idSet.has(cur.id)) cursor = refOrId(remaining, cur);
|
|
343
|
+
return finishRemoval(cfg, s, remaining, cursor, removed, positions, s.originalOrder.filter((id) => !idSet.has(id)));
|
|
133
344
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const remaining = counts.get(id) ?? 0;
|
|
147
|
-
if (remaining > 0) counts.set(id, remaining - 1);
|
|
148
|
-
else kept.push(id);
|
|
149
|
-
}
|
|
150
|
-
kept.reverse();
|
|
151
|
-
return kept;
|
|
345
|
+
function clearUpNextTransition(cfg, raw) {
|
|
346
|
+
const s = markMut(raw);
|
|
347
|
+
const splitAt = s.cursor >= 0 ? s.cursor + 1 : 0;
|
|
348
|
+
const removed = s.entries.slice(splitAt);
|
|
349
|
+
return removed.length === 0 ? [{
|
|
350
|
+
removed: [],
|
|
351
|
+
state: s,
|
|
352
|
+
undoToken: void 0
|
|
353
|
+
}, s] : finishRemoval(cfg, s, s.entries.slice(0, splitAt), s.cursor, removed, removed.map((entry, i) => ({
|
|
354
|
+
entry,
|
|
355
|
+
index: splitAt + i
|
|
356
|
+
})), removeOccurrences(s.originalOrder, removed));
|
|
152
357
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
const targetIdx = without.indexOf(beforeEntryId);
|
|
178
|
-
if (targetIdx !== -1) orderToIndex = targetIdx;
|
|
179
|
-
}
|
|
180
|
-
return [
|
|
181
|
-
...without.slice(0, orderToIndex),
|
|
182
|
-
entryId,
|
|
183
|
-
...without.slice(orderToIndex)
|
|
184
|
-
];
|
|
358
|
+
function restoreRemovedTransition(cfg, s, token) {
|
|
359
|
+
const snap = s.undoRemoval;
|
|
360
|
+
if (!snap || snap.token !== token) return [qErr("UNDO_MISMATCH", "Undo token mismatch."), s];
|
|
361
|
+
if (cfg.now() > snap.expiresAtMs) return [qErr("UNDO_EXPIRED", "Undo removal expired."), {
|
|
362
|
+
...s,
|
|
363
|
+
undoRemoval: void 0
|
|
364
|
+
}];
|
|
365
|
+
const cur = curOf(s);
|
|
366
|
+
const same = s.entries.length === snap.queueIds.length && s.entries.every((e, i) => e.id === snap.queueIds[i]);
|
|
367
|
+
if (cur?.id !== snap.currentEntryId || !same) return [qErr("UNDO_MISMATCH", "Queue changed since removal."), s];
|
|
368
|
+
if (s.entries.length + snap.entries.length > cfg.max) return [qErr("QUEUE_FULL", `Restore exceeds capacity (${cfg.max}).`), s];
|
|
369
|
+
const entries = [...s.entries];
|
|
370
|
+
for (const item of snap.positions.toSorted((a, b) => a.index - b.index)) entries.splice(Math.min(item.index, entries.length), 0, item.entry);
|
|
371
|
+
let cursor = -1;
|
|
372
|
+
if (cur !== void 0) cursor = refOrId(entries, cur);
|
|
373
|
+
else if (entries.length > 0) cursor = Math.min(snap.index, entries.length - 1);
|
|
374
|
+
const next = {
|
|
375
|
+
...s,
|
|
376
|
+
cursor,
|
|
377
|
+
entries,
|
|
378
|
+
originalOrder: snap.priorOriginalOrder,
|
|
379
|
+
undoRemoval: void 0
|
|
380
|
+
};
|
|
381
|
+
return [next, next];
|
|
185
382
|
}
|
|
186
|
-
function
|
|
187
|
-
const
|
|
188
|
-
if (!
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
const dropCount = trimmedEntries.length - keepCount;
|
|
205
|
-
droppedEntries = trimmedEntries.slice(0, dropCount);
|
|
206
|
-
trimmedEntries = trimmedEntries.slice(-keepCount);
|
|
207
|
-
trimmedCursor = trimmedEntries.length - 1;
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
const nextEntries = [...trimmedEntries, nextEntry];
|
|
211
|
-
const newCursor = trimmedCursor + 1;
|
|
212
|
-
const nextOriginal = [...removeOccurrencesFromOrder(state.originalOrder, droppedEntries), nextEntry.id].slice(-maxEntries);
|
|
213
|
-
return {
|
|
214
|
-
nextEntry,
|
|
215
|
-
nextState: {
|
|
216
|
-
...state,
|
|
217
|
-
continuation: rest.length > 0 ? {
|
|
218
|
-
entries: rest,
|
|
219
|
-
title: continuation.title
|
|
220
|
-
} : void 0,
|
|
221
|
-
cursor: newCursor,
|
|
222
|
-
entries: nextEntries,
|
|
223
|
-
history: appendHistory(state.history, current, maxEntries),
|
|
224
|
-
originalOrder: nextOriginal,
|
|
225
|
-
undoStop: void 0
|
|
383
|
+
function stopTransition(cfg, s, positionSeconds, paused) {
|
|
384
|
+
const cur = curOf(s);
|
|
385
|
+
if (!cur) return [{
|
|
386
|
+
state: s,
|
|
387
|
+
undoToken: void 0
|
|
388
|
+
}, s];
|
|
389
|
+
const token = cfg.nextToken();
|
|
390
|
+
const next = {
|
|
391
|
+
...s,
|
|
392
|
+
cursor: -1,
|
|
393
|
+
undoStop: {
|
|
394
|
+
cursor: s.cursor,
|
|
395
|
+
entryId: cur.id,
|
|
396
|
+
expiresAtMs: cfg.now() + cfg.undoStopTtl,
|
|
397
|
+
mutated: false,
|
|
398
|
+
paused,
|
|
399
|
+
positionSeconds,
|
|
400
|
+
token
|
|
226
401
|
}
|
|
227
402
|
};
|
|
403
|
+
return [{
|
|
404
|
+
state: next,
|
|
405
|
+
undoToken: token
|
|
406
|
+
}, next];
|
|
228
407
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
}
|
|
244
|
-
if (preShuffleRank) {
|
|
245
|
-
for (const bucket of buckets.values()) if (bucket.items.length > 1) bucket.items.sort((a, b) => (preShuffleRank.get(a) ?? Number.MAX_SAFE_INTEGER) - (preShuffleRank.get(b) ?? Number.MAX_SAFE_INTEGER));
|
|
246
|
-
}
|
|
247
|
-
const restored = [];
|
|
248
|
-
for (const id of state.originalOrder) {
|
|
249
|
-
const bucket = buckets.get(id);
|
|
250
|
-
if (bucket && bucket.offset < bucket.items.length) {
|
|
251
|
-
const item = bucket.items[bucket.offset];
|
|
252
|
-
bucket.offset += 1;
|
|
253
|
-
if (item !== void 0) restored.push(item);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
for (const bucket of buckets.values()) while (bucket.offset < bucket.items.length) {
|
|
257
|
-
const item = bucket.items[bucket.offset];
|
|
258
|
-
bucket.offset += 1;
|
|
259
|
-
if (item !== void 0) restored.push(item);
|
|
260
|
-
}
|
|
261
|
-
let nextCursor = state.cursor;
|
|
262
|
-
if (current !== void 0) {
|
|
263
|
-
const refIdx = restored.indexOf(current);
|
|
264
|
-
nextCursor = refIdx === -1 ? restored.findIndex((entry) => entry.id === current.id) : refIdx;
|
|
265
|
-
}
|
|
266
|
-
return {
|
|
267
|
-
cursor: nextCursor,
|
|
268
|
-
entries: restored
|
|
408
|
+
function undoStopTransition(cfg, s, token) {
|
|
409
|
+
const snap = s.undoStop;
|
|
410
|
+
if (!snap || snap.token !== token) return [qErr("UNDO_MISMATCH", "Undo stop token mismatch."), s];
|
|
411
|
+
const cleared = {
|
|
412
|
+
...s,
|
|
413
|
+
undoStop: void 0
|
|
414
|
+
};
|
|
415
|
+
if (snap.mutated) return [qErr("UNDO_MISMATCH", "Queue mutated after stop."), cleared];
|
|
416
|
+
if (cfg.now() > snap.expiresAtMs) return [qErr("UNDO_EXPIRED", "Undo stop expired."), cleared];
|
|
417
|
+
const idx = s.entries.findIndex((e) => e.id === snap.entryId);
|
|
418
|
+
if (idx === -1) return [qErr("ENTRY_NOT_FOUND", "Stopped entry missing."), s];
|
|
419
|
+
const next = {
|
|
420
|
+
...cleared,
|
|
421
|
+
cursor: idx
|
|
269
422
|
};
|
|
423
|
+
return [{
|
|
424
|
+
paused: snap.paused,
|
|
425
|
+
positionSeconds: snap.positionSeconds,
|
|
426
|
+
state: next
|
|
427
|
+
}, next];
|
|
270
428
|
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
const
|
|
320
|
-
const
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
}, nextState];
|
|
327
|
-
}).pipe(Effect.tap(({ changed, nextState }) => changed ? publishState(nextState) : Effect.void), Effect.flatMap(({ outcome }) => outcome.ok ? Effect.succeed(outcome.value) : Effect.fail(outcome.error)));
|
|
328
|
-
const getState = Ref.get(ref);
|
|
329
|
-
const currentEntry = getState.pipe(Effect.map((state) => state.cursor >= 0 ? state.entries[state.cursor] : void 0));
|
|
330
|
-
const upcoming = getState.pipe(Effect.map((state) => state.cursor >= 0 ? state.entries.slice(state.cursor + 1) : state.entries));
|
|
331
|
-
const enqueue = (entries, placement = "end") => modifyStateOrFail((rawState) => {
|
|
332
|
-
if (entries.length === 0) {
|
|
333
|
-
const state = markUndoStopMutated(rawState);
|
|
334
|
-
return {
|
|
335
|
-
changed: state !== rawState,
|
|
336
|
-
nextState: state,
|
|
337
|
-
ok: true,
|
|
338
|
-
value: state
|
|
339
|
-
};
|
|
340
|
-
}
|
|
341
|
-
const state = markUndoStopMutated(rawState);
|
|
342
|
-
if (state.entries.length + entries.length > maxEntries) return {
|
|
343
|
-
error: new AudioQueueError({
|
|
344
|
-
code: "QUEUE_FULL",
|
|
345
|
-
message: `Queue capacity of ${maxEntries} entries exceeded.`
|
|
346
|
-
}),
|
|
347
|
-
ok: false
|
|
348
|
-
};
|
|
349
|
-
const insertAt = placement === "next" && state.cursor >= 0 ? state.cursor + 1 : state.entries.length;
|
|
350
|
-
const nextEntries = [
|
|
351
|
-
...state.entries.slice(0, insertAt),
|
|
352
|
-
...entries,
|
|
353
|
-
...state.entries.slice(insertAt)
|
|
354
|
-
];
|
|
355
|
-
const newIds = entries.map((entry) => entry.id);
|
|
356
|
-
let nextOriginal;
|
|
357
|
-
if (!state.shuffle) nextOriginal = nextEntries.map((entry) => entry.id);
|
|
358
|
-
else if (placement === "next" && state.cursor >= 0) {
|
|
359
|
-
const orderAnchor = findCursorIndexInOrder(state);
|
|
360
|
-
const orderInsertAt = orderAnchor === -1 ? Math.min(insertAt, state.originalOrder.length) : orderAnchor + 1;
|
|
361
|
-
nextOriginal = [
|
|
362
|
-
...state.originalOrder.slice(0, orderInsertAt),
|
|
363
|
-
...newIds,
|
|
364
|
-
...state.originalOrder.slice(orderInsertAt)
|
|
365
|
-
];
|
|
366
|
-
} else nextOriginal = [...state.originalOrder, ...newIds];
|
|
367
|
-
const nextCursor = state.cursor === -1 ? 0 : state.cursor;
|
|
368
|
-
const nextState = {
|
|
369
|
-
...state,
|
|
370
|
-
cursor: nextCursor,
|
|
371
|
-
entries: nextEntries,
|
|
372
|
-
originalOrder: nextOriginal,
|
|
373
|
-
undoRemoval: void 0
|
|
374
|
-
};
|
|
375
|
-
return {
|
|
376
|
-
changed: true,
|
|
377
|
-
nextState,
|
|
378
|
-
ok: true,
|
|
379
|
-
value: nextState
|
|
380
|
-
};
|
|
381
|
-
});
|
|
382
|
-
const load = (entries, mode = "replace") => {
|
|
383
|
-
if (mode === "append") return enqueue(entries, "end");
|
|
384
|
-
return modifyStateOrFail((prev) => {
|
|
385
|
-
if (entries.length > maxEntries) return {
|
|
386
|
-
error: new AudioQueueError({
|
|
387
|
-
code: "QUEUE_FULL",
|
|
388
|
-
message: `Cannot load ${entries.length} entries; max is ${maxEntries}.`
|
|
389
|
-
}),
|
|
390
|
-
ok: false
|
|
391
|
-
};
|
|
392
|
-
const current = prev.cursor >= 0 ? prev.entries[prev.cursor] : void 0;
|
|
393
|
-
const nextEntries = [...entries];
|
|
394
|
-
const nextState = {
|
|
395
|
-
...prev,
|
|
396
|
-
continuation: void 0,
|
|
397
|
-
cursor: nextEntries.length > 0 ? 0 : -1,
|
|
398
|
-
entries: nextEntries,
|
|
399
|
-
history: mode === "play" ? appendHistory(prev.history, current, maxEntries) : prev.history,
|
|
400
|
-
originalOrder: nextEntries.map((entry) => entry.id),
|
|
401
|
-
shuffle: false,
|
|
402
|
-
shuffleSeed: void 0,
|
|
403
|
-
undoRemoval: void 0,
|
|
404
|
-
undoStop: void 0
|
|
405
|
-
};
|
|
406
|
-
return {
|
|
407
|
-
changed: true,
|
|
408
|
-
nextState,
|
|
409
|
-
ok: true,
|
|
410
|
-
value: nextState
|
|
411
|
-
};
|
|
412
|
-
});
|
|
413
|
-
};
|
|
414
|
-
const remove = (entryIds) => modifyStateOrFail((rawState) => {
|
|
415
|
-
const state = markUndoStopMutated(rawState);
|
|
416
|
-
if (entryIds.length === 0) return {
|
|
417
|
-
changed: state !== rawState,
|
|
418
|
-
nextState: state,
|
|
419
|
-
ok: true,
|
|
420
|
-
value: {
|
|
421
|
-
removed: [],
|
|
422
|
-
state,
|
|
423
|
-
undoToken: void 0
|
|
424
|
-
}
|
|
425
|
-
};
|
|
426
|
-
const idSet = new Set(entryIds);
|
|
427
|
-
let firstRemovedIndex = -1;
|
|
428
|
-
const removed = [];
|
|
429
|
-
const positions = [];
|
|
430
|
-
const remaining = [];
|
|
431
|
-
for (let i = 0; i < state.entries.length; i += 1) {
|
|
432
|
-
const entry = state.entries[i];
|
|
433
|
-
if (!entry) continue;
|
|
434
|
-
if (idSet.has(entry.id)) {
|
|
435
|
-
if (firstRemovedIndex === -1) firstRemovedIndex = i;
|
|
436
|
-
removed.push(entry);
|
|
437
|
-
positions.push({
|
|
438
|
-
entry,
|
|
439
|
-
index: i
|
|
440
|
-
});
|
|
441
|
-
} else remaining.push(entry);
|
|
442
|
-
}
|
|
443
|
-
if (removed.length === 0) return {
|
|
444
|
-
error: new AudioQueueError({
|
|
445
|
-
code: "ENTRY_NOT_FOUND",
|
|
446
|
-
message: "None of the specified queue entries were found."
|
|
447
|
-
}),
|
|
448
|
-
ok: false
|
|
449
|
-
};
|
|
450
|
-
const currentBefore = state.cursor >= 0 ? state.entries[state.cursor] : void 0;
|
|
451
|
-
let nextCursor = state.cursor;
|
|
452
|
-
if (remaining.length === 0) nextCursor = -1;
|
|
453
|
-
else if (currentBefore && !idSet.has(currentBefore.id)) {
|
|
454
|
-
const refIdx = remaining.indexOf(currentBefore);
|
|
455
|
-
nextCursor = refIdx === -1 ? remaining.findIndex((e) => e.id === currentBefore.id) : refIdx;
|
|
456
|
-
} else nextCursor = Math.min(state.cursor, remaining.length - 1);
|
|
457
|
-
const token = nextToken();
|
|
458
|
-
const nextState = {
|
|
459
|
-
...state,
|
|
460
|
-
cursor: nextCursor,
|
|
461
|
-
entries: remaining,
|
|
462
|
-
originalOrder: state.originalOrder.filter((id) => !idSet.has(id)),
|
|
463
|
-
undoRemoval: {
|
|
464
|
-
currentEntryId: nextCursor >= 0 ? remaining[nextCursor]?.id : void 0,
|
|
465
|
-
entries: removed,
|
|
466
|
-
expiresAtMs: now() + undoRemovalTtlMs,
|
|
467
|
-
index: firstRemovedIndex,
|
|
468
|
-
positions,
|
|
469
|
-
priorOriginalOrder: state.originalOrder,
|
|
470
|
-
queueIds: remaining.map((entry) => entry.id),
|
|
471
|
-
token
|
|
472
|
-
}
|
|
473
|
-
};
|
|
474
|
-
return {
|
|
475
|
-
changed: true,
|
|
476
|
-
nextState,
|
|
477
|
-
ok: true,
|
|
478
|
-
value: {
|
|
479
|
-
removed,
|
|
480
|
-
state: nextState,
|
|
481
|
-
undoToken: token
|
|
482
|
-
}
|
|
483
|
-
};
|
|
484
|
-
});
|
|
485
|
-
const clearUpNext = () => modifyState((rawState) => {
|
|
486
|
-
const state = markUndoStopMutated(rawState);
|
|
487
|
-
const splitAt = state.cursor >= 0 ? state.cursor + 1 : 0;
|
|
488
|
-
const removed = state.entries.slice(splitAt);
|
|
489
|
-
if (removed.length === 0) return [
|
|
490
|
-
{
|
|
491
|
-
removed: [],
|
|
492
|
-
state,
|
|
493
|
-
undoToken: void 0
|
|
494
|
-
},
|
|
495
|
-
state,
|
|
496
|
-
state !== rawState
|
|
497
|
-
];
|
|
498
|
-
const remaining = state.entries.slice(0, splitAt);
|
|
499
|
-
const token = nextToken();
|
|
500
|
-
const nextState = {
|
|
501
|
-
...state,
|
|
502
|
-
entries: remaining,
|
|
503
|
-
originalOrder: removeOccurrencesFromOrder(state.originalOrder, removed),
|
|
504
|
-
undoRemoval: {
|
|
505
|
-
currentEntryId: state.cursor >= 0 ? remaining[state.cursor]?.id : void 0,
|
|
506
|
-
entries: removed,
|
|
507
|
-
expiresAtMs: now() + undoRemovalTtlMs,
|
|
508
|
-
index: splitAt,
|
|
509
|
-
positions: removed.map((entry, idx) => ({
|
|
510
|
-
entry,
|
|
511
|
-
index: splitAt + idx
|
|
512
|
-
})),
|
|
513
|
-
priorOriginalOrder: state.originalOrder,
|
|
514
|
-
queueIds: remaining.map((entry) => entry.id),
|
|
515
|
-
token
|
|
516
|
-
}
|
|
517
|
-
};
|
|
518
|
-
return [
|
|
519
|
-
{
|
|
520
|
-
removed,
|
|
521
|
-
state: nextState,
|
|
522
|
-
undoToken: token
|
|
523
|
-
},
|
|
524
|
-
nextState,
|
|
525
|
-
true
|
|
526
|
-
];
|
|
527
|
-
});
|
|
528
|
-
const restoreRemoved = (token) => modifyStateOrFail((state) => {
|
|
529
|
-
const snapshot = state.undoRemoval;
|
|
530
|
-
if (!snapshot || snapshot.token !== token) return {
|
|
531
|
-
error: new AudioQueueError({
|
|
532
|
-
code: "UNDO_MISMATCH",
|
|
533
|
-
message: "Undo removal snapshot does not match token."
|
|
534
|
-
}),
|
|
535
|
-
ok: false
|
|
536
|
-
};
|
|
537
|
-
if (now() > snapshot.expiresAtMs) return {
|
|
538
|
-
error: new AudioQueueError({
|
|
539
|
-
code: "UNDO_EXPIRED",
|
|
540
|
-
message: "Undo removal window has expired."
|
|
541
|
-
}),
|
|
542
|
-
nextState: {
|
|
543
|
-
...state,
|
|
544
|
-
undoRemoval: void 0
|
|
545
|
-
},
|
|
546
|
-
ok: false
|
|
547
|
-
};
|
|
548
|
-
const currentEntryBefore = state.cursor >= 0 ? state.entries[state.cursor] : void 0;
|
|
549
|
-
const currentId = currentEntryBefore?.id;
|
|
550
|
-
const currentIds = state.entries.map((entry) => entry.id);
|
|
551
|
-
if (currentId !== snapshot.currentEntryId || !arraysEqual(currentIds, snapshot.queueIds)) return {
|
|
552
|
-
error: new AudioQueueError({
|
|
553
|
-
code: "UNDO_MISMATCH",
|
|
554
|
-
message: "Queue changed since removal; cannot safely restore."
|
|
555
|
-
}),
|
|
556
|
-
ok: false
|
|
557
|
-
};
|
|
558
|
-
if (state.entries.length + snapshot.entries.length > maxEntries) return {
|
|
559
|
-
error: new AudioQueueError({
|
|
560
|
-
code: "QUEUE_FULL",
|
|
561
|
-
message: `Restoring entries would exceed queue capacity (${maxEntries}).`
|
|
562
|
-
}),
|
|
563
|
-
ok: false
|
|
564
|
-
};
|
|
565
|
-
const restoredEntries = [...state.entries];
|
|
566
|
-
const sortedPositions = [...snapshot.positions].toSorted((a, b) => a.index - b.index);
|
|
567
|
-
for (const item of sortedPositions) {
|
|
568
|
-
const targetIdx = Math.min(item.index, restoredEntries.length);
|
|
569
|
-
restoredEntries.splice(targetIdx, 0, item.entry);
|
|
570
|
-
}
|
|
571
|
-
let restoredCursor = -1;
|
|
572
|
-
if (currentEntryBefore !== void 0) {
|
|
573
|
-
const refIdx = restoredEntries.indexOf(currentEntryBefore);
|
|
574
|
-
restoredCursor = refIdx === -1 ? restoredEntries.findIndex((e) => e.id === currentId) : refIdx;
|
|
575
|
-
} else if (restoredEntries.length > 0) restoredCursor = Math.min(snapshot.index, restoredEntries.length - 1);
|
|
576
|
-
const nextState = {
|
|
577
|
-
...state,
|
|
578
|
-
cursor: restoredCursor,
|
|
579
|
-
entries: restoredEntries,
|
|
580
|
-
originalOrder: snapshot.priorOriginalOrder,
|
|
581
|
-
undoRemoval: void 0
|
|
582
|
-
};
|
|
583
|
-
return {
|
|
584
|
-
changed: true,
|
|
585
|
-
nextState,
|
|
586
|
-
ok: true,
|
|
587
|
-
value: nextState
|
|
588
|
-
};
|
|
589
|
-
});
|
|
590
|
-
const move = (entryId, beforeEntryId) => modifyStateOrFail((rawState) => {
|
|
591
|
-
const state = markUndoStopMutated(rawState);
|
|
592
|
-
const fromIndex = state.entries.findIndex((e) => e.id === entryId);
|
|
593
|
-
if (fromIndex === -1) return {
|
|
594
|
-
error: new AudioQueueError({
|
|
595
|
-
code: "ENTRY_NOT_FOUND",
|
|
596
|
-
message: `Queue entry "${entryId}" not found.`
|
|
597
|
-
}),
|
|
598
|
-
ok: false
|
|
599
|
-
};
|
|
600
|
-
const moving = state.entries[fromIndex];
|
|
601
|
-
if (!moving) return {
|
|
602
|
-
changed: state !== rawState,
|
|
603
|
-
nextState: state,
|
|
604
|
-
ok: true,
|
|
605
|
-
value: state
|
|
606
|
-
};
|
|
607
|
-
if (entryId === beforeEntryId) return {
|
|
608
|
-
changed: state !== rawState,
|
|
609
|
-
nextState: state,
|
|
610
|
-
ok: true,
|
|
611
|
-
value: state
|
|
612
|
-
};
|
|
613
|
-
const currentBefore = state.cursor >= 0 ? state.entries[state.cursor] : void 0;
|
|
614
|
-
const without = [...state.entries.slice(0, fromIndex), ...state.entries.slice(fromIndex + 1)];
|
|
615
|
-
let toIndex = without.length;
|
|
616
|
-
if (beforeEntryId !== void 0) {
|
|
617
|
-
const targetIdx = without.findIndex((e) => e.id === beforeEntryId);
|
|
618
|
-
if (targetIdx === -1) return {
|
|
619
|
-
error: new AudioQueueError({
|
|
620
|
-
code: "ENTRY_NOT_FOUND",
|
|
621
|
-
message: `Target entry "${beforeEntryId}" not found.`
|
|
622
|
-
}),
|
|
623
|
-
ok: false
|
|
624
|
-
};
|
|
625
|
-
toIndex = targetIdx;
|
|
626
|
-
}
|
|
627
|
-
const nextEntries = [
|
|
628
|
-
...without.slice(0, toIndex),
|
|
629
|
-
moving,
|
|
630
|
-
...without.slice(toIndex)
|
|
631
|
-
];
|
|
632
|
-
let nextCursor = state.cursor;
|
|
633
|
-
if (currentBefore !== void 0) {
|
|
634
|
-
const refIdx = nextEntries.indexOf(currentBefore);
|
|
635
|
-
nextCursor = refIdx === -1 ? nextEntries.findIndex((e) => e.id === currentBefore.id) : refIdx;
|
|
636
|
-
}
|
|
637
|
-
const nextOriginalOrder = updateOriginalOrderOnMove(state, nextEntries, entryId, beforeEntryId);
|
|
638
|
-
const nextState = {
|
|
639
|
-
...state,
|
|
640
|
-
cursor: nextCursor,
|
|
641
|
-
entries: nextEntries,
|
|
642
|
-
originalOrder: nextOriginalOrder,
|
|
643
|
-
undoRemoval: void 0
|
|
644
|
-
};
|
|
645
|
-
return {
|
|
646
|
-
changed: true,
|
|
647
|
-
nextState,
|
|
648
|
-
ok: true,
|
|
649
|
-
value: nextState
|
|
429
|
+
// src/queue/state.ts
|
|
430
|
+
var QueueService = class extends Context.Service()("@avoid.quest/audio/QueueService") {};
|
|
431
|
+
function buildAudioQueue(ref, initial, options) {
|
|
432
|
+
const renewAheadMs = options.renewAheadMs ?? 6e4;
|
|
433
|
+
const preShuffleRank = /* @__PURE__ */ new WeakMap();
|
|
434
|
+
const cfg = {
|
|
435
|
+
max: Math.max(1, Math.floor(options.maxEntries ?? 500)),
|
|
436
|
+
nextToken: options.nextToken ?? defaultTokenGen(),
|
|
437
|
+
now: options.now ?? (() => Date.now()),
|
|
438
|
+
undoRemTtl: options.undoRemovalTtlMs ?? 6e3,
|
|
439
|
+
undoStopTtl: options.undoStopTtlMs ?? 11e3
|
|
440
|
+
};
|
|
441
|
+
const atomRef = AtomRef.make(initial);
|
|
442
|
+
const stateAtom = Atom.readable((get) => {
|
|
443
|
+
get.addFinalizer(atomRef.subscribe((v) => get.setSelf(v)));
|
|
444
|
+
return atomRef.value;
|
|
445
|
+
});
|
|
446
|
+
let generation = 0;
|
|
447
|
+
const tx = (fn) => Ref.modify(ref, (s) => {
|
|
448
|
+
const [val, next] = fn(s);
|
|
449
|
+
const changed = next !== s;
|
|
450
|
+
if (changed) generation += 1;
|
|
451
|
+
return [{
|
|
452
|
+
changed,
|
|
453
|
+
next,
|
|
454
|
+
val
|
|
455
|
+
}, next];
|
|
456
|
+
}).pipe(Effect.tap(({ changed, next }) => changed ? Effect.sync(() => atomRef.set(next)) : Effect.void), Effect.map(({ val }) => val));
|
|
457
|
+
const txErr = (fn) => tx(fn).pipe(Effect.flatMap((out) => out instanceof AudioQueueError ? Effect.fail(out) : Effect.succeed(out)));
|
|
458
|
+
const txState = (fn) => txErr((s) => {
|
|
459
|
+
const out = fn(s);
|
|
460
|
+
return out instanceof AudioQueueError ? [out, s] : [out, out];
|
|
461
|
+
});
|
|
462
|
+
const update = (fn) => tx((s) => {
|
|
463
|
+
const next = fn(s);
|
|
464
|
+
return [next, next];
|
|
465
|
+
});
|
|
466
|
+
const getState = Ref.get(ref);
|
|
467
|
+
const findEntry = (id) => getState.pipe(Effect.flatMap((s) => {
|
|
468
|
+
const entry = s.entries.find((e) => e.id === id);
|
|
469
|
+
return entry ? Effect.succeed(entry) : Effect.fail(qErr("ENTRY_NOT_FOUND", `Entry "${id}" not found.`));
|
|
470
|
+
}));
|
|
471
|
+
const enqueue = (entries, placement = "end") => txState((s) => enqueueTransition(s, entries, placement, cfg.max));
|
|
472
|
+
const renewEntryUrl = (entryId, reason = "expired") => Effect.gen(function* () {
|
|
473
|
+
const entry = yield* findEntry(entryId);
|
|
474
|
+
if (!options.onRenewUrl) return yield* Effect.fail(qErr("RENEWAL_FAILED", "No onRenewUrl hook."));
|
|
475
|
+
const renewed = yield* options.onRenewUrl(entry, reason);
|
|
476
|
+
return yield* tx((latest) => {
|
|
477
|
+
const idx = latest.entries.findIndex((e) => e.id === entryId);
|
|
478
|
+
const base = idx === -1 ? entry : latest.entries[idx] ?? entry;
|
|
479
|
+
const updated = {
|
|
480
|
+
...base,
|
|
481
|
+
durationSeconds: renewed.durationSeconds ?? base.durationSeconds,
|
|
482
|
+
expiresAtMs: renewed.expiresAtMs ?? extractLinkExpiryMs(renewed.url),
|
|
483
|
+
url: renewed.url
|
|
650
484
|
};
|
|
485
|
+
return idx === -1 ? [updated, latest] : [updated, {
|
|
486
|
+
...latest,
|
|
487
|
+
entries: latest.entries.with(idx, updated)
|
|
488
|
+
}];
|
|
651
489
|
});
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
const
|
|
656
|
-
if (
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
changed: true,
|
|
666
|
-
nextState: {
|
|
667
|
-
...state,
|
|
668
|
-
cursor: targetIndex,
|
|
669
|
-
history: current && current.id !== target.id ? appendHistory(state.history, current, maxEntries) : state.history,
|
|
490
|
+
});
|
|
491
|
+
const next = () => Effect.gen(function* () {
|
|
492
|
+
const out = yield* tx((s) => {
|
|
493
|
+
const cur = curOf(s);
|
|
494
|
+
if (s.entries.length === 0) return [void 0, s];
|
|
495
|
+
if (s.repeat === "one" && cur) return [cur, s];
|
|
496
|
+
const nextIdx = s.cursor + 1;
|
|
497
|
+
if (nextIdx < s.entries.length || s.repeat === "all") {
|
|
498
|
+
const idx = nextIdx < s.entries.length ? nextIdx : 0;
|
|
499
|
+
return [s.entries[idx], {
|
|
500
|
+
...s,
|
|
501
|
+
cursor: idx,
|
|
502
|
+
history: pushHist(s.history, cur, cfg.max),
|
|
670
503
|
undoStop: void 0
|
|
671
|
-
}
|
|
672
|
-
ok: true,
|
|
673
|
-
value: target
|
|
674
|
-
};
|
|
675
|
-
});
|
|
676
|
-
const next = () => Effect.gen(function* () {
|
|
677
|
-
const step = yield* modifyState((state) => {
|
|
678
|
-
if (state.entries.length === 0) return [
|
|
679
|
-
{
|
|
680
|
-
entry: void 0,
|
|
681
|
-
kind: "done"
|
|
682
|
-
},
|
|
683
|
-
state,
|
|
684
|
-
false
|
|
685
|
-
];
|
|
686
|
-
const current = state.cursor >= 0 ? state.entries[state.cursor] : void 0;
|
|
687
|
-
if (state.repeat === "one" && current !== void 0) return [
|
|
688
|
-
{
|
|
689
|
-
entry: current,
|
|
690
|
-
kind: "done"
|
|
691
|
-
},
|
|
692
|
-
state,
|
|
693
|
-
false
|
|
694
|
-
];
|
|
695
|
-
const nextIndex = state.cursor + 1;
|
|
696
|
-
if (nextIndex < state.entries.length) {
|
|
697
|
-
const nextEntry = state.entries[nextIndex];
|
|
698
|
-
const nextState = {
|
|
699
|
-
...state,
|
|
700
|
-
cursor: nextIndex,
|
|
701
|
-
history: appendHistory(state.history, current, maxEntries),
|
|
702
|
-
undoStop: void 0
|
|
703
|
-
};
|
|
704
|
-
return [
|
|
705
|
-
{
|
|
706
|
-
entry: nextEntry,
|
|
707
|
-
kind: "done"
|
|
708
|
-
},
|
|
709
|
-
nextState,
|
|
710
|
-
true
|
|
711
|
-
];
|
|
712
|
-
}
|
|
713
|
-
if (state.repeat === "all") {
|
|
714
|
-
const [first] = state.entries;
|
|
715
|
-
const nextState = {
|
|
716
|
-
...state,
|
|
717
|
-
cursor: 0,
|
|
718
|
-
history: appendHistory(state.history, current, maxEntries),
|
|
719
|
-
undoStop: void 0
|
|
720
|
-
};
|
|
721
|
-
return [
|
|
722
|
-
{
|
|
723
|
-
entry: first,
|
|
724
|
-
kind: "done"
|
|
725
|
-
},
|
|
726
|
-
nextState,
|
|
727
|
-
true
|
|
728
|
-
];
|
|
729
|
-
}
|
|
730
|
-
if (state.continuation && state.continuation.entries.length > 0) {
|
|
731
|
-
const advanced = advanceWithContinuation(state, state.continuation, current, maxEntries);
|
|
732
|
-
if (advanced) return [
|
|
733
|
-
{
|
|
734
|
-
entry: advanced.nextEntry,
|
|
735
|
-
kind: "done"
|
|
736
|
-
},
|
|
737
|
-
advanced.nextState,
|
|
738
|
-
true
|
|
739
|
-
];
|
|
740
|
-
}
|
|
741
|
-
if (options.onAutoplay && current !== void 0) return [
|
|
742
|
-
{
|
|
743
|
-
current,
|
|
744
|
-
kind: "fetchAutoplay",
|
|
745
|
-
snapshot: state,
|
|
746
|
-
startGeneration: generation
|
|
747
|
-
},
|
|
748
|
-
state,
|
|
749
|
-
false
|
|
750
|
-
];
|
|
751
|
-
return [
|
|
752
|
-
{
|
|
753
|
-
entry: void 0,
|
|
754
|
-
kind: "done"
|
|
755
|
-
},
|
|
756
|
-
state,
|
|
757
|
-
false
|
|
758
|
-
];
|
|
759
|
-
});
|
|
760
|
-
if (step.kind === "done") return step.entry;
|
|
761
|
-
const { onAutoplay } = options;
|
|
762
|
-
if (!onAutoplay) return;
|
|
763
|
-
const fetched = yield* onAutoplay(step.current, step.snapshot);
|
|
764
|
-
if (!fetched || fetched.entries.length === 0) return;
|
|
765
|
-
return yield* modifyState((latestState) => {
|
|
766
|
-
if (generation !== step.startGeneration) return [
|
|
767
|
-
void 0,
|
|
768
|
-
latestState,
|
|
769
|
-
false
|
|
770
|
-
];
|
|
771
|
-
const latestCurrent = latestState.cursor >= 0 ? latestState.entries[latestState.cursor] : void 0;
|
|
772
|
-
const advanced = advanceWithContinuation(latestState, fetched, latestCurrent, maxEntries);
|
|
773
|
-
if (!advanced) return [
|
|
774
|
-
void 0,
|
|
775
|
-
latestState,
|
|
776
|
-
false
|
|
777
|
-
];
|
|
778
|
-
return [
|
|
779
|
-
advanced.nextEntry,
|
|
780
|
-
advanced.nextState,
|
|
781
|
-
true
|
|
782
|
-
];
|
|
783
|
-
});
|
|
784
|
-
});
|
|
785
|
-
const previous = () => modifyState((state) => {
|
|
786
|
-
if (state.entries.length === 0) return [
|
|
787
|
-
void 0,
|
|
788
|
-
state,
|
|
789
|
-
false
|
|
790
|
-
];
|
|
791
|
-
if (state.cursor > 0) {
|
|
792
|
-
const prevIndex = state.cursor - 1;
|
|
793
|
-
return [
|
|
794
|
-
state.entries[prevIndex],
|
|
795
|
-
{
|
|
796
|
-
...state,
|
|
797
|
-
cursor: prevIndex,
|
|
798
|
-
undoStop: void 0
|
|
799
|
-
},
|
|
800
|
-
true
|
|
801
|
-
];
|
|
504
|
+
}];
|
|
802
505
|
}
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
undoStop: void 0
|
|
811
|
-
},
|
|
812
|
-
true
|
|
813
|
-
];
|
|
814
|
-
}
|
|
815
|
-
return [
|
|
816
|
-
state.entries[0],
|
|
817
|
-
state,
|
|
818
|
-
false
|
|
819
|
-
];
|
|
820
|
-
});
|
|
821
|
-
const setRepeat = (mode) => modifyState((state) => {
|
|
822
|
-
const nextState = {
|
|
823
|
-
...state,
|
|
824
|
-
repeat: mode
|
|
825
|
-
};
|
|
826
|
-
return [
|
|
827
|
-
nextState,
|
|
828
|
-
nextState,
|
|
829
|
-
true
|
|
830
|
-
];
|
|
831
|
-
});
|
|
832
|
-
const setShuffle = (enabled, seed) => modifyState((state) => {
|
|
833
|
-
if (enabled === state.shuffle && (seed === void 0 || seed === state.shuffleSeed)) return [
|
|
834
|
-
state,
|
|
835
|
-
state,
|
|
836
|
-
false
|
|
837
|
-
];
|
|
838
|
-
const current = state.cursor >= 0 ? state.entries[state.cursor] : void 0;
|
|
839
|
-
if (enabled) {
|
|
840
|
-
const chosenSeed = seed ?? Math.trunc(now()) % 4294967296;
|
|
841
|
-
if (!state.shuffle) for (const [idx, entry] of state.entries.entries()) preShuffleRank.set(entry, idx);
|
|
842
|
-
const originalOrder = state.shuffle ? state.originalOrder : state.entries.map((entry) => entry.id);
|
|
843
|
-
if (state.cursor >= 0 && current !== void 0) {
|
|
844
|
-
const played = state.entries.slice(0, state.cursor + 1);
|
|
845
|
-
const shuffledRest = fisherYatesShuffle(state.entries.slice(state.cursor + 1), chosenSeed);
|
|
846
|
-
const nextState = {
|
|
847
|
-
...state,
|
|
848
|
-
entries: [...played, ...shuffledRest],
|
|
849
|
-
originalOrder,
|
|
850
|
-
shuffle: true,
|
|
851
|
-
shuffleSeed: chosenSeed
|
|
852
|
-
};
|
|
853
|
-
return [
|
|
854
|
-
nextState,
|
|
855
|
-
nextState,
|
|
856
|
-
true
|
|
857
|
-
];
|
|
858
|
-
}
|
|
859
|
-
const nextState = {
|
|
860
|
-
...state,
|
|
861
|
-
entries: fisherYatesShuffle(state.entries, chosenSeed),
|
|
862
|
-
originalOrder,
|
|
863
|
-
shuffle: true,
|
|
864
|
-
shuffleSeed: chosenSeed
|
|
865
|
-
};
|
|
866
|
-
return [
|
|
867
|
-
nextState,
|
|
868
|
-
nextState,
|
|
869
|
-
true
|
|
870
|
-
];
|
|
871
|
-
}
|
|
872
|
-
const { cursor: nextCursor, entries: restored } = unshuffleEntries(state, preShuffleRank);
|
|
873
|
-
const nextState = {
|
|
874
|
-
...state,
|
|
875
|
-
cursor: nextCursor,
|
|
876
|
-
entries: restored,
|
|
877
|
-
shuffle: false,
|
|
878
|
-
shuffleSeed: void 0
|
|
879
|
-
};
|
|
880
|
-
return [
|
|
881
|
-
nextState,
|
|
882
|
-
nextState,
|
|
883
|
-
true
|
|
884
|
-
];
|
|
885
|
-
});
|
|
886
|
-
const setContinuation = (continuation) => modifyState((state) => {
|
|
887
|
-
const nextState = {
|
|
888
|
-
...state,
|
|
889
|
-
continuation
|
|
890
|
-
};
|
|
891
|
-
return [
|
|
892
|
-
nextState,
|
|
893
|
-
nextState,
|
|
894
|
-
true
|
|
895
|
-
];
|
|
896
|
-
});
|
|
897
|
-
const stop = (positionSeconds = 0, paused = false) => modifyState((state) => {
|
|
898
|
-
const current = state.cursor >= 0 ? state.entries[state.cursor] : void 0;
|
|
899
|
-
if (!current) return [
|
|
900
|
-
{
|
|
901
|
-
state,
|
|
902
|
-
undoToken: void 0
|
|
903
|
-
},
|
|
904
|
-
state,
|
|
905
|
-
false
|
|
906
|
-
];
|
|
907
|
-
const token = nextToken();
|
|
908
|
-
const nextState = {
|
|
909
|
-
...state,
|
|
910
|
-
cursor: -1,
|
|
911
|
-
undoStop: {
|
|
912
|
-
cursor: state.cursor,
|
|
913
|
-
entryId: current.id,
|
|
914
|
-
expiresAtMs: now() + undoStopTtlMs,
|
|
915
|
-
mutated: false,
|
|
916
|
-
paused,
|
|
917
|
-
positionSeconds,
|
|
918
|
-
token
|
|
919
|
-
}
|
|
920
|
-
};
|
|
921
|
-
return [
|
|
922
|
-
{
|
|
923
|
-
state: nextState,
|
|
924
|
-
undoToken: token
|
|
925
|
-
},
|
|
926
|
-
nextState,
|
|
927
|
-
true
|
|
928
|
-
];
|
|
929
|
-
});
|
|
930
|
-
const undoStop = (token) => modifyStateOrFail((state) => {
|
|
931
|
-
const snapshot = state.undoStop;
|
|
932
|
-
if (!snapshot || snapshot.token !== token) return {
|
|
933
|
-
error: new AudioQueueError({
|
|
934
|
-
code: "UNDO_MISMATCH",
|
|
935
|
-
message: "Undo stop snapshot does not match token."
|
|
936
|
-
}),
|
|
937
|
-
ok: false
|
|
938
|
-
};
|
|
939
|
-
if (snapshot.mutated) return {
|
|
940
|
-
error: new AudioQueueError({
|
|
941
|
-
code: "UNDO_MISMATCH",
|
|
942
|
-
message: "Queue was mutated after stop; cannot undo stop."
|
|
943
|
-
}),
|
|
944
|
-
nextState: {
|
|
945
|
-
...state,
|
|
946
|
-
undoStop: void 0
|
|
947
|
-
},
|
|
948
|
-
ok: false
|
|
949
|
-
};
|
|
950
|
-
if (now() > snapshot.expiresAtMs) return {
|
|
951
|
-
error: new AudioQueueError({
|
|
952
|
-
code: "UNDO_EXPIRED",
|
|
953
|
-
message: "Undo stop window has expired."
|
|
954
|
-
}),
|
|
955
|
-
nextState: {
|
|
956
|
-
...state,
|
|
957
|
-
undoStop: void 0
|
|
958
|
-
},
|
|
959
|
-
ok: false
|
|
960
|
-
};
|
|
961
|
-
const entryIndex = state.entries.findIndex((e) => e.id === snapshot.entryId);
|
|
962
|
-
if (entryIndex === -1) return {
|
|
963
|
-
error: new AudioQueueError({
|
|
964
|
-
code: "ENTRY_NOT_FOUND",
|
|
965
|
-
message: "Stopped entry is no longer in queue."
|
|
966
|
-
}),
|
|
967
|
-
ok: false
|
|
968
|
-
};
|
|
969
|
-
const nextState = {
|
|
970
|
-
...state,
|
|
971
|
-
cursor: entryIndex,
|
|
972
|
-
undoStop: void 0
|
|
973
|
-
};
|
|
974
|
-
return {
|
|
975
|
-
changed: true,
|
|
976
|
-
nextState,
|
|
977
|
-
ok: true,
|
|
978
|
-
value: {
|
|
979
|
-
paused: snapshot.paused,
|
|
980
|
-
positionSeconds: snapshot.positionSeconds,
|
|
981
|
-
state: nextState
|
|
982
|
-
}
|
|
983
|
-
};
|
|
506
|
+
const adv = s.continuation && s.continuation.entries.length > 0 ? advanceCont(s, s.continuation, cur, cfg.max) : void 0;
|
|
507
|
+
if (adv) return adv;
|
|
508
|
+
return options.onAutoplay && cur ? [{
|
|
509
|
+
current: cur,
|
|
510
|
+
gen: generation,
|
|
511
|
+
snapshot: s
|
|
512
|
+
}, s] : [void 0, s];
|
|
984
513
|
});
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
message: `Queue entry "${entryId}" not found.`
|
|
990
|
-
}));
|
|
991
|
-
if (!options.onRenewUrl) return yield* Effect.fail(new AudioQueueError({
|
|
992
|
-
code: "RENEWAL_FAILED",
|
|
993
|
-
message: "No onRenewUrl hook configured for this queue."
|
|
994
|
-
}));
|
|
995
|
-
const renewed = yield* options.onRenewUrl(entry, reason);
|
|
996
|
-
return yield* modifyState((latest) => {
|
|
997
|
-
const latestIndex = latest.entries.findIndex((e) => e.id === entryId);
|
|
998
|
-
const baseEntry = latestIndex === -1 ? entry : latest.entries[latestIndex] ?? entry;
|
|
999
|
-
const updatedEntry = {
|
|
1000
|
-
...baseEntry,
|
|
1001
|
-
durationSeconds: renewed.durationSeconds ?? baseEntry.durationSeconds,
|
|
1002
|
-
expiresAtMs: renewed.expiresAtMs ?? extractLinkExpiryMs(renewed.url),
|
|
1003
|
-
url: renewed.url
|
|
1004
|
-
};
|
|
1005
|
-
if (latestIndex === -1) return [
|
|
1006
|
-
updatedEntry,
|
|
1007
|
-
latest,
|
|
1008
|
-
false
|
|
1009
|
-
];
|
|
1010
|
-
const nextEntries = [...latest.entries];
|
|
1011
|
-
nextEntries[latestIndex] = updatedEntry;
|
|
1012
|
-
return [
|
|
1013
|
-
updatedEntry,
|
|
1014
|
-
{
|
|
1015
|
-
...latest,
|
|
1016
|
-
entries: nextEntries
|
|
1017
|
-
},
|
|
1018
|
-
true
|
|
1019
|
-
];
|
|
1020
|
-
});
|
|
1021
|
-
});
|
|
1022
|
-
const ensureFreshUrl = (entryId) => Effect.gen(function* () {
|
|
1023
|
-
const entry = (yield* Ref.get(ref)).entries.find((e) => e.id === entryId);
|
|
1024
|
-
if (!entry) return yield* Effect.fail(new AudioQueueError({
|
|
1025
|
-
code: "ENTRY_NOT_FOUND",
|
|
1026
|
-
message: `Queue entry "${entryId}" not found.`
|
|
1027
|
-
}));
|
|
1028
|
-
if (options.onRenewUrl && isUrlExpiringSoon(entry, now(), renewAheadMs)) return yield* renewEntryUrl(entryId, "proactive");
|
|
1029
|
-
return entry;
|
|
1030
|
-
});
|
|
1031
|
-
return {
|
|
1032
|
-
atomRef,
|
|
1033
|
-
clearUpNext,
|
|
1034
|
-
currentEntry,
|
|
1035
|
-
enqueue,
|
|
1036
|
-
ensureFreshUrl,
|
|
1037
|
-
getState,
|
|
1038
|
-
jumpTo,
|
|
1039
|
-
load,
|
|
1040
|
-
move,
|
|
1041
|
-
next,
|
|
1042
|
-
previous,
|
|
1043
|
-
ref,
|
|
1044
|
-
remove,
|
|
1045
|
-
renewEntryUrl,
|
|
1046
|
-
restoreRemoved,
|
|
1047
|
-
setContinuation,
|
|
1048
|
-
setRepeat,
|
|
1049
|
-
setShuffle,
|
|
1050
|
-
stateAtom,
|
|
1051
|
-
stop,
|
|
1052
|
-
undoStop,
|
|
1053
|
-
upcoming
|
|
1054
|
-
};
|
|
514
|
+
if (!out || !("snapshot" in out)) return out;
|
|
515
|
+
const fetched = options.onAutoplay ? yield* options.onAutoplay(out.current, out.snapshot) : void 0;
|
|
516
|
+
if (!fetched || fetched.entries.length === 0) return;
|
|
517
|
+
return yield* tx((latest) => generation === out.gen ? advanceCont(latest, fetched, curOf(latest), cfg.max) ?? [void 0, latest] : [void 0, latest]);
|
|
1055
518
|
});
|
|
519
|
+
return {
|
|
520
|
+
atomRef,
|
|
521
|
+
clearUpNext: () => tx((s) => clearUpNextTransition(cfg, s)),
|
|
522
|
+
currentEntry: getState.pipe(Effect.map(curOf)),
|
|
523
|
+
enqueue,
|
|
524
|
+
ensureFreshUrl: (entryId) => Effect.gen(function* () {
|
|
525
|
+
const entry = yield* findEntry(entryId);
|
|
526
|
+
return options.onRenewUrl && isUrlExpiringSoon(entry, cfg.now(), renewAheadMs) ? yield* renewEntryUrl(entryId, "proactive") : entry;
|
|
527
|
+
}),
|
|
528
|
+
getState,
|
|
529
|
+
jumpTo: (entryId) => txErr((s) => jumpToTransition(s, entryId, cfg.max)),
|
|
530
|
+
load: (entries, mode = "replace") => mode === "append" ? enqueue(entries, "end") : txState((s) => loadTransition(s, entries, mode, cfg.max)),
|
|
531
|
+
move: (entryId, beforeEntryId) => txState((s) => moveTransition(s, entryId, beforeEntryId)),
|
|
532
|
+
next,
|
|
533
|
+
previous: () => tx(previousTransition),
|
|
534
|
+
ref,
|
|
535
|
+
remove: (entryIds) => txErr((s) => removeTransition(cfg, s, entryIds)),
|
|
536
|
+
renewEntryUrl,
|
|
537
|
+
restoreRemoved: (token) => txErr((s) => restoreRemovedTransition(cfg, s, token)),
|
|
538
|
+
setContinuation: (continuation) => update((s) => ({
|
|
539
|
+
...s,
|
|
540
|
+
continuation
|
|
541
|
+
})),
|
|
542
|
+
setRepeat: (repeat) => update((s) => ({
|
|
543
|
+
...s,
|
|
544
|
+
repeat
|
|
545
|
+
})),
|
|
546
|
+
setShuffle: (enabled, seed) => update((s) => setShuffleTransition(s, enabled, seed, cfg.now, preShuffleRank)),
|
|
547
|
+
stateAtom,
|
|
548
|
+
stop: (positionSeconds = 0, paused = false) => tx((s) => stopTransition(cfg, s, positionSeconds, paused)),
|
|
549
|
+
undoStop: (token) => txErr((s) => undoStopTransition(cfg, s, token)),
|
|
550
|
+
upcoming: getState.pipe(Effect.map((s) => s.cursor >= 0 ? s.entries.slice(s.cursor + 1) : s.entries))
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
function createAudioQueue(options = {}) {
|
|
554
|
+
const max = Math.max(1, Math.floor(options.maxEntries ?? 500));
|
|
555
|
+
const initial = createInitialQueueState((options.initialEntries ?? []).slice(0, max));
|
|
556
|
+
return Ref.make(initial).pipe(Effect.map((ref) => buildAudioQueue(ref, initial, options)));
|
|
1056
557
|
}
|
|
1057
558
|
function makeQueueServiceLayer(options = {}) {
|
|
1058
559
|
return Layer.effect(QueueService, createAudioQueue(options));
|