@banou/ripple 0.0.4 → 0.0.8

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.
Files changed (41) hide show
  1. package/README.md +141 -3
  2. package/_headers +14 -0
  3. package/api-Jq4OSTp1-B4uxQLLk.js +338 -0
  4. package/app.webmanifest +50 -0
  5. package/assets/empty-BvRk9kiK.js +0 -0
  6. package/assets/libtorrent-DS_9Yo8M.js +6 -0
  7. package/assets/sintel.torrent +0 -0
  8. package/assets/worker-SJZDGuG-.js +23 -0
  9. package/build-DEOZ3A7n.js +1846 -0
  10. package/index.js +44658 -0
  11. package/jassub/default.woff2 +0 -0
  12. package/jassub/fonts.js +1 -0
  13. package/jassub/jassub-assets.js +0 -0
  14. package/jassub/jassub-worker-modern.wasm +0 -0
  15. package/jassub/jassub-worker.js +1 -0
  16. package/jassub/jassub-worker.wasm +0 -0
  17. package/jassub/worker.js +148 -0
  18. package/libav-jspi.wasm +0 -0
  19. package/libav-worker.js +4824 -0
  20. package/libav.wasm +0 -0
  21. package/package.json +14 -81
  22. package/remote-BLaEXLm6.js +209 -0
  23. package/sw.js +192 -0
  24. package/build/app.webmanifest +0 -65
  25. package/build/assets/index-be95001c.js +0 -211
  26. package/build/assets/index-d5b720ec.js +0 -109
  27. package/build/assets/io-worker-bde41815.js +0 -17681
  28. package/build/index-3576f032.js +0 -14881
  29. package/build/index-65b2197a.js +0 -116
  30. package/build/index-9a320571.js +0 -107
  31. package/build/index.js +0 -33826
  32. package/build/io-worker-190cc8d7.js +0 -2862
  33. package/build/jassub-worker-modern.wasm +0 -0
  34. package/build/jassub-worker.js +0 -15
  35. package/build/libav-worker.js +0 -1565
  36. package/build/libav.wasm +0 -0
  37. package/build/shared-worker.js +0 -13
  38. package/build/worker.js +0 -38
  39. /package/{build/assets → assets}/logo.png +0 -0
  40. /package/{build/assets → assets}/logo.svg +0 -0
  41. /package/{build/index.html → index.html} +0 -0
