@avoid.quest/audio 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +70 -2
- package/dist/effects/index.d.ts +266 -0
- package/dist/effects/index.js +744 -0
- package/dist/effects/index.js.map +1 -0
- package/dist/errors-BT9HxDLn.js +74 -0
- package/dist/errors-BT9HxDLn.js.map +1 -0
- package/dist/errors-ChTQFwuZ.d.ts +53 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/midi/index.d.ts +162 -0
- package/dist/midi/index.js +338 -0
- package/dist/midi/index.js.map +1 -0
- package/dist/playback/index.d.ts +349 -0
- package/dist/playback/index.js +1372 -0
- package/dist/playback/index.js.map +1 -0
- package/dist/queue/index.d.ts +144 -0
- package/dist/queue/index.js +1062 -0
- package/dist/queue/index.js.map +1 -0
- package/dist/recognize/index.d.ts +161 -0
- package/dist/recognize/index.js +2816 -0
- package/dist/recognize/index.js.map +1 -0
- package/dist/routing/index.d.ts +140 -0
- package/dist/routing/index.js +678 -0
- package/dist/routing/index.js.map +1 -0
- package/dist/sync/index.d.ts +282 -0
- package/dist/sync/index.js +1103 -0
- package/dist/sync/index.js.map +1 -0
- package/dist/worklet-loader-BvEW0oVO.d.ts +44 -0
- package/dist/worklet-loader-S7R8ZAmP.js +175 -0
- package/dist/worklet-loader-S7R8ZAmP.js.map +1 -0
- package/dist/worklets/couch-live.d.ts +1 -0
- package/dist/worklets/couch-live.js +365 -0
- package/dist/worklets/couch-live.js.map +1 -0
- package/dist/worklets/meter.d.ts +7 -0
- package/dist/worklets/meter.js +80 -0
- package/dist/worklets/meter.js.map +1 -0
- package/package.json +109 -4
|
@@ -0,0 +1,1062 @@
|
|
|
1
|
+
import { Context, Effect, Layer, Ref, Schema } from "effect";
|
|
2
|
+
import * as Atom from "effect/reactivity/Atom";
|
|
3
|
+
import * as AtomRef from "effect/reactivity/AtomRef";
|
|
4
|
+
import { o as AudioQueueError } from "../errors-BT9HxDLn.js";
|
|
5
|
+
// src/queue/shuffle.ts
|
|
6
|
+
/**
|
|
7
|
+
* Mulberry32 deterministic 32-bit PRNG returning values in `[0, 1)`.
|
|
8
|
+
*/
|
|
9
|
+
function createSeededRandom(seed) {
|
|
10
|
+
let state = seed >>> 0;
|
|
11
|
+
return () => {
|
|
12
|
+
state = state + 0x6d2b79f5 >>> 0;
|
|
13
|
+
let t = Math.imul(state ^ state >>> 15, 1 | state);
|
|
14
|
+
t ^= t + Math.imul(t ^ t >>> 7, 61 | t);
|
|
15
|
+
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Deterministic Fisher-Yates shuffle returning a new array without mutating the input.
|
|
20
|
+
*/
|
|
21
|
+
function fisherYatesShuffle(items, seed) {
|
|
22
|
+
const result = [...items];
|
|
23
|
+
const random = createSeededRandom(seed);
|
|
24
|
+
for (let i = result.length - 1; i > 0; i -= 1) {
|
|
25
|
+
const j = Math.floor(random() * (i + 1));
|
|
26
|
+
const current = result[i];
|
|
27
|
+
const swap = result[j];
|
|
28
|
+
if (current !== void 0 && swap !== void 0) {
|
|
29
|
+
result[i] = swap;
|
|
30
|
+
result[j] = current;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
// src/queue/types.ts
|
|
36
|
+
const RepeatModeSchema = Schema.Literals([
|
|
37
|
+
"off",
|
|
38
|
+
"one",
|
|
39
|
+
"all"
|
|
40
|
+
]);
|
|
41
|
+
const EnqueuePlacementSchema = Schema.Literals(["end", "next"]);
|
|
42
|
+
const QueueLoadModeSchema = Schema.Literals([
|
|
43
|
+
"replace",
|
|
44
|
+
"append",
|
|
45
|
+
"play"
|
|
46
|
+
]);
|
|
47
|
+
const UrlRenewalReasonSchema = Schema.Literals([
|
|
48
|
+
"proactive",
|
|
49
|
+
"expired",
|
|
50
|
+
"playback-error",
|
|
51
|
+
"early-end"
|
|
52
|
+
]);
|
|
53
|
+
const DEFAULT_MAX_QUEUE_ENTRIES = 500;
|
|
54
|
+
const DEFAULT_UNDO_REMOVAL_TTL_MS = 6e3;
|
|
55
|
+
const DEFAULT_UNDO_STOP_TTL_MS = 11e3;
|
|
56
|
+
const DEFAULT_URL_RENEW_AHEAD_MS = 6e4;
|
|
57
|
+
// src/queue/state.ts
|
|
58
|
+
var QueueService = class extends Context.Service()("@avoid.quest/audio/QueueService") {};
|
|
59
|
+
const EXPIRE_PATH_REGEX = /\/expire\/(?<epoch>\d{9,11})(?:\/|$)/iu;
|
|
60
|
+
const EXPIRE_JSON_REGEX = /"expires"\s*:\s*(?<epoch>\d{9,11})/u;
|
|
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
|
+
*/
|
|
66
|
+
function extractLinkExpiryMs(url) {
|
|
67
|
+
let parsed;
|
|
68
|
+
try {
|
|
69
|
+
parsed = new URL(url);
|
|
70
|
+
} catch {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
for (const key of ["expire", "expires"]) {
|
|
74
|
+
const val = parsed.searchParams.get(key);
|
|
75
|
+
if (val !== null && EXPIRE_DIGITS_REGEX.test(val)) return Number(val) * 1e3;
|
|
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;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
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) {
|
|
88
|
+
const expiry = entry.expiresAtMs ?? extractLinkExpiryMs(entry.url);
|
|
89
|
+
if (expiry === void 0) return false;
|
|
90
|
+
return expiry - nowMs < renewAheadMs;
|
|
91
|
+
}
|
|
92
|
+
function createInitialQueueState(initialEntries = []) {
|
|
93
|
+
return {
|
|
94
|
+
continuation: void 0,
|
|
95
|
+
cursor: initialEntries.length > 0 ? 0 : -1,
|
|
96
|
+
entries: [...initialEntries],
|
|
97
|
+
history: [],
|
|
98
|
+
originalOrder: initialEntries.map((entry) => entry.id),
|
|
99
|
+
repeat: "off",
|
|
100
|
+
shuffle: false,
|
|
101
|
+
shuffleSeed: void 0,
|
|
102
|
+
undoRemoval: void 0,
|
|
103
|
+
undoStop: void 0
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function defaultTokenGenerator() {
|
|
107
|
+
let counter = 0;
|
|
108
|
+
return () => {
|
|
109
|
+
if (globalThis.crypto !== void 0 && typeof globalThis.crypto.randomUUID === "function") return globalThis.crypto.randomUUID();
|
|
110
|
+
counter += 1;
|
|
111
|
+
return `undo-${Date.now()}-${counter}`;
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
function appendHistory(history, entry, maxEntries) {
|
|
115
|
+
if (!entry) return history;
|
|
116
|
+
const next = [...history, entry];
|
|
117
|
+
return next.length > maxEntries ? next.slice(next.length - maxEntries) : next;
|
|
118
|
+
}
|
|
119
|
+
function markUndoStopMutated(state) {
|
|
120
|
+
if (!state.undoStop || state.undoStop.mutated) return state;
|
|
121
|
+
return {
|
|
122
|
+
...state,
|
|
123
|
+
undoStop: {
|
|
124
|
+
...state.undoStop,
|
|
125
|
+
mutated: true
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
function arraysEqual(a, b) {
|
|
130
|
+
if (a.length !== b.length) return false;
|
|
131
|
+
for (let i = 0; i < a.length; i += 1) if (a[i] !== b[i]) return false;
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Removes exact per-ID occurrence counts from `order` (scanning from the right
|
|
136
|
+
* so earlier retained duplicate occurrences keep their original positions).
|
|
137
|
+
*/
|
|
138
|
+
function removeOccurrencesFromOrder(order, removedEntries) {
|
|
139
|
+
if (removedEntries.length === 0) return [...order];
|
|
140
|
+
const counts = /* @__PURE__ */ new Map();
|
|
141
|
+
for (const entry of removedEntries) counts.set(entry.id, (counts.get(entry.id) ?? 0) + 1);
|
|
142
|
+
const kept = [];
|
|
143
|
+
for (let i = order.length - 1; i >= 0; i -= 1) {
|
|
144
|
+
const id = order[i];
|
|
145
|
+
if (id === void 0) continue;
|
|
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;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Finds the index in `originalOrder` corresponding to the entry at `cursor`
|
|
155
|
+
* (respecting duplicate IDs by matching occurrence rank).
|
|
156
|
+
*/
|
|
157
|
+
function findCursorIndexInOrder(state) {
|
|
158
|
+
if (state.cursor < 0) return -1;
|
|
159
|
+
const current = state.entries[state.cursor];
|
|
160
|
+
if (!current) return -1;
|
|
161
|
+
let occurrence = 0;
|
|
162
|
+
for (let i = 0; i < state.cursor; i += 1) if (state.entries[i]?.id === current.id) occurrence += 1;
|
|
163
|
+
let seen = 0;
|
|
164
|
+
for (let i = 0; i < state.originalOrder.length; i += 1) if (state.originalOrder[i] === current.id) {
|
|
165
|
+
if (seen === occurrence) return i;
|
|
166
|
+
seen += 1;
|
|
167
|
+
}
|
|
168
|
+
return state.originalOrder.indexOf(current.id);
|
|
169
|
+
}
|
|
170
|
+
function updateOriginalOrderOnMove(state, nextEntries, entryId, beforeEntryId) {
|
|
171
|
+
if (!state.shuffle) return nextEntries.map((entry) => entry.id);
|
|
172
|
+
const orderFromIndex = state.originalOrder.indexOf(entryId);
|
|
173
|
+
if (orderFromIndex === -1) return state.originalOrder;
|
|
174
|
+
const without = [...state.originalOrder.slice(0, orderFromIndex), ...state.originalOrder.slice(orderFromIndex + 1)];
|
|
175
|
+
let orderToIndex = without.length;
|
|
176
|
+
if (beforeEntryId !== void 0) {
|
|
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
|
+
];
|
|
185
|
+
}
|
|
186
|
+
function advanceWithContinuation(state, continuation, current, maxEntries) {
|
|
187
|
+
const [nextEntry, ...rest] = continuation.entries;
|
|
188
|
+
if (!nextEntry) return;
|
|
189
|
+
const keepCount = maxEntries - 1;
|
|
190
|
+
let trimmedEntries = [...state.entries];
|
|
191
|
+
let trimmedCursor = state.cursor;
|
|
192
|
+
let droppedEntries = [];
|
|
193
|
+
if (trimmedEntries.length >= maxEntries) {
|
|
194
|
+
if (keepCount <= 0) {
|
|
195
|
+
droppedEntries = trimmedEntries;
|
|
196
|
+
trimmedEntries = [];
|
|
197
|
+
trimmedCursor = -1;
|
|
198
|
+
} else if (trimmedCursor > 0) {
|
|
199
|
+
const dropCount = trimmedEntries.length - keepCount;
|
|
200
|
+
droppedEntries = trimmedEntries.slice(0, dropCount);
|
|
201
|
+
trimmedEntries = trimmedEntries.slice(dropCount);
|
|
202
|
+
trimmedCursor = Math.max(-1, trimmedCursor - dropCount);
|
|
203
|
+
} else {
|
|
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
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Restores `originalOrder` while preserving exact per-occurrence identity for
|
|
231
|
+
* duplicate item IDs via pre-shuffle rank ordering.
|
|
232
|
+
*/
|
|
233
|
+
function unshuffleEntries(state, preShuffleRank) {
|
|
234
|
+
const current = state.cursor >= 0 ? state.entries[state.cursor] : void 0;
|
|
235
|
+
const buckets = /* @__PURE__ */ new Map();
|
|
236
|
+
for (const entry of state.entries) {
|
|
237
|
+
const bucket = buckets.get(entry.id);
|
|
238
|
+
if (bucket) bucket.items.push(entry);
|
|
239
|
+
else buckets.set(entry.id, {
|
|
240
|
+
items: [entry],
|
|
241
|
+
offset: 0
|
|
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
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Creates a DOM-free Effect v4 reactive audio queue backed by atomic `Ref.modify`
|
|
273
|
+
* transitions, `AtomRef`, and `Atom` (`effect/reactivity`).
|
|
274
|
+
*/
|
|
275
|
+
function createAudioQueue(options = {}) {
|
|
276
|
+
return Effect.gen(function* () {
|
|
277
|
+
const maxEntries = Math.max(1, Math.floor(options.maxEntries ?? 500));
|
|
278
|
+
const undoRemovalTtlMs = options.undoRemovalTtlMs ?? 6e3;
|
|
279
|
+
const undoStopTtlMs = options.undoStopTtlMs ?? 11e3;
|
|
280
|
+
const renewAheadMs = options.renewAheadMs ?? 6e4;
|
|
281
|
+
const now = options.now ?? (() => Date.now());
|
|
282
|
+
const nextToken = options.nextToken ?? defaultTokenGenerator();
|
|
283
|
+
const initialState = createInitialQueueState((options.initialEntries ?? []).slice(0, maxEntries));
|
|
284
|
+
const ref = yield* Ref.make(initialState);
|
|
285
|
+
const atomRef = AtomRef.make(initialState);
|
|
286
|
+
const stateAtom = Atom.readable((get) => {
|
|
287
|
+
const unsubscribe = atomRef.subscribe((value) => {
|
|
288
|
+
get.setSelf(value);
|
|
289
|
+
});
|
|
290
|
+
get.addFinalizer(unsubscribe);
|
|
291
|
+
return atomRef.value;
|
|
292
|
+
});
|
|
293
|
+
const preShuffleRank = /* @__PURE__ */ new WeakMap();
|
|
294
|
+
let generation = 0;
|
|
295
|
+
const publishState = (nextState) => Effect.sync(() => {
|
|
296
|
+
atomRef.set(nextState);
|
|
297
|
+
});
|
|
298
|
+
const modifyState = (fn) => Ref.modify(ref, (state) => {
|
|
299
|
+
const [result, nextState, explicitChanged] = fn(state);
|
|
300
|
+
const changed = explicitChanged ?? nextState !== state;
|
|
301
|
+
if (changed) generation += 1;
|
|
302
|
+
return [{
|
|
303
|
+
changed,
|
|
304
|
+
nextState,
|
|
305
|
+
result
|
|
306
|
+
}, nextState];
|
|
307
|
+
}).pipe(Effect.tap(({ changed, nextState }) => changed ? publishState(nextState) : Effect.void), Effect.map(({ result }) => result));
|
|
308
|
+
const modifyStateOrFail = (fn) => Ref.modify(ref, (state) => {
|
|
309
|
+
const outcome = fn(state);
|
|
310
|
+
if (outcome.ok) {
|
|
311
|
+
const changed = outcome.changed ?? outcome.nextState !== state;
|
|
312
|
+
if (changed) generation += 1;
|
|
313
|
+
return [{
|
|
314
|
+
changed,
|
|
315
|
+
nextState: outcome.nextState,
|
|
316
|
+
outcome
|
|
317
|
+
}, outcome.nextState];
|
|
318
|
+
}
|
|
319
|
+
const nextState = outcome.nextState ?? state;
|
|
320
|
+
const changed = nextState !== state;
|
|
321
|
+
if (changed) generation += 1;
|
|
322
|
+
return [{
|
|
323
|
+
changed,
|
|
324
|
+
nextState,
|
|
325
|
+
outcome
|
|
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
|
|
650
|
+
};
|
|
651
|
+
});
|
|
652
|
+
const jumpTo = (entryId) => modifyStateOrFail((rawState) => {
|
|
653
|
+
const state = markUndoStopMutated(rawState);
|
|
654
|
+
const targetIndex = state.entries.findIndex((e) => e.id === entryId);
|
|
655
|
+
const target = targetIndex === -1 ? void 0 : state.entries[targetIndex];
|
|
656
|
+
if (!target) return {
|
|
657
|
+
error: new AudioQueueError({
|
|
658
|
+
code: "ENTRY_NOT_FOUND",
|
|
659
|
+
message: `Queue entry "${entryId}" not found.`
|
|
660
|
+
}),
|
|
661
|
+
ok: false
|
|
662
|
+
};
|
|
663
|
+
const current = state.cursor >= 0 ? state.entries[state.cursor] : void 0;
|
|
664
|
+
return {
|
|
665
|
+
changed: true,
|
|
666
|
+
nextState: {
|
|
667
|
+
...state,
|
|
668
|
+
cursor: targetIndex,
|
|
669
|
+
history: current && current.id !== target.id ? appendHistory(state.history, current, maxEntries) : state.history,
|
|
670
|
+
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
|
+
];
|
|
802
|
+
}
|
|
803
|
+
if (state.repeat === "all") {
|
|
804
|
+
const lastIndex = state.entries.length - 1;
|
|
805
|
+
return [
|
|
806
|
+
state.entries[lastIndex],
|
|
807
|
+
{
|
|
808
|
+
...state,
|
|
809
|
+
cursor: lastIndex,
|
|
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
|
+
};
|
|
984
|
+
});
|
|
985
|
+
const renewEntryUrl = (entryId, reason = "expired") => Effect.gen(function* () {
|
|
986
|
+
const entry = (yield* Ref.get(ref)).entries.find((e) => e.id === entryId);
|
|
987
|
+
if (!entry) return yield* Effect.fail(new AudioQueueError({
|
|
988
|
+
code: "ENTRY_NOT_FOUND",
|
|
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
|
+
};
|
|
1055
|
+
});
|
|
1056
|
+
}
|
|
1057
|
+
function makeQueueServiceLayer(options = {}) {
|
|
1058
|
+
return Layer.effect(QueueService, createAudioQueue(options));
|
|
1059
|
+
}
|
|
1060
|
+
export { DEFAULT_MAX_QUEUE_ENTRIES, DEFAULT_UNDO_REMOVAL_TTL_MS, DEFAULT_UNDO_STOP_TTL_MS, DEFAULT_URL_RENEW_AHEAD_MS, EnqueuePlacementSchema, QueueLoadModeSchema, QueueService, RepeatModeSchema, UrlRenewalReasonSchema, createAudioQueue, createInitialQueueState, createSeededRandom, extractLinkExpiryMs, fisherYatesShuffle, isUrlExpiringSoon, makeQueueServiceLayer };
|
|
1061
|
+
|
|
1062
|
+
//# sourceMappingURL=index.js.map
|