@zodal/media 0.1.0 → 0.2.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/README.md +21 -0
- package/dist/chunk-GALBANEF.js +175 -0
- package/dist/chunk-GALBANEF.js.map +1 -0
- package/dist/commands.cjs +97 -0
- package/dist/commands.cjs.map +1 -1
- package/dist/commands.d.cts +16 -1
- package/dist/commands.d.ts +16 -1
- package/dist/commands.js +57 -0
- package/dist/commands.js.map +1 -1
- package/dist/end-mode.cjs +206 -0
- package/dist/end-mode.cjs.map +1 -0
- package/dist/end-mode.d.cts +126 -0
- package/dist/end-mode.d.ts +126 -0
- package/dist/end-mode.js +21 -0
- package/dist/end-mode.js.map +1 -0
- package/dist/index.cjs +180 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +22 -4
- package/package.json +17 -9
package/dist/commands.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/commands.ts"],"sourcesContent":["/**\n * The transport's actions as acture commands, bound to one player. Register them with an acture\n * registry; a palette, hotkeys, MCP or a gesture controller then all send the same commands.\n *\n * Continuous commands (`seekTo`, `setRate`, `setVolume`) take a target value and are idempotent, so\n * a sender may emit them at 60 Hz; they are listed in `CONTINUOUS_COMMAND_IDS` so history and undo\n * can skip them. Kept apart from `./transport` so the transport needs neither acture nor zod.\n */\nimport { z } from 'zod';\nimport { defineCommand, ok, err, type AnyCommandRecord } from 'acture';\nimport type { MediaItem, MediaPlayer } from './transport.js';\nimport type { SleepTimer } from './sleep.js';\n\n/** Commands that carry a target value and are safe at 60 Hz; history and undo should skip them. */\nexport const CONTINUOUS_COMMAND_IDS = ['media.seekTo', 'media.setRate', 'media.setVolume'] as const;\n\n/** Builds the transport commands bound to one player. Register them with an acture registry. */\nexport function mediaCommands(player: MediaPlayer): AnyCommandRecord[] {\n const cat = 'media';\n const done = () => ok(undefined);\n return [\n defineCommand({ id: 'media.play', title: 'Play', category: cat, keybinding: 'Space',\n execute: async () => { await player.play(); return done(); } }),\n defineCommand({ id: 'media.pause', title: 'Pause', category: cat,\n execute: () => { player.pause(); return done(); } }),\n defineCommand({ id: 'media.toggle', title: 'Play or pause', category: cat, keybinding: 'k',\n execute: async () => { await player.toggle(); return done(); } }),\n defineCommand({ id: 'media.next', title: 'Next', category: cat, keybinding: 'n',\n execute: () => { player.next(); return done(); } }),\n defineCommand({ id: 'media.previous', title: 'Previous', category: cat, keybinding: 'p',\n execute: () => { player.previous(); return done(); } }),\n defineCommand({ id: 'media.skipBy', title: 'Skip by seconds', category: cat,\n params: z.object({ seconds: z.number() }),\n execute: (p: { seconds: number }) => { player.skipBy(p.seconds); return done(); } }),\n defineCommand({ id: 'media.seekTo', title: 'Seek to position', category: 'media.continuous',\n description: 'Absolute target in seconds; idempotent, safe to send at 60 Hz.',\n params: z.object({ seconds: z.number().min(0) }),\n execute: (p: { seconds: number }) => { player.seekTo(p.seconds); return done(); } }),\n defineCommand({ id: 'media.setRate', title: 'Set speed', category: 'media.continuous',\n params: z.object({ rate: z.number().positive() }),\n execute: (p: { rate: number }) => { player.setRate(p.rate); return done(); } }),\n defineCommand({ id: 'media.setVolume', title: 'Set volume', category: 'media.continuous',\n params: z.object({ volume: z.number().min(0).max(1) }),\n execute: (p: { volume: number }) => { player.setVolume(p.volume); return done(); } }),\n defineCommand({ id: 'media.enqueue', title: 'Add to queue', category: cat,\n params: z.object({ item: z.object({ id: z.string(), src: z.string(), title: z.string() }), next: z.boolean().optional() }),\n execute: (p: { item: MediaItem; next?: boolean }) => {\n if (!p.item.src) return err('invalid_item', 'src is required');\n player.enqueue(p.item, { next: p.next });\n return done();\n } }),\n ];\n}\n\n/**\n * A sleep timer's actions as acture commands (`./sleep`). `sleep.at` takes a media time, so a host\n * that knows its chapters registers its own \"end of chapter\" command on top of it.\n */\nexport function sleepCommands(timer: SleepTimer): AnyCommandRecord[] {\n const cat = 'media.sleep';\n const done = () => ok(undefined);\n return [\n defineCommand({ id: 'sleep.after', title: 'Sleep after minutes', category: cat,\n params: z.object({ minutes: z.number().positive() }),\n execute: (p: { minutes: number }) => { timer.set({ kind: 'after', minutes: p.minutes }); return done(); } }),\n defineCommand({ id: 'sleep.at', title: 'Sleep at a point in the item', category: cat,\n params: z.object({ seconds: z.number().min(0), label: z.string().optional() }),\n execute: (p: { seconds: number; label?: string }) => { timer.set({ kind: 'at', ...p }); return done(); } }),\n defineCommand({ id: 'sleep.end', title: 'Sleep at the end of the item', category: cat,\n execute: () => { timer.set({ kind: 'end' }); return done(); } }),\n defineCommand({ id: 'sleep.extend', title: 'Add minutes to the sleep timer', category: cat,\n params: z.object({ minutes: z.number().positive() }),\n execute: (p: { minutes: number }) => { timer.extend(p.minutes); return done(); } }),\n defineCommand({ id: 'sleep.cancel', title: 'Turn the sleep timer off', category: cat,\n execute: () => { timer.cancel(); return done(); } }),\n ];\n}\n"],"mappings":";AAQA,SAAS,SAAS;AAClB,SAAS,eAAe,IAAI,WAAkC;AAKvD,IAAM,yBAAyB,CAAC,gBAAgB,iBAAiB,iBAAiB;AAGlF,SAAS,cAAc,QAAyC;AACrE,QAAM,MAAM;AACZ,QAAM,OAAO,MAAM,GAAG,MAAS;AAC/B,SAAO;AAAA,IACL,cAAc;AAAA,MAAE,IAAI;AAAA,MAAc,OAAO;AAAA,MAAQ,UAAU;AAAA,MAAK,YAAY;AAAA,MAC1E,SAAS,YAAY;AAAE,cAAM,OAAO,KAAK;AAAG,eAAO,KAAK;AAAA,MAAG;AAAA,IAAE,CAAC;AAAA,IAChE,cAAc;AAAA,MAAE,IAAI;AAAA,MAAe,OAAO;AAAA,MAAS,UAAU;AAAA,MAC3D,SAAS,MAAM;AAAE,eAAO,MAAM;AAAG,eAAO,KAAK;AAAA,MAAG;AAAA,IAAE,CAAC;AAAA,IACrD,cAAc;AAAA,MAAE,IAAI;AAAA,MAAgB,OAAO;AAAA,MAAiB,UAAU;AAAA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,IAAI;AAAA,MAAY,OAAO;AAAA,MAAgC,UAAU;AAAA,MAC/E,QAAQ,EAAE,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,IAAI,CAAC,GAAG,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,CAAC;AAAA,MAC7E,SAAS,CAAC,MAA2C;AAAE,cAAM,IAAI,EAAE,MAAM,MAAM,GAAG,EAAE,CAAC;AAAG,eAAO,KAAK;AAAA,MAAG;AAAA,IAAE,CAAC;AAAA,IAC5G,cAAc;AAAA,MAAE,IAAI;AAAA,MAAa,OAAO;AAAA,MAAgC,UAAU;AAAA,MAChF,SAAS,MAAM;AAAE,cAAM,IAAI,EAAE,MAAM,MAAM,CAAC;AAAG,eAAO,KAAK;AAAA,MAAG;AAAA,IAAE,CAAC;AAAA,IACjE,cAAc;AAAA,MAAE,IAAI;AAAA,MAAgB,OAAO;AAAA,MAAkC,UAAU;AAAA,MACrF,QAAQ,EAAE,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,SAAS,EAAE,CAAC;AAAA,MACnD,SAAS,CAAC,MAA2B;AAAE,cAAM,OAAO,EAAE,OAAO;AAAG,eAAO,KAAK;AAAA,MAAG;AAAA,IAAE,CAAC;AAAA,IACpF,cAAc;AAAA,MAAE,IAAI;AAAA,MAAgB,OAAO;AAAA,MAA4B,UAAU;AAAA,MAC/E,SAAS,MAAM;AAAE,cAAM,OAAO;AAAG,eAAO,KAAK;AAAA,MAAG;AAAA,IAAE,CAAC;AAAA,EACvD;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/commands.ts"],"sourcesContent":["/**\n * The transport's actions as acture commands, bound to one player. Register them with an acture\n * registry; a palette, hotkeys, MCP or a gesture controller then all send the same commands.\n *\n * Continuous commands (`seekTo`, `setRate`, `setVolume`) take a target value and are idempotent, so\n * a sender may emit them at 60 Hz; they are listed in `CONTINUOUS_COMMAND_IDS` so history and undo\n * can skip them. Kept apart from `./transport` so the transport needs neither acture nor zod.\n */\nimport { z } from 'zod';\nimport { defineCommand, ok, err, type AnyCommandRecord } from 'acture';\nimport type { MediaItem, MediaPlayer } from './transport.js';\nimport type { SleepTimer } from './sleep.js';\nimport { END_MODES, END_MODE_INFO, nextEndMode, type EndMode } from './end-mode.js';\n\n/** Commands that carry a target value and are safe at 60 Hz; history and undo should skip them. */\nexport const CONTINUOUS_COMMAND_IDS = ['media.seekTo', 'media.setRate', 'media.setVolume'] as const;\n\n/** Builds the transport commands bound to one player. Register them with an acture registry. */\nexport function mediaCommands(player: MediaPlayer): AnyCommandRecord[] {\n const cat = 'media';\n const done = () => ok(undefined);\n return [\n defineCommand({ id: 'media.play', title: 'Play', category: cat, keybinding: 'Space',\n execute: async () => { await player.play(); return done(); } }),\n defineCommand({ id: 'media.pause', title: 'Pause', category: cat,\n execute: () => { player.pause(); return done(); } }),\n defineCommand({ id: 'media.toggle', title: 'Play or pause', category: cat, keybinding: 'k',\n execute: async () => { await player.toggle(); return done(); } }),\n defineCommand({ id: 'media.next', title: 'Next', category: cat, keybinding: 'n',\n execute: () => { player.next(); return done(); } }),\n defineCommand({ id: 'media.previous', title: 'Previous', category: cat, keybinding: 'p',\n execute: () => { player.previous(); return done(); } }),\n defineCommand({ id: 'media.skipBy', title: 'Skip by seconds', category: cat,\n params: z.object({ seconds: z.number() }),\n execute: (p: { seconds: number }) => { player.skipBy(p.seconds); return done(); } }),\n defineCommand({ id: 'media.seekTo', title: 'Seek to position', category: 'media.continuous',\n description: 'Absolute target in seconds; idempotent, safe to send at 60 Hz.',\n params: z.object({ seconds: z.number().min(0) }),\n execute: (p: { seconds: number }) => { player.seekTo(p.seconds); return done(); } }),\n defineCommand({ id: 'media.setRate', title: 'Set speed', category: 'media.continuous',\n params: z.object({ rate: z.number().positive() }),\n execute: (p: { rate: number }) => { player.setRate(p.rate); return done(); } }),\n defineCommand({ id: 'media.setVolume', title: 'Set volume', category: 'media.continuous',\n params: z.object({ volume: z.number().min(0).max(1) }),\n execute: (p: { volume: number }) => { player.setVolume(p.volume); return done(); } }),\n defineCommand({ id: 'media.enqueue', title: 'Add to queue', category: cat,\n params: z.object({ item: z.object({ id: z.string(), src: z.string(), title: z.string() }), next: z.boolean().optional() }),\n execute: (p: { item: MediaItem; next?: boolean }) => {\n if (!p.item.src) return err('invalid_item', 'src is required');\n player.enqueue(p.item, { next: p.next });\n return done();\n } }),\n ];\n}\n\n/**\n * A sleep timer's actions as acture commands (`./sleep`). `sleep.at` takes a media time, so a host\n * that knows its chapters registers its own \"end of chapter\" command on top of it.\n */\nexport function sleepCommands(timer: SleepTimer): AnyCommandRecord[] {\n const cat = 'media.sleep';\n const done = () => ok(undefined);\n return [\n defineCommand({ id: 'sleep.after', title: 'Sleep after minutes', category: cat,\n params: z.object({ minutes: z.number().positive() }),\n execute: (p: { minutes: number }) => { timer.set({ kind: 'after', minutes: p.minutes }); return done(); } }),\n defineCommand({ id: 'sleep.at', title: 'Sleep at a point in the item', category: cat,\n params: z.object({ seconds: z.number().min(0), label: z.string().optional() }),\n execute: (p: { seconds: number; label?: string }) => { timer.set({ kind: 'at', ...p }); return done(); } }),\n defineCommand({ id: 'sleep.end', title: 'Sleep at the end of the item', category: cat,\n execute: () => { timer.set({ kind: 'end' }); return done(); } }),\n defineCommand({ id: 'sleep.extend', title: 'Add minutes to the sleep timer', category: cat,\n params: z.object({ minutes: z.number().positive() }),\n execute: (p: { minutes: number }) => { timer.extend(p.minutes); return done(); } }),\n defineCommand({ id: 'sleep.cancel', title: 'Turn the sleep timer off', category: cat,\n execute: () => { timer.cancel(); return done(); } }),\n ];\n}\n\n/**\n * The end-of-item mode's actions as acture commands (`./end-mode`). The host owns where the mode\n * lives (a persisted preference); these read and write it through `get` and `set`.\n */\nexport function endModeCommands({ get, set, playNext, skip, back }: {\n get: () => EndMode;\n set: (mode: EndMode) => void;\n /** Play now what the mode would play next; false when nothing would. Registers `end.playNext` (an \"Up next\" button). */\n playNext?: () => boolean;\n /** The Next button (`EndPolicy.skip`); false when nothing is next. Registers `end.skip` (▶|, a lock screen's next track). */\n skip?: () => boolean;\n /** The Previous button (`EndPolicy.back`): restart, or the item before. Registers `end.back` (|◀, a lock screen's previous track). */\n back?: () => void;\n}): AnyCommandRecord[] {\n const cat = 'media';\n const next = playNext\n ? [defineCommand({ id: 'end.playNext', title: 'Play now what would play next', category: cat,\n execute: () => (playNext() ? ok(undefined) : err('nothing-next', 'Nothing plays next in this mode.')) })]\n : [];\n const buttons = [\n ...(skip ? [defineCommand({ id: 'end.skip', title: 'Next item', category: cat,\n execute: () => (skip() ? ok(undefined) : err('nothing-next', 'Nothing comes next.')) })] : []),\n ...(back ? [defineCommand({ id: 'end.back', title: 'Previous item (or the start of this one)', category: cat,\n execute: () => { back(); return ok(undefined); } })] : []),\n ];\n return [\n ...next,\n ...buttons,\n defineCommand({ id: 'end.setMode', title: 'When an item ends: stop, repeat it, play the list in order, repeat the list or shuffle it', category: cat,\n params: z.object({ mode: z.enum(END_MODES) }),\n execute: (p: { mode: EndMode }) => { set(p.mode); return ok(END_MODE_INFO[p.mode].label); } }),\n defineCommand({ id: 'end.cycle', title: 'Step to the next end-of-item mode', category: cat,\n execute: () => { const mode = nextEndMode(get()); set(mode); return ok(END_MODE_INFO[mode].label); } }),\n ];\n}\n"],"mappings":";;;;;;;AAQA,SAAS,SAAS;AAClB,SAAS,eAAe,IAAI,WAAkC;AAMvD,IAAM,yBAAyB,CAAC,gBAAgB,iBAAiB,iBAAiB;AAGlF,SAAS,cAAc,QAAyC;AACrE,QAAM,MAAM;AACZ,QAAM,OAAO,MAAM,GAAG,MAAS;AAC/B,SAAO;AAAA,IACL,cAAc;AAAA,MAAE,IAAI;AAAA,MAAc,OAAO;AAAA,MAAQ,UAAU;AAAA,MAAK,YAAY;AAAA,MAC1E,SAAS,YAAY;AAAE,cAAM,OAAO,KAAK;AAAG,eAAO,KAAK;AAAA,MAAG;AAAA,IAAE,CAAC;AAAA,IAChE,cAAc;AAAA,MAAE,IAAI;AAAA,MAAe,OAAO;AAAA,MA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|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/end-mode.ts
|
|
21
|
+
var end_mode_exports = {};
|
|
22
|
+
__export(end_mode_exports, {
|
|
23
|
+
DEFAULT_END_MODE: () => DEFAULT_END_MODE,
|
|
24
|
+
END_MODES: () => END_MODES,
|
|
25
|
+
END_MODE_INFO: () => END_MODE_INFO,
|
|
26
|
+
PREVIOUS_RESTARTS_AFTER_S: () => PREVIOUS_RESTARTS_AFTER_S,
|
|
27
|
+
createEndPolicy: () => createEndPolicy,
|
|
28
|
+
isEndMode: () => isEndMode,
|
|
29
|
+
nextEndMode: () => nextEndMode,
|
|
30
|
+
skipMode: () => skipMode
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(end_mode_exports);
|
|
33
|
+
var END_MODES = ["stop", "repeat-one", "next", "repeat-all", "shuffle"];
|
|
34
|
+
var DEFAULT_END_MODE = "next";
|
|
35
|
+
var END_MODE_INFO = {
|
|
36
|
+
stop: {
|
|
37
|
+
mode: "stop",
|
|
38
|
+
label: "Stop after this one",
|
|
39
|
+
short: "Then stop",
|
|
40
|
+
description: "When this item ends, playback stops.",
|
|
41
|
+
icon: { material: "keyboard_tab", lucide: "arrow-right-to-line" }
|
|
42
|
+
},
|
|
43
|
+
"repeat-one": {
|
|
44
|
+
mode: "repeat-one",
|
|
45
|
+
label: "Repeat this one",
|
|
46
|
+
short: "Repeat 1",
|
|
47
|
+
description: "This item plays again from the start each time it ends.",
|
|
48
|
+
icon: { material: "repeat_one", lucide: "repeat-1" }
|
|
49
|
+
},
|
|
50
|
+
next: {
|
|
51
|
+
mode: "next",
|
|
52
|
+
label: "Play the list in order",
|
|
53
|
+
short: "In order",
|
|
54
|
+
description: "The next item of the list plays; playback stops after the last one.",
|
|
55
|
+
icon: { material: "playlist_play", lucide: "list-video" }
|
|
56
|
+
},
|
|
57
|
+
"repeat-all": {
|
|
58
|
+
mode: "repeat-all",
|
|
59
|
+
label: "Repeat the list",
|
|
60
|
+
short: "Repeat all",
|
|
61
|
+
description: "The list plays in order and starts again after its last item.",
|
|
62
|
+
icon: { material: "repeat", lucide: "repeat" }
|
|
63
|
+
},
|
|
64
|
+
shuffle: {
|
|
65
|
+
mode: "shuffle",
|
|
66
|
+
label: "Shuffle the list",
|
|
67
|
+
short: "Shuffle",
|
|
68
|
+
description: "A random item of the list plays next; each plays once before any plays again.",
|
|
69
|
+
icon: { material: "shuffle", lucide: "shuffle" }
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
var isEndMode = (value) => END_MODES.includes(value);
|
|
73
|
+
function nextEndMode(mode) {
|
|
74
|
+
return END_MODES[(END_MODES.indexOf(mode) + 1) % END_MODES.length];
|
|
75
|
+
}
|
|
76
|
+
var PREVIOUS_RESTARTS_AFTER_S = 3;
|
|
77
|
+
var skipMode = (mode) => mode === "stop" || mode === "repeat-one" ? "next" : mode;
|
|
78
|
+
var STOP = { kind: "stop" };
|
|
79
|
+
var REPLAY = { kind: "replay" };
|
|
80
|
+
var HISTORY_LIMIT = 500;
|
|
81
|
+
function createEndPolicy({ random = Math.random, restartAfterS = PREVIOUS_RESTARTS_AFTER_S } = {}) {
|
|
82
|
+
let played = /* @__PURE__ */ new Set();
|
|
83
|
+
let history = [];
|
|
84
|
+
let forward = [];
|
|
85
|
+
let picked = null;
|
|
86
|
+
let moved = null;
|
|
87
|
+
let shuffling = false;
|
|
88
|
+
const enter = (mode) => {
|
|
89
|
+
const s = mode === "shuffle";
|
|
90
|
+
if (s !== shuffling) {
|
|
91
|
+
history = [];
|
|
92
|
+
forward = [];
|
|
93
|
+
shuffling = s;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
const remember = (id) => {
|
|
97
|
+
if (history[history.length - 1] !== id) history.push(id);
|
|
98
|
+
if (history.length > HISTORY_LIMIT) history.splice(0, history.length - HISTORY_LIMIT);
|
|
99
|
+
};
|
|
100
|
+
const forwardTop = (ids, current) => {
|
|
101
|
+
while (forward.length) {
|
|
102
|
+
const id = forward[forward.length - 1];
|
|
103
|
+
if (id !== current && ids.includes(id)) return id;
|
|
104
|
+
forward.pop();
|
|
105
|
+
}
|
|
106
|
+
return null;
|
|
107
|
+
};
|
|
108
|
+
const shufflePick = (ids, current) => {
|
|
109
|
+
const again = forwardTop(ids, current);
|
|
110
|
+
if (again) return { kind: "play", id: again, wrapped: false };
|
|
111
|
+
if (picked && picked.after === current && ids.includes(picked.id) && picked.id !== current) {
|
|
112
|
+
return { kind: "play", id: picked.id, wrapped: picked.wrapped };
|
|
113
|
+
}
|
|
114
|
+
const others = ids.filter((id2) => id2 !== current);
|
|
115
|
+
if (!others.length) return ids.includes(current) ? REPLAY : STOP;
|
|
116
|
+
const fresh = others.filter((id2) => !played.has(id2));
|
|
117
|
+
const wrapped = fresh.length === 0;
|
|
118
|
+
const pool = wrapped ? others : fresh;
|
|
119
|
+
const id = pool[Math.min(pool.length - 1, Math.floor(random() * pool.length))];
|
|
120
|
+
picked = { after: current, id, wrapped };
|
|
121
|
+
return { kind: "play", id, wrapped };
|
|
122
|
+
};
|
|
123
|
+
const decide = (mode, ids, current) => {
|
|
124
|
+
enter(mode);
|
|
125
|
+
if (mode === "stop") return STOP;
|
|
126
|
+
if (mode === "repeat-one") return REPLAY;
|
|
127
|
+
if (mode === "shuffle") return shufflePick(ids, current);
|
|
128
|
+
const i = ids.indexOf(current);
|
|
129
|
+
if (mode === "next") return i >= 0 && i + 1 < ids.length ? { kind: "play", id: ids[i + 1], wrapped: false } : STOP;
|
|
130
|
+
if (!ids.length) return STOP;
|
|
131
|
+
if (i < 0) return { kind: "play", id: ids[0], wrapped: false };
|
|
132
|
+
if (ids.length === 1) return REPLAY;
|
|
133
|
+
return i + 1 < ids.length ? { kind: "play", id: ids[i + 1], wrapped: false } : { kind: "play", id: ids[0], wrapped: true };
|
|
134
|
+
};
|
|
135
|
+
const take = (mode, ids, current) => {
|
|
136
|
+
const d = decide(mode, ids, current);
|
|
137
|
+
if (mode === "shuffle") {
|
|
138
|
+
if (d.kind === "play" && forward[forward.length - 1] === d.id) forward.pop();
|
|
139
|
+
else if (d.kind === "play" && d.wrapped) played = /* @__PURE__ */ new Set();
|
|
140
|
+
else played.add(current);
|
|
141
|
+
if (d.kind === "play") remember(current);
|
|
142
|
+
}
|
|
143
|
+
moved = d.kind === "play" ? d.id : null;
|
|
144
|
+
picked = null;
|
|
145
|
+
return d;
|
|
146
|
+
};
|
|
147
|
+
const back = (mode, ids, current, position) => {
|
|
148
|
+
enter(mode);
|
|
149
|
+
moved = null;
|
|
150
|
+
if (position > restartAfterS) return REPLAY;
|
|
151
|
+
if (mode === "shuffle") {
|
|
152
|
+
while (history.length) {
|
|
153
|
+
const id = history.pop();
|
|
154
|
+
if (id !== current && ids.includes(id)) {
|
|
155
|
+
forward.push(current);
|
|
156
|
+
picked = null;
|
|
157
|
+
moved = id;
|
|
158
|
+
return { kind: "play", id, wrapped: false };
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return REPLAY;
|
|
162
|
+
}
|
|
163
|
+
const i = ids.indexOf(current);
|
|
164
|
+
const d = i > 0 ? { kind: "play", id: ids[i - 1], wrapped: false } : mode === "repeat-all" && ids.length > (i === 0 ? 1 : 0) ? { kind: "play", id: ids[ids.length - 1], wrapped: i === 0 } : REPLAY;
|
|
165
|
+
moved = d.kind === "play" ? d.id : null;
|
|
166
|
+
return d;
|
|
167
|
+
};
|
|
168
|
+
return {
|
|
169
|
+
peek: decide,
|
|
170
|
+
take,
|
|
171
|
+
peekSkip: (mode, ids, current) => decide(skipMode(mode), ids, current),
|
|
172
|
+
skip: (mode, ids, current) => take(skipMode(mode), ids, current),
|
|
173
|
+
back,
|
|
174
|
+
opened(from, to) {
|
|
175
|
+
if (to === moved) {
|
|
176
|
+
moved = null;
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
moved = null;
|
|
180
|
+
picked = null;
|
|
181
|
+
forward = [];
|
|
182
|
+
if (!shuffling) return;
|
|
183
|
+
if (from && from !== to) remember(from);
|
|
184
|
+
played.add(to);
|
|
185
|
+
},
|
|
186
|
+
reset() {
|
|
187
|
+
played = /* @__PURE__ */ new Set();
|
|
188
|
+
picked = null;
|
|
189
|
+
history = [];
|
|
190
|
+
forward = [];
|
|
191
|
+
moved = null;
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
196
|
+
0 && (module.exports = {
|
|
197
|
+
DEFAULT_END_MODE,
|
|
198
|
+
END_MODES,
|
|
199
|
+
END_MODE_INFO,
|
|
200
|
+
PREVIOUS_RESTARTS_AFTER_S,
|
|
201
|
+
createEndPolicy,
|
|
202
|
+
isEndMode,
|
|
203
|
+
nextEndMode,
|
|
204
|
+
skipMode
|
|
205
|
+
});
|
|
206
|
+
//# sourceMappingURL=end-mode.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/end-mode.ts"],"sourcesContent":["/**\n * What plays when an item ends: one mode, chosen by the listener, applied to the set playback\n * started from (the list, filter, playlist or album the item was played from: its \"play context\").\n * Headless and pure apart from the shuffle's memory: no DOM, no zustand, no React.\n *\n * ```ts\n * const policy = createEndPolicy();\n * audio.addEventListener('ended', () => {\n * const d = policy.take(mode, setIds, currentId); // 'stop' | 'replay' | 'play' (an id)\n * if (d.kind === 'play') open(d.id); else if (d.kind === 'replay') restart();\n * });\n * policy.peek(mode, setIds, currentId); // the same decision, for an \"Up next\" line\n * nextButton.onclick = () => act(policy.skip(mode, setIds, currentId)); // ▶|\n * prevButton.onclick = () => act(policy.back(mode, setIds, currentId, audio.currentTime)); // |◀\n * ```\n *\n * The five modes are the classic players' states folded into one choice (Android's and Apple's\n * repeat off / one / all plus shuffle on / off, and foobar2000's single \"playback order\" menu with\n * \"stop after current\"), named in words, because a cycling repeat button whose only difference\n * between two states is a tiny \"1\" is the most reported confusion in the genre. Research and the\n * decision: `docs/research/end-of-item-modes.md` in this repository.\n *\n * Choices, each a default the host can change:\n * - **Shuffle is a shuffle, not a die.** Every item of the set plays once, in random order, before\n * any plays again; then a new round starts, never with the item that just ended. It goes on until\n * the listener stops (or a sleep timer does). Items added to the set join the current round;\n * items taken out are never picked.\n * - **Repeat one replays the item**; \"In order\" stops after the last item of the set; \"Repeat all\"\n * wraps to the first. A set of one item under \"Repeat all\" or \"Shuffle\" replays it.\n * - **An item not in the set** (taken out of the playlist while it played): \"In order\" stops,\n * \"Repeat all\" goes to the set's first item, \"Shuffle\" picks from the set.\n *\n * The transport's buttons follow the same set and mode, as every mainstream player does:\n * - **Next (▶|)** moves on now. \"Repeat this one\" and \"Stop after this one\" are about what happens\n * when an item *ends*, so they do not trap the button: it goes to the next item of the list in\n * order. Past the last item it wraps under \"Repeat all\", picks the next of the round under\n * \"Shuffle\", and otherwise does nothing (a host disables the button: `peekSkip` says so).\n * - **Previous (|◀)** restarts the current item when more than `restartAfterS` (3 s, the CD-player\n * rule) has played; otherwise it goes to the item before: the one played before it under\n * \"Shuffle\", the one above it in the list otherwise, wrapping to the last under \"Repeat all\". With\n * nothing before, it restarts.\n */\n\n/** The modes, in the order a menu lists them (stop first, then the loops). */\nexport const END_MODES = ['stop', 'repeat-one', 'next', 'repeat-all', 'shuffle'] as const;\nexport type EndMode = (typeof END_MODES)[number];\n\n/** The default: play on through the set, in order, and stop after its last item (a playlist played through). */\nexport const DEFAULT_END_MODE: EndMode = 'next';\n\nexport interface EndModeInfo {\n mode: EndMode;\n /** The menu's words: what happens when an item ends. */\n label: string;\n /** The word shown on the control beside its icon (always shown: the icon alone is ambiguous). */\n short: string;\n /** One sentence for a tooltip or a screen reader. */\n description: string;\n /** The standard icon's name in Material Symbols and in Lucide (a host draws it from either set). */\n icon: { material: string; lucide: string };\n}\n\n/** The terms and icons, one source for every host (chosen in `docs/research/end-of-item-modes.md`). */\nexport const END_MODE_INFO: Record<EndMode, EndModeInfo> = {\n stop: {\n mode: 'stop', label: 'Stop after this one', short: 'Then stop',\n description: 'When this item ends, playback stops.',\n icon: { material: 'keyboard_tab', lucide: 'arrow-right-to-line' },\n },\n 'repeat-one': {\n mode: 'repeat-one', label: 'Repeat this one', short: 'Repeat 1',\n description: 'This item plays again from the start each time it ends.',\n icon: { material: 'repeat_one', lucide: 'repeat-1' },\n },\n next: {\n mode: 'next', label: 'Play the list in order', short: 'In order',\n description: 'The next item of the list plays; playback stops after the last one.',\n icon: { material: 'playlist_play', lucide: 'list-video' },\n },\n 'repeat-all': {\n mode: 'repeat-all', label: 'Repeat the list', short: 'Repeat all',\n description: 'The list plays in order and starts again after its last item.',\n icon: { material: 'repeat', lucide: 'repeat' },\n },\n shuffle: {\n mode: 'shuffle', label: 'Shuffle the list', short: 'Shuffle',\n description: 'A random item of the list plays next; each plays once before any plays again.',\n icon: { material: 'shuffle', lucide: 'shuffle' },\n },\n};\n\n/** Whether a value is one of the modes (a persisted preference may hold an old or foreign value). */\nexport const isEndMode = (value: unknown): value is EndMode => (END_MODES as readonly unknown[]).includes(value);\n\n/** The mode after `mode` in menu order, wrapping: for a single key or button that steps through them. */\nexport function nextEndMode(mode: EndMode): EndMode {\n return END_MODES[(END_MODES.indexOf(mode) + 1) % END_MODES.length];\n}\n\n/** What to do when the current item ends. */\nexport type EndDecision =\n | { kind: 'stop' }\n /** Play the current item again from its start. */\n | { kind: 'replay' }\n /** Play this item of the set; `wrapped`: the set starts over (repeat all past the end, a new shuffle round). */\n | { kind: 'play'; id: string; wrapped: boolean };\n\n/** How far into an item (seconds) Previous restarts it instead of going to the item before: the CD-player rule. */\nexport const PREVIOUS_RESTARTS_AFTER_S = 3;\n\n/**\n * The mode the Next button follows: the end-of-item mode, except that \"Repeat this one\" and\n * \"Stop after this one\" say what happens at an item's end, not where Next goes, so Next goes on in order.\n */\nexport const skipMode = (mode: EndMode): EndMode => (mode === 'stop' || mode === 'repeat-one' ? 'next' : mode);\n\nexport interface EndPolicy {\n /** What `take` would decide now, without moving on (an \"Up next\" line). Stable until `take` or `reset`. */\n peek(mode: EndMode, ids: readonly string[], current: string): EndDecision;\n /** Decide at the end of `current`, and remember it (the shuffle's round). */\n take(mode: EndMode, ids: readonly string[], current: string): EndDecision;\n /**\n * What `skip` would do now (Next, ▶|), without moving on: `stop` means there is nothing next (disable the button).\n * Under \"Shuffle\" this draws (and keeps) the pick, as `peek` does, so the button and an \"Up next\" line agree.\n */\n peekSkip(mode: EndMode, ids: readonly string[], current: string): EndDecision;\n /** Next (▶|): go on now, in the list's order (see `skipMode`), and remember it like `take`. */\n skip(mode: EndMode, ids: readonly string[], current: string): EndDecision;\n /**\n * Previous (|◀) at `position` seconds into `current`: `replay` (restart it) when past `restartAfterS`\n * or when nothing comes before, else the item before. `wrapped` here means it went round backwards (from the\n * first to the last under \"Repeat all\"), not that a new round starts. Under \"Shuffle\", Next and the end then\n * go forward again through the items Previous came back from, before any new pick.\n */\n back(mode: EndMode, ids: readonly string[], current: string, position: number): EndDecision;\n /**\n * Tell the policy the host opened `to` itself (a row tapped), from `from` (null: nothing was playing).\n * Call it on every open; the opens that carry out this policy's own decisions are recognised and skipped.\n * Under \"Shuffle\" Previous then comes back to `from`, and `to` counts as played this round.\n */\n opened(from: string | null, to: string): void;\n /** Forget the shuffle's round and what was played before: call when the set changes to another one (a new list was played from). */\n reset(): void;\n}\n\nexport interface EndPolicyOptions {\n /** A number in [0, 1): `Math.random` by default; a seeded one for tests. */\n random?: () => number;\n /** Seconds into an item past which Previous restarts it (default `PREVIOUS_RESTARTS_AFTER_S`). */\n restartAfterS?: number;\n}\n\nconst STOP: EndDecision = { kind: 'stop' };\nconst REPLAY: EndDecision = { kind: 'replay' };\n\n/** At most this many items are remembered for Previous under \"Shuffle\". */\nconst HISTORY_LIMIT = 500;\n\n/** The policy for one player. Every mode but shuffle is a pure function of the set and the current item. */\nexport function createEndPolicy({ random = Math.random, restartAfterS = PREVIOUS_RESTARTS_AFTER_S }: EndPolicyOptions = {}): EndPolicy {\n /** Items played in this shuffle round. */\n let played = new Set<string>();\n /** Under \"Shuffle\", the items moved on from, latest last: where Previous goes. */\n let history: string[] = [];\n /** Under \"Shuffle\", the items Previous came back from, latest last: where Next (and the end) go again first. */\n let forward: string[] = [];\n /** The shuffle's pick for `after`, kept so `peek` and `take` agree and an \"Up next\" line does not flicker. */\n let picked: { after: string; id: string; wrapped: boolean } | null = null;\n /** The item the policy's last move went to, so the host's `opened` call for that move is not counted twice. */\n let moved: string | null = null;\n /** Whether the last call was under \"Shuffle\": leaving or entering it forgets the shuffle's back and forth. */\n let shuffling = false;\n\n const enter = (mode: EndMode) => {\n const s = mode === 'shuffle';\n if (s !== shuffling) { history = []; forward = []; shuffling = s; }\n };\n const remember = (id: string) => {\n if (history[history.length - 1] !== id) history.push(id);\n if (history.length > HISTORY_LIMIT) history.splice(0, history.length - HISTORY_LIMIT);\n };\n /** The top of `forward` still worth going to (in the set, not the current item), dropping the others. */\n const forwardTop = (ids: readonly string[], current: string): string | null => {\n while (forward.length) {\n const id = forward[forward.length - 1];\n if (id !== current && ids.includes(id)) return id;\n forward.pop();\n }\n return null;\n };\n\n const shufflePick = (ids: readonly string[], current: string): EndDecision => {\n const again = forwardTop(ids, current);\n if (again) return { kind: 'play', id: again, wrapped: false };\n if (picked && picked.after === current && ids.includes(picked.id) && picked.id !== current) {\n return { kind: 'play', id: picked.id, wrapped: picked.wrapped };\n }\n const others = ids.filter((id) => id !== current);\n if (!others.length) return ids.includes(current) ? REPLAY : STOP;\n const fresh = others.filter((id) => !played.has(id));\n const wrapped = fresh.length === 0;\n const pool = wrapped ? others : fresh;\n const id = pool[Math.min(pool.length - 1, Math.floor(random() * pool.length))];\n picked = { after: current, id, wrapped };\n return { kind: 'play', id, wrapped };\n };\n\n const decide = (mode: EndMode, ids: readonly string[], current: string): EndDecision => {\n enter(mode);\n if (mode === 'stop') return STOP;\n if (mode === 'repeat-one') return REPLAY;\n if (mode === 'shuffle') return shufflePick(ids, current);\n const i = ids.indexOf(current);\n if (mode === 'next') return i >= 0 && i + 1 < ids.length ? { kind: 'play', id: ids[i + 1], wrapped: false } : STOP;\n // repeat-all\n if (!ids.length) return STOP;\n if (i < 0) return { kind: 'play', id: ids[0], wrapped: false };\n if (ids.length === 1) return REPLAY;\n return i + 1 < ids.length ? { kind: 'play', id: ids[i + 1], wrapped: false } : { kind: 'play', id: ids[0], wrapped: true };\n };\n\n const take = (mode: EndMode, ids: readonly string[], current: string): EndDecision => {\n const d = decide(mode, ids, current);\n if (mode === 'shuffle') {\n if (d.kind === 'play' && forward[forward.length - 1] === d.id) forward.pop(); // forward again, through what Previous left\n // A new round: everything is fresh again, the item that just ended included (it only may not\n // start the round, which `others` already ensures).\n else if (d.kind === 'play' && d.wrapped) played = new Set();\n else played.add(current);\n if (d.kind === 'play') remember(current);\n }\n moved = d.kind === 'play' ? d.id : null;\n picked = null;\n return d;\n };\n\n const back = (mode: EndMode, ids: readonly string[], current: string, position: number): EndDecision => {\n enter(mode);\n moved = null;\n if (position > restartAfterS) return REPLAY;\n if (mode === 'shuffle') {\n // Back through what was played, skipping items that left the set (or the current one, met again).\n while (history.length) {\n const id = history.pop()!;\n if (id !== current && ids.includes(id)) {\n forward.push(current);\n picked = null;\n moved = id;\n return { kind: 'play', id, wrapped: false };\n }\n }\n return REPLAY;\n }\n const i = ids.indexOf(current);\n const d: EndDecision = i > 0 ? { kind: 'play', id: ids[i - 1], wrapped: false }\n // Under \"Repeat all\" the list goes round backwards too: from the first (or an item not in it) to the last.\n : mode === 'repeat-all' && ids.length > (i === 0 ? 1 : 0) ? { kind: 'play', id: ids[ids.length - 1], wrapped: i === 0 }\n : REPLAY;\n moved = d.kind === 'play' ? d.id : null;\n return d;\n };\n\n return {\n peek: decide,\n take,\n peekSkip: (mode, ids, current) => decide(skipMode(mode), ids, current),\n skip: (mode, ids, current) => take(skipMode(mode), ids, current),\n back,\n opened(from, to) {\n if (to === moved) { moved = null; return; } // the policy's own move, already counted\n moved = null;\n picked = null;\n forward = [];\n if (!shuffling) return;\n if (from && from !== to) remember(from);\n played.add(to);\n },\n reset() { played = new Set(); picked = null; history = []; forward = []; moved = null; },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA4CO,IAAM,YAAY,CAAC,QAAQ,cAAc,QAAQ,cAAc,SAAS;AAIxE,IAAM,mBAA4B;AAelC,IAAM,gBAA8C;AAAA,EACzD,MAAM;AAAA,IACJ,MAAM;AAAA,IAAQ,OAAO;AAAA,IAAuB,OAAO;AAAA,IACnD,aAAa;AAAA,IACb,MAAM,EAAE,UAAU,gBAAgB,QAAQ,sBAAsB;AAAA,EAClE;AAAA,EACA,cAAc;AAAA,IACZ,MAAM;AAAA,IAAc,OAAO;AAAA,IAAmB,OAAO;AAAA,IACrD,aAAa;AAAA,IACb,MAAM,EAAE,UAAU,cAAc,QAAQ,WAAW;AAAA,EACrD;AAAA,EACA,MAAM;AAAA,IACJ,MAAM;AAAA,IAAQ,OAAO;AAAA,IAA0B,OAAO;AAAA,IACtD,aAAa;AAA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AA,QAC5C;AAAA,MACF;AACA,aAAO;AAAA,IACT;AACA,UAAM,IAAI,IAAI,QAAQ,OAAO;AAC7B,UAAM,IAAiB,IAAI,IAAI,EAAE,MAAM,QAAQ,IAAI,IAAI,IAAI,CAAC,GAAG,SAAS,MAAM,IAE1E,SAAS,gBAAgB,IAAI,UAAU,MAAM,IAAI,IAAI,KAAK,EAAE,MAAM,QAAQ,IAAI,IAAI,IAAI,SAAS,CAAC,GAAG,SAAS,MAAM,EAAE,IACpH;AACJ,YAAQ,EAAE,SAAS,SAAS,EAAE,KAAK;AACnC,WAAO;AAAA,EACT;AAEA,SAAO;AAAA,IACL,MAAM;AAAA,IACN;AAAA,IACA,UAAU,CAAC,MAAM,KAAK,YAAY,OAAO,SAAS,IAAI,GAAG,KAAK,OAAO;AAAA,IACrE,MAAM,CAAC,MAAM,KAAK,YAAY,KAAK,SAAS,IAAI,GAAG,KAAK,OAAO;AAAA,IAC/D;AAAA,IACA,OAAO,MAAM,IAAI;AACf,UAAI,OAAO,OAAO;AAAE,gBAAQ;AAAM;AAAA,MAAQ;AAC1C,cAAQ;AACR,eAAS;AACT,gBAAU,CAAC;AACX,UAAI,CAAC,UAAW;AAChB,UAAI,QAAQ,SAAS,GAAI,UAAS,IAAI;AACtC,aAAO,IAAI,EAAE;AAAA,IACf;AAAA,IACA,QAAQ;AAAE,eAAS,oBAAI,IAAI;AAAG,eAAS;AAAM,gBAAU,CAAC;AAAG,gBAAU,CAAC;AAAG,cAAQ;AAAA,IAAM;AAAA,EACzF;AACF;","names":["id"]}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What plays when an item ends: one mode, chosen by the listener, applied to the set playback
|
|
3
|
+
* started from (the list, filter, playlist or album the item was played from: its "play context").
|
|
4
|
+
* Headless and pure apart from the shuffle's memory: no DOM, no zustand, no React.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* const policy = createEndPolicy();
|
|
8
|
+
* audio.addEventListener('ended', () => {
|
|
9
|
+
* const d = policy.take(mode, setIds, currentId); // 'stop' | 'replay' | 'play' (an id)
|
|
10
|
+
* if (d.kind === 'play') open(d.id); else if (d.kind === 'replay') restart();
|
|
11
|
+
* });
|
|
12
|
+
* policy.peek(mode, setIds, currentId); // the same decision, for an "Up next" line
|
|
13
|
+
* nextButton.onclick = () => act(policy.skip(mode, setIds, currentId)); // ▶|
|
|
14
|
+
* prevButton.onclick = () => act(policy.back(mode, setIds, currentId, audio.currentTime)); // |◀
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* The five modes are the classic players' states folded into one choice (Android's and Apple's
|
|
18
|
+
* repeat off / one / all plus shuffle on / off, and foobar2000's single "playback order" menu with
|
|
19
|
+
* "stop after current"), named in words, because a cycling repeat button whose only difference
|
|
20
|
+
* between two states is a tiny "1" is the most reported confusion in the genre. Research and the
|
|
21
|
+
* decision: `docs/research/end-of-item-modes.md` in this repository.
|
|
22
|
+
*
|
|
23
|
+
* Choices, each a default the host can change:
|
|
24
|
+
* - **Shuffle is a shuffle, not a die.** Every item of the set plays once, in random order, before
|
|
25
|
+
* any plays again; then a new round starts, never with the item that just ended. It goes on until
|
|
26
|
+
* the listener stops (or a sleep timer does). Items added to the set join the current round;
|
|
27
|
+
* items taken out are never picked.
|
|
28
|
+
* - **Repeat one replays the item**; "In order" stops after the last item of the set; "Repeat all"
|
|
29
|
+
* wraps to the first. A set of one item under "Repeat all" or "Shuffle" replays it.
|
|
30
|
+
* - **An item not in the set** (taken out of the playlist while it played): "In order" stops,
|
|
31
|
+
* "Repeat all" goes to the set's first item, "Shuffle" picks from the set.
|
|
32
|
+
*
|
|
33
|
+
* The transport's buttons follow the same set and mode, as every mainstream player does:
|
|
34
|
+
* - **Next (▶|)** moves on now. "Repeat this one" and "Stop after this one" are about what happens
|
|
35
|
+
* when an item *ends*, so they do not trap the button: it goes to the next item of the list in
|
|
36
|
+
* order. Past the last item it wraps under "Repeat all", picks the next of the round under
|
|
37
|
+
* "Shuffle", and otherwise does nothing (a host disables the button: `peekSkip` says so).
|
|
38
|
+
* - **Previous (|◀)** restarts the current item when more than `restartAfterS` (3 s, the CD-player
|
|
39
|
+
* rule) has played; otherwise it goes to the item before: the one played before it under
|
|
40
|
+
* "Shuffle", the one above it in the list otherwise, wrapping to the last under "Repeat all". With
|
|
41
|
+
* nothing before, it restarts.
|
|
42
|
+
*/
|
|
43
|
+
/** The modes, in the order a menu lists them (stop first, then the loops). */
|
|
44
|
+
declare const END_MODES: readonly ["stop", "repeat-one", "next", "repeat-all", "shuffle"];
|
|
45
|
+
type EndMode = (typeof END_MODES)[number];
|
|
46
|
+
/** The default: play on through the set, in order, and stop after its last item (a playlist played through). */
|
|
47
|
+
declare const DEFAULT_END_MODE: EndMode;
|
|
48
|
+
interface EndModeInfo {
|
|
49
|
+
mode: EndMode;
|
|
50
|
+
/** The menu's words: what happens when an item ends. */
|
|
51
|
+
label: string;
|
|
52
|
+
/** The word shown on the control beside its icon (always shown: the icon alone is ambiguous). */
|
|
53
|
+
short: string;
|
|
54
|
+
/** One sentence for a tooltip or a screen reader. */
|
|
55
|
+
description: string;
|
|
56
|
+
/** The standard icon's name in Material Symbols and in Lucide (a host draws it from either set). */
|
|
57
|
+
icon: {
|
|
58
|
+
material: string;
|
|
59
|
+
lucide: string;
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/** The terms and icons, one source for every host (chosen in `docs/research/end-of-item-modes.md`). */
|
|
63
|
+
declare const END_MODE_INFO: Record<EndMode, EndModeInfo>;
|
|
64
|
+
/** Whether a value is one of the modes (a persisted preference may hold an old or foreign value). */
|
|
65
|
+
declare const isEndMode: (value: unknown) => value is EndMode;
|
|
66
|
+
/** The mode after `mode` in menu order, wrapping: for a single key or button that steps through them. */
|
|
67
|
+
declare function nextEndMode(mode: EndMode): EndMode;
|
|
68
|
+
/** What to do when the current item ends. */
|
|
69
|
+
type EndDecision = {
|
|
70
|
+
kind: 'stop';
|
|
71
|
+
}
|
|
72
|
+
/** Play the current item again from its start. */
|
|
73
|
+
| {
|
|
74
|
+
kind: 'replay';
|
|
75
|
+
}
|
|
76
|
+
/** Play this item of the set; `wrapped`: the set starts over (repeat all past the end, a new shuffle round). */
|
|
77
|
+
| {
|
|
78
|
+
kind: 'play';
|
|
79
|
+
id: string;
|
|
80
|
+
wrapped: boolean;
|
|
81
|
+
};
|
|
82
|
+
/** How far into an item (seconds) Previous restarts it instead of going to the item before: the CD-player rule. */
|
|
83
|
+
declare const PREVIOUS_RESTARTS_AFTER_S = 3;
|
|
84
|
+
/**
|
|
85
|
+
* The mode the Next button follows: the end-of-item mode, except that "Repeat this one" and
|
|
86
|
+
* "Stop after this one" say what happens at an item's end, not where Next goes, so Next goes on in order.
|
|
87
|
+
*/
|
|
88
|
+
declare const skipMode: (mode: EndMode) => EndMode;
|
|
89
|
+
interface EndPolicy {
|
|
90
|
+
/** What `take` would decide now, without moving on (an "Up next" line). Stable until `take` or `reset`. */
|
|
91
|
+
peek(mode: EndMode, ids: readonly string[], current: string): EndDecision;
|
|
92
|
+
/** Decide at the end of `current`, and remember it (the shuffle's round). */
|
|
93
|
+
take(mode: EndMode, ids: readonly string[], current: string): EndDecision;
|
|
94
|
+
/**
|
|
95
|
+
* What `skip` would do now (Next, ▶|), without moving on: `stop` means there is nothing next (disable the button).
|
|
96
|
+
* Under "Shuffle" this draws (and keeps) the pick, as `peek` does, so the button and an "Up next" line agree.
|
|
97
|
+
*/
|
|
98
|
+
peekSkip(mode: EndMode, ids: readonly string[], current: string): EndDecision;
|
|
99
|
+
/** Next (▶|): go on now, in the list's order (see `skipMode`), and remember it like `take`. */
|
|
100
|
+
skip(mode: EndMode, ids: readonly string[], current: string): EndDecision;
|
|
101
|
+
/**
|
|
102
|
+
* Previous (|◀) at `position` seconds into `current`: `replay` (restart it) when past `restartAfterS`
|
|
103
|
+
* or when nothing comes before, else the item before. `wrapped` here means it went round backwards (from the
|
|
104
|
+
* first to the last under "Repeat all"), not that a new round starts. Under "Shuffle", Next and the end then
|
|
105
|
+
* go forward again through the items Previous came back from, before any new pick.
|
|
106
|
+
*/
|
|
107
|
+
back(mode: EndMode, ids: readonly string[], current: string, position: number): EndDecision;
|
|
108
|
+
/**
|
|
109
|
+
* Tell the policy the host opened `to` itself (a row tapped), from `from` (null: nothing was playing).
|
|
110
|
+
* Call it on every open; the opens that carry out this policy's own decisions are recognised and skipped.
|
|
111
|
+
* Under "Shuffle" Previous then comes back to `from`, and `to` counts as played this round.
|
|
112
|
+
*/
|
|
113
|
+
opened(from: string | null, to: string): void;
|
|
114
|
+
/** Forget the shuffle's round and what was played before: call when the set changes to another one (a new list was played from). */
|
|
115
|
+
reset(): void;
|
|
116
|
+
}
|
|
117
|
+
interface EndPolicyOptions {
|
|
118
|
+
/** A number in [0, 1): `Math.random` by default; a seeded one for tests. */
|
|
119
|
+
random?: () => number;
|
|
120
|
+
/** Seconds into an item past which Previous restarts it (default `PREVIOUS_RESTARTS_AFTER_S`). */
|
|
121
|
+
restartAfterS?: number;
|
|
122
|
+
}
|
|
123
|
+
/** The policy for one player. Every mode but shuffle is a pure function of the set and the current item. */
|
|
124
|
+
declare function createEndPolicy({ random, restartAfterS }?: EndPolicyOptions): EndPolicy;
|
|
125
|
+
|
|
126
|
+
export { DEFAULT_END_MODE, END_MODES, END_MODE_INFO, type EndDecision, type EndMode, type EndModeInfo, type EndPolicy, type EndPolicyOptions, PREVIOUS_RESTARTS_AFTER_S, createEndPolicy, isEndMode, nextEndMode, skipMode };
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What plays when an item ends: one mode, chosen by the listener, applied to the set playback
|
|
3
|
+
* started from (the list, filter, playlist or album the item was played from: its "play context").
|
|
4
|
+
* Headless and pure apart from the shuffle's memory: no DOM, no zustand, no React.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* const policy = createEndPolicy();
|
|
8
|
+
* audio.addEventListener('ended', () => {
|
|
9
|
+
* const d = policy.take(mode, setIds, currentId); // 'stop' | 'replay' | 'play' (an id)
|
|
10
|
+
* if (d.kind === 'play') open(d.id); else if (d.kind === 'replay') restart();
|
|
11
|
+
* });
|
|
12
|
+
* policy.peek(mode, setIds, currentId); // the same decision, for an "Up next" line
|
|
13
|
+
* nextButton.onclick = () => act(policy.skip(mode, setIds, currentId)); // ▶|
|
|
14
|
+
* prevButton.onclick = () => act(policy.back(mode, setIds, currentId, audio.currentTime)); // |◀
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* The five modes are the classic players' states folded into one choice (Android's and Apple's
|
|
18
|
+
* repeat off / one / all plus shuffle on / off, and foobar2000's single "playback order" menu with
|
|
19
|
+
* "stop after current"), named in words, because a cycling repeat button whose only difference
|
|
20
|
+
* between two states is a tiny "1" is the most reported confusion in the genre. Research and the
|
|
21
|
+
* decision: `docs/research/end-of-item-modes.md` in this repository.
|
|
22
|
+
*
|
|
23
|
+
* Choices, each a default the host can change:
|
|
24
|
+
* - **Shuffle is a shuffle, not a die.** Every item of the set plays once, in random order, before
|
|
25
|
+
* any plays again; then a new round starts, never with the item that just ended. It goes on until
|
|
26
|
+
* the listener stops (or a sleep timer does). Items added to the set join the current round;
|
|
27
|
+
* items taken out are never picked.
|
|
28
|
+
* - **Repeat one replays the item**; "In order" stops after the last item of the set; "Repeat all"
|
|
29
|
+
* wraps to the first. A set of one item under "Repeat all" or "Shuffle" replays it.
|
|
30
|
+
* - **An item not in the set** (taken out of the playlist while it played): "In order" stops,
|
|
31
|
+
* "Repeat all" goes to the set's first item, "Shuffle" picks from the set.
|
|
32
|
+
*
|
|
33
|
+
* The transport's buttons follow the same set and mode, as every mainstream player does:
|
|
34
|
+
* - **Next (▶|)** moves on now. "Repeat this one" and "Stop after this one" are about what happens
|
|
35
|
+
* when an item *ends*, so they do not trap the button: it goes to the next item of the list in
|
|
36
|
+
* order. Past the last item it wraps under "Repeat all", picks the next of the round under
|
|
37
|
+
* "Shuffle", and otherwise does nothing (a host disables the button: `peekSkip` says so).
|
|
38
|
+
* - **Previous (|◀)** restarts the current item when more than `restartAfterS` (3 s, the CD-player
|
|
39
|
+
* rule) has played; otherwise it goes to the item before: the one played before it under
|
|
40
|
+
* "Shuffle", the one above it in the list otherwise, wrapping to the last under "Repeat all". With
|
|
41
|
+
* nothing before, it restarts.
|
|
42
|
+
*/
|
|
43
|
+
/** The modes, in the order a menu lists them (stop first, then the loops). */
|
|
44
|
+
declare const END_MODES: readonly ["stop", "repeat-one", "next", "repeat-all", "shuffle"];
|
|
45
|
+
type EndMode = (typeof END_MODES)[number];
|
|
46
|
+
/** The default: play on through the set, in order, and stop after its last item (a playlist played through). */
|
|
47
|
+
declare const DEFAULT_END_MODE: EndMode;
|
|
48
|
+
interface EndModeInfo {
|
|
49
|
+
mode: EndMode;
|
|
50
|
+
/** The menu's words: what happens when an item ends. */
|
|
51
|
+
label: string;
|
|
52
|
+
/** The word shown on the control beside its icon (always shown: the icon alone is ambiguous). */
|
|
53
|
+
short: string;
|
|
54
|
+
/** One sentence for a tooltip or a screen reader. */
|
|
55
|
+
description: string;
|
|
56
|
+
/** The standard icon's name in Material Symbols and in Lucide (a host draws it from either set). */
|
|
57
|
+
icon: {
|
|
58
|
+
material: string;
|
|
59
|
+
lucide: string;
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/** The terms and icons, one source for every host (chosen in `docs/research/end-of-item-modes.md`). */
|
|
63
|
+
declare const END_MODE_INFO: Record<EndMode, EndModeInfo>;
|
|
64
|
+
/** Whether a value is one of the modes (a persisted preference may hold an old or foreign value). */
|
|
65
|
+
declare const isEndMode: (value: unknown) => value is EndMode;
|
|
66
|
+
/** The mode after `mode` in menu order, wrapping: for a single key or button that steps through them. */
|
|
67
|
+
declare function nextEndMode(mode: EndMode): EndMode;
|
|
68
|
+
/** What to do when the current item ends. */
|
|
69
|
+
type EndDecision = {
|
|
70
|
+
kind: 'stop';
|
|
71
|
+
}
|
|
72
|
+
/** Play the current item again from its start. */
|
|
73
|
+
| {
|
|
74
|
+
kind: 'replay';
|
|
75
|
+
}
|
|
76
|
+
/** Play this item of the set; `wrapped`: the set starts over (repeat all past the end, a new shuffle round). */
|
|
77
|
+
| {
|
|
78
|
+
kind: 'play';
|
|
79
|
+
id: string;
|
|
80
|
+
wrapped: boolean;
|
|
81
|
+
};
|
|
82
|
+
/** How far into an item (seconds) Previous restarts it instead of going to the item before: the CD-player rule. */
|
|
83
|
+
declare const PREVIOUS_RESTARTS_AFTER_S = 3;
|
|
84
|
+
/**
|
|
85
|
+
* The mode the Next button follows: the end-of-item mode, except that "Repeat this one" and
|
|
86
|
+
* "Stop after this one" say what happens at an item's end, not where Next goes, so Next goes on in order.
|
|
87
|
+
*/
|
|
88
|
+
declare const skipMode: (mode: EndMode) => EndMode;
|
|
89
|
+
interface EndPolicy {
|
|
90
|
+
/** What `take` would decide now, without moving on (an "Up next" line). Stable until `take` or `reset`. */
|
|
91
|
+
peek(mode: EndMode, ids: readonly string[], current: string): EndDecision;
|
|
92
|
+
/** Decide at the end of `current`, and remember it (the shuffle's round). */
|
|
93
|
+
take(mode: EndMode, ids: readonly string[], current: string): EndDecision;
|
|
94
|
+
/**
|
|
95
|
+
* What `skip` would do now (Next, ▶|), without moving on: `stop` means there is nothing next (disable the button).
|
|
96
|
+
* Under "Shuffle" this draws (and keeps) the pick, as `peek` does, so the button and an "Up next" line agree.
|
|
97
|
+
*/
|
|
98
|
+
peekSkip(mode: EndMode, ids: readonly string[], current: string): EndDecision;
|
|
99
|
+
/** Next (▶|): go on now, in the list's order (see `skipMode`), and remember it like `take`. */
|
|
100
|
+
skip(mode: EndMode, ids: readonly string[], current: string): EndDecision;
|
|
101
|
+
/**
|
|
102
|
+
* Previous (|◀) at `position` seconds into `current`: `replay` (restart it) when past `restartAfterS`
|
|
103
|
+
* or when nothing comes before, else the item before. `wrapped` here means it went round backwards (from the
|
|
104
|
+
* first to the last under "Repeat all"), not that a new round starts. Under "Shuffle", Next and the end then
|
|
105
|
+
* go forward again through the items Previous came back from, before any new pick.
|
|
106
|
+
*/
|
|
107
|
+
back(mode: EndMode, ids: readonly string[], current: string, position: number): EndDecision;
|
|
108
|
+
/**
|
|
109
|
+
* Tell the policy the host opened `to` itself (a row tapped), from `from` (null: nothing was playing).
|
|
110
|
+
* Call it on every open; the opens that carry out this policy's own decisions are recognised and skipped.
|
|
111
|
+
* Under "Shuffle" Previous then comes back to `from`, and `to` counts as played this round.
|
|
112
|
+
*/
|
|
113
|
+
opened(from: string | null, to: string): void;
|
|
114
|
+
/** Forget the shuffle's round and what was played before: call when the set changes to another one (a new list was played from). */
|
|
115
|
+
reset(): void;
|
|
116
|
+
}
|
|
117
|
+
interface EndPolicyOptions {
|
|
118
|
+
/** A number in [0, 1): `Math.random` by default; a seeded one for tests. */
|
|
119
|
+
random?: () => number;
|
|
120
|
+
/** Seconds into an item past which Previous restarts it (default `PREVIOUS_RESTARTS_AFTER_S`). */
|
|
121
|
+
restartAfterS?: number;
|
|
122
|
+
}
|
|
123
|
+
/** The policy for one player. Every mode but shuffle is a pure function of the set and the current item. */
|
|
124
|
+
declare function createEndPolicy({ random, restartAfterS }?: EndPolicyOptions): EndPolicy;
|
|
125
|
+
|
|
126
|
+
export { DEFAULT_END_MODE, END_MODES, END_MODE_INFO, type EndDecision, type EndMode, type EndModeInfo, type EndPolicy, type EndPolicyOptions, PREVIOUS_RESTARTS_AFTER_S, createEndPolicy, isEndMode, nextEndMode, skipMode };
|
package/dist/end-mode.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_END_MODE,
|
|
3
|
+
END_MODES,
|
|
4
|
+
END_MODE_INFO,
|
|
5
|
+
PREVIOUS_RESTARTS_AFTER_S,
|
|
6
|
+
createEndPolicy,
|
|
7
|
+
isEndMode,
|
|
8
|
+
nextEndMode,
|
|
9
|
+
skipMode
|
|
10
|
+
} from "./chunk-GALBANEF.js";
|
|
11
|
+
export {
|
|
12
|
+
DEFAULT_END_MODE,
|
|
13
|
+
END_MODES,
|
|
14
|
+
END_MODE_INFO,
|
|
15
|
+
PREVIOUS_RESTARTS_AFTER_S,
|
|
16
|
+
createEndPolicy,
|
|
17
|
+
isEndMode,
|
|
18
|
+
nextEndMode,
|
|
19
|
+
skipMode
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=end-mode.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|