@@ -1,2862 +0,0 @@
1
- import { R as J, D as H } from "./index-9a320571.js";
2
- import { o as lt, h as ht, E as ge, i as Jt, j as it, k as me, O as ve, m as zt, n as xt, s as Ee, d as Dt, p as Se, b as we, q as _e, v as rt, w as et, x as It, t as Te } from "./index-3576f032.js";
3
- import { P as be, u as Re } from "./index-65b2197a.js";
4
- function xe(t) {
5
- return lt(function(e, n) {
6
- var s = !1;
7
- e.subscribe(ht(n, function(i) {
8
- s = !0, n.next(i);
9
- }, function() {
10
- s || n.next(t), n.complete();
11
- }));
12
- });
13
- }
14
- function De(t) {
15
- return t <= 0 ? function() {
16
- return ge;
17
- } : lt(function(e, n) {
18
- var s = 0;
19
- e.subscribe(ht(n, function(i) {
20
- ++s <= t && (n.next(i), t <= s && n.complete());
21
- }));
22
- });
23
- }
24
- function Ie(t) {
25
- return t === void 0 && (t = Ae), lt(function(e, n) {
26
- var s = !1;
27
- e.subscribe(ht(n, function(i) {
28
- s = !0, n.next(i);
29
- }, function() {
30
- return s ? n.complete() : n.error(t());
31
- }));
32
- });
33
- }
34
- function Ae() {
35
- return new Jt();
36
- }
37
- function At(t, e) {
38
- var n = arguments.length >= 2;
39
- return function(s) {
40
- return s.pipe(t ? it(function(i, r) {
41
- return t(i, r, s);
42
- }) : me, De(1), n ? xe(e) : Ie(function() {
43
- return new Jt();
44
- }));
45
- };
46
- }
47
- function ke() {
48
- if (typeof globalThis < "u")
49
- return globalThis;
50
- if (typeof self < "u")
51
- return self;
52
- if (typeof window < "u")
53
- return window;
54
- if (typeof global < "u")
55
- return global;
56
- }
57
- function Oe() {
58
- const t = ke();
59
- if (t.__xstate__)
60
- return t.__xstate__;
61
- }
62
- const $e = (t) => {
63
- if (typeof window > "u")
64
- return;
65
- const e = Oe();
66
- e && e.register(t);
67
- };
68
- class kt {
69
- constructor(e) {
70
- this._process = e, this._active = !1, this._current = null, this._last = null;
71
- }
72
- start() {
73
- this._active = !0, this.flush();
74
- }
75
- clear() {
76
- this._current && (this._current.next = null, this._last = this._current);
77
- }
78
- enqueue(e) {
79
- const n = {
80
- value: e,
81
- next: null
82
- };
83
- if (this._current) {
84
- this._last.next = n, this._last = n;
85
- return;
86
- }
87
- this._current = n, this._last = n, this._active && this.flush();
88
- }
89
- flush() {
90
- for (; this._current; ) {
91
- const e = this._current;
92
- this._process(e.value), this._current = e.next;
93
- }
94
- this._last = null;
95
- }
96
- }
97
- const q = ".", Me = "", Gt = "", Le = "#", Fe = "*", Kt = "xstate.init", Ce = "xstate.error", C = "xstate.stop";
98
- function Ne(t, e) {
99
- const n = e ? `#${e}` : "";
100
- return {
101
- type: `xstate.after(${t})${n}`
102
- };
103
- }
104
- function ot(t, e) {
105
- return {
106
- type: `xstate.done.state.${t}`,
107
- output: e
108
- };
109
- }
110
- function je(t, e) {
111
- return {
112
- type: `xstate.done.actor.${t}`,
113
- output: e
114
- };
115
- }
116
- function k(t, e) {
117
- return {
118
- type: `xstate.error.actor.${t}`,
119
- data: e
120
- };
121
- }
122
- function Vt(t) {
123
- return {
124
- type: Kt,
125
- input: t
126
- };
127
- }
128
- function x(t) {
129
- setTimeout(() => {
130
- throw t;
131
- });
132
- }
133
- const Pe = (() => typeof Symbol == "function" && Symbol.observable || "@@observable")();
134
- let Ue = 0;
135
- function He(t) {
136
- const e = /* @__PURE__ */ new Map(), n = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new WeakMap(), i = /* @__PURE__ */ new Set(), r = {
137
- _bookId: () => `x:${Ue++}`,
138
- _register: (o, a) => (e.set(o, a), o),
139
- _unregister: (o) => {
140
- e.delete(o.sessionId);
141
- const a = s.get(o);
142
- a !== void 0 && (n.delete(a), s.delete(o));
143
- },
144
- get: (o) => n.get(o),
145
- _set: (o, a) => {
146
- const c = n.get(o);
147
- if (c && c !== a)
148
- throw new Error(`Actor with system ID '${o}' already exists.`);
149
- n.set(o, a), s.set(a, o);
150
- },
151
- inspect: (o) => {
152
- i.add(o);
153
- },
154
- _sendInspectionEvent: (o) => {
155
- const a = {
156
- ...o,
157
- rootId: t.sessionId
158
- };
159
- i.forEach((c) => c.next?.(a));
160
- },
161
- _relay: (o, a, c) => {
162
- r._sendInspectionEvent({
163
- type: "@xstate.event",
164
- sourceRef: o,
165
- actorRef: a,
166
- event: c
167
- }), a._send(c);
168
- }
169
- };
170
- return r;
171
- }
172
- function qt(t, e) {
173
- const n = Ot(t), s = Ot(e);
174
- return typeof s == "string" ? typeof n == "string" ? s === n : !1 : typeof n == "string" ? n in s : Object.keys(n).every((i) => i in s ? qt(n[i], s[i]) : !1);
175
- }
176
- function Yt(t) {
177
- try {
178
- return Qt(t) ? t : t.split(q);
179
- } catch {
180
- throw new Error(`'${t}' is not a valid state path.`);
181
- }
182
- }
183
- function Ot(t) {
184
- if (wn(t))
185
- return t.value;
186
- if (typeof t != "string")
187
- return t;
188
- const e = Yt(t);
189
- return Be(e);
190
- }
191
- function Be(t) {
192
- if (t.length === 1)
193
- return t[0];
194
- const e = {};
195
- let n = e;
196
- for (let s = 0; s < t.length - 1; s++)
197
- if (s === t.length - 2)
198
- n[t[s]] = t[s + 1];
199
- else {
200
- const i = n;
201
- n = {}, i[t[s]] = n;
202
- }
203
- return e;
204
- }
205
- function $t(t, e) {
206
- const n = {}, s = Object.keys(t);
207
- for (let i = 0; i < s.length; i++) {
208
- const r = s[i];
209
- n[r] = e(t[r], r, t, i);
210
- }
211
- return n;
212
- }
213
- function Xt(t) {
214
- return [].concat(...t);
215
- }
216
- function Zt(t) {
217
- return Qt(t) ? t : [t];
218
- }
219
- function S(t) {
220
- return t === void 0 ? [] : Zt(t);
221
- }
222
- function at(t, e, n, s) {
223
- return typeof t == "function" ? t({
224
- context: e,
225
- event: n,
226
- self: s
227
- }) : t;
228
- }
229
- function Qt(t) {
230
- return Array.isArray(t);
231
- }
232
- function We(t) {
233
- return t.type.startsWith("xstate.error.actor");
234
- }
235
- function I(t) {
236
- return Zt(t).map((e) => typeof e > "u" || typeof e == "string" ? {
237
- target: e
238
- } : e);
239
- }
240
- function te(t) {
241
- if (!(t === void 0 || t === Me))
242
- return S(t);
243
- }
244
- function Mt(t, e, n) {
245
- const s = typeof t == "object", i = s ? t : void 0;
246
- return {
247
- next: (s ? t.next : t)?.bind(i),
248
- error: (s ? t.error : e)?.bind(i),
249
- complete: (s ? t.complete : n)?.bind(i)
250
- };
251
- }
252
- function Lt(t, e) {
253
- return `${t}[${e}]`;
254
- }
255
- function pt(t, e) {
256
- if (e.startsWith("xstate#")) {
257
- const [, n] = e.match(/\[(\d+)\]$/), i = t.getStateNodeById(e.slice(7, -(n.length + 2))).config.invoke;
258
- return (Array.isArray(i) ? i[n] : i).src;
259
- }
260
- return t.implementations.actors[e];
261
- }
262
- function Je(t) {
263
- return [...new Set(Xt([...t._nodes.map((e) => e.ownEvents)]))];
264
- }
265
- const yt = 1;
266
- let m = /* @__PURE__ */ function(t) {
267
- return t[t.NotStarted = 0] = "NotStarted", t[t.Running = 1] = "Running", t[t.Stopped = 2] = "Stopped", t;
268
- }({});
269
- const ze = {
270
- clock: {
271
- setTimeout: (t, e) => setTimeout(t, e),
272
- clearTimeout: (t) => clearTimeout(t)
273
- },
274
- logger: console.log.bind(console),
275
- devTools: !1
276
- };
277
- class Ge {
278
- /**
279
- * Creates a new actor instance for the given logic with the provided options, if any.
280
- *
281
- * @param logic The logic to create an actor from
282
- * @param options Actor options
283
- */
284
- constructor(e, n) {
285
- this.logic = e, this._state = void 0, this.clock = void 0, this.options = void 0, this.id = void 0, this.mailbox = new kt(this._process.bind(this)), this.delayedEventsMap = {}, this.observers = /* @__PURE__ */ new Set(), this.logger = void 0, this._processingStatus = m.NotStarted, this._parent = void 0, this._syncSnapshot = void 0, this.ref = void 0, this._actorScope = void 0, this._systemId = void 0, this.sessionId = void 0, this.system = void 0, this._doneEvent = void 0, this.src = void 0, this._deferred = [];
286
- const s = {
287
- ...ze,
288
- ...n
289
- }, {
290
- clock: i,
291
- logger: r,
292
- parent: o,
293
- syncSnapshot: a,
294
- id: c,
295
- systemId: u,
296
- inspect: l
297
- } = s;
298
- this.system = o?.system ?? He(this), l && !o && this.system.inspect(Mt(l)), this.sessionId = this.system._bookId(), this.id = c ?? this.sessionId, this.logger = r, this.clock = i, this._parent = o, this._syncSnapshot = a, this.options = s, this.src = s.src ?? e, this.ref = this, this._actorScope = {
299
- self: this,
300
- id: this.id,
301
- sessionId: this.sessionId,
302
- logger: this.logger,
303
- defer: (f) => {
304
- this._deferred.push(f);
305
- },
306
- system: this.system,
307
- stopChild: (f) => {
308
- if (f._parent !== this)
309
- throw new Error(`Cannot stop child actor ${f.id} of ${this.id} because it is not a child`);
310
- f._stop();
311
- }
312
- }, this.send = this.send.bind(this), this.system._sendInspectionEvent({
313
- type: "@xstate.actor",
314
- actorRef: this
315
- }), u && (this._systemId = u, this.system._set(u, this)), this._initState(n?.state), u && this._state.status !== "active" && this.system._unregister(this);
316
- }
317
- _initState(e) {
318
- this._state = e ? this.logic.restoreState ? this.logic.restoreState(e, this._actorScope) : e : this.logic.getInitialState(this._actorScope, this.options?.input);
319
- }
320
- update(e, n) {
321
- this._state = e;
322
- let s;
323
- for (; s = this._deferred.shift(); )
324
- s();
325
- for (const i of this.observers)
326
- try {
327
- i.next?.(e);
328
- } catch (r) {
329
- x(r);
330
- }
331
- switch (this._state.status) {
332
- case "done":
333
- this._stopProcedure(), this._complete(), this._doneEvent = je(this.id, this._state.output), this._parent && this.system._relay(this, this._parent, this._doneEvent);
334
- break;
335
- case "error":
336
- this._stopProcedure(), this._error(this._state.error), this._parent && this.system._relay(this, this._parent, k(this.id, this._state.error));
337
- break;
338
- }
339
- this.system._sendInspectionEvent({
340
- type: "@xstate.snapshot",
341
- actorRef: this,
342
- event: n,
343
- snapshot: e
344
- });
345
- }
346
- /**
347
- * Subscribe an observer to an actor’s snapshot values.
348
- *
349
- * @remarks
350
- * The observer will receive the actor’s snapshot value when it is emitted. The observer can be:
351
- * - A plain function that receives the latest snapshot, or
352
- * - An observer object whose `.next(snapshot)` method receives the latest snapshot
353
- *
354
- * @example
355
- * ```ts
356
- * // Observer as a plain function
357
- * const subscription = actor.subscribe((snapshot) => {
358
- * console.log(snapshot);
359
- * });
360
- * ```
361
- *
362
- * @example
363
- * ```ts
364
- * // Observer as an object
365
- * const subscription = actor.subscribe({
366
- * next(snapshot) {
367
- * console.log(snapshot);
368
- * },
369
- * error(err) {
370
- * // ...
371
- * },
372
- * complete() {
373
- * // ...
374
- * },
375
- * });
376
- * ```
377
- *
378
- * The return value of `actor.subscribe(observer)` is a subscription object that has an `.unsubscribe()` method. You can call `subscription.unsubscribe()` to unsubscribe the observer:
379
- *
380
- * @example
381
- * ```ts
382
- * const subscription = actor.subscribe((snapshot) => {
383
- * // ...
384
- * });
385
- *
386
- * // Unsubscribe the observer
387
- * subscription.unsubscribe();
388
- * ```
389
- *
390
- * When the actor is stopped, all of its observers will automatically be unsubscribed.
391
- *
392
- * @param observer - Either a plain function that receives the latest snapshot, or an observer object whose `.next(snapshot)` method receives the latest snapshot
393
- */
394
- subscribe(e, n, s) {
395
- const i = Mt(e, n, s);
396
- if (this._processingStatus !== m.Stopped)
397
- this.observers.add(i);
398
- else
399
- try {
400
- i.complete?.();
401
- } catch (r) {
402
- x(r);
403
- }
404
- return {
405
- unsubscribe: () => {
406
- this.observers.delete(i);
407
- }
408
- };
409
- }
410
- /**
411
- * Starts the Actor from the initial state
412
- */
413
- start() {
414
- if (this._processingStatus === m.Running)
415
- return this;
416
- this._syncSnapshot && this.subscribe({
417
- next: (s) => {
418
- s.status === "active" && this._parent.send({
419
- type: `xstate.snapshot.${this.id}`,
420
- snapshot: s
421
- });
422
- },
423
- error: () => {
424
- }
425
- }), this.system._register(this.sessionId, this), this._systemId && this.system._set(this._systemId, this), this._processingStatus = m.Running;
426
- const e = Vt(this.options.input);
427
- switch (this.system._sendInspectionEvent({
428
- type: "@xstate.event",
429
- sourceRef: this._parent,
430
- actorRef: this,
431
- event: e
432
- }), this._state.status) {
433
- case "done":
434
- this.update(this._state, e);
435
- case "error":
436
- return this;
437
- }
438
- if (this.logic.start)
439
- try {
440
- this.logic.start(this._state, this._actorScope);
441
- } catch (s) {
442
- return this._stopProcedure(), this._error(s), this._parent?.send(k(this.id, s)), this;
443
- }
444
- return this.update(this._state, e), this.options.devTools && this.attachDevTools(), this.mailbox.start(), this;
445
- }
446
- _process(e) {
447
- let n, s;
448
- try {
449
- n = this.logic.transition(this._state, e, this._actorScope);
450
- } catch (i) {
451
- s = {
452
- err: i
453
- };
454
- }
455
- if (s) {
456
- const {
457
- err: i
458
- } = s;
459
- this._stopProcedure(), this._error(i), this._parent?.send(k(this.id, i));
460
- return;
461
- }
462
- this.update(n, e), e.type === C && (this._stopProcedure(), this._complete());
463
- }
464
- _stop() {
465
- return this._processingStatus === m.Stopped ? this : (this.mailbox.clear(), this._processingStatus === m.NotStarted ? (this._processingStatus = m.Stopped, this) : (this.mailbox.enqueue({
466
- type: C
467
- }), this));
468
- }
469
- /**
470
- * Stops the Actor and unsubscribe all listeners.
471
- */
472
- stop() {
473
- if (this._parent)
474
- throw new Error("A non-root actor cannot be stopped directly.");
475
- return this._stop();
476
- }
477
- _complete() {
478
- for (const e of this.observers)
479
- try {
480
- e.complete?.();
481
- } catch (n) {
482
- x(n);
483
- }
484
- this.observers.clear();
485
- }
486
- _error(e) {
487
- if (!this.observers.size) {
488
- this._parent || x(e);
489
- return;
490
- }
491
- let n = !1;
492
- for (const s of this.observers) {
493
- const i = s.error;
494
- n ||= !i;
495
- try {
496
- i?.(e);
497
- } catch (r) {
498
- x(r);
499
- }
500
- }
501
- this.observers.clear(), n && x(e);
502
- }
503
- _stopProcedure() {
504
- if (this._processingStatus !== m.Running)
505
- return this;
506
- for (const e of Object.keys(this.delayedEventsMap))
507
- this.clock.clearTimeout(this.delayedEventsMap[e]);
508
- return this.mailbox.clear(), this.mailbox = new kt(this._process.bind(this)), this._processingStatus = m.Stopped, this.system._unregister(this), this;
509
- }
510
- /**
511
- * @internal
512
- */
513
- _send(e) {
514
- this._processingStatus !== m.Stopped && this.mailbox.enqueue(e);
515
- }
516
- /**
517
- * Sends an event to the running Actor to trigger a transition.
518
- *
519
- * @param event The event to send
520
- */
521
- send(e) {
522
- this.system._relay(void 0, this, e);
523
- }
524
- /**
525
- * TODO: figure out a way to do this within the machine
526
- * @internal
527
- */
528
- delaySend(e) {
529
- const {
530
- event: n,
531
- id: s,
532
- delay: i
533
- } = e, r = this.clock.setTimeout(() => {
534
- this.system._relay(this, e.to ?? this, n);
535
- }, i);
536
- s && (this.delayedEventsMap[s] = r);
537
- }
538
- /**
539
- * TODO: figure out a way to do this within the machine
540
- * @internal
541
- */
542
- cancel(e) {
543
- this.clock.clearTimeout(this.delayedEventsMap[e]), delete this.delayedEventsMap[e];
544
- }
545
- attachDevTools() {
546
- const {
547
- devTools: e
548
- } = this.options;
549
- e && (typeof e == "function" ? e : $e)(this);
550
- }
551
- toJSON() {
552
- return {
553
- xstate$$type: yt,
554
- id: this.id
555
- };
556
- }
557
- /**
558
- * Obtain the internal state of the actor, which can be persisted.
559
- *
560
- * @remarks
561
- * The internal state can be persisted from any actor, not only machines.
562
- *
563
- * Note that the persisted state is not the same as the snapshot from {@link Actor.getSnapshot}. Persisted state represents the internal state of the actor, while snapshots represent the actor's last emitted value.
564
- *
565
- * Can be restored with {@link ActorOptions.state}
566
- *
567
- * @see https://stately.ai/docs/persistence
568
- */
569
- getPersistedState(e) {
570
- return this.logic.getPersistedState(this._state, e);
571
- }
572
- [Pe]() {
573
- return this;
574
- }
575
- /**
576
- * Read an actor’s snapshot synchronously.
577
- *
578
- * @remarks
579
- * The snapshot represent an actor's last emitted value.
580
- *
581
- * When an actor receives an event, its internal state may change.
582
- * An actor may emit a snapshot when a state transition occurs.
583
- *
584
- * Note that some actors, such as callback actors generated with `fromCallback`, will not emit snapshots.
585
- *
586
- * @see {@link Actor.subscribe} to subscribe to an actor’s snapshot values.
587
- * @see {@link Actor.getPersistedState} to persist the internal state of an actor (which is more than just a snapshot).
588
- */
589
- getSnapshot() {
590
- return this._state;
591
- }
592
- }
593
- function N(t, e) {
594
- return new Ge(t, e);
595
- }
596
- function Ke(t, e, n, s, {
597
- sendId: i
598
- }) {
599
- const r = typeof i == "function" ? i(n, s) : i;
600
- return [e, r];
601
- }
602
- function Ve(t, e) {
603
- t.self.cancel(e);
604
- }
605
- function qe(t) {
606
- function e(n, s) {
607
- }
608
- return e.type = "xstate.cancel", e.sendId = t, e.resolve = Ke, e.execute = Ve, e;
609
- }
610
- function Ye(t, e, n, s, {
611
- id: i,
612
- systemId: r,
613
- src: o,
614
- input: a,
615
- syncSnapshot: c
616
- }) {
617
- const u = typeof o == "string" ? pt(e.machine, o) : o, l = typeof i == "function" ? i(n) : i;
618
- let f;
619
- return u && (f = N(u, {
620
- id: l,
621
- src: o,
622
- parent: t?.self,
623
- syncSnapshot: c,
624
- systemId: r,
625
- input: typeof a == "function" ? a({
626
- context: e.context,
627
- event: n.event,
628
- self: t?.self
629
- }) : a
630
- })), [b(e, {
631
- children: {
632
- ...e.children,
633
- [l]: f
634
- }
635
- }), {
636
- id: i,
637
- actorRef: f
638
- }];
639
- }
640
- function Xe(t, {
641
- id: e,
642
- actorRef: n
643
- }) {
644
- n && t.defer(() => {
645
- if (n._processingStatus !== m.Stopped)
646
- try {
647
- n.start?.();
648
- } catch (s) {
649
- t.self.send(k(e, s));
650
- return;
651
- }
652
- });
653
- }
654
- function Ze(...[t, {
655
- id: e,
656
- systemId: n,
657
- input: s,
658
- syncSnapshot: i = !1
659
- } = {}]) {
660
- function r(o, a) {
661
- }
662
- return r.type = "xstate.spawnChild", r.id = e, r.systemId = n, r.src = t, r.input = s, r.syncSnapshot = i, r.resolve = Ye, r.execute = Xe, r;
663
- }
664
- function Qe(t, e, n, s, {
665
- actorRef: i
666
- }) {
667
- const r = typeof i == "function" ? i(n, s) : i, o = typeof r == "string" ? e.children[r] : r;
668
- let a = e.children;
669
- return o && (a = {
670
- ...a
671
- }, delete a[o.id]), [b(e, {
672
- children: a
673
- }), o];
674
- }
675
- function tn(t, e) {
676
- if (e) {
677
- if (t.system._unregister(e), e._processingStatus !== m.Running) {
678
- t.stopChild(e);
679
- return;
680
- }
681
- t.defer(() => {
682
- t.stopChild(e);
683
- });
684
- }
685
- }
686
- function ee(t) {
687
- function e(n, s) {
688
- }
689
- return e.type = "xstate.stopChild", e.actorRef = t, e.resolve = Qe, e.execute = tn, e;
690
- }
691
- function en(t, {
692
- context: e,
693
- event: n
694
- }, {
695
- guards: s
696
- }) {
697
- return s.some((i) => Y(i, e, n, t));
698
- }
699
- function Ft(t) {
700
- function e(n, s) {
701
- return !1;
702
- }
703
- return e.check = en, e.guards = t, e;
704
- }
705
- function Y(t, e, n, s) {
706
- const {
707
- machine: i
708
- } = s, r = typeof t == "function", o = r ? t : i.implementations.guards[typeof t == "string" ? t : t.type];
709
- if (!r && !o)
710
- throw new Error(`Guard '${typeof t == "string" ? t : t.type}' is not implemented.'.`);
711
- if (typeof o != "function")
712
- return Y(o, e, n, s);
713
- const a = {
714
- context: e,
715
- event: n
716
- }, c = r || typeof t == "string" ? void 0 : "params" in t ? typeof t.params == "function" ? t.params({
717
- context: e,
718
- event: n
719
- }) : t.params : void 0;
720
- return "check" in o ? o.check(
721
- s,
722
- a,
723
- o
724
- // this holds all params
725
- ) : o(a, c);
726
- }
727
- const gt = (t) => t.type === "atomic" || t.type === "final";
728
- function O(t) {
729
- return Object.values(t.states).filter((e) => e.type !== "history");
730
- }
731
- function P(t, e) {
732
- const n = [];
733
- if (e === t)
734
- return n;
735
- let s = t.parent;
736
- for (; s && s !== e; )
737
- n.push(s), s = s.parent;
738
- return n;
739
- }
740
- function z(t) {
741
- const e = new Set(t), n = se(e);
742
- for (const s of e)
743
- if (s.type === "compound" && (!n.get(s) || !n.get(s).length))
744
- Ct(s).forEach((i) => e.add(i));
745
- else if (s.type === "parallel") {
746
- for (const i of O(s))
747
- if (i.type !== "history" && !e.has(i)) {
748
- const r = Ct(i);
749
- for (const o of r)
750
- e.add(o);
751
- }
752
- }
753
- for (const s of e) {
754
- let i = s.parent;
755
- for (; i; )
756
- e.add(i), i = i.parent;
757
- }
758
- return e;
759
- }
760
- function ne(t, e) {
761
- const n = e.get(t);
762
- if (!n)
763
- return {};
764
- if (t.type === "compound") {
765
- const i = n[0];
766
- if (i) {
767
- if (gt(i))
768
- return i.key;
769
- } else
770
- return {};
771
- }
772
- const s = {};
773
- for (const i of n)
774
- s[i.key] = ne(i, e);
775
- return s;
776
- }
777
- function se(t) {
778
- const e = /* @__PURE__ */ new Map();
779
- for (const n of t)
780
- e.has(n) || e.set(n, []), n.parent && (e.has(n.parent) || e.set(n.parent, []), e.get(n.parent).push(n));
781
- return e;
782
- }
783
- function ie(t, e) {
784
- const n = z(e);
785
- return ne(t, se(n));
786
- }
787
- function mt(t, e) {
788
- return e.type === "compound" ? O(e).some((n) => n.type === "final" && t.has(n)) : e.type === "parallel" ? O(e).every((n) => mt(t, n)) : e.type === "final";
789
- }
790
- const X = (t) => t[0] === Le;
791
- function nn(t, e) {
792
- return t.transitions.get(e) || [...t.transitions.keys()].filter((s) => {
793
- if (s === Fe)
794
- return !0;
795
- if (!s.endsWith(".*"))
796
- return !1;
797
- const i = s.split("."), r = e.split(".");
798
- for (let o = 0; o < i.length; o++) {
799
- const a = i[o], c = r[o];
800
- if (a === "*")
801
- return o === i.length - 1;
802
- if (a !== c)
803
- return !1;
804
- }
805
- return !0;
806
- }).sort((s, i) => i.length - s.length).flatMap((s) => t.transitions.get(s));
807
- }
808
- function sn(t) {
809
- const e = t.config.after;
810
- if (!e)
811
- return [];
812
- const n = (i, r) => {
813
- const o = typeof i == "function" ? `${t.id}:delay[${r}]` : i, a = Ne(o, t.id), c = a.type;
814
- return t.entry.push(kn(a, {
815
- id: c,
816
- delay: i
817
- })), t.exit.push(qe(c)), c;
818
- };
819
- return Object.keys(e).flatMap((i, r) => {
820
- const o = e[i], a = typeof o == "string" ? {
821
- target: o
822
- } : o, c = isNaN(+i) ? i : +i, u = n(c, r);
823
- return S(a).map((l) => ({
824
- ...l,
825
- event: u,
826
- delay: c
827
- }));
828
- }).map((i) => {
829
- const {
830
- delay: r
831
- } = i;
832
- return {
833
- ..._(t, i.event, i),
834
- delay: r
835
- };
836
- });
837
- }
838
- function _(t, e, n) {
839
- const s = te(n.target), i = n.reenter ?? !1, r = an(t, s), o = {
840
- ...n,
841
- actions: S(n.actions),
842
- guard: n.guard,
843
- target: r,
844
- source: t,
845
- reenter: i,
846
- eventType: e,
847
- toJSON: () => ({
848
- ...o,
849
- source: `#${t.id}`,
850
- target: r ? r.map((a) => `#${a.id}`) : void 0
851
- })
852
- };
853
- return o;
854
- }
855
- function rn(t) {
856
- const e = /* @__PURE__ */ new Map();
857
- if (t.config.on)
858
- for (const n of Object.keys(t.config.on)) {
859
- if (n === Gt)
860
- throw new Error('Null events ("") cannot be specified as a transition key. Use `always: { ... }` instead.');
861
- const s = t.config.on[n];
862
- e.set(n, I(s).map((i) => _(t, n, i)));
863
- }
864
- if (t.config.onDone) {
865
- const n = `xstate.done.state.${t.id}`;
866
- e.set(n, I(t.config.onDone).map((s) => _(t, n, s)));
867
- }
868
- for (const n of t.invoke) {
869
- if (n.onDone) {
870
- const s = `xstate.done.actor.${n.id}`;
871
- e.set(s, I(n.onDone).map((i) => _(t, s, i)));
872
- }
873
- if (n.onError) {
874
- const s = `xstate.error.actor.${n.id}`;
875
- e.set(s, I(n.onError).map((i) => _(t, s, i)));
876
- }
877
- if (n.onSnapshot) {
878
- const s = `xstate.snapshot.${n.id}`;
879
- e.set(s, I(n.onSnapshot).map((i) => _(t, s, i)));
880
- }
881
- }
882
- for (const n of t.after) {
883
- let s = e.get(n.eventType);
884
- s || (s = [], e.set(n.eventType, s)), s.push(n);
885
- }
886
- return e;
887
- }
888
- function on(t, e) {
889
- const n = typeof e == "string" ? t.states[e] : e ? t.states[e.target] : void 0;
890
- if (!n && e)
891
- throw new Error(`Initial state node "${e}" not found on parent state node #${t.id}`);
892
- const s = {
893
- source: t,
894
- actions: !e || typeof e == "string" ? [] : S(e.actions),
895
- eventType: null,
896
- reenter: !1,
897
- target: n ? [n] : [],
898
- toJSON: () => ({
899
- ...s,
900
- source: `#${t.id}`,
901
- target: n ? [`#${n.id}`] : []
902
- })
903
- };
904
- return s;
905
- }
906
- function an(t, e) {
907
- if (e !== void 0)
908
- return e.map((n) => {
909
- if (typeof n != "string")
910
- return n;
911
- if (X(n))
912
- return t.machine.getStateNodeById(n);
913
- const s = n[0] === q;
914
- if (s && !t.parent)
915
- return G(t, n.slice(1));
916
- const i = s ? t.key + n : n;
917
- if (t.parent)
918
- try {
919
- return G(t.parent, i);
920
- } catch (r) {
921
- throw new Error(`Invalid transition definition for state node '${t.id}':
922
- ${r.message}`);
923
- }
924
- else
925
- throw new Error(`Invalid target: "${n}" is not a valid target from the root node. Did you mean ".${n}"?`);
926
- });
927
- }
928
- function re(t) {
929
- const e = te(t.config.target);
930
- return e ? {
931
- target: e.map((n) => typeof n == "string" ? G(t.parent, n) : n)
932
- } : t.parent.initial;
933
- }
934
- function T(t) {
935
- return t.type === "history";
936
- }
937
- function Ct(t) {
938
- const e = oe(t);
939
- for (const n of e)
940
- for (const s of P(n, t))
941
- e.add(s);
942
- return e;
943
- }
944
- function oe(t) {
945
- const e = /* @__PURE__ */ new Set();
946
- function n(s) {
947
- if (!e.has(s)) {
948
- if (e.add(s), s.type === "compound")
949
- n(s.initial.target[0]);
950
- else if (s.type === "parallel")
951
- for (const i of O(s))
952
- n(i);
953
- }
954
- }
955
- return n(t), e;
956
- }
957
- function $(t, e) {
958
- if (X(e))
959
- return t.machine.getStateNodeById(e);
960
- if (!t.states)
961
- throw new Error(`Unable to retrieve child state '${e}' from '${t.id}'; no child states exist.`);
962
- const n = t.states[e];
963
- if (!n)
964
- throw new Error(`Child state '${e}' does not exist on '${t.id}'`);
965
- return n;
966
- }
967
- function G(t, e) {
968
- if (typeof e == "string" && X(e))
969
- try {
970
- return t.machine.getStateNodeById(e);
971
- } catch {
972
- }
973
- const n = Yt(e).slice();
974
- let s = t;
975
- for (; n.length; ) {
976
- const i = n.shift();
977
- if (!i.length)
978
- break;
979
- s = $(s, i);
980
- }
981
- return s;
982
- }
983
- function K(t, e) {
984
- if (typeof e == "string")
985
- return [t, t.states[e]];
986
- const n = Object.keys(e), s = n.map((i) => $(t, i)).filter(Boolean);
987
- return [t.machine.root, t].concat(s, n.reduce((i, r) => {
988
- const o = $(t, r);
989
- if (!o)
990
- return i;
991
- const a = K(o, e[r]);
992
- return i.concat(a);
993
- }, []));
994
- }
995
- function cn(t, e, n, s) {
996
- const r = $(t, e).next(n, s);
997
- return !r || !r.length ? t.next(n, s) : r;
998
- }
999
- function dn(t, e, n, s) {
1000
- const i = Object.keys(e), r = $(t, i[0]), o = vt(r, e[i[0]], n, s);
1001
- return !o || !o.length ? t.next(n, s) : o;
1002
- }
1003
- function un(t, e, n, s) {
1004
- const i = [];
1005
- for (const r of Object.keys(e)) {
1006
- const o = e[r];
1007
- if (!o)
1008
- continue;
1009
- const a = $(t, r), c = vt(a, o, n, s);
1010
- c && i.push(...c);
1011
- }
1012
- return i.length ? i : t.next(n, s);
1013
- }
1014
- function vt(t, e, n, s) {
1015
- return typeof e == "string" ? cn(t, e, n, s) : Object.keys(e).length === 1 ? dn(t, e, n, s) : un(t, e, n, s);
1016
- }
1017
- function fn(t) {
1018
- return Object.keys(t.states).map((e) => t.states[e]).filter((e) => e.type === "history");
1019
- }
1020
- function w(t, e) {
1021
- let n = t;
1022
- for (; n.parent && n.parent !== e; )
1023
- n = n.parent;
1024
- return n.parent === e;
1025
- }
1026
- function ln(t, e) {
1027
- const n = new Set(t), s = new Set(e);
1028
- for (const i of n)
1029
- if (s.has(i))
1030
- return !0;
1031
- for (const i of s)
1032
- if (n.has(i))
1033
- return !0;
1034
- return !1;
1035
- }
1036
- function ae(t, e, n) {
1037
- const s = /* @__PURE__ */ new Set();
1038
- for (const i of t) {
1039
- let r = !1;
1040
- const o = /* @__PURE__ */ new Set();
1041
- for (const a of s)
1042
- if (ln(ct([i], e, n), ct([a], e, n)))
1043
- if (w(i.source, a.source))
1044
- o.add(a);
1045
- else {
1046
- r = !0;
1047
- break;
1048
- }
1049
- if (!r) {
1050
- for (const a of o)
1051
- s.delete(a);
1052
- s.add(i);
1053
- }
1054
- }
1055
- return Array.from(s);
1056
- }
1057
- function hn(t) {
1058
- const [e, ...n] = t;
1059
- for (const s of P(e, void 0))
1060
- if (n.every((i) => w(i, s)))
1061
- return s;
1062
- }
1063
- function Et(t, e) {
1064
- if (!t.target)
1065
- return [];
1066
- const n = /* @__PURE__ */ new Set();
1067
- for (const s of t.target)
1068
- if (T(s))
1069
- if (e[s.id])
1070
- for (const i of e[s.id])
1071
- n.add(i);
1072
- else
1073
- for (const i of Et(re(s), e))
1074
- n.add(i);
1075
- else
1076
- n.add(s);
1077
- return [...n];
1078
- }
1079
- function ce(t, e) {
1080
- const n = Et(t, e);
1081
- if (!n)
1082
- return;
1083
- if (!t.reenter && n.every((i) => i === t.source || w(i, t.source)))
1084
- return t.source;
1085
- const s = hn(n.concat(t.source));
1086
- if (s)
1087
- return s;
1088
- if (!t.reenter)
1089
- return t.source.machine.root;
1090
- }
1091
- function ct(t, e, n) {
1092
- const s = /* @__PURE__ */ new Set();
1093
- for (const i of t)
1094
- if (i.target?.length) {
1095
- const r = ce(i, n);
1096
- i.reenter && i.source === r && s.add(r);
1097
- for (const o of e)
1098
- w(o, r) && s.add(o);
1099
- }
1100
- return [...s];
1101
- }
1102
- function pn(t, e) {
1103
- if (t.length !== e.size)
1104
- return !1;
1105
- for (const n of t)
1106
- if (!e.has(n))
1107
- return !1;
1108
- return !0;
1109
- }
1110
- function dt(t, e, n, s, i, r) {
1111
- if (!t.length)
1112
- return e;
1113
- const o = new Set(e._nodes);
1114
- let a = e.historyValue;
1115
- const c = ae(t, o, a);
1116
- let u = e;
1117
- i || ([u, a] = vn(u, s, n, c, o, a, r)), u = M(u, s, n, c.flatMap((f) => f.actions), r), u = gn(u, s, n, c, o, r, a, i);
1118
- const l = [...o];
1119
- u.status === "done" && (u = M(u, s, n, l.sort((f, d) => d.order - f.order).flatMap((f) => f.exit), r));
1120
- try {
1121
- return a === e.historyValue && pn(e._nodes, o) ? u : b(u, {
1122
- _nodes: l,
1123
- historyValue: a
1124
- });
1125
- } catch (f) {
1126
- throw f;
1127
- }
1128
- }
1129
- function yn(t, e, n, s, i) {
1130
- if (!s.output)
1131
- return;
1132
- const r = ot(i.id, i.output && i.parent ? at(i.output, t.context, e, n.self) : void 0);
1133
- return at(s.output, t.context, r, n.self);
1134
- }
1135
- function gn(t, e, n, s, i, r, o, a) {
1136
- let c = t;
1137
- const u = /* @__PURE__ */ new Set(), l = /* @__PURE__ */ new Set();
1138
- mn(s, o, l, u), a && l.add(t.machine.root);
1139
- const f = /* @__PURE__ */ new Set();
1140
- for (const d of [...u].sort((h, p) => h.order - p.order)) {
1141
- i.add(d);
1142
- const h = [];
1143
- h.push(...d.entry);
1144
- for (const p of d.invoke)
1145
- h.push(Ze(p.src, {
1146
- ...p,
1147
- syncSnapshot: !!p.onSnapshot
1148
- }));
1149
- if (l.has(d)) {
1150
- const p = d.initial.actions;
1151
- h.push(...p);
1152
- }
1153
- if (c = M(c, e, n, h, r, d.invoke.map((p) => p.id)), d.type === "final") {
1154
- const p = d.parent;
1155
- let g = p?.type === "parallel" ? p : p?.parent, v = g || d;
1156
- for (p?.type === "compound" && r.push(ot(p.id, d.output ? at(d.output, c.context, e, n.self) : void 0)); g?.type === "parallel" && !f.has(g) && mt(i, g); )
1157
- f.add(g), r.push(ot(g.id)), v = g, g = g.parent;
1158
- if (g)
1159
- continue;
1160
- c = b(c, {
1161
- status: "done",
1162
- output: yn(c, e, n, c.machine.root, v)
1163
- });
1164
- }
1165
- }
1166
- return c;
1167
- }
1168
- function mn(t, e, n, s) {
1169
- for (const i of t) {
1170
- const r = ce(i, e);
1171
- for (const a of i.target || [])
1172
- !T(a) && // if the target is different than the source then it will *definitely* be entered
1173
- (i.source !== a || // we know that the domain can't lie within the source
1174
- // if it's different than the source then it's outside of it and it means that the target has to be entered as well
1175
- i.source !== r || // reentering transitions always enter the target, even if it's the source itself
1176
- i.reenter) && (s.add(a), n.add(a)), A(a, e, n, s);
1177
- const o = Et(i, e);
1178
- for (const a of o) {
1179
- const c = P(a, r);
1180
- r?.type === "parallel" && c.push(r), de(s, e, n, c, !i.source.parent && i.reenter ? void 0 : r);
1181
- }
1182
- }
1183
- }
1184
- function A(t, e, n, s) {
1185
- if (T(t))
1186
- if (e[t.id]) {
1187
- const i = e[t.id];
1188
- for (const r of i)
1189
- s.add(r), A(r, e, n, s);
1190
- for (const r of i)
1191
- nt(r, t.parent, s, e, n);
1192
- } else {
1193
- const i = re(t);
1194
- for (const r of i.target)
1195
- s.add(r), i === t.parent?.initial && n.add(t.parent), A(r, e, n, s);
1196
- for (const r of i.target)
1197
- nt(r, t, s, e, n);
1198
- }
1199
- else if (t.type === "compound") {
1200
- const [i] = t.initial.target;
1201
- T(i) || (s.add(i), n.add(i)), A(i, e, n, s), nt(i, t, s, e, n);
1202
- } else if (t.type === "parallel")
1203
- for (const i of O(t).filter((r) => !T(r)))
1204
- [...s].some((r) => w(r, i)) || (T(i) || (s.add(i), n.add(i)), A(i, e, n, s));
1205
- }
1206
- function de(t, e, n, s, i) {
1207
- for (const r of s)
1208
- if ((!i || w(r, i)) && t.add(r), r.type === "parallel")
1209
- for (const o of O(r).filter((a) => !T(a)))
1210
- [...t].some((a) => w(a, o)) || (t.add(o), A(o, e, n, t));
1211
- }
1212
- function nt(t, e, n, s, i) {
1213
- de(n, s, i, P(t, e));
1214
- }
1215
- function vn(t, e, n, s, i, r, o) {
1216
- let a = t;
1217
- const c = ct(s, i, r);
1218
- c.sort((l, f) => f.order - l.order);
1219
- let u;
1220
- for (const l of c)
1221
- for (const f of fn(l)) {
1222
- let d;
1223
- f.history === "deep" ? d = (h) => gt(h) && w(h, l) : d = (h) => h.parent === l, u ??= {
1224
- ...r
1225
- }, u[f.id] = Array.from(i).filter(d);
1226
- }
1227
- for (const l of c)
1228
- a = M(a, e, n, [...l.exit, ...l.invoke.map((f) => ee(f.id))], o), i.delete(l);
1229
- return [a, u || r];
1230
- }
1231
- function ue(t, e, n, s, i, r) {
1232
- const {
1233
- machine: o
1234
- } = t;
1235
- let a = t;
1236
- for (const c of s) {
1237
- const u = typeof c == "function", l = u ? c : (
1238
- // the existing type of `.actions` assumes non-nullable `TExpressionAction`
1239
- // it's fine to cast this here to get a common type and lack of errors in the rest of the code
1240
- // our logic below makes sure that we call those 2 "variants" correctly
1241
- o.implementations.actions[typeof c == "string" ? c : c.type]
1242
- );
1243
- if (!l)
1244
- continue;
1245
- const f = {
1246
- context: a.context,
1247
- event: e,
1248
- self: n?.self,
1249
- system: n?.system
1250
- }, d = u || typeof c == "string" ? void 0 : "params" in c ? typeof c.params == "function" ? c.params({
1251
- context: a.context,
1252
- event: e
1253
- }) : c.params : void 0;
1254
- if (!("resolve" in l)) {
1255
- n?.self._processingStatus === m.Running ? l(f, d) : n?.defer(() => {
1256
- l(f, d);
1257
- });
1258
- continue;
1259
- }
1260
- const h = l, [p, g, v] = h.resolve(
1261
- n,
1262
- a,
1263
- f,
1264
- d,
1265
- l,
1266
- // this holds all params
1267
- i
1268
- );
1269
- a = p, "retryResolve" in h && r?.push([h, g]), "execute" in h && (n?.self._processingStatus === m.Running ? h.execute(n, g) : n?.defer(h.execute.bind(null, n, g))), v && (a = ue(a, e, n, v, i, r));
1270
- }
1271
- return a;
1272
- }
1273
- function M(t, e, n, s, i, r) {
1274
- const o = r ? [] : void 0, a = ue(t, e, n, s, {
1275
- internalQueue: i,
1276
- deferredActorIds: r
1277
- }, o);
1278
- return o?.forEach(([c, u]) => {
1279
- c.retryResolve(n, a, u);
1280
- }), a;
1281
- }
1282
- function st(t, e, n, s = []) {
1283
- let i = t;
1284
- const r = [];
1285
- if (e.type === C)
1286
- return i = b(Nt(i, e, n), {
1287
- status: "stopped"
1288
- }), r.push(i), {
1289
- state: i,
1290
- microstates: r
1291
- };
1292
- let o = e;
1293
- if (o.type !== Kt) {
1294
- const c = jt(o, i);
1295
- i = dt(c, t, n, o, !1, s), r.push(i);
1296
- }
1297
- let a = !0;
1298
- for (; i.status === "active"; ) {
1299
- let c = a ? En(i, o) : [];
1300
- const u = c.length ? i : void 0;
1301
- if (!c.length) {
1302
- if (!s.length)
1303
- break;
1304
- o = s.shift(), c = jt(o, i);
1305
- }
1306
- i = dt(c, i, n, o, !1, s), a = i !== u, r.push(i);
1307
- }
1308
- return i.status !== "active" && Nt(i, o, n), {
1309
- state: i,
1310
- microstates: r
1311
- };
1312
- }
1313
- function Nt(t, e, n) {
1314
- return M(t, e, n, Object.values(t.children).map((s) => ee(s)), []);
1315
- }
1316
- function jt(t, e) {
1317
- return e.machine.getTransitionData(e, t);
1318
- }
1319
- function En(t, e) {
1320
- const n = /* @__PURE__ */ new Set(), s = t._nodes.filter(gt);
1321
- for (const i of s)
1322
- t:
1323
- for (const r of [i].concat(P(i, void 0)))
1324
- if (r.always) {
1325
- for (const o of r.always)
1326
- if (o.guard === void 0 || Y(o.guard, t.context, e, t)) {
1327
- n.add(o);
1328
- break t;
1329
- }
1330
- }
1331
- return ae(Array.from(n), new Set(t._nodes), t.historyValue);
1332
- }
1333
- function Sn(t, e) {
1334
- const n = z(K(t, e));
1335
- return ie(t, [...n]);
1336
- }
1337
- function wn(t) {
1338
- return !!t && typeof t == "object" && "machine" in t && "value" in t;
1339
- }
1340
- const _n = function(e) {
1341
- return qt(e, this.value);
1342
- }, Tn = function(e) {
1343
- return this.tags.has(e);
1344
- }, bn = function(e) {
1345
- const n = this.machine.getTransitionData(this, e);
1346
- return !!n?.length && // Check that at least one transition is not forbidden
1347
- n.some((s) => s.target !== void 0 || s.actions.length);
1348
- }, Rn = function() {
1349
- const {
1350
- _nodes: e,
1351
- tags: n,
1352
- machine: s,
1353
- getMeta: i,
1354
- toJSON: r,
1355
- can: o,
1356
- hasTag: a,
1357
- matches: c,
1358
- ...u
1359
- } = this;
1360
- return {
1361
- ...u,
1362
- tags: Array.from(n)
1363
- };
1364
- }, xn = function() {
1365
- return this._nodes.reduce((e, n) => (n.meta !== void 0 && (e[n.id] = n.meta), e), {});
1366
- };
1367
- function B(t, e) {
1368
- return {
1369
- status: t.status,
1370
- output: t.output,
1371
- error: t.error,
1372
- machine: e,
1373
- context: t.context,
1374
- _nodes: t._nodes,
1375
- value: ie(e.root, t._nodes),
1376
- tags: new Set(Xt(t._nodes.map((n) => n.tags))),
1377
- children: t.children,
1378
- historyValue: t.historyValue || {},
1379
- // this one is generic in the target and it's hard to create a matching non-generic source signature
1380
- matches: _n,
1381
- hasTag: Tn,
1382
- can: bn,
1383
- getMeta: xn,
1384
- toJSON: Rn
1385
- };
1386
- }
1387
- function b(t, e = {}) {
1388
- return B({
1389
- ...t,
1390
- ...e
1391
- }, t.machine);
1392
- }
1393
- function Dn(t, e) {
1394
- const {
1395
- _nodes: n,
1396
- tags: s,
1397
- machine: i,
1398
- children: r,
1399
- context: o,
1400
- can: a,
1401
- hasTag: c,
1402
- matches: u,
1403
- getMeta: l,
1404
- toJSON: f,
1405
- ...d
1406
- } = t, h = {};
1407
- for (const g in r) {
1408
- const v = r[g];
1409
- h[g] = {
1410
- state: v.getPersistedState(e),
1411
- src: v.src,
1412
- systemId: v._systemId,
1413
- syncSnapshot: v._syncSnapshot
1414
- };
1415
- }
1416
- return {
1417
- ...d,
1418
- context: fe(o),
1419
- children: h
1420
- };
1421
- }
1422
- function fe(t) {
1423
- let e;
1424
- for (const n in t) {
1425
- const s = t[n];
1426
- if (s && typeof s == "object")
1427
- if ("sessionId" in s && "send" in s && "ref" in s)
1428
- e ??= Array.isArray(t) ? t.slice() : {
1429
- ...t
1430
- }, e[n] = {
1431
- xstate$$type: yt,
1432
- id: s.id
1433
- };
1434
- else {
1435
- const i = fe(s);
1436
- i !== s && (e ??= Array.isArray(t) ? t.slice() : {
1437
- ...t
1438
- }, e[n] = i);
1439
- }
1440
- }
1441
- return e ?? t;
1442
- }
1443
- function In(t, e, n, s, {
1444
- event: i,
1445
- id: r,
1446
- delay: o
1447
- }, {
1448
- internalQueue: a
1449
- }) {
1450
- const c = e.machine.implementations.delays;
1451
- if (typeof i == "string")
1452
- throw new Error(`Only event objects may be used with raise; use raise({ type: "${i}" }) instead`);
1453
- const u = typeof i == "function" ? i(n, s) : i;
1454
- let l;
1455
- if (typeof o == "string") {
1456
- const f = c && c[o];
1457
- l = typeof f == "function" ? f(n, s) : f;
1458
- } else
1459
- l = typeof o == "function" ? o(n, s) : o;
1460
- return typeof l != "number" && a.push(u), [e, {
1461
- event: u,
1462
- id: r,
1463
- delay: l
1464
- }];
1465
- }
1466
- function An(t, e) {
1467
- if (typeof e.delay == "number") {
1468
- t.self.delaySend(e);
1469
- return;
1470
- }
1471
- }
1472
- function kn(t, e) {
1473
- function n(s, i) {
1474
- }
1475
- return n.type = "xstate.raise", n.event = t, n.id = e?.id, n.delay = e?.delay, n.resolve = In, n.execute = An, n;
1476
- }
1477
- function j(t) {
1478
- const e = "$$xstate.next", n = "$$xstate.error", s = "$$xstate.complete";
1479
- return {
1480
- config: t,
1481
- transition: (r, o, {
1482
- self: a,
1483
- id: c,
1484
- defer: u,
1485
- system: l
1486
- }) => {
1487
- if (r.status !== "active")
1488
- return r;
1489
- switch (o.type) {
1490
- case e:
1491
- return {
1492
- ...r,
1493
- context: o.data
1494
- };
1495
- case n:
1496
- return {
1497
- ...r,
1498
- status: "error",
1499
- error: o.data,
1500
- input: void 0,
1501
- _subscription: void 0
1502
- };
1503
- case s:
1504
- return {
1505
- ...r,
1506
- status: "done",
1507
- input: void 0,
1508
- _subscription: void 0
1509
- };
1510
- case C:
1511
- return r._subscription.unsubscribe(), {
1512
- ...r,
1513
- status: "stopped",
1514
- input: void 0,
1515
- _subscription: void 0
1516
- };
1517
- default:
1518
- return r;
1519
- }
1520
- },
1521
- getInitialState: (r, o) => ({
1522
- status: "active",
1523
- output: void 0,
1524
- error: void 0,
1525
- context: void 0,
1526
- input: o,
1527
- _subscription: void 0
1528
- }),
1529
- start: (r, {
1530
- self: o,
1531
- system: a
1532
- }) => {
1533
- r.status !== "done" && (r._subscription = t({
1534
- input: r.input,
1535
- system: a,
1536
- self: o
1537
- }).subscribe({
1538
- next: (c) => {
1539
- a._relay(o, o, {
1540
- type: e,
1541
- data: c
1542
- });
1543
- },
1544
- error: (c) => {
1545
- a._relay(o, o, {
1546
- type: n,
1547
- data: c
1548
- });
1549
- },
1550
- complete: () => {
1551
- a._relay(o, o, {
1552
- type: s
1553
- });
1554
- }
1555
- }));
1556
- },
1557
- getPersistedState: ({
1558
- _subscription: r,
1559
- ...o
1560
- }) => o,
1561
- restoreState: (r) => ({
1562
- ...r,
1563
- _subscription: void 0
1564
- })
1565
- };
1566
- }
1567
- const Pt = "$$xstate.resolve", Ut = "$$xstate.reject";
1568
- function W(t) {
1569
- return {
1570
- config: t,
1571
- transition: (n, s) => {
1572
- if (n.status !== "active")
1573
- return n;
1574
- switch (s.type) {
1575
- case Pt: {
1576
- const i = s.data;
1577
- return {
1578
- ...n,
1579
- status: "done",
1580
- output: i,
1581
- input: void 0
1582
- };
1583
- }
1584
- case Ut:
1585
- return {
1586
- ...n,
1587
- status: "error",
1588
- error: s.data,
1589
- input: void 0
1590
- };
1591
- case C:
1592
- return {
1593
- ...n,
1594
- status: "stopped",
1595
- input: void 0
1596
- };
1597
- default:
1598
- return n;
1599
- }
1600
- },
1601
- start: (n, {
1602
- self: s,
1603
- system: i
1604
- }) => {
1605
- if (n.status !== "active")
1606
- return;
1607
- Promise.resolve(t({
1608
- input: n.input,
1609
- system: i,
1610
- self: s
1611
- })).then((o) => {
1612
- s.getSnapshot().status === "active" && i._relay(s, s, {
1613
- type: Pt,
1614
- data: o
1615
- });
1616
- }, (o) => {
1617
- s.getSnapshot().status === "active" && i._relay(s, s, {
1618
- type: Ut,
1619
- data: o
1620
- });
1621
- });
1622
- },
1623
- getInitialState: (n, s) => ({
1624
- status: "active",
1625
- output: void 0,
1626
- error: void 0,
1627
- input: s
1628
- }),
1629
- getPersistedState: (n) => n,
1630
- restoreState: (n) => n
1631
- };
1632
- }
1633
- function On(t, {
1634
- machine: e,
1635
- context: n
1636
- }, s, i) {
1637
- const r = (o, a = {}) => {
1638
- const {
1639
- systemId: c,
1640
- input: u
1641
- } = a;
1642
- if (typeof o == "string") {
1643
- const l = pt(e, o);
1644
- if (!l)
1645
- throw new Error(`Actor logic '${o}' not implemented in machine '${e.id}'`);
1646
- const f = N(l, {
1647
- id: a.id,
1648
- parent: t.self,
1649
- syncSnapshot: a.syncSnapshot,
1650
- input: typeof u == "function" ? u({
1651
- context: n,
1652
- event: s,
1653
- self: t.self
1654
- }) : u,
1655
- src: o,
1656
- systemId: c
1657
- });
1658
- return i[f.id] = f, f;
1659
- } else
1660
- return N(o, {
1661
- id: a.id,
1662
- parent: t.self,
1663
- syncSnapshot: a.syncSnapshot,
1664
- input: a.input,
1665
- src: o,
1666
- systemId: c
1667
- });
1668
- };
1669
- return (o, a) => {
1670
- const c = r(o, a);
1671
- return i[c.id] = c, t.defer(() => {
1672
- if (c._processingStatus !== m.Stopped)
1673
- try {
1674
- c.start?.();
1675
- } catch (u) {
1676
- t.self.send(k(c.id, u));
1677
- return;
1678
- }
1679
- }), c;
1680
- };
1681
- }
1682
- function $n(t, e, n, s, {
1683
- assignment: i
1684
- }) {
1685
- if (!e.context)
1686
- throw new Error("Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.");
1687
- const r = {}, o = {
1688
- context: e.context,
1689
- event: n.event,
1690
- spawn: On(t, e, n.event, r),
1691
- self: t?.self,
1692
- system: t?.system
1693
- };
1694
- let a = {};
1695
- if (typeof i == "function")
1696
- a = i(o, s);
1697
- else
1698
- for (const u of Object.keys(i)) {
1699
- const l = i[u];
1700
- a[u] = typeof l == "function" ? l(o, s) : l;
1701
- }
1702
- const c = Object.assign({}, e.context, a);
1703
- return [b(e, {
1704
- context: c,
1705
- children: Object.keys(r).length ? {
1706
- ...e.children,
1707
- ...r
1708
- } : e.children
1709
- })];
1710
- }
1711
- function y(t) {
1712
- function e(n, s) {
1713
- }
1714
- return e.type = "xstate.assign", e.assignment = t, e.resolve = $n, e;
1715
- }
1716
- let ut = /* @__PURE__ */ function(t) {
1717
- return t.Parent = "#_parent", t.Internal = "#_internal", t;
1718
- }({});
1719
- function Mn(t, e, n, s, {
1720
- to: i,
1721
- event: r,
1722
- id: o,
1723
- delay: a
1724
- }, c) {
1725
- const u = e.machine.implementations.delays;
1726
- if (typeof r == "string")
1727
- throw new Error(`Only event objects may be used with sendTo; use sendTo({ type: "${r}" }) instead`);
1728
- const l = typeof r == "function" ? r(n, s) : r;
1729
- let f;
1730
- if (typeof a == "string") {
1731
- const p = u && u[a];
1732
- f = typeof p == "function" ? p(n, s) : p;
1733
- } else
1734
- f = typeof a == "function" ? a(n, s) : a;
1735
- const d = typeof i == "function" ? i(n, s) : i;
1736
- let h;
1737
- if (typeof d == "string") {
1738
- if (d === ut.Parent ? h = t?.self._parent : d === ut.Internal ? h = t?.self : d.startsWith("#_") ? h = e.children[d.slice(2)] : h = c.deferredActorIds?.includes(d) ? d : e.children[d], !h)
1739
- throw new Error(`Unable to send event to actor '${d}' from machine '${e.machine.id}'.`);
1740
- } else
1741
- h = d || t?.self;
1742
- return [e, {
1743
- to: h,
1744
- event: l,
1745
- id: o,
1746
- delay: f
1747
- }];
1748
- }
1749
- function Ln(t, e, n) {
1750
- typeof n.to == "string" && (n.to = e.children[n.to]);
1751
- }
1752
- function Fn(t, e) {
1753
- if (typeof e.delay == "number") {
1754
- t.self.delaySend(e);
1755
- return;
1756
- }
1757
- t.defer(() => {
1758
- const {
1759
- to: n,
1760
- event: s
1761
- } = e;
1762
- t?.system._relay(t.self, n, s.type === Ce ? k(t.self.id, s.data) : s);
1763
- });
1764
- }
1765
- function Cn(t, e, n) {
1766
- function s(i, r) {
1767
- }
1768
- return s.type = "xstate.sendTo", s.to = t, s.event = e, s.id = n?.id, s.delay = n?.delay, s.resolve = Mn, s.retryResolve = Ln, s.execute = Fn, s;
1769
- }
1770
- function F(t, e) {
1771
- return Cn(ut.Parent, t, e);
1772
- }
1773
- const Ht = /* @__PURE__ */ new WeakMap();
1774
- function D(t, e, n) {
1775
- let s = Ht.get(t);
1776
- return s ? e in s || (s[e] = n()) : (s = {
1777
- [e]: n()
1778
- }, Ht.set(t, s)), s[e];
1779
- }
1780
- const Nn = {}, L = (t) => typeof t == "string" ? {
1781
- type: t
1782
- } : typeof t == "function" ? "resolve" in t ? {
1783
- type: t.type
1784
- } : {
1785
- type: t.name
1786
- } : t;
1787
- class St {
1788
- constructor(e, n) {
1789
- if (this.config = e, this.key = void 0, this.id = void 0, this.type = void 0, this.path = void 0, this.states = void 0, this.history = void 0, this.entry = void 0, this.exit = void 0, this.parent = void 0, this.machine = void 0, this.meta = void 0, this.output = void 0, this.order = -1, this.description = void 0, this.tags = [], this.transitions = void 0, this.always = void 0, this.parent = n._parent, this.key = n._key, this.machine = n._machine, this.path = this.parent ? this.parent.path.concat(this.key) : [], this.id = this.config.id || [this.machine.id, ...this.path].join(q), this.type = this.config.type || (this.config.states && Object.keys(this.config.states).length ? "compound" : this.config.history ? "history" : "atomic"), this.description = this.config.description, this.order = this.machine.idMap.size, this.machine.idMap.set(this.id, this), this.states = this.config.states ? $t(this.config.states, (s, i) => new St(s, {
1790
- _parent: this,
1791
- _key: i,
1792
- _machine: this.machine
1793
- })) : Nn, this.type === "compound" && !this.config.initial)
1794
- throw new Error(`No initial state specified for compound state node "#${this.id}". Try adding { initial: "${Object.keys(this.states)[0]}" } to the state config.`);
1795
- this.history = this.config.history === !0 ? "shallow" : this.config.history || !1, this.entry = S(this.config.entry).slice(), this.exit = S(this.config.exit).slice(), this.meta = this.config.meta, this.output = this.type === "final" || !this.parent ? this.config.output : void 0, this.tags = S(e.tags).slice();
1796
- }
1797
- _initialize() {
1798
- this.transitions = rn(this), this.config.always && (this.always = I(this.config.always).map((e) => _(this, Gt, e))), Object.keys(this.states).forEach((e) => {
1799
- this.states[e]._initialize();
1800
- });
1801
- }
1802
- /**
1803
- * The well-structured state node definition.
1804
- */
1805
- get definition() {
1806
- return {
1807
- id: this.id,
1808
- key: this.key,
1809
- version: this.machine.version,
1810
- type: this.type,
1811
- initial: this.initial ? {
1812
- target: this.initial.target,
1813
- source: this,
1814
- actions: this.initial.actions.map(L),
1815
- eventType: null,
1816
- reenter: !1,
1817
- toJSON: () => ({
1818
- target: this.initial.target.map((e) => `#${e.id}`),
1819
- source: `#${this.id}`,
1820
- actions: this.initial.actions.map(L),
1821
- eventType: null
1822
- })
1823
- } : void 0,
1824
- history: this.history,
1825
- states: $t(this.states, (e) => e.definition),
1826
- on: this.on,
1827
- transitions: [...this.transitions.values()].flat().map((e) => ({
1828
- ...e,
1829
- actions: e.actions.map(L)
1830
- })),
1831
- entry: this.entry.map(L),
1832
- exit: this.exit.map(L),
1833
- meta: this.meta,
1834
- order: this.order || -1,
1835
- output: this.output,
1836
- invoke: this.invoke,
1837
- description: this.description,
1838
- tags: this.tags
1839
- };
1840
- }
1841
- toJSON() {
1842
- return this.definition;
1843
- }
1844
- /**
1845
- * The logic invoked as actors by this state node.
1846
- */
1847
- get invoke() {
1848
- return D(this, "invoke", () => S(this.config.invoke).map((e, n) => {
1849
- const {
1850
- src: s,
1851
- systemId: i
1852
- } = e, r = e.id || Lt(this.id, n), o = typeof s == "string" ? s : `xstate#${Lt(this.id, n)}`;
1853
- return {
1854
- ...e,
1855
- src: o,
1856
- id: r,
1857
- systemId: i,
1858
- toJSON() {
1859
- const {
1860
- onDone: a,
1861
- onError: c,
1862
- ...u
1863
- } = e;
1864
- return {
1865
- ...u,
1866
- type: "xstate.invoke",
1867
- src: o,
1868
- id: r
1869
- };
1870
- }
1871
- };
1872
- }));
1873
- }
1874
- /**
1875
- * The mapping of events to transitions.
1876
- */
1877
- get on() {
1878
- return D(this, "on", () => [...this.transitions].flatMap(([n, s]) => s.map((i) => [n, i])).reduce((n, [s, i]) => (n[s] = n[s] || [], n[s].push(i), n), {}));
1879
- }
1880
- get after() {
1881
- return D(this, "delayedTransitions", () => sn(this));
1882
- }
1883
- get initial() {
1884
- return D(this, "initial", () => on(this, this.config.initial));
1885
- }
1886
- next(e, n) {
1887
- const s = n.type, i = [];
1888
- let r;
1889
- const o = D(this, `candidates-${s}`, () => nn(this, s));
1890
- for (const a of o) {
1891
- const {
1892
- guard: c
1893
- } = a, u = e.context;
1894
- let l = !1;
1895
- try {
1896
- l = !c || Y(c, u, n, e);
1897
- } catch (f) {
1898
- const d = typeof c == "string" ? c : typeof c == "object" ? c.type : void 0;
1899
- throw new Error(`Unable to evaluate guard ${d ? `'${d}' ` : ""}in transition for event '${s}' in state node '${this.id}':
1900
- ${f.message}`);
1901
- }
1902
- if (l) {
1903
- i.push(...a.actions), r = a;
1904
- break;
1905
- }
1906
- }
1907
- return r ? [r] : void 0;
1908
- }
1909
- /**
1910
- * All the event types accepted by this state node and its descendants.
1911
- */
1912
- get events() {
1913
- return D(this, "events", () => {
1914
- const {
1915
- states: e
1916
- } = this, n = new Set(this.ownEvents);
1917
- if (e)
1918
- for (const s of Object.keys(e)) {
1919
- const i = e[s];
1920
- if (i.states)
1921
- for (const r of i.events)
1922
- n.add(`${r}`);
1923
- }
1924
- return Array.from(n);
1925
- });
1926
- }
1927
- /**
1928
- * All the events that have transitions directly from this state node.
1929
- *
1930
- * Excludes any inert events.
1931
- */
1932
- get ownEvents() {
1933
- const e = new Set([...this.transitions.keys()].filter((n) => this.transitions.get(n).some((s) => !(!s.target && !s.actions.length && !s.reenter))));
1934
- return Array.from(e);
1935
- }
1936
- }
1937
- const jn = "#";
1938
- class wt {
1939
- constructor(e, n) {
1940
- this.config = e, this.version = void 0, this.implementations = void 0, this.types = void 0, this.__xstatenode = !0, this.idMap = /* @__PURE__ */ new Map(), this.root = void 0, this.id = void 0, this.states = void 0, this.events = void 0, this.__TResolvedTypesMeta = void 0, this.id = e.id || "(machine)", this.implementations = {
1941
- actors: n?.actors ?? {},
1942
- actions: n?.actions ?? {},
1943
- delays: n?.delays ?? {},
1944
- guards: n?.guards ?? {}
1945
- }, this.version = this.config.version, this.types = this.config.types ?? {}, this.transition = this.transition.bind(this), this.getInitialState = this.getInitialState.bind(this), this.restoreState = this.restoreState.bind(this), this.start = this.start.bind(this), this.root = new St(e, {
1946
- _key: this.id,
1947
- _machine: this
1948
- }), this.root._initialize(), this.states = this.root.states, this.events = this.root.events;
1949
- }
1950
- /**
1951
- * Clones this state machine with the provided implementations
1952
- * and merges the `context` (if provided).
1953
- *
1954
- * @param implementations Options (`actions`, `guards`, `actors`, `delays`, `context`)
1955
- * to recursively merge with the existing options.
1956
- *
1957
- * @returns A new `StateMachine` instance with the provided implementations.
1958
- */
1959
- provide(e) {
1960
- const {
1961
- actions: n,
1962
- guards: s,
1963
- actors: i,
1964
- delays: r
1965
- } = this.implementations;
1966
- return new wt(this.config, {
1967
- actions: {
1968
- ...n,
1969
- ...e.actions
1970
- },
1971
- guards: {
1972
- ...s,
1973
- ...e.guards
1974
- },
1975
- actors: {
1976
- ...i,
1977
- ...e.actors
1978
- },
1979
- delays: {
1980
- ...r,
1981
- ...e.delays
1982
- }
1983
- });
1984
- }
1985
- resolveState(e) {
1986
- const n = Sn(this.root, e.value), s = z(K(this.root, n));
1987
- return B({
1988
- _nodes: [...s],
1989
- context: e.context || {},
1990
- children: {},
1991
- status: mt(s, this.root) ? "done" : e.status || "active",
1992
- output: e.output,
1993
- error: e.error,
1994
- historyValue: e.historyValue
1995
- }, this);
1996
- }
1997
- /**
1998
- * Determines the next state given the current `state` and received `event`.
1999
- * Calculates a full macrostep from all microsteps.
2000
- *
2001
- * @param state The current State instance or state value
2002
- * @param event The received event
2003
- */
2004
- transition(e, n, s) {
2005
- if (We(n) && !Je(e).some((r) => r === n.type))
2006
- return b(e, {
2007
- status: "error",
2008
- error: n.data
2009
- });
2010
- const {
2011
- state: i
2012
- } = st(e, n, s);
2013
- return i;
2014
- }
2015
- /**
2016
- * Determines the next state given the current `state` and `event`.
2017
- * Calculates a microstep.
2018
- *
2019
- * @param state The current state
2020
- * @param event The received event
2021
- */
2022
- microstep(e, n, s) {
2023
- return st(e, n, s).microstates;
2024
- }
2025
- getTransitionData(e, n) {
2026
- return vt(this.root, e.value, e, n) || [];
2027
- }
2028
- /**
2029
- * The initial state _before_ evaluating any microsteps.
2030
- * This "pre-initial" state is provided to initial actions executed in the initial state.
2031
- */
2032
- getPreInitialState(e, n, s) {
2033
- const {
2034
- context: i
2035
- } = this.config, r = B({
2036
- context: typeof i != "function" && i ? i : {},
2037
- _nodes: [this.root],
2038
- children: {},
2039
- status: "active"
2040
- }, this);
2041
- return typeof i == "function" ? M(r, n, e, [y(({
2042
- spawn: a,
2043
- event: c
2044
- }) => i({
2045
- spawn: a,
2046
- input: c.input
2047
- }))], s) : r;
2048
- }
2049
- /**
2050
- * Returns the initial `State` instance, with reference to `self` as an `ActorRef`.
2051
- */
2052
- getInitialState(e, n) {
2053
- const s = Vt(n), i = [], r = this.getPreInitialState(e, s, i), o = dt([{
2054
- target: [...oe(this.root)],
2055
- source: this.root,
2056
- reenter: !0,
2057
- actions: [],
2058
- eventType: null,
2059
- toJSON: null
2060
- // TODO: fix
2061
- }], r, e, s, !0, i), {
2062
- state: a
2063
- } = st(o, s, e, i);
2064
- return a;
2065
- }
2066
- start(e) {
2067
- Object.values(e.children).forEach((n) => {
2068
- n.getSnapshot().status === "active" && n.start();
2069
- });
2070
- }
2071
- getStateNodeById(e) {
2072
- const n = e.split(q), s = n.slice(1), i = X(n[0]) ? n[0].slice(jn.length) : n[0], r = this.idMap.get(i);
2073
- if (!r)
2074
- throw new Error(`Child state node '#${i}' does not exist on machine '${this.id}'`);
2075
- return G(r, s);
2076
- }
2077
- get definition() {
2078
- return this.root.definition;
2079
- }
2080
- toJSON() {
2081
- return this.definition;
2082
- }
2083
- getPersistedState(e, n) {
2084
- return Dn(e, n);
2085
- }
2086
- restoreState(e, n) {
2087
- const s = {}, i = e.children;
2088
- Object.keys(i).forEach((c) => {
2089
- const u = i[c], l = u.state, f = u.src, d = typeof f == "string" ? pt(this, f) : f;
2090
- if (!d)
2091
- return;
2092
- const h = N(d, {
2093
- id: c,
2094
- parent: n?.self,
2095
- syncSnapshot: u.syncSnapshot,
2096
- state: l,
2097
- src: f,
2098
- systemId: u.systemId
2099
- });
2100
- s[c] = h;
2101
- });
2102
- const r = B({
2103
- ...e,
2104
- children: s,
2105
- _nodes: Array.from(z(K(this.root, e.value)))
2106
- }, this);
2107
- let o = /* @__PURE__ */ new Set();
2108
- function a(c, u) {
2109
- if (!o.has(c)) {
2110
- o.add(c);
2111
- for (let l in c) {
2112
- const f = c[l];
2113
- if (f && typeof f == "object") {
2114
- if ("xstate$$type" in f && f.xstate$$type === yt) {
2115
- c[l] = u[f.id];
2116
- continue;
2117
- }
2118
- a(f, u);
2119
- }
2120
- }
2121
- }
2122
- }
2123
- return a(r.context, s), r;
2124
- }
2125
- }
2126
- function _t(t, e) {
2127
- return new wt(t, e);
2128
- }
2129
- const Pn = (t, e) => new ReadableStream({
2130
- start() {
2131
- this.reader = t.getReader();
2132
- },
2133
- async pull(n) {
2134
- const s = Date.now(), { done: i, value: r } = await this.reader.read();
2135
- if (i) {
2136
- n.close();
2137
- return;
2138
- }
2139
- n.enqueue(r);
2140
- const a = Date.now() - s, u = r.length / (e() ?? Number.MAX_VALUE) * 1e3 - a;
2141
- u > 0 && await new Promise((l) => setTimeout(l, u));
2142
- },
2143
- cancel() {
2144
- this.reader?.cancel();
2145
- }
2146
- }), Un = (t) => {
2147
- if (t.length === 0)
2148
- return [];
2149
- const e = t.sort((s, i) => s.start - i.start), n = [e[0]];
2150
- for (let s = 1; s < e.length; s++) {
2151
- const i = e[s], r = n[n.length - 1];
2152
- i.start <= r.end + 1 ? r.end = Math.max(r.end, i.end) : n.push(i);
2153
- }
2154
- return n;
2155
- }, Bt = 5e3, Hn = _t({
2156
- initial: "checkingFile",
2157
- context: ({ input: t }) => ({
2158
- settingsDbDocument: t.settingsDbDocument,
2159
- document: t.document,
2160
- file: t.file,
2161
- downloadedRanges: t.file.downloadedRanges ?? [],
2162
- downloaded: t.file.downloaded ?? 0,
2163
- progress: t.file.progress ?? 0,
2164
- selected: t.file.selected ?? !1,
2165
- priority: t.file.priority ?? 0,
2166
- length: t.file.length ?? 0,
2167
- offset: t.file.offset ?? 0,
2168
- path: t.file.path ?? "",
2169
- pathArray: t.file.pathArray,
2170
- name: t.file.name ?? "",
2171
- index: t.file.index,
2172
- status: t.file.status ?? "checking",
2173
- streamBandwithLogs: t.file.streamBandwithLogs ?? [],
2174
- downloadSpeed: t.file.downloadSpeed ?? 0
2175
- }),
2176
- on: {
2177
- "FILE.PAUSE": {
2178
- target: ".paused"
2179
- },
2180
- "FILE.RESUME": {
2181
- target: ".downloading"
2182
- }
2183
- },
2184
- // invoke: [
2185
- // {
2186
- // id: 'syncDBState',
2187
- // src: fromObservable(({ context, self, ...rest }) => {
2188
- // const pause$ =
2189
- // from(self)
2190
- // .pipe(
2191
- // filter((event) => event.type === 'FILE.PAUSE'),
2192
- // tap(() => {
2193
- // context.document.incrementalModify((doc) => {
2194
- // const file = doc.state.files.find((file) => file.path === context.file.path)
2195
- // if (!file) return doc
2196
- // file.status = 'paused'
2197
- // return doc
2198
- // })
2199
- // })
2200
- // )
2201
- // const resume$ =
2202
- // from(self)
2203
- // .pipe(
2204
- // filter((event) => event.type === 'FILE.RESUME'),
2205
- // tap((event) => {
2206
- // context.document.incrementalModify((doc) => {
2207
- // const file = doc.state.files.find((file) => file.path === context.file.path)
2208
- // if (!file) return doc
2209
- // // file.status = 'downloading'
2210
- // return doc
2211
- // })
2212
- // })
2213
- // )
2214
- // return (
2215
- // merge(
2216
- // pause$,
2217
- // resume$
2218
- // )
2219
- // )
2220
- // })
2221
- // }
2222
- // ],
2223
- states: {
2224
- checkingFile: {
2225
- entry: F({ type: "FILE.CHECKING_FILE" }),
2226
- always: {
2227
- target: "downloading",
2228
- guard: ({ context: t }) => t?.selected === !0
2229
- }
2230
- },
2231
- downloading: {
2232
- entry: F({ type: "FILE.DOWNLOADING" }),
2233
- invoke: {
2234
- input: (t) => t,
2235
- src: j(
2236
- (t) => new ve((e) => {
2237
- const n = { input: t.input.context }, s = n.input.document;
2238
- if (n.input.file.selected === !1)
2239
- return e.complete();
2240
- const r = n.input.file.downloadedRanges.sort((f, d) => f.start - d.start).at(0)?.end || 0;
2241
- if (r === n.input.file.length) {
2242
- e.complete();
2243
- return;
2244
- }
2245
- let o = [...n.input.streamBandwithLogs], a = [...n.input.file.downloadedRanges.map((f) => ({ ...f }))], c = !1, u, l = new Promise((f, d) => {
2246
- u = f;
2247
- });
2248
- return be({
2249
- magnet: s.state.magnet,
2250
- path: n.input.file.pathArray.join("/"),
2251
- offset: r
2252
- }).then(async (f) => {
2253
- const d = await n.input.settingsDbDocument.getLatest();
2254
- let h = d.downloadSpeedLimitEnabled, p = d.downloadSpeedLimit;
2255
- d.$.subscribe((U) => {
2256
- h = U.downloadSpeedLimitEnabled, p = U.downloadSpeedLimit;
2257
- });
2258
- const v = Pn(f.body, () => h ? p : Number.MAX_VALUE).getReader(), { write: he, close: Z } = await J(le(), { key: "io-worker" })(
2259
- "openWriteStream",
2260
- {
2261
- filePath: `${n.input.document.infoHash}/${n.input.file.path}`,
2262
- offset: r,
2263
- size: n.input.file.length
2264
- }
2265
- );
2266
- u(Z);
2267
- const Tt = async () => {
2268
- if (t.self.getSnapshot().status !== "active")
2269
- return;
2270
- if (c)
2271
- return await v.cancel(), await Z(), e.error(err);
2272
- const { done: U, value: bt } = await v.read();
2273
- if (U)
2274
- return await Z(), e.complete();
2275
- const Q = bt.byteLength;
2276
- try {
2277
- await he(bt.buffer);
2278
- } catch (E) {
2279
- return e.error(E);
2280
- }
2281
- const tt = a?.find((E) => E.start <= r && E.end >= r);
2282
- tt ? tt.end = tt.end + Q : a = [
2283
- ...a,
2284
- { start: r, end: r + Q }
2285
- ], a = Un(a), o = [
2286
- ...o,
2287
- { timestamp: Date.now(), byteLength: Q }
2288
- ];
2289
- const pe = o.filter((E) => E.timestamp > Date.now() - Bt).sort((E, R) => E.timestamp - R.timestamp), Rt = a.reduce((E, R) => E + (R.end - R.start), 0), ye = pe.reduce((E, R) => E + R.byteLength, 0) / Bt * 1e3;
2290
- e.next({
2291
- type: "FILE.DOWNLOADING_UPDATE",
2292
- value: {
2293
- downloadedRanges: a,
2294
- downloaded: Rt,
2295
- progress: Rt / n.input.file.length,
2296
- status: "downloading",
2297
- streamBandwithLogs: [...o],
2298
- downloadSpeed: ye
2299
- }
2300
- }), c || Tt();
2301
- };
2302
- Tt();
2303
- }).catch((f) => {
2304
- u(void 0), console.log("downloadFile err", f), e.error(f);
2305
- }), () => {
2306
- c = !0, l.then((f) => f?.());
2307
- };
2308
- })
2309
- ),
2310
- onSnapshot: {
2311
- guard: ({ event: t }) => t.snapshot.context?.type === "FILE.DOWNLOADING_UPDATE",
2312
- actions: y(({ event: t }) => ({
2313
- downloadedRanges: t.snapshot.context.value.downloadedRanges,
2314
- downloaded: t.snapshot.context.value.downloaded,
2315
- progress: t.snapshot.context.value.progress,
2316
- status: t.snapshot.context.value.status,
2317
- streamBandwithLogs: t.snapshot.context.value.streamBandwithLogs,
2318
- downloadSpeed: t.snapshot.context.value.downloadSpeed
2319
- }))
2320
- },
2321
- onDone: {
2322
- target: "finished"
2323
- }
2324
- },
2325
- on: {
2326
- "FILE.PAUSE": {
2327
- target: "paused",
2328
- actions: y({
2329
- status: "paused"
2330
- })
2331
- }
2332
- }
2333
- },
2334
- paused: {
2335
- entry: [
2336
- F({ type: "FILE.PAUSED" }),
2337
- y({
2338
- status: "paused"
2339
- })
2340
- ],
2341
- on: {
2342
- "FILE.RESUME": {
2343
- target: "downloading",
2344
- actions: y({
2345
- status: "downloading"
2346
- })
2347
- }
2348
- }
2349
- },
2350
- finished: {
2351
- entry: [
2352
- F({ type: "FILE.FINISHED" }),
2353
- y({
2354
- status: "finished",
2355
- downloaded: ({ context: t }) => t.length,
2356
- progress: 1,
2357
- downloadedRanges: ({ context: t }) => [{ start: 0, end: t.length }]
2358
- })
2359
- ]
2360
- }
2361
- }
2362
- }), Wt = _t({
2363
- initial: "init",
2364
- context: ({ input: t }) => ({
2365
- settingsDbDocument: t.settingsDbDocument,
2366
- infoHash: t.infoHash,
2367
- magnet: t.magnet,
2368
- torrentFile: t.torrentFile,
2369
- shouldRemoveFiles: !1,
2370
- lastEvent: null,
2371
- document: t.document,
2372
- dbDocument: t.dbDocument,
2373
- files: []
2374
- }),
2375
- types: {
2376
- events: {}
2377
- },
2378
- always: [{
2379
- actions: [
2380
- async ({ context: t, event: e, self: n }) => {
2381
- const s = t.files.map((d) => d.getSnapshot().context);
2382
- if (!t.document || !t.dbDocument || e.type === "TORRENT.REMOVE-AND-KEEP-FILES" || e.type === "TORRENT.REMOVE-AND-DELETE-FILES")
2383
- return;
2384
- const i = {
2385
- ...t.document,
2386
- state: {
2387
- ...t.document.state,
2388
- torrentFile: t.document.state.torrentFile && zt(t.document.state.torrentFile)
2389
- }
2390
- }, r = n.getSnapshot().value, o = s.reduce((d, h) => d + h.length, 0), a = s.reduce((d, h) => d + h.downloaded, 0), c = r === "downloading" ? s.reduce((d, h) => d + h.downloadSpeed, 0) : 0, u = (o - a) / c, { infoHash: l, ...f } = xt({
2391
- state: {
2392
- name: i.state.name,
2393
- status: r,
2394
- progress: s.filter((d) => d.selected).reduce((d, h) => d + h.progress, 0) / s.filter((d) => d.selected).length,
2395
- size: i.state.size,
2396
- peers: i.state.peers,
2397
- proxy: i.state.proxy,
2398
- p2p: i.state.p2p,
2399
- addedAt: i.state.addedAt,
2400
- remainingTime: u,
2401
- peersCount: i.state.peersCount,
2402
- seedersCount: i.state.seedersCount,
2403
- leechersCount: i.state.leechersCount,
2404
- downloaded: a,
2405
- uploaded: s.reduce((d, h) => d + h.uploaded, 0),
2406
- downloadSpeed: c,
2407
- uploadSpeed: s.reduce((d, h) => d + h.uploadSpeed, 0),
2408
- ratio: i.state.ratio,
2409
- files: s.map((d, h) => ({
2410
- index: h,
2411
- name: d.name,
2412
- path: d.path,
2413
- pathArray: d.pathArray,
2414
- offset: d.offset,
2415
- length: d.length,
2416
- downloaded: d.downloaded,
2417
- progress: d.progress,
2418
- selected: d.selected,
2419
- priority: d.priority,
2420
- downloadedRanges: d.downloadedRanges.map((p) => ({
2421
- start: p.start,
2422
- end: p.end
2423
- })),
2424
- downloadSpeed: d.downloadSpeed,
2425
- streamBandwithLogs: d.streamBandwithLogs.map((p) => ({
2426
- byteLength: p.byteLength,
2427
- timestamp: p.timestamp
2428
- }))
2429
- })),
2430
- streamBandwithLogs: s.reduce((d, h) => d.concat(h.streamBandwithLogs), []).sort((d, h) => d.timestamp - h.timestamp),
2431
- magnet: i.state.magnet,
2432
- torrentFile: Ee(i.state.torrentFile)
2433
- },
2434
- options: i.options && {
2435
- paused: i.options.paused
2436
- }
2437
- });
2438
- await t.dbDocument.incrementalUpdate({
2439
- $set: {
2440
- ...f
2441
- }
2442
- });
2443
- },
2444
- y(({ event: t }) => ({
2445
- lastEvent: t
2446
- }))
2447
- ],
2448
- guard: ({ context: t, event: e }) => t.lastEvent !== e
2449
- }],
2450
- on: {
2451
- "FILE.CHECKING": ".checkingFiles",
2452
- "FILE.FINISHED": {
2453
- guard: ({ context: t }) => t.files.every((e) => e.getSnapshot().value === "finished"),
2454
- target: ".finished"
2455
- },
2456
- "TORRENT.PAUSE": {
2457
- actions: y({
2458
- files: ({ context: t }) => (t.files.forEach((e) => e.send({ type: "FILE.PAUSE" })), t.files)
2459
- })
2460
- },
2461
- "TORRENT.RESUME": {
2462
- actions: y({
2463
- files: ({ context: t }) => (t.files.forEach((e) => e.send({ type: "FILE.RESUME" })), t.files)
2464
- })
2465
- },
2466
- "TORRENT.REMOVE-AND-DELETE-FILES": {
2467
- actions: [
2468
- y({
2469
- shouldRemoveFiles: () => !0
2470
- })
2471
- ],
2472
- target: ".removing"
2473
- },
2474
- "TORRENT.REMOVE-AND-KEEP-FILES": {
2475
- actions: [
2476
- y({
2477
- shouldRemoveFiles: () => !1
2478
- })
2479
- ],
2480
- target: ".removing"
2481
- }
2482
- },
2483
- states: {
2484
- init: {
2485
- invoke: {
2486
- input: ({ context: t, event: e }) => ({ context: t, data: e }),
2487
- src: W(async ({ input: t }) => {
2488
- const { context: e, data: { input: n } } = t;
2489
- if (e.dbDocument)
2490
- return e.dbDocument;
2491
- const s = await Dt.torrents.findOne(n.infoHash).exec();
2492
- if (s)
2493
- return s;
2494
- const i = n.torrentFile?.infoBuffer ? n.torrentFile : await Re({ magnet: n.magnet }).then((o) => o.arrayBuffer()).then((o) => Se(we.Buffer.from(o))), r = xt({
2495
- infoHash: i.infoHash,
2496
- state: {
2497
- magnet: n.magnet ? n.magnet : _e(n.torrentFile),
2498
- torrentFile: i
2499
- }
2500
- });
2501
- return Dt.torrents.insert(r);
2502
- }),
2503
- onDone: {
2504
- target: "checkingFiles",
2505
- actions: [
2506
- y({
2507
- document: ({ event: t }) => t.output.toJSON(),
2508
- dbDocument: ({ event: t }) => t.output,
2509
- files: ({ spawn: t, event: e, context: n }) => e.output.state.files.filter((s) => s.selected).map((s) => t(
2510
- Hn,
2511
- {
2512
- id: `torrent-${e.output.infoHash}-${s.path}`,
2513
- input: {
2514
- settingsDbDocument: n.settingsDbDocument,
2515
- document: e.output.toJSON(),
2516
- file: s
2517
- },
2518
- syncSnapshot: !0
2519
- }
2520
- ))
2521
- })
2522
- ]
2523
- }
2524
- }
2525
- },
2526
- checkingFiles: {
2527
- invoke: {
2528
- src: W(async () => {
2529
- }),
2530
- onDone: [
2531
- {
2532
- target: "downloadingMetadata",
2533
- guard: ({ context: t }) => !t.torrentFile
2534
- },
2535
- {
2536
- guard: Ft([
2537
- ({ context: t }) => t.document.state.stats === "finished",
2538
- ({ context: t }) => t.torrent && t.files.every((e) => e.getSnapshot().value === "finished")
2539
- ]),
2540
- target: "finished"
2541
- },
2542
- {
2543
- target: "downloading",
2544
- guard: Ft([
2545
- ({ context: t }) => t.document.state.status === "downloading",
2546
- ({ context: t }) => t.torrentFile
2547
- ])
2548
- }
2549
- ]
2550
- }
2551
- },
2552
- downloadingMetadata: {
2553
- invoke: {
2554
- input: ({ context: t }) => t,
2555
- src: j((t) => t.input.document.$),
2556
- onSnapshot: {
2557
- target: "downloading",
2558
- guard: ({ event: t }) => t.snapshot.context?.state?.torrentFile !== void 0
2559
- }
2560
- }
2561
- },
2562
- downloading: {
2563
- invoke: {
2564
- input: ({ context: t }) => t,
2565
- src: j(({ input: t, ...e }) => rt([]))
2566
- // onDone: {
2567
- // target: 'paused'
2568
- // }
2569
- },
2570
- on: {
2571
- "FILES.FINISHED": "finished",
2572
- "TORRENT.PAUSE": {
2573
- target: "paused",
2574
- actions: y({
2575
- files: ({ context: t, event: e }) => (t.files.forEach((n) => n.send({ type: "FILE.PAUSE" })), t.files)
2576
- })
2577
- }
2578
- }
2579
- },
2580
- paused: {
2581
- on: {
2582
- "TORRENT.RESUME": {
2583
- target: "downloading",
2584
- actions: y({
2585
- files: ({ context: t, event: e }) => (t.files.forEach((n) => n.send({ type: "FILE.RESUME" })), t.files)
2586
- })
2587
- }
2588
- }
2589
- },
2590
- finished: {},
2591
- removing: {
2592
- invoke: {
2593
- input: ({ context: t, self: e }) => ({ context: t, self: e }),
2594
- src: W(async ({ input: t }) => {
2595
- if (t.context.dbDocument) {
2596
- const n = async () => {
2597
- try {
2598
- await (await t.context.dbDocument.getLatest()).remove();
2599
- } catch {
2600
- await new Promise((i) => setTimeout(i, 100)), await n();
2601
- }
2602
- };
2603
- n();
2604
- }
2605
- return t.context.files.forEach((n) => t.self._actorScope.stopChild(n)), await (await (await navigator.storage.getDirectory()).getDirectoryHandle("torrents")).removeEntry(t.context.infoHash, { recursive: !0 }), t.context.document;
2606
- }),
2607
- onDone: {
2608
- actions: [
2609
- y({
2610
- document: () => {
2611
- },
2612
- dbDocument: () => {
2613
- },
2614
- files: []
2615
- }),
2616
- F(({ context: t }) => ({
2617
- type: "TORRENT.REMOVED",
2618
- input: {
2619
- infoHash: t.infoHash
2620
- }
2621
- }))
2622
- ]
2623
- }
2624
- }
2625
- }
2626
- }
2627
- }), Bn = () => Te.find().exec(), Wn = _t({
2628
- id: "torrentManager",
2629
- initial: "init",
2630
- context: {
2631
- settingsDbDocument: void 0,
2632
- torrents: [],
2633
- workerReady: !1
2634
- },
2635
- types: {
2636
- events: {}
2637
- },
2638
- on: {
2639
- "WORKER.DISCONNECTED": {
2640
- actions: y({ workerReady: !1 })
2641
- },
2642
- "WORKER.READY": {
2643
- actions: y({ workerReady: !0 })
2644
- }
2645
- },
2646
- states: {
2647
- init: {
2648
- invoke: {
2649
- src: W(async () => {
2650
- const [t, e] = await Promise.all([
2651
- et(),
2652
- Bn()
2653
- ]);
2654
- return {
2655
- settingsDbDocument: t,
2656
- torrentDbDocuments: e
2657
- };
2658
- }),
2659
- onDone: {
2660
- target: "waitingForDocuments",
2661
- actions: y({
2662
- settingsDbDocument: ({ event: t }) => t.output.settingsDbDocument,
2663
- torrents: ({ spawn: t, event: e }) => e.output.torrentDbDocuments.map((n) => t(
2664
- Wt,
2665
- {
2666
- id: `torrent-${n.infoHash}`,
2667
- input: {
2668
- settingsDbDocument: e.output.settingsDbDocument,
2669
- infoHash: n.infoHash,
2670
- magnet: n.state.magnet,
2671
- torrentFile: n.state.torrentFile,
2672
- document: n.toJSON(),
2673
- dbDocument: n
2674
- },
2675
- syncSnapshot: !0
2676
- }
2677
- ))
2678
- })
2679
- }
2680
- }
2681
- },
2682
- waitingForDocuments: {
2683
- always: {
2684
- target: "idle",
2685
- guard: ({ context: t }) => t.workerReady
2686
- }
2687
- // invoke: {
2688
- // id: 'getTorrentDocuments',
2689
- // src: fromPromise(getTorrentDocuments),
2690
- // onDone: {
2691
- // target: 'waitingForWorker',
2692
- // actions: assign({
2693
- // torrentDocuments: ({ event }) => event.output
2694
- // })
2695
- // },
2696
- // onError: {}
2697
- // }
2698
- },
2699
- // waitingForWorker: {
2700
- // entry: assign({
2701
- // torrents: ({ context }) => {
2702
- // console.log('waitingForWorker ENTRY', context)
2703
- // context.torrents.forEach(torrent => torrent.stop())
2704
- // return []
2705
- // }
2706
- // }),
2707
- // exit: assign({
2708
- // torrents: ({ spawn, context, self }) => {
2709
- // console.log('waitingForWorker EXIT')
2710
- // context.torrents.forEach(torrent => torrent.stop())
2711
- // return context.torrentDocuments.map(torrent => {
2712
- // return spawn(
2713
- // torrentMachine,
2714
- // {
2715
- // id: `torrent-${torrent.infoHash}`,
2716
- // input: {
2717
- // parent: self,
2718
- // document: torrent
2719
- // },
2720
- // syncSnapshot: true
2721
- // }
2722
- // )
2723
- // })
2724
- // }
2725
- // }),
2726
- // always: {
2727
- // target: 'idle',
2728
- // guard: ({ context }) => console.log('waitingForWorker always guard', context.workerReady) || context.workerReady
2729
- // },
2730
- // on: {
2731
- // 'WORKER.READY': 'idle'
2732
- // }
2733
- // },
2734
- idle: {
2735
- invoke: {
2736
- input: ({ context: t }) => t,
2737
- src: j(({ input: t }) => (t.torrents.filter((e) => e.getSnapshot().value !== "finished").forEach((e) => e.send({ type: "TORRENT.RESUME" })), rt(et()).pipe(
2738
- It((e) => e?.$),
2739
- it((e) => e?.paused),
2740
- At()
2741
- ))),
2742
- onDone: {
2743
- target: "paused"
2744
- }
2745
- },
2746
- on: {
2747
- "TORRENT.ADD": {
2748
- actions: y({
2749
- torrents: ({ spawn: t, context: e, event: n }) => [
2750
- ...e.torrents,
2751
- t(
2752
- Wt,
2753
- {
2754
- id: `torrent-${n.input.infoHash}`,
2755
- input: {
2756
- settingsDbDocument: e.settingsDbDocument,
2757
- ...n.input
2758
- },
2759
- syncSnapshot: !0
2760
- }
2761
- )
2762
- ]
2763
- })
2764
- },
2765
- "TORRENT.PAUSE": {
2766
- actions: y({
2767
- torrents: ({ context: t, event: e }) => (t.torrents.find((s) => s.id === `torrent-${e.output.infoHash}`)?.send({ type: "TORRENT.PAUSE" }), t.torrents)
2768
- })
2769
- },
2770
- "TORRENT.RESUME": {
2771
- actions: y({
2772
- torrents: ({ context: t, event: e }) => (t.torrents.find((s) => s.id === `torrent-${e.output.infoHash}`)?.send({ type: "TORRENT.RESUME" }), t.torrents)
2773
- })
2774
- },
2775
- "TORRENT.REMOVE-AND-DELETE-FILES": {
2776
- actions: ({ context: t, event: e }) => {
2777
- t.torrents.find((s) => s.id === `torrent-${e.input.infoHash}`)?.send({ type: "TORRENT.REMOVE-AND-DELETE-FILES" });
2778
- }
2779
- },
2780
- "TORRENT.REMOVE-AND-KEEP-FILES": {
2781
- actions: ({ context: t, event: e }) => {
2782
- t.torrents.find((s) => s.id === `torrent-${e.input.infoHash}`)?.send({ type: "TORRENT.REMOVE-AND-KEEP-FILES" });
2783
- }
2784
- },
2785
- "TORRENT.REMOVED": {
2786
- actions: y({
2787
- torrents: ({ context: t, event: e }) => t.torrents.filter((n) => n.id !== `torrent-${e.input.infoHash}`)
2788
- })
2789
- },
2790
- PAUSE: {
2791
- actions: y({
2792
- torrents: ({ context: t }) => (t.torrents.filter((e) => e.getSnapshot().value !== "finished").forEach((e) => e.send({ type: "TORRENT.PAUSE" })), t.torrents)
2793
- }),
2794
- target: "paused"
2795
- }
2796
- }
2797
- },
2798
- paused: {
2799
- invoke: {
2800
- id: "paused status watch",
2801
- input: ({ context: t }) => t,
2802
- src: j(({ input: t }) => (t.torrents.filter((e) => e.getSnapshot().value !== "finished").forEach((e) => e.send({ type: "TORRENT.PAUSE" })), rt(et()).pipe(
2803
- It((e) => e?.$),
2804
- it((e) => !e?.paused),
2805
- At()
2806
- ))),
2807
- onDone: {
2808
- target: "idle"
2809
- }
2810
- },
2811
- on: {
2812
- RESUME: {
2813
- actions: y({
2814
- torrents: ({ context: t }) => (t.torrents.filter((e) => e.getSnapshot().value !== "finished").forEach((e) => e.send({ type: "TORRENT.RESUME" })), t.torrents)
2815
- }),
2816
- target: "idle"
2817
- }
2818
- }
2819
- }
2820
- }
2821
- }), V = N(Wn).start();
2822
- let ft;
2823
- const le = () => ft, Jn = H(async ({ workerPort: t }) => {
2824
- ft && V.send({ type: "WORKER.DISCONNECTED" }), ft = t, await new Promise((e, n) => {
2825
- const s = setInterval(() => {
2826
- J(t, { key: "torrent-manager" })("ping").then((i) => {
2827
- clearInterval(s), e(void 0);
2828
- });
2829
- }, 100);
2830
- J(t, { key: "torrent-manager" })("ping").then((i) => {
2831
- clearInterval(s), e(void 0);
2832
- }), setTimeout(() => {
2833
- clearInterval(s), n();
2834
- }, 1e3);
2835
- }), V.send({ type: "WORKER.READY" });
2836
- }), Vn = {
2837
- newLeader: Jn,
2838
- addTorrent: H(async ({ infoHash: t, magnet: e, torrentFile: n }) => {
2839
- V.send({
2840
- type: "TORRENT.ADD",
2841
- input: {
2842
- infoHash: t,
2843
- magnet: e,
2844
- torrentFile: n && zt(n)
2845
- }
2846
- });
2847
- }),
2848
- removeTorrent: H(async ({ infoHash: t, removeFiles: e }) => {
2849
- V.send({
2850
- type: e ? "TORRENT.REMOVE-AND-DELETE-FILES" : "TORRENT.REMOVE-AND-KEEP-FILES",
2851
- input: { infoHash: t }
2852
- });
2853
- }),
2854
- readTorrentFile: H(
2855
- ({ infoHash: t, filePath: e, offset: n, size: s }) => J(le(), { key: "io-worker" })("readTorrentFile", { infoHash: t, filePath: e, offset: n, size: s })
2856
- )
2857
- };
2858
- export {
2859
- le as getIoWorkerPort,
2860
- Jn as newLeader,
2861
- Vn as resolvers
2862
- };