@dxos/async 0.8.4-main.1da679c → 0.8.4-main.21d9917
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/index.mjs +243 -288
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +243 -288
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/cleanup.d.ts +1 -0
- package/dist/types/src/cleanup.d.ts.map +1 -1
- package/dist/types/src/debounce.d.ts +15 -0
- package/dist/types/src/debounce.d.ts.map +1 -1
- package/dist/types/src/persistent-lifecycle.d.ts +2 -2
- package/dist/types/src/persistent-lifecycle.d.ts.map +1 -1
- package/dist/types/src/task-scheduling.d.ts +28 -0
- package/dist/types/src/task-scheduling.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +11 -7
- package/src/cleanup.ts +1 -0
- package/src/debounce.ts +52 -4
- package/src/persistent-lifecycle.ts +2 -2
- package/src/task-scheduling.ts +93 -0
|
@@ -20,19 +20,6 @@ var chain = (chain2) => async (elements) => {
|
|
|
20
20
|
|
|
21
21
|
// src/cleanup.ts
|
|
22
22
|
import { ComplexMap } from "@dxos/util";
|
|
23
|
-
function _define_property(obj, key, value) {
|
|
24
|
-
if (key in obj) {
|
|
25
|
-
Object.defineProperty(obj, key, {
|
|
26
|
-
value,
|
|
27
|
-
enumerable: true,
|
|
28
|
-
configurable: true,
|
|
29
|
-
writable: true
|
|
30
|
-
});
|
|
31
|
-
} else {
|
|
32
|
-
obj[key] = value;
|
|
33
|
-
}
|
|
34
|
-
return obj;
|
|
35
|
-
}
|
|
36
23
|
var combine = (...cleanupFns) => {
|
|
37
24
|
return () => {
|
|
38
25
|
cleanupFns.flat().forEach((cleanupFn) => cleanupFn());
|
|
@@ -51,6 +38,7 @@ var addEventListener = (target, type, listener, options) => {
|
|
|
51
38
|
return () => target.removeEventListener(type, listener, options);
|
|
52
39
|
};
|
|
53
40
|
var SubscriptionList = class {
|
|
41
|
+
_cleanups = [];
|
|
54
42
|
add(cb) {
|
|
55
43
|
this._cleanups.push(cb);
|
|
56
44
|
return this;
|
|
@@ -59,11 +47,12 @@ var SubscriptionList = class {
|
|
|
59
47
|
this._cleanups.forEach((cb) => cb());
|
|
60
48
|
this._cleanups.length = 0;
|
|
61
49
|
}
|
|
62
|
-
constructor() {
|
|
63
|
-
_define_property(this, "_cleanups", []);
|
|
64
|
-
}
|
|
65
50
|
};
|
|
66
51
|
var SubscriptionSet = class {
|
|
52
|
+
_cleanupMap;
|
|
53
|
+
constructor(keyProjection) {
|
|
54
|
+
this._cleanupMap = new ComplexMap(keyProjection);
|
|
55
|
+
}
|
|
67
56
|
set(key, cb) {
|
|
68
57
|
this._cleanupMap.set(key, cb);
|
|
69
58
|
return this;
|
|
@@ -72,36 +61,58 @@ var SubscriptionSet = class {
|
|
|
72
61
|
this._cleanupMap.forEach((cb) => cb());
|
|
73
62
|
this._cleanupMap.clear();
|
|
74
63
|
}
|
|
75
|
-
constructor(keyProjection) {
|
|
76
|
-
_define_property(this, "_cleanupMap", void 0);
|
|
77
|
-
this._cleanupMap = new ComplexMap(keyProjection);
|
|
78
|
-
}
|
|
79
64
|
};
|
|
80
65
|
|
|
81
66
|
// src/debounce.ts
|
|
82
|
-
var
|
|
67
|
+
var delay = (cb, delay2 = 100) => {
|
|
68
|
+
let pending = false;
|
|
69
|
+
return (...args) => {
|
|
70
|
+
if (pending) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
pending = true;
|
|
74
|
+
setTimeout(() => {
|
|
75
|
+
try {
|
|
76
|
+
cb(...args);
|
|
77
|
+
} finally {
|
|
78
|
+
pending = false;
|
|
79
|
+
}
|
|
80
|
+
}, delay2);
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
var debounce = (cb, delay2 = 100) => {
|
|
83
84
|
let t;
|
|
84
85
|
return (...args) => {
|
|
85
86
|
clearTimeout(t);
|
|
86
|
-
t = setTimeout(() => cb(...args),
|
|
87
|
+
t = setTimeout(() => cb(...args), delay2);
|
|
87
88
|
};
|
|
88
89
|
};
|
|
89
|
-
var throttle = (cb,
|
|
90
|
+
var throttle = (cb, delay2 = 100) => {
|
|
90
91
|
let lastCall = 0;
|
|
91
92
|
return (...args) => {
|
|
92
93
|
const now = Date.now();
|
|
93
|
-
if (now - lastCall >=
|
|
94
|
+
if (now - lastCall >= delay2) {
|
|
94
95
|
cb(...args);
|
|
95
96
|
lastCall = now;
|
|
96
97
|
}
|
|
97
98
|
};
|
|
98
99
|
};
|
|
99
|
-
var debounceAndThrottle = (cb,
|
|
100
|
-
|
|
101
|
-
|
|
100
|
+
var debounceAndThrottle = (cb, delay2 = 100) => {
|
|
101
|
+
let timeout2;
|
|
102
|
+
let lastCall = 0;
|
|
102
103
|
return (...args) => {
|
|
103
|
-
|
|
104
|
-
|
|
104
|
+
const now = Date.now();
|
|
105
|
+
const delta = now - lastCall;
|
|
106
|
+
clearTimeout(timeout2);
|
|
107
|
+
if (delta >= delay2) {
|
|
108
|
+
cb(...args);
|
|
109
|
+
lastCall = now;
|
|
110
|
+
} else {
|
|
111
|
+
timeout2 = setTimeout(() => {
|
|
112
|
+
cb(...args);
|
|
113
|
+
lastCall = Date.now();
|
|
114
|
+
}, delay2 - delta);
|
|
115
|
+
}
|
|
105
116
|
};
|
|
106
117
|
};
|
|
107
118
|
|
|
@@ -202,19 +213,6 @@ var waitForEvent = (eventEmitter, eventName, test, timeout2, error) => {
|
|
|
202
213
|
|
|
203
214
|
// src/events.ts
|
|
204
215
|
import { Context } from "@dxos/context";
|
|
205
|
-
function _define_property2(obj, key, value) {
|
|
206
|
-
if (key in obj) {
|
|
207
|
-
Object.defineProperty(obj, key, {
|
|
208
|
-
value,
|
|
209
|
-
enumerable: true,
|
|
210
|
-
configurable: true,
|
|
211
|
-
writable: true
|
|
212
|
-
});
|
|
213
|
-
} else {
|
|
214
|
-
obj[key] = value;
|
|
215
|
-
}
|
|
216
|
-
return obj;
|
|
217
|
-
}
|
|
218
216
|
var __dxlog_file = "/__w/dxos/dxos/packages/common/async/src/events.ts";
|
|
219
217
|
var DO_NOT_ERROR_ON_ASYNC_CALLBACK = true;
|
|
220
218
|
var Event = class _Event {
|
|
@@ -230,6 +228,8 @@ var Event = class _Event {
|
|
|
230
228
|
});
|
|
231
229
|
return event;
|
|
232
230
|
}
|
|
231
|
+
_listeners = /* @__PURE__ */ new Set();
|
|
232
|
+
_effects = /* @__PURE__ */ new Set();
|
|
233
233
|
/**
|
|
234
234
|
* Emit an event.
|
|
235
235
|
* In most cases should only be called by the class or entity containing the event.
|
|
@@ -471,12 +471,30 @@ var Event = class _Event {
|
|
|
471
471
|
handle.cleanup = void 0;
|
|
472
472
|
}
|
|
473
473
|
}
|
|
474
|
-
constructor() {
|
|
475
|
-
_define_property2(this, "_listeners", /* @__PURE__ */ new Set());
|
|
476
|
-
_define_property2(this, "_effects", /* @__PURE__ */ new Set());
|
|
477
|
-
}
|
|
478
474
|
};
|
|
479
475
|
var EventListener = class {
|
|
476
|
+
ctx;
|
|
477
|
+
once;
|
|
478
|
+
weak;
|
|
479
|
+
callback;
|
|
480
|
+
_clearDispose = void 0;
|
|
481
|
+
constructor(event, listener, ctx, once, weak) {
|
|
482
|
+
this.ctx = ctx;
|
|
483
|
+
this.once = once;
|
|
484
|
+
this.weak = weak;
|
|
485
|
+
this._clearDispose = ctx.onDispose(() => {
|
|
486
|
+
event._removeListener(this);
|
|
487
|
+
});
|
|
488
|
+
if (weak) {
|
|
489
|
+
this.callback = new WeakRef(listener);
|
|
490
|
+
weakListeners().registry?.register(listener, {
|
|
491
|
+
event: new WeakRef(event),
|
|
492
|
+
listener: this
|
|
493
|
+
}, this);
|
|
494
|
+
} else {
|
|
495
|
+
this.callback = listener;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
480
498
|
derefCallback() {
|
|
481
499
|
return this.weak ? this.callback.deref() : this.callback;
|
|
482
500
|
}
|
|
@@ -506,29 +524,6 @@ var EventListener = class {
|
|
|
506
524
|
this._clearDispose?.();
|
|
507
525
|
weakListeners().registry?.unregister(this);
|
|
508
526
|
}
|
|
509
|
-
constructor(event, listener, ctx, once, weak) {
|
|
510
|
-
_define_property2(this, "ctx", void 0);
|
|
511
|
-
_define_property2(this, "once", void 0);
|
|
512
|
-
_define_property2(this, "weak", void 0);
|
|
513
|
-
_define_property2(this, "callback", void 0);
|
|
514
|
-
_define_property2(this, "_clearDispose", void 0);
|
|
515
|
-
this.ctx = ctx;
|
|
516
|
-
this.once = once;
|
|
517
|
-
this.weak = weak;
|
|
518
|
-
this._clearDispose = void 0;
|
|
519
|
-
this._clearDispose = ctx.onDispose(() => {
|
|
520
|
-
event._removeListener(this);
|
|
521
|
-
});
|
|
522
|
-
if (weak) {
|
|
523
|
-
this.callback = new WeakRef(listener);
|
|
524
|
-
weakListeners().registry?.register(listener, {
|
|
525
|
-
event: new WeakRef(event),
|
|
526
|
-
listener: this
|
|
527
|
-
}, this);
|
|
528
|
-
} else {
|
|
529
|
-
this.callback = listener;
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
527
|
};
|
|
533
528
|
var weakListenersState = null;
|
|
534
529
|
var FINALIZATION_REGISTRY_SUPPORTED = !!globalThis.FinalizationRegistry;
|
|
@@ -538,9 +533,9 @@ var weakListeners = () => {
|
|
|
538
533
|
registry: void 0
|
|
539
534
|
};
|
|
540
535
|
}
|
|
541
|
-
weakListenersState
|
|
536
|
+
weakListenersState ??= new FinalizationRegistry(({ event, listener }) => {
|
|
542
537
|
event.deref()?._removeListener(listener);
|
|
543
|
-
})
|
|
538
|
+
});
|
|
544
539
|
return {
|
|
545
540
|
registry: weakListenersState
|
|
546
541
|
};
|
|
@@ -549,20 +544,10 @@ var weakListeners = () => {
|
|
|
549
544
|
// src/mutex.ts
|
|
550
545
|
import "@dxos/util";
|
|
551
546
|
import { warnAfterTimeout } from "@dxos/debug";
|
|
552
|
-
function _define_property3(obj, key, value) {
|
|
553
|
-
if (key in obj) {
|
|
554
|
-
Object.defineProperty(obj, key, {
|
|
555
|
-
value,
|
|
556
|
-
enumerable: true,
|
|
557
|
-
configurable: true,
|
|
558
|
-
writable: true
|
|
559
|
-
});
|
|
560
|
-
} else {
|
|
561
|
-
obj[key] = value;
|
|
562
|
-
}
|
|
563
|
-
return obj;
|
|
564
|
-
}
|
|
565
547
|
var Mutex = class {
|
|
548
|
+
_queue = Promise.resolve();
|
|
549
|
+
_queueLength = 0;
|
|
550
|
+
_tag = null;
|
|
566
551
|
get tag() {
|
|
567
552
|
return this._tag;
|
|
568
553
|
}
|
|
@@ -606,13 +591,12 @@ var Mutex = class {
|
|
|
606
591
|
guard.release();
|
|
607
592
|
}
|
|
608
593
|
}
|
|
609
|
-
constructor() {
|
|
610
|
-
_define_property3(this, "_queue", Promise.resolve());
|
|
611
|
-
_define_property3(this, "_queueLength", 0);
|
|
612
|
-
_define_property3(this, "_tag", null);
|
|
613
|
-
}
|
|
614
594
|
};
|
|
615
595
|
var MutexGuard = class {
|
|
596
|
+
_release;
|
|
597
|
+
constructor(_release) {
|
|
598
|
+
this._release = _release;
|
|
599
|
+
}
|
|
616
600
|
/**
|
|
617
601
|
* Releases the lock.
|
|
618
602
|
*/
|
|
@@ -622,10 +606,6 @@ var MutexGuard = class {
|
|
|
622
606
|
[Symbol.dispose]() {
|
|
623
607
|
this.release();
|
|
624
608
|
}
|
|
625
|
-
constructor(_release) {
|
|
626
|
-
_define_property3(this, "_release", void 0);
|
|
627
|
-
this._release = _release;
|
|
628
|
-
}
|
|
629
609
|
};
|
|
630
610
|
var classMutexSymbol = Symbol("class-mutex");
|
|
631
611
|
var FORCE_DISABLE_WARNING = false;
|
|
@@ -633,8 +613,7 @@ var enableWarning = !FORCE_DISABLE_WARNING && globalThis.mochaExecutor;
|
|
|
633
613
|
var synchronized = (target, propertyName, descriptor) => {
|
|
634
614
|
const method = descriptor.value;
|
|
635
615
|
descriptor.value = async function synchronizedMethod(...args) {
|
|
636
|
-
|
|
637
|
-
const mutex = this[_classMutexSymbol = classMutexSymbol] ?? (this[_classMutexSymbol] = new Mutex());
|
|
616
|
+
const mutex = this[classMutexSymbol] ??= new Mutex();
|
|
638
617
|
const tag = `${target.constructor.name}.${propertyName}`;
|
|
639
618
|
let guard;
|
|
640
619
|
if (!enableWarning) {
|
|
@@ -659,19 +638,6 @@ import PushStream from "zen-push";
|
|
|
659
638
|
|
|
660
639
|
// src/trigger.ts
|
|
661
640
|
import { invariant } from "@dxos/invariant";
|
|
662
|
-
function _define_property4(obj, key, value) {
|
|
663
|
-
if (key in obj) {
|
|
664
|
-
Object.defineProperty(obj, key, {
|
|
665
|
-
value,
|
|
666
|
-
enumerable: true,
|
|
667
|
-
configurable: true,
|
|
668
|
-
writable: true
|
|
669
|
-
});
|
|
670
|
-
} else {
|
|
671
|
-
obj[key] = value;
|
|
672
|
-
}
|
|
673
|
-
return obj;
|
|
674
|
-
}
|
|
675
641
|
var __dxlog_file2 = "/__w/dxos/dxos/packages/common/async/src/trigger.ts";
|
|
676
642
|
var trigger = (timeout2) => {
|
|
677
643
|
let callback;
|
|
@@ -695,6 +661,17 @@ var TriggerState = /* @__PURE__ */ (function(TriggerState2) {
|
|
|
695
661
|
return TriggerState2;
|
|
696
662
|
})({});
|
|
697
663
|
var Trigger = class {
|
|
664
|
+
_options;
|
|
665
|
+
_promise;
|
|
666
|
+
_resolve;
|
|
667
|
+
_reject;
|
|
668
|
+
_state = "WAITING";
|
|
669
|
+
constructor(_options = {
|
|
670
|
+
autoReset: false
|
|
671
|
+
}) {
|
|
672
|
+
this._options = _options;
|
|
673
|
+
this.reset();
|
|
674
|
+
}
|
|
698
675
|
get state() {
|
|
699
676
|
return this._state;
|
|
700
677
|
}
|
|
@@ -751,18 +728,6 @@ var Trigger = class {
|
|
|
751
728
|
}
|
|
752
729
|
return this;
|
|
753
730
|
}
|
|
754
|
-
constructor(_options = {
|
|
755
|
-
autoReset: false
|
|
756
|
-
}) {
|
|
757
|
-
_define_property4(this, "_options", void 0);
|
|
758
|
-
_define_property4(this, "_promise", void 0);
|
|
759
|
-
_define_property4(this, "_resolve", void 0);
|
|
760
|
-
_define_property4(this, "_reject", void 0);
|
|
761
|
-
_define_property4(this, "_state", void 0);
|
|
762
|
-
this._options = _options;
|
|
763
|
-
this._state = "WAITING";
|
|
764
|
-
this.reset();
|
|
765
|
-
}
|
|
766
731
|
};
|
|
767
732
|
var latch = ({ count = 1, timeout: timeout2 } = {}) => {
|
|
768
733
|
invariant(count >= 0, void 0, {
|
|
@@ -812,20 +777,16 @@ var latch = ({ count = 1, timeout: timeout2 } = {}) => {
|
|
|
812
777
|
};
|
|
813
778
|
|
|
814
779
|
// src/observable.ts
|
|
815
|
-
function _define_property5(obj, key, value) {
|
|
816
|
-
if (key in obj) {
|
|
817
|
-
Object.defineProperty(obj, key, {
|
|
818
|
-
value,
|
|
819
|
-
enumerable: true,
|
|
820
|
-
configurable: true,
|
|
821
|
-
writable: true
|
|
822
|
-
});
|
|
823
|
-
} else {
|
|
824
|
-
obj[key] = value;
|
|
825
|
-
}
|
|
826
|
-
return obj;
|
|
827
|
-
}
|
|
828
780
|
var MulticastObservable = class _MulticastObservable extends Observable {
|
|
781
|
+
_value;
|
|
782
|
+
_observers = /* @__PURE__ */ new Set();
|
|
783
|
+
_observable;
|
|
784
|
+
_completed = new Trigger();
|
|
785
|
+
constructor(subscriber, _value) {
|
|
786
|
+
super((observer) => this._subscribe(observer)), this._value = _value;
|
|
787
|
+
this._observable = typeof subscriber === "function" ? new Observable(subscriber) : subscriber;
|
|
788
|
+
this._observable.subscribe(this._handlers);
|
|
789
|
+
}
|
|
829
790
|
static from(value, initialValue) {
|
|
830
791
|
if ("emit" in value) {
|
|
831
792
|
return new _MulticastObservable((observer) => {
|
|
@@ -906,42 +867,30 @@ var MulticastObservable = class _MulticastObservable extends Observable {
|
|
|
906
867
|
this._observers.delete(observer);
|
|
907
868
|
};
|
|
908
869
|
}
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
};
|
|
923
|
-
this._observable = typeof subscriber === "function" ? new Observable(subscriber) : subscriber;
|
|
924
|
-
this._observable.subscribe(this._handlers);
|
|
925
|
-
}
|
|
870
|
+
_handlers = {
|
|
871
|
+
next: (value) => {
|
|
872
|
+
this._value = value;
|
|
873
|
+
this._observers.forEach((observer) => observer.next?.(value));
|
|
874
|
+
},
|
|
875
|
+
error: (err) => {
|
|
876
|
+
this._observers.forEach((observer) => observer.error?.(err));
|
|
877
|
+
},
|
|
878
|
+
complete: () => {
|
|
879
|
+
this._completed.wake();
|
|
880
|
+
this._observers.forEach((observer) => observer.complete?.());
|
|
881
|
+
}
|
|
882
|
+
};
|
|
926
883
|
};
|
|
927
884
|
var EMPTY_OBSERVABLE = MulticastObservable.of(null);
|
|
928
885
|
|
|
929
886
|
// src/observable-value.ts
|
|
930
887
|
import { createSetDispatch } from "@dxos/util";
|
|
931
|
-
function _define_property6(obj, key, value) {
|
|
932
|
-
if (key in obj) {
|
|
933
|
-
Object.defineProperty(obj, key, {
|
|
934
|
-
value,
|
|
935
|
-
enumerable: true,
|
|
936
|
-
configurable: true,
|
|
937
|
-
writable: true
|
|
938
|
-
});
|
|
939
|
-
} else {
|
|
940
|
-
obj[key] = value;
|
|
941
|
-
}
|
|
942
|
-
return obj;
|
|
943
|
-
}
|
|
944
888
|
var ObservableProvider = class {
|
|
889
|
+
_handlers = /* @__PURE__ */ new Set();
|
|
890
|
+
_proxy = createSetDispatch({
|
|
891
|
+
handlers: this._handlers
|
|
892
|
+
});
|
|
893
|
+
_value;
|
|
945
894
|
/**
|
|
946
895
|
* Proxy used to dispatch callbacks to each subscription.
|
|
947
896
|
*/
|
|
@@ -960,15 +909,13 @@ var ObservableProvider = class {
|
|
|
960
909
|
this._handlers.delete(handler);
|
|
961
910
|
};
|
|
962
911
|
}
|
|
963
|
-
constructor() {
|
|
964
|
-
_define_property6(this, "_handlers", /* @__PURE__ */ new Set());
|
|
965
|
-
_define_property6(this, "_proxy", createSetDispatch({
|
|
966
|
-
handlers: this._handlers
|
|
967
|
-
}));
|
|
968
|
-
_define_property6(this, "_value", void 0);
|
|
969
|
-
}
|
|
970
912
|
};
|
|
971
913
|
var CancellableObservableProvider = class extends ObservableProvider {
|
|
914
|
+
_handleCancel;
|
|
915
|
+
_cancelled = false;
|
|
916
|
+
constructor(_handleCancel) {
|
|
917
|
+
super(), this._handleCancel = _handleCancel;
|
|
918
|
+
}
|
|
972
919
|
get cancelled() {
|
|
973
920
|
return this._cancelled;
|
|
974
921
|
}
|
|
@@ -980,9 +927,6 @@ var CancellableObservableProvider = class extends ObservableProvider {
|
|
|
980
927
|
await this._handleCancel?.();
|
|
981
928
|
this.callback.onCancelled?.();
|
|
982
929
|
}
|
|
983
|
-
constructor(_handleCancel) {
|
|
984
|
-
super(), _define_property6(this, "_handleCancel", void 0), _define_property6(this, "_cancelled", void 0), this._handleCancel = _handleCancel, this._cancelled = false;
|
|
985
|
-
}
|
|
986
930
|
};
|
|
987
931
|
|
|
988
932
|
// src/persistent-lifecycle.ts
|
|
@@ -1079,20 +1023,16 @@ if (enabled) {
|
|
|
1079
1023
|
}
|
|
1080
1024
|
|
|
1081
1025
|
// src/task-scheduling.ts
|
|
1082
|
-
function _define_property7(obj, key, value) {
|
|
1083
|
-
if (key in obj) {
|
|
1084
|
-
Object.defineProperty(obj, key, {
|
|
1085
|
-
value,
|
|
1086
|
-
enumerable: true,
|
|
1087
|
-
configurable: true,
|
|
1088
|
-
writable: true
|
|
1089
|
-
});
|
|
1090
|
-
} else {
|
|
1091
|
-
obj[key] = value;
|
|
1092
|
-
}
|
|
1093
|
-
return obj;
|
|
1094
|
-
}
|
|
1095
1026
|
var DeferredTask = class {
|
|
1027
|
+
_ctx;
|
|
1028
|
+
_callback;
|
|
1029
|
+
_scheduled = false;
|
|
1030
|
+
_currentTask = null;
|
|
1031
|
+
_nextTask = new Trigger();
|
|
1032
|
+
constructor(_ctx, _callback) {
|
|
1033
|
+
this._ctx = _ctx;
|
|
1034
|
+
this._callback = _callback;
|
|
1035
|
+
}
|
|
1096
1036
|
get scheduled() {
|
|
1097
1037
|
return this._scheduled;
|
|
1098
1038
|
}
|
|
@@ -1131,17 +1071,77 @@ var DeferredTask = class {
|
|
|
1131
1071
|
async join() {
|
|
1132
1072
|
await this._currentTask;
|
|
1133
1073
|
}
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
this
|
|
1143
|
-
|
|
1144
|
-
|
|
1074
|
+
};
|
|
1075
|
+
var AsyncTask = class {
|
|
1076
|
+
#callback;
|
|
1077
|
+
#ctx = void 0;
|
|
1078
|
+
#scheduled = false;
|
|
1079
|
+
#currentTask = null;
|
|
1080
|
+
#nextTask = new Trigger();
|
|
1081
|
+
constructor(callback) {
|
|
1082
|
+
this.#callback = callback;
|
|
1083
|
+
}
|
|
1084
|
+
get scheduled() {
|
|
1085
|
+
return this.#scheduled;
|
|
1086
|
+
}
|
|
1087
|
+
/**
|
|
1088
|
+
* Context of the resource that owns the task.
|
|
1089
|
+
* When the context is disposed, the task is cancelled and cannot be scheduled again.
|
|
1090
|
+
*/
|
|
1091
|
+
// TODO(dmaretskyi): We don't really need to pass ctx in here, since close will also signal dispose.
|
|
1092
|
+
open(ctx) {
|
|
1093
|
+
this.#ctx = ctx;
|
|
1094
|
+
}
|
|
1095
|
+
/**
|
|
1096
|
+
* Closes the task and waits for it to finish if it is running.
|
|
1097
|
+
*/
|
|
1098
|
+
async close() {
|
|
1099
|
+
this.#ctx = void 0;
|
|
1100
|
+
await this.join();
|
|
1101
|
+
}
|
|
1102
|
+
[Symbol.asyncDispose]() {
|
|
1103
|
+
return this.close();
|
|
1104
|
+
}
|
|
1105
|
+
/**
|
|
1106
|
+
* Schedule the task to run asynchronously.
|
|
1107
|
+
*/
|
|
1108
|
+
schedule() {
|
|
1109
|
+
if (!this.#ctx || this.#ctx.disposed) {
|
|
1110
|
+
throw new Error("AsyncTask not open");
|
|
1111
|
+
}
|
|
1112
|
+
if (this.#scheduled) {
|
|
1113
|
+
return;
|
|
1114
|
+
}
|
|
1115
|
+
scheduleTask(this.#ctx, async () => {
|
|
1116
|
+
await this.#currentTask;
|
|
1117
|
+
if (!this.#ctx || this.#ctx.disposed) {
|
|
1118
|
+
return;
|
|
1119
|
+
}
|
|
1120
|
+
this.#scheduled = false;
|
|
1121
|
+
const completionTrigger = this.#nextTask;
|
|
1122
|
+
this.#nextTask = new Trigger();
|
|
1123
|
+
this.#currentTask = runInContextAsync(this.#ctx, () => this.#callback()).then(() => {
|
|
1124
|
+
completionTrigger.wake();
|
|
1125
|
+
});
|
|
1126
|
+
});
|
|
1127
|
+
this.#scheduled = true;
|
|
1128
|
+
}
|
|
1129
|
+
/**
|
|
1130
|
+
* Schedule the task to run and wait for it to finish.
|
|
1131
|
+
*/
|
|
1132
|
+
async runBlocking() {
|
|
1133
|
+
if (this.#ctx?.disposed) {
|
|
1134
|
+
throw new ContextDisposedError2();
|
|
1135
|
+
}
|
|
1136
|
+
this.schedule();
|
|
1137
|
+
await this.#nextTask.wait();
|
|
1138
|
+
}
|
|
1139
|
+
/**
|
|
1140
|
+
* Waits for the current task to finish if it is running.
|
|
1141
|
+
* Does not schedule a new task.
|
|
1142
|
+
*/
|
|
1143
|
+
async join() {
|
|
1144
|
+
await this.#currentTask;
|
|
1145
1145
|
}
|
|
1146
1146
|
};
|
|
1147
1147
|
var runInContext = (ctx, fn) => {
|
|
@@ -1223,19 +1223,6 @@ var scheduleExponentialBackoffTaskInterval = (ctx, task, initialInterval) => {
|
|
|
1223
1223
|
};
|
|
1224
1224
|
|
|
1225
1225
|
// src/persistent-lifecycle.ts
|
|
1226
|
-
function _define_property8(obj, key, value) {
|
|
1227
|
-
if (key in obj) {
|
|
1228
|
-
Object.defineProperty(obj, key, {
|
|
1229
|
-
value,
|
|
1230
|
-
enumerable: true,
|
|
1231
|
-
configurable: true,
|
|
1232
|
-
writable: true
|
|
1233
|
-
});
|
|
1234
|
-
} else {
|
|
1235
|
-
obj[key] = value;
|
|
1236
|
-
}
|
|
1237
|
-
return obj;
|
|
1238
|
-
}
|
|
1239
1226
|
function _ts_decorate(decorators, target, key, desc) {
|
|
1240
1227
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
1241
1228
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -1246,6 +1233,20 @@ var __dxlog_file4 = "/__w/dxos/dxos/packages/common/async/src/persistent-lifecyc
|
|
|
1246
1233
|
var INIT_RESTART_DELAY = 100;
|
|
1247
1234
|
var DEFAULT_MAX_RESTART_DELAY = 5e3;
|
|
1248
1235
|
var PersistentLifecycle = class extends Resource {
|
|
1236
|
+
_start;
|
|
1237
|
+
_stop;
|
|
1238
|
+
_onRestart;
|
|
1239
|
+
_maxRestartDelay;
|
|
1240
|
+
_currentState = void 0;
|
|
1241
|
+
_restartTask = void 0;
|
|
1242
|
+
_restartAfter = 0;
|
|
1243
|
+
constructor({ start, stop, onRestart, maxRestartDelay = DEFAULT_MAX_RESTART_DELAY }) {
|
|
1244
|
+
super();
|
|
1245
|
+
this._start = start;
|
|
1246
|
+
this._stop = stop;
|
|
1247
|
+
this._onRestart = onRestart;
|
|
1248
|
+
this._maxRestartDelay = maxRestartDelay;
|
|
1249
|
+
}
|
|
1249
1250
|
get state() {
|
|
1250
1251
|
return this._currentState;
|
|
1251
1252
|
}
|
|
@@ -1328,13 +1329,6 @@ var PersistentLifecycle = class extends Resource {
|
|
|
1328
1329
|
}
|
|
1329
1330
|
this._restartTask.schedule();
|
|
1330
1331
|
}
|
|
1331
|
-
constructor({ start, stop, onRestart, maxRestartDelay = DEFAULT_MAX_RESTART_DELAY }) {
|
|
1332
|
-
super(), _define_property8(this, "_start", void 0), _define_property8(this, "_stop", void 0), _define_property8(this, "_onRestart", void 0), _define_property8(this, "_maxRestartDelay", void 0), _define_property8(this, "_currentState", void 0), _define_property8(this, "_restartTask", void 0), _define_property8(this, "_restartAfter", 0);
|
|
1333
|
-
this._start = start;
|
|
1334
|
-
this._stop = stop;
|
|
1335
|
-
this._onRestart = onRestart;
|
|
1336
|
-
this._maxRestartDelay = maxRestartDelay;
|
|
1337
|
-
}
|
|
1338
1332
|
};
|
|
1339
1333
|
_ts_decorate([
|
|
1340
1334
|
synchronized
|
|
@@ -1453,19 +1447,6 @@ var streamToArray = (stream) => {
|
|
|
1453
1447
|
|
|
1454
1448
|
// src/test-stream.ts
|
|
1455
1449
|
import { Duplex } from "@dxos/node-std/stream";
|
|
1456
|
-
function _define_property9(obj, key, value) {
|
|
1457
|
-
if (key in obj) {
|
|
1458
|
-
Object.defineProperty(obj, key, {
|
|
1459
|
-
value,
|
|
1460
|
-
enumerable: true,
|
|
1461
|
-
configurable: true,
|
|
1462
|
-
writable: true
|
|
1463
|
-
});
|
|
1464
|
-
} else {
|
|
1465
|
-
obj[key] = value;
|
|
1466
|
-
}
|
|
1467
|
-
return obj;
|
|
1468
|
-
}
|
|
1469
1450
|
var TestStream = class extends Duplex {
|
|
1470
1451
|
static async assertConnectivity(stream1, stream2, { timeout: timeout2 = 200 } = {}) {
|
|
1471
1452
|
stream1.push("ping");
|
|
@@ -1479,6 +1460,8 @@ var TestStream = class extends Duplex {
|
|
|
1479
1460
|
})
|
|
1480
1461
|
]);
|
|
1481
1462
|
}
|
|
1463
|
+
_received = Buffer.alloc(0);
|
|
1464
|
+
_onWrite = new Event();
|
|
1482
1465
|
_write(chunk, encoding, callback) {
|
|
1483
1466
|
this._received = Buffer.concat([
|
|
1484
1467
|
this._received,
|
|
@@ -1493,9 +1476,6 @@ var TestStream = class extends Duplex {
|
|
|
1493
1476
|
const dataBuffer = Buffer.isBuffer(data) ? data : Buffer.from(data);
|
|
1494
1477
|
return asyncTimeout(this._onWrite.waitForCondition(() => this._received.equals(dataBuffer)), timeout2);
|
|
1495
1478
|
}
|
|
1496
|
-
constructor(...args) {
|
|
1497
|
-
super(...args), _define_property9(this, "_received", Buffer.alloc(0)), _define_property9(this, "_onWrite", new Event());
|
|
1498
|
-
}
|
|
1499
1479
|
};
|
|
1500
1480
|
|
|
1501
1481
|
// src/testing.ts
|
|
@@ -1558,20 +1538,14 @@ var untilError = (cb) => {
|
|
|
1558
1538
|
};
|
|
1559
1539
|
|
|
1560
1540
|
// src/timer.ts
|
|
1561
|
-
function _define_property10(obj, key, value) {
|
|
1562
|
-
if (key in obj) {
|
|
1563
|
-
Object.defineProperty(obj, key, {
|
|
1564
|
-
value,
|
|
1565
|
-
enumerable: true,
|
|
1566
|
-
configurable: true,
|
|
1567
|
-
writable: true
|
|
1568
|
-
});
|
|
1569
|
-
} else {
|
|
1570
|
-
obj[key] = value;
|
|
1571
|
-
}
|
|
1572
|
-
return obj;
|
|
1573
|
-
}
|
|
1574
1541
|
var Timer = class {
|
|
1542
|
+
_callback;
|
|
1543
|
+
_state = new Event();
|
|
1544
|
+
_timer;
|
|
1545
|
+
_count = 0;
|
|
1546
|
+
constructor(_callback) {
|
|
1547
|
+
this._callback = _callback;
|
|
1548
|
+
}
|
|
1575
1549
|
get state() {
|
|
1576
1550
|
return this._state;
|
|
1577
1551
|
}
|
|
@@ -1611,33 +1585,29 @@ var Timer = class {
|
|
|
1611
1585
|
this._state.emit(false);
|
|
1612
1586
|
return this;
|
|
1613
1587
|
}
|
|
1614
|
-
constructor(_callback) {
|
|
1615
|
-
_define_property10(this, "_callback", void 0);
|
|
1616
|
-
_define_property10(this, "_state", void 0);
|
|
1617
|
-
_define_property10(this, "_timer", void 0);
|
|
1618
|
-
_define_property10(this, "_count", void 0);
|
|
1619
|
-
this._callback = _callback;
|
|
1620
|
-
this._state = new Event();
|
|
1621
|
-
this._count = 0;
|
|
1622
|
-
}
|
|
1623
1588
|
};
|
|
1624
1589
|
|
|
1625
1590
|
// src/update-scheduler.ts
|
|
1626
|
-
function _define_property11(obj, key, value) {
|
|
1627
|
-
if (key in obj) {
|
|
1628
|
-
Object.defineProperty(obj, key, {
|
|
1629
|
-
value,
|
|
1630
|
-
enumerable: true,
|
|
1631
|
-
configurable: true,
|
|
1632
|
-
writable: true
|
|
1633
|
-
});
|
|
1634
|
-
} else {
|
|
1635
|
-
obj[key] = value;
|
|
1636
|
-
}
|
|
1637
|
-
return obj;
|
|
1638
|
-
}
|
|
1639
1591
|
var TIME_PERIOD = 1e3;
|
|
1640
1592
|
var UpdateScheduler = class {
|
|
1593
|
+
_ctx;
|
|
1594
|
+
_callback;
|
|
1595
|
+
_params;
|
|
1596
|
+
/**
|
|
1597
|
+
* Promise that resolves when the callback is done.
|
|
1598
|
+
* Never rejects.
|
|
1599
|
+
*/
|
|
1600
|
+
_promise = null;
|
|
1601
|
+
_scheduled = false;
|
|
1602
|
+
_lastUpdateTime = -TIME_PERIOD;
|
|
1603
|
+
constructor(_ctx, _callback, _params = {}) {
|
|
1604
|
+
this._ctx = _ctx;
|
|
1605
|
+
this._callback = _callback;
|
|
1606
|
+
this._params = _params;
|
|
1607
|
+
_ctx.onDispose(async () => {
|
|
1608
|
+
await this._promise;
|
|
1609
|
+
});
|
|
1610
|
+
}
|
|
1641
1611
|
trigger() {
|
|
1642
1612
|
if (this._scheduled) {
|
|
1643
1613
|
return;
|
|
@@ -1646,13 +1616,13 @@ var UpdateScheduler = class {
|
|
|
1646
1616
|
await this._promise;
|
|
1647
1617
|
if (this._params.maxFrequency) {
|
|
1648
1618
|
const now = performance.now();
|
|
1649
|
-
const
|
|
1650
|
-
if (
|
|
1619
|
+
const delay2 = this._lastUpdateTime + TIME_PERIOD / this._params.maxFrequency - now;
|
|
1620
|
+
if (delay2 > 0) {
|
|
1651
1621
|
await new Promise((resolve) => {
|
|
1652
1622
|
const timeoutId = setTimeout(() => {
|
|
1653
1623
|
clearContext();
|
|
1654
1624
|
resolve();
|
|
1655
|
-
},
|
|
1625
|
+
}, delay2);
|
|
1656
1626
|
const clearContext = this._ctx.onDispose(() => {
|
|
1657
1627
|
clearTimeout(timeoutId);
|
|
1658
1628
|
resolve();
|
|
@@ -1694,25 +1664,9 @@ var UpdateScheduler = class {
|
|
|
1694
1664
|
this._promise = this._callback();
|
|
1695
1665
|
await this._promise;
|
|
1696
1666
|
}
|
|
1697
|
-
constructor(_ctx, _callback, _params = {}) {
|
|
1698
|
-
_define_property11(this, "_ctx", void 0);
|
|
1699
|
-
_define_property11(this, "_callback", void 0);
|
|
1700
|
-
_define_property11(this, "_params", void 0);
|
|
1701
|
-
_define_property11(this, "_promise", void 0);
|
|
1702
|
-
_define_property11(this, "_scheduled", void 0);
|
|
1703
|
-
_define_property11(this, "_lastUpdateTime", void 0);
|
|
1704
|
-
this._ctx = _ctx;
|
|
1705
|
-
this._callback = _callback;
|
|
1706
|
-
this._params = _params;
|
|
1707
|
-
this._promise = null;
|
|
1708
|
-
this._scheduled = false;
|
|
1709
|
-
this._lastUpdateTime = -TIME_PERIOD;
|
|
1710
|
-
_ctx.onDispose(async () => {
|
|
1711
|
-
await this._promise;
|
|
1712
|
-
});
|
|
1713
|
-
}
|
|
1714
1667
|
};
|
|
1715
1668
|
export {
|
|
1669
|
+
AsyncTask,
|
|
1716
1670
|
CancellableObservableProvider,
|
|
1717
1671
|
DeferredTask,
|
|
1718
1672
|
Event,
|
|
@@ -1739,6 +1693,7 @@ export {
|
|
|
1739
1693
|
combine,
|
|
1740
1694
|
debounce,
|
|
1741
1695
|
debounceAndThrottle,
|
|
1696
|
+
delay,
|
|
1742
1697
|
dumpLeaks,
|
|
1743
1698
|
interval,
|
|
1744
1699
|
latch,
|