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