@torrent-tv/proxy 2.83.7 → 2.84.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.
@@ -1,318 +1,316 @@
1
- /**
2
- * @file The priority map, built once and handed to everybody who acts on it.
3
- *
4
- * One map per film, in seconds of film against a number. It is built from where
5
- * the viewers are and nothing else, and both of the things that do work — the
6
- * encoding and the downloading — read it and decide for themselves. They do not
7
- * talk to each other, and neither of them tells this class anything.
8
- *
9
- * **Why it has to be published rather than asked for.** The downloading lives
10
- * in another thread. Until now it took its orders from the reads themselves:
11
- * every read declared a window around its own head, so fifteen reads declared
12
- * fifteen windows on a piece store that holds sixteen pieces. Half of all
13
- * evictions then took a piece a reader had said it wanted, two thirds of reads
14
- * came back from disk, and what `/stream` handed out stopped being the file's
15
- * bytes — twenty-two source-parse errors, a segment the player could not
16
- * append, and an empty picture for six minutes (field 2026-09-05).
17
- */
18
-
19
- import { emptyMap, mapForViewer, mergeMaps, runsOf } from "./PriorityMap.js";
20
-
21
- export class PriorityOrchestrator {
22
- /** Where the map goes once it is built. @type {(published: object) => void} */
23
- #publish;
24
-
25
- /**
26
- * The last map published per film and file, so an unchanged one is not
27
- * resent — and what it was about, so that a file everybody has left can be
28
- * published as wanting nothing.
29
- *
30
- * @type {Map<string, { shape: string, sourceKey: string, fileIndex: number, durationSeconds: number }>}
31
- */
32
- #last = new Map();
33
-
34
- /** The last map BUILT per film and file, for whoever reads instead of being
35
- * handed it. @type {Map<string, import("./PriorityMap.js").PriorityMap>} */
36
- #maps = new Map();
37
-
38
- /**
39
- * The last map built per OUTPUT, from the viewers of that output alone.
40
- *
41
- * The same fact answered at two scopes, because the two things that act on it
42
- * ask at two scopes and both are right. The swarm is asked for bytes of a
43
- * FILE, and the picture, a quality step and a soundtrack of one film read the
44
- * same bytes — so every viewer of any of them wants that file's bytes.
45
- * Encoders are placed per OUTPUT, and a person watching 480p wants nothing of
46
- * the 1080p output at all.
47
- *
48
- * One map for both was the second authority over encoders. Every output of a
49
- * film was handed the whole film's map, so the plan wanted an encoder on every
50
- * one of them; what actually stopped the ones nobody was watching was the
51
- * session manager killing them by its own judgement — and since a viewer
52
- * moving between steps also announces itself, the plan started them again on
53
- * the next pass. Two parties answering "should this encoder exist" by different
54
- * rules, several times a second.
55
- *
56
- * @type {Map<string, import("./PriorityMap.js").PriorityMap>}
57
- */
58
- #byOutput = new Map();
59
-
60
- /** Who is watching one session. @type {(session: object) => Map<string, object>} */
61
- #viewersOf;
62
-
63
- /** How wide the first band of one session's file is. @type {(session: object) => number} */
64
- #allowanceFor;
65
-
66
- /** Whether this output is what that person is consuming, as opposed to one
67
- * they merely hold a record on. @type {(session: object, viewer: object) => boolean} */
68
- #watchedBy;
69
-
70
- /**
71
- * This layer states facts and imports nothing above itself, so what it needs
72
- * of a session — who is watching it, and how wide an interruption this file
73
- * has shown on this swarm — is passed in.
74
- *
75
- * @param {object} params
76
- * @param {(published: { sourceKey: string, fileIndex: number, durationSeconds: number,
77
- * zones: { from: number, to: number, priority: number }[] }) => void} params.publish
78
- * @param {(session: object) => Map<string, object>} [params.viewersOf]
79
- * @param {(session: object) => number} [params.allowanceFor]
80
- * @param {(session: object, viewer: object) => boolean} [params.watchedBy] -
81
- * Whether this output is the one that person is consuming. Which of a film's
82
- * outputs a person has on screen is a fact about the film's shape, which
83
- * this layer does not know; absent, every registered viewer counts, and then
84
- * the per-output map says the same as the per-file one.
85
- */
86
- constructor({ publish, viewersOf, allowanceFor, watchedBy }) {
87
- this.#publish = typeof publish === "function" ? publish : () => {};
88
- this.#viewersOf = typeof viewersOf === "function" ? viewersOf : () => new Map();
89
- this.#allowanceFor = typeof allowanceFor === "function" ? allowanceFor : () => 0;
90
- this.#watchedBy = typeof watchedBy === "function" ? watchedBy : () => true;
91
- }
92
-
93
- /**
94
- * A map from a set of viewers, and the ONE statement of how one is built.
95
- *
96
- * Asked at both scopes — once per film for the swarm, once per output for the
97
- * encoders — and written once, because two copies of how a viewer's map is
98
- * built is the same two-owners fault this class was split for.
99
- *
100
- * @param {object} params
101
- * @param {number} params.durationSeconds
102
- * @param {number} params.allowanceSeconds
103
- * @param {{ atSeconds: number, playing: boolean }[]} params.viewers
104
- * @returns {import("./PriorityMap.js").PriorityMap} A map of no length where
105
- * nobody is watching or the film's length is unknown, which says the same as
106
- * a map with nothing in it.
107
- */
108
- #mapFrom({ durationSeconds, allowanceSeconds, viewers }) {
109
- if (!(durationSeconds > 0) || !(viewers?.length > 0)) {
110
- return emptyMap(0);
111
- }
112
- return mergeMaps(
113
- viewers.map((viewer) =>
114
- mapForViewer({
115
- atSeconds: viewer.atSeconds,
116
- durationSeconds,
117
- allowanceSeconds,
118
- playing: viewer.playing !== false
119
- })
120
- )
121
- );
122
- }
123
-
124
- /**
125
- * The map for one film, from everyone watching it.
126
- *
127
- * @param {object} params
128
- * @param {string} params.sourceKey
129
- * @param {number} params.fileIndex
130
- * @param {number} params.durationSeconds
131
- * @param {number} params.allowanceSeconds - The measured depth below which an
132
- * interruption reaches a viewer of this file.
133
- * @param {{ atSeconds: number, playing: boolean }[]} params.viewers
134
- * @returns {import("./PriorityMap.js").PriorityMap} One number per second of
135
- * film, merged over everyone watching it.
136
- */
137
- build({ sourceKey, fileIndex, durationSeconds, allowanceSeconds, viewers }) {
138
- const map = this.#mapFrom({ durationSeconds, allowanceSeconds, viewers });
139
- const key = `${sourceKey}:${fileIndex}`;
140
- this.#maps.set(key, map);
141
- // Unchanged maps are not republished: the downloading rebuilds what it asks
142
- // the swarm for on every one, and a viewer sitting still would otherwise
143
- // make it do that several times a second. Compared as stretches rather than
144
- // second by second, which is the same comparison over far fewer values.
145
- const zones = runsOf(map);
146
- const shape = JSON.stringify(zones);
147
- if (this.#last.get(key)?.shape !== shape) {
148
- // The length is remembered with the shape, so that a file whose viewers
149
- // have all gone can still be spoken for: what is published then is the
150
- // same statement with nothing in it, and it has to name the file it is
151
- // about.
152
- this.#last.set(key, { shape, sourceKey, fileIndex, durationSeconds });
153
- this.#publish({ sourceKey, fileIndex, durationSeconds, zones });
154
- }
155
- return map;
156
- }
157
-
158
- /**
159
- * NOBODY WANTS ANYTHING OF THIS FILE ANY MORE — said, rather than left to be
160
- * inferred from silence.
161
- *
162
- * The map used to be deleted from this class's memory and published nowhere,
163
- * so what had been stated about that file on the swarm's behalf stood until
164
- * the torrent itself was removed. Read from the register, a film nobody had
165
- * watched for an hour was indistinguishable from one being watched now.
166
- *
167
- * @param {{ sourceKey: string, fileIndex: number, durationSeconds: number }} what
168
- * @returns {void}
169
- */
170
- #publishNothingFor({ sourceKey, fileIndex, durationSeconds }) {
171
- this.#publish({ sourceKey, fileIndex, durationSeconds, zones: [] });
172
- }
173
-
174
- /**
175
- * Build and publish the map for every file anybody is watching.
176
- *
177
- * One map per FILE, not per output: the picture, a quality step and a
178
- * soundtrack of one film are three outputs reading the same bytes, and the
179
- * swarm is asked for bytes. Viewers of all of them merge into one map.
180
- *
181
- * @param {object} params
182
- * @param {Iterable<object[]>} params.sessionGroups - The live sessions, in
183
- * whatever grouping the caller holds them; they are regrouped by file here.
184
- * @param {number} params.staleAfterMs - How long a viewer may be silent and
185
- * still count as watching.
186
- * @param {number} [params.now]
187
- * @returns {void}
188
- */
189
- publishFor({ sessionGroups, staleAfterMs, now = Date.now() }) {
190
- /** @type {Map<string, { sourceKey: string, fileIndex: number, durationSeconds: number, allowanceSeconds: number, viewers: object[] }>} */
191
- const byFile = new Map();
192
- /** @type {Map<string, { durationSeconds: number, allowanceSeconds: number, viewers: object[] }>} */
193
- const byOutput = new Map();
194
- for (const sessions of sessionGroups) {
195
- for (const session of sessions) {
196
- const key = `${session.sourceKey}:${session.fileIndex}`;
197
- const durationSeconds = Number(session.file?.durationSeconds) || 0;
198
- // The first band is as wide as an interruption this file has actually
199
- // shown on this swarm, never a chosen number.
200
- const allowanceSeconds = this.#allowanceFor(session);
201
- let held = byFile.get(key);
202
- if (!held) {
203
- held = {
204
- sourceKey: session.sourceKey,
205
- fileIndex: session.fileIndex,
206
- durationSeconds,
207
- allowanceSeconds,
208
- viewers: []
209
- };
210
- byFile.set(key, held);
211
- }
212
- // Every output anybody holds a session for, whether or not a viewer is
213
- // consuming it — an output with nobody on it must get a map with
214
- // nothing in it, which is how the plan is told to stop its encoders.
215
- // Left out, it would keep the map it had when somebody was watching.
216
- const address = session.outputKey ?? "";
217
- let mine = byOutput.get(address);
218
- if (!mine) {
219
- mine = { durationSeconds, allowanceSeconds, viewers: [] };
220
- byOutput.set(address, mine);
221
- }
222
- for (const viewer of this.#viewersOf(session).values()) {
223
- if (!viewer.isPresent(now, staleAfterMs)) {
224
- continue;
225
- }
226
- const stated = {
227
- atSeconds: viewer.positionSeconds() ?? 0,
228
- // CONSUMING, not merely playing. A page that is not on screen has
229
- // its timers throttled and asks for nothing, which is exactly what
230
- // a viewer holding a full cushion looks like; the two are told
231
- // apart on the page and folded into one question here.
232
- playing: typeof viewer.consumesFilm === "function"
233
- ? viewer.consumesFilm()
234
- : viewer.playing !== false
235
- };
236
- held.viewers.push(stated);
237
- if (this.#watchedBy(session, viewer)) {
238
- mine.viewers.push(stated);
239
- }
240
- }
241
- }
242
- }
243
- // WHAT THIS PASS SAW IS ALL THERE IS. Everything below is derived from the
244
- // live sessions, so a file or an output that is not among them is gone —
245
- // and these maps are the projection of that, never a memory of it. Left to
246
- // accumulate they were three maps that only grew, which is the shape of half
247
- // the memory faults recorded in this repository, and `forget` was written
248
- // for it and called from nowhere.
249
- for (const key of [...this.#maps.keys()]) {
250
- if (!byFile.has(key)) {
251
- const last = this.#last.get(key);
252
- this.#maps.delete(key);
253
- this.#last.delete(key);
254
- if (last) {
255
- this.#publishNothingFor(last);
256
- }
257
- }
258
- }
259
- for (const address of [...this.#byOutput.keys()]) {
260
- if (!byOutput.has(address)) {
261
- this.#byOutput.delete(address);
262
- }
263
- }
264
- for (const one of byFile.values()) {
265
- // A file of unknown length cannot be divided into zones at all. A file
266
- // whose viewers have all gone CAN be spoken for, and must be: `build`
267
- // with nobody watching produces a map with nothing in it, which is the
268
- // truth and is what withdraws what was stated for them. Skipped, as it
269
- // was, the last thing said about that file stood for as long as the
270
- // torrent did.
271
- if (one.durationSeconds > 0) {
272
- this.build(one);
273
- }
274
- }
275
- for (const [address, one] of byOutput) {
276
- this.#byOutput.set(address, this.#mapFrom(one));
277
- }
278
- }
279
-
280
- /**
281
- * Nobody is watching this file any more.
282
- *
283
- * @param {string} sourceKey
284
- * @param {number} fileIndex
285
- */
286
- /**
287
- * The map this class last built for one file.
288
- *
289
- * Read by whoever acts on it and cannot be handed it at the moment it is
290
- * made — the encoding decides per output, and one file has several. It is the
291
- * SAME map: built once here, from where the viewers are, and neither read
292
- * changes it.
293
- *
294
- * @param {string} sourceKey
295
- * @param {number} fileIndex
296
- * @returns {import("./PriorityMap.js").PriorityMap} A map of no length where
297
- * none was built, which says the same as a map with nothing in it.
298
- */
299
- mapFor(sourceKey, fileIndex) {
300
- return this.#maps.get(`${sourceKey}:${fileIndex}`) ?? emptyMap(0);
301
- }
302
-
303
- /**
304
- * The map for ONE output, from the viewers consuming that output.
305
- *
306
- * What the encoding reads. A map of no length says nobody is on this output,
307
- * which is what makes an encoder on it unwanted — and it is a statement, not
308
- * an absence: the walk above writes one for every output a session exists
309
- * for, including the ones everybody has left.
310
- *
311
- * @param {string} address
312
- * @returns {import("./PriorityMap.js").PriorityMap}
313
- */
314
- mapForOutput(address) {
315
- return this.#byOutput.get(address) ?? emptyMap(0);
316
- }
317
-
318
- }
1
+ /**
2
+ * @file The priority map, built once and handed to everybody who acts on it.
3
+ *
4
+ * One map per film, in seconds of film against a number. It is built from where
5
+ * the viewers are and nothing else, and both of the things that do work — the
6
+ * encoding and the downloading — read it and decide for themselves. They do not
7
+ * talk to each other, and neither of them tells this class anything.
8
+ *
9
+ * **Why it has to be published rather than asked for.** The downloading lives
10
+ * in another thread. Until now it took its orders from the reads themselves:
11
+ * every read declared a window around its own head, so fifteen reads declared
12
+ * fifteen windows on a piece store that holds sixteen pieces. Half of all
13
+ * evictions then took a piece a reader had said it wanted, two thirds of reads
14
+ * came back from disk, and what `/stream` handed out stopped being the file's
15
+ * bytes — twenty-two source-parse errors, a segment the player could not
16
+ * append, and an empty picture for six minutes (field 2026-09-05).
17
+ */
18
+
19
+ import { emptyMap, mapForViewer, mergeMaps, runsOf } from "./PriorityMap.js";
20
+
21
+ export class PriorityOrchestrator {
22
+ /** Where the map goes once it is built. @type {(published: object) => void} */
23
+ #publish;
24
+
25
+ /**
26
+ * The last map published per film and file, so an unchanged one is not
27
+ * resent — and what it was about, so that a file everybody has left can be
28
+ * published as wanting nothing.
29
+ *
30
+ * @type {Map<string, { shape: string, sourceKey: string, fileIndex: number, durationSeconds: number }>}
31
+ */
32
+ #last = new Map();
33
+
34
+ /** The last map BUILT per film and file, for whoever reads instead of being
35
+ * handed it. @type {Map<string, import("./PriorityMap.js").PriorityMap>} */
36
+ #maps = new Map();
37
+
38
+ /**
39
+ * The last map built per OUTPUT, from the viewers of that output alone.
40
+ *
41
+ * The same fact answered at two scopes, because the two things that act on it
42
+ * ask at two scopes and both are right. The swarm is asked for bytes of a
43
+ * FILE, and the picture, a quality step and a soundtrack of one film read the
44
+ * same bytes — so every viewer of any of them wants that file's bytes.
45
+ * Encoders are placed per OUTPUT, and a person watching 480p wants nothing of
46
+ * the 1080p output at all.
47
+ *
48
+ * One map for both was the second authority over encoders. Every output of a
49
+ * film was handed the whole film's map, so the plan wanted an encoder on every
50
+ * one of them; what actually stopped the ones nobody was watching was the
51
+ * session manager killing them by its own judgement — and since a viewer
52
+ * moving between steps also announces itself, the plan started them again on
53
+ * the next pass. Two parties answering "should this encoder exist" by different
54
+ * rules, several times a second.
55
+ *
56
+ * @type {Map<string, import("./PriorityMap.js").PriorityMap>}
57
+ */
58
+ #byOutput = new Map();
59
+
60
+ /** Who is watching one session. @type {(session: object) => Map<string, object>} */
61
+ #viewersOf;
62
+
63
+ /** How wide the first band of one session's file is. @type {(session: object) => number} */
64
+ #allowanceFor;
65
+
66
+ /** Whether this output is what that person is consuming, as opposed to one
67
+ * they merely hold a record on. @type {(session: object, viewer: object) => boolean} */
68
+ #watchedBy;
69
+
70
+ /**
71
+ * This layer states facts and imports nothing above itself, so what it needs
72
+ * of a session — who is watching it, and how wide an interruption this file
73
+ * has shown on this swarm — is passed in.
74
+ *
75
+ * @param {object} params
76
+ * @param {(published: { sourceKey: string, fileIndex: number, durationSeconds: number,
77
+ * zones: { from: number, to: number, priority: number }[] }) => void} params.publish
78
+ * @param {(session: object) => Map<string, object>} [params.viewersOf]
79
+ * @param {(session: object) => number} [params.allowanceFor]
80
+ * @param {(session: object, viewer: object) => boolean} [params.watchedBy] -
81
+ * Whether this output is the one that person is consuming. Which of a film's
82
+ * outputs a person has on screen is a fact about the film's shape, which
83
+ * this layer does not know; absent, every registered viewer counts, and then
84
+ * the per-output map says the same as the per-file one.
85
+ */
86
+ constructor({ publish, viewersOf, allowanceFor, watchedBy }) {
87
+ this.#publish = typeof publish === "function" ? publish : () => {};
88
+ this.#viewersOf = typeof viewersOf === "function" ? viewersOf : () => new Map();
89
+ this.#allowanceFor = typeof allowanceFor === "function" ? allowanceFor : () => 0;
90
+ this.#watchedBy = typeof watchedBy === "function" ? watchedBy : () => true;
91
+ }
92
+
93
+ /**
94
+ * A map from a set of viewers, and the ONE statement of how one is built.
95
+ *
96
+ * Asked at both scopes — once per film for the swarm, once per output for the
97
+ * encoders — and written once, because two copies of how a viewer's map is
98
+ * built is the same two-owners fault this class was split for.
99
+ *
100
+ * @param {object} params
101
+ * @param {number} params.durationSeconds
102
+ * @param {number} params.allowanceSeconds
103
+ * @param {{ atSeconds: number, playing: boolean }[]} params.viewers
104
+ * @returns {import("./PriorityMap.js").PriorityMap} A map of no length where
105
+ * nobody is watching or the film's length is unknown, which says the same as
106
+ * a map with nothing in it.
107
+ */
108
+ #mapFrom({ durationSeconds, allowanceSeconds, viewers }) {
109
+ if (!(durationSeconds > 0) || !(viewers?.length > 0)) {
110
+ return emptyMap(0);
111
+ }
112
+ return mergeMaps(
113
+ viewers.map((viewer) =>
114
+ mapForViewer({
115
+ atSeconds: viewer.atSeconds,
116
+ durationSeconds,
117
+ allowanceSeconds,
118
+ playing: viewer.playing !== false
119
+ })
120
+ )
121
+ );
122
+ }
123
+
124
+ /**
125
+ * The map for one film, from everyone watching it.
126
+ *
127
+ * @param {object} params
128
+ * @param {string} params.sourceKey
129
+ * @param {number} params.fileIndex
130
+ * @param {number} params.durationSeconds
131
+ * @param {number} params.allowanceSeconds - The measured depth below which an
132
+ * interruption reaches a viewer of this file.
133
+ * @param {{ atSeconds: number, playing: boolean }[]} params.viewers
134
+ * @returns {import("./PriorityMap.js").PriorityMap} One number per second of
135
+ * film, merged over everyone watching it.
136
+ */
137
+ build({ sourceKey, fileIndex, durationSeconds, allowanceSeconds, viewers }) {
138
+ const map = this.#mapFrom({ durationSeconds, allowanceSeconds, viewers });
139
+ const key = `${sourceKey}:${fileIndex}`;
140
+ this.#maps.set(key, map);
141
+ // Unchanged maps are not republished: the downloading rebuilds what it asks
142
+ // the swarm for on every one, and a viewer sitting still would otherwise
143
+ // make it do that several times a second. Compared as stretches rather than
144
+ // second by second, which is the same comparison over far fewer values.
145
+ const zones = runsOf(map);
146
+ const shape = JSON.stringify(zones);
147
+ if (this.#last.get(key)?.shape !== shape) {
148
+ // The length is remembered with the shape, so that a file whose viewers
149
+ // have all gone can still be spoken for: what is published then is the
150
+ // same statement with nothing in it, and it has to name the file it is
151
+ // about.
152
+ this.#last.set(key, { shape, sourceKey, fileIndex, durationSeconds });
153
+ this.#publish({ sourceKey, fileIndex, durationSeconds, zones });
154
+ }
155
+ return map;
156
+ }
157
+
158
+ /**
159
+ * NOBODY WANTS ANYTHING OF THIS FILE ANY MORE — said, rather than left to be
160
+ * inferred from silence.
161
+ *
162
+ * The map used to be deleted from this class's memory and published nowhere,
163
+ * so what had been stated about that file on the swarm's behalf stood until
164
+ * the torrent itself was removed. Read from the register, a film nobody had
165
+ * watched for an hour was indistinguishable from one being watched now.
166
+ *
167
+ * @param {{ sourceKey: string, fileIndex: number, durationSeconds: number }} what
168
+ * @returns {void}
169
+ */
170
+ #publishNothingFor({ sourceKey, fileIndex, durationSeconds }) {
171
+ this.#publish({ sourceKey, fileIndex, durationSeconds, zones: [] });
172
+ }
173
+
174
+ /**
175
+ * Build and publish the map for every file anybody is watching.
176
+ *
177
+ * One map per FILE, not per output: the picture, a quality step and a
178
+ * soundtrack of one film are three outputs reading the same bytes, and the
179
+ * swarm is asked for bytes. Viewers of all of them merge into one map.
180
+ *
181
+ * @param {object} params
182
+ * @param {Iterable<object[]>} params.sessionGroups - The live sessions, in
183
+ * whatever grouping the caller holds them; they are regrouped by file here.
184
+ * @param {number} [params.now]
185
+ * @returns {void}
186
+ */
187
+ publishFor({ sessionGroups, now = Date.now() }) {
188
+ /** @type {Map<string, { sourceKey: string, fileIndex: number, durationSeconds: number, allowanceSeconds: number, viewers: object[] }>} */
189
+ const byFile = new Map();
190
+ /** @type {Map<string, { durationSeconds: number, allowanceSeconds: number, viewers: object[] }>} */
191
+ const byOutput = new Map();
192
+ for (const sessions of sessionGroups) {
193
+ for (const session of sessions) {
194
+ const key = `${session.sourceKey}:${session.fileIndex}`;
195
+ const durationSeconds = Number(session.file?.durationSeconds) || 0;
196
+ // The first band is as wide as an interruption this file has actually
197
+ // shown on this swarm, never a chosen number.
198
+ const allowanceSeconds = this.#allowanceFor(session);
199
+ let held = byFile.get(key);
200
+ if (!held) {
201
+ held = {
202
+ sourceKey: session.sourceKey,
203
+ fileIndex: session.fileIndex,
204
+ durationSeconds,
205
+ allowanceSeconds,
206
+ viewers: []
207
+ };
208
+ byFile.set(key, held);
209
+ }
210
+ // Every output anybody holds a session for, whether or not a viewer is
211
+ // consuming it — an output with nobody on it must get a map with
212
+ // nothing in it, which is how the plan is told to stop its encoders.
213
+ // Left out, it would keep the map it had when somebody was watching.
214
+ const address = session.outputKey ?? "";
215
+ let mine = byOutput.get(address);
216
+ if (!mine) {
217
+ mine = { durationSeconds, allowanceSeconds, viewers: [] };
218
+ byOutput.set(address, mine);
219
+ }
220
+ for (const viewer of this.#viewersOf(session).values()) {
221
+ if (!viewer.isPresent()) {
222
+ continue;
223
+ }
224
+ const stated = {
225
+ atSeconds: viewer.positionSeconds(now) ?? 0,
226
+ // CONSUMING, not merely playing. A page that is not on screen has
227
+ // its timers throttled and asks for nothing, which is exactly what
228
+ // a viewer holding a full cushion looks like; the two are told
229
+ // apart on the page and folded into one question here.
230
+ playing: typeof viewer.consumesFilm === "function"
231
+ ? viewer.consumesFilm()
232
+ : viewer.playing !== false
233
+ };
234
+ held.viewers.push(stated);
235
+ if (this.#watchedBy(session, viewer)) {
236
+ mine.viewers.push(stated);
237
+ }
238
+ }
239
+ }
240
+ }
241
+ // WHAT THIS PASS SAW IS ALL THERE IS. Everything below is derived from the
242
+ // live sessions, so a file or an output that is not among them is gone —
243
+ // and these maps are the projection of that, never a memory of it. Left to
244
+ // accumulate they were three maps that only grew, which is the shape of half
245
+ // the memory faults recorded in this repository, and `forget` was written
246
+ // for it and called from nowhere.
247
+ for (const key of [...this.#maps.keys()]) {
248
+ if (!byFile.has(key)) {
249
+ const last = this.#last.get(key);
250
+ this.#maps.delete(key);
251
+ this.#last.delete(key);
252
+ if (last) {
253
+ this.#publishNothingFor(last);
254
+ }
255
+ }
256
+ }
257
+ for (const address of [...this.#byOutput.keys()]) {
258
+ if (!byOutput.has(address)) {
259
+ this.#byOutput.delete(address);
260
+ }
261
+ }
262
+ for (const one of byFile.values()) {
263
+ // A file of unknown length cannot be divided into zones at all. A file
264
+ // whose viewers have all gone CAN be spoken for, and must be: `build`
265
+ // with nobody watching produces a map with nothing in it, which is the
266
+ // truth and is what withdraws what was stated for them. Skipped, as it
267
+ // was, the last thing said about that file stood for as long as the
268
+ // torrent did.
269
+ if (one.durationSeconds > 0) {
270
+ this.build(one);
271
+ }
272
+ }
273
+ for (const [address, one] of byOutput) {
274
+ this.#byOutput.set(address, this.#mapFrom(one));
275
+ }
276
+ }
277
+
278
+ /**
279
+ * Nobody is watching this file any more.
280
+ *
281
+ * @param {string} sourceKey
282
+ * @param {number} fileIndex
283
+ */
284
+ /**
285
+ * The map this class last built for one file.
286
+ *
287
+ * Read by whoever acts on it and cannot be handed it at the moment it is
288
+ * made — the encoding decides per output, and one file has several. It is the
289
+ * SAME map: built once here, from where the viewers are, and neither read
290
+ * changes it.
291
+ *
292
+ * @param {string} sourceKey
293
+ * @param {number} fileIndex
294
+ * @returns {import("./PriorityMap.js").PriorityMap} A map of no length where
295
+ * none was built, which says the same as a map with nothing in it.
296
+ */
297
+ mapFor(sourceKey, fileIndex) {
298
+ return this.#maps.get(`${sourceKey}:${fileIndex}`) ?? emptyMap(0);
299
+ }
300
+
301
+ /**
302
+ * The map for ONE output, from the viewers consuming that output.
303
+ *
304
+ * What the encoding reads. A map of no length says nobody is on this output,
305
+ * which is what makes an encoder on it unwanted — and it is a statement, not
306
+ * an absence: the walk above writes one for every output a session exists
307
+ * for, including the ones everybody has left.
308
+ *
309
+ * @param {string} address
310
+ * @returns {import("./PriorityMap.js").PriorityMap}
311
+ */
312
+ mapForOutput(address) {
313
+ return this.#byOutput.get(address) ?? emptyMap(0);
314
+ }
315
+
316
+ }