@dxos/async 0.8.4-main.406dc2a → 0.8.4-main.52d7546f51
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 +258 -298
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +258 -298
- 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 +2 -1
- 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 +29 -1
- 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 +3 -2
- package/src/debounce.ts +52 -4
- package/src/persistent-lifecycle.ts +2 -2
- package/src/task-scheduling.ts +95 -1
|
@@ -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,19 +40,21 @@ var addEventListener = (target, type, listener, options) => {
|
|
|
53
40
|
return () => target.removeEventListener(type, listener, options);
|
|
54
41
|
};
|
|
55
42
|
var SubscriptionList = class {
|
|
56
|
-
|
|
57
|
-
|
|
43
|
+
_cleanups = [];
|
|
44
|
+
add(...cb) {
|
|
45
|
+
this._cleanups.push(...cb);
|
|
58
46
|
return this;
|
|
59
47
|
}
|
|
60
48
|
clear() {
|
|
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
|
|
@@ -993,7 +937,7 @@ import { warnAfterTimeout as warnAfterTimeout2 } from "@dxos/debug";
|
|
|
993
937
|
import { log as log2 } from "@dxos/log";
|
|
994
938
|
|
|
995
939
|
// src/task-scheduling.ts
|
|
996
|
-
import { ContextDisposedError as ContextDisposedError2 } from "@dxos/context";
|
|
940
|
+
import { Context as Context2, ContextDisposedError as ContextDisposedError2 } from "@dxos/context";
|
|
997
941
|
import { StackTrace as StackTrace2 } from "@dxos/debug";
|
|
998
942
|
|
|
999
943
|
// src/track-leaks.ts
|
|
@@ -1081,20 +1025,17 @@ if (enabled) {
|
|
|
1081
1025
|
}
|
|
1082
1026
|
|
|
1083
1027
|
// src/task-scheduling.ts
|
|
1084
|
-
|
|
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
|
-
}
|
|
1028
|
+
var __dxlog_file4 = "/__w/dxos/dxos/packages/common/async/src/task-scheduling.ts";
|
|
1097
1029
|
var DeferredTask = class {
|
|
1030
|
+
_ctx;
|
|
1031
|
+
_callback;
|
|
1032
|
+
_scheduled = false;
|
|
1033
|
+
_currentTask = null;
|
|
1034
|
+
_nextTask = new Trigger();
|
|
1035
|
+
constructor(_ctx, _callback) {
|
|
1036
|
+
this._ctx = _ctx;
|
|
1037
|
+
this._callback = _callback;
|
|
1038
|
+
}
|
|
1098
1039
|
get scheduled() {
|
|
1099
1040
|
return this._scheduled;
|
|
1100
1041
|
}
|
|
@@ -1133,17 +1074,81 @@ var DeferredTask = class {
|
|
|
1133
1074
|
async join() {
|
|
1134
1075
|
await this._currentTask;
|
|
1135
1076
|
}
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
this
|
|
1145
|
-
|
|
1146
|
-
|
|
1077
|
+
};
|
|
1078
|
+
var AsyncTask = class {
|
|
1079
|
+
#callback;
|
|
1080
|
+
#ctx = void 0;
|
|
1081
|
+
#scheduled = false;
|
|
1082
|
+
#currentTask = null;
|
|
1083
|
+
#nextTask = new Trigger();
|
|
1084
|
+
constructor(callback) {
|
|
1085
|
+
this.#callback = callback;
|
|
1086
|
+
}
|
|
1087
|
+
get scheduled() {
|
|
1088
|
+
return this.#scheduled;
|
|
1089
|
+
}
|
|
1090
|
+
/**
|
|
1091
|
+
* Context of the resource that owns the task.
|
|
1092
|
+
* When the context is disposed, the task is cancelled and cannot be scheduled again.
|
|
1093
|
+
*/
|
|
1094
|
+
open() {
|
|
1095
|
+
this.#ctx = new Context2(void 0, {
|
|
1096
|
+
F: __dxlog_file4,
|
|
1097
|
+
L: 102
|
|
1098
|
+
});
|
|
1099
|
+
}
|
|
1100
|
+
/**
|
|
1101
|
+
* Closes the task and waits for it to finish if it is running.
|
|
1102
|
+
*/
|
|
1103
|
+
async close() {
|
|
1104
|
+
await this.#ctx?.dispose();
|
|
1105
|
+
await this.join();
|
|
1106
|
+
this.#ctx = void 0;
|
|
1107
|
+
}
|
|
1108
|
+
[Symbol.asyncDispose]() {
|
|
1109
|
+
return this.close();
|
|
1110
|
+
}
|
|
1111
|
+
/**
|
|
1112
|
+
* Schedule the task to run asynchronously.
|
|
1113
|
+
*/
|
|
1114
|
+
// TODO(dmaretskyi): Add scheduleAt. Where the earlier time will override the later one.
|
|
1115
|
+
schedule() {
|
|
1116
|
+
if (!this.#ctx || this.#ctx.disposed) {
|
|
1117
|
+
throw new Error("AsyncTask not open");
|
|
1118
|
+
}
|
|
1119
|
+
if (this.#scheduled) {
|
|
1120
|
+
return;
|
|
1121
|
+
}
|
|
1122
|
+
scheduleTask(this.#ctx, async () => {
|
|
1123
|
+
await this.#currentTask;
|
|
1124
|
+
if (!this.#ctx || this.#ctx.disposed) {
|
|
1125
|
+
return;
|
|
1126
|
+
}
|
|
1127
|
+
this.#scheduled = false;
|
|
1128
|
+
const completionTrigger = this.#nextTask;
|
|
1129
|
+
this.#nextTask = new Trigger();
|
|
1130
|
+
this.#currentTask = runInContextAsync(this.#ctx, () => this.#callback()).then(() => {
|
|
1131
|
+
completionTrigger.wake();
|
|
1132
|
+
});
|
|
1133
|
+
});
|
|
1134
|
+
this.#scheduled = true;
|
|
1135
|
+
}
|
|
1136
|
+
/**
|
|
1137
|
+
* Schedule the task to run and wait for it to finish.
|
|
1138
|
+
*/
|
|
1139
|
+
async runBlocking() {
|
|
1140
|
+
if (this.#ctx?.disposed) {
|
|
1141
|
+
throw new ContextDisposedError2();
|
|
1142
|
+
}
|
|
1143
|
+
this.schedule();
|
|
1144
|
+
await this.#nextTask.wait();
|
|
1145
|
+
}
|
|
1146
|
+
/**
|
|
1147
|
+
* Waits for the current task to finish if it is running.
|
|
1148
|
+
* Does not schedule a new task.
|
|
1149
|
+
*/
|
|
1150
|
+
async join() {
|
|
1151
|
+
await this.#currentTask;
|
|
1147
1152
|
}
|
|
1148
1153
|
};
|
|
1149
1154
|
var runInContext = (ctx, fn) => {
|
|
@@ -1225,29 +1230,30 @@ var scheduleExponentialBackoffTaskInterval = (ctx, task, initialInterval) => {
|
|
|
1225
1230
|
};
|
|
1226
1231
|
|
|
1227
1232
|
// 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
1233
|
function _ts_decorate(decorators, target, key, desc) {
|
|
1242
1234
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
1243
1235
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
1244
1236
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
1245
1237
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
1246
1238
|
}
|
|
1247
|
-
var
|
|
1239
|
+
var __dxlog_file5 = "/__w/dxos/dxos/packages/common/async/src/persistent-lifecycle.ts";
|
|
1248
1240
|
var INIT_RESTART_DELAY = 100;
|
|
1249
1241
|
var DEFAULT_MAX_RESTART_DELAY = 5e3;
|
|
1250
1242
|
var PersistentLifecycle = class extends Resource {
|
|
1243
|
+
_start;
|
|
1244
|
+
_stop;
|
|
1245
|
+
_onRestart;
|
|
1246
|
+
_maxRestartDelay;
|
|
1247
|
+
_currentState = void 0;
|
|
1248
|
+
_restartTask = void 0;
|
|
1249
|
+
_restartAfter = 0;
|
|
1250
|
+
constructor({ start, stop, onRestart, maxRestartDelay = DEFAULT_MAX_RESTART_DELAY }) {
|
|
1251
|
+
super();
|
|
1252
|
+
this._start = start;
|
|
1253
|
+
this._stop = stop;
|
|
1254
|
+
this._onRestart = onRestart;
|
|
1255
|
+
this._maxRestartDelay = maxRestartDelay;
|
|
1256
|
+
}
|
|
1251
1257
|
get state() {
|
|
1252
1258
|
return this._currentState;
|
|
1253
1259
|
}
|
|
@@ -1259,7 +1265,7 @@ var PersistentLifecycle = class extends Resource {
|
|
|
1259
1265
|
log2.warn("Restart failed", {
|
|
1260
1266
|
err
|
|
1261
1267
|
}, {
|
|
1262
|
-
F:
|
|
1268
|
+
F: __dxlog_file5,
|
|
1263
1269
|
L: 72,
|
|
1264
1270
|
S: this,
|
|
1265
1271
|
C: (f, a) => f(...a)
|
|
@@ -1271,7 +1277,7 @@ var PersistentLifecycle = class extends Resource {
|
|
|
1271
1277
|
log2.warn("Start failed", {
|
|
1272
1278
|
err
|
|
1273
1279
|
}, {
|
|
1274
|
-
F:
|
|
1280
|
+
F: __dxlog_file5,
|
|
1275
1281
|
L: 78,
|
|
1276
1282
|
S: this,
|
|
1277
1283
|
C: (f, a) => f(...a)
|
|
@@ -1289,7 +1295,7 @@ var PersistentLifecycle = class extends Resource {
|
|
|
1289
1295
|
log2(`restarting in ${this._restartAfter}ms`, {
|
|
1290
1296
|
state: this._lifecycleState
|
|
1291
1297
|
}, {
|
|
1292
|
-
F:
|
|
1298
|
+
F: __dxlog_file5,
|
|
1293
1299
|
L: 91,
|
|
1294
1300
|
S: this,
|
|
1295
1301
|
C: (f, a) => f(...a)
|
|
@@ -1312,7 +1318,7 @@ var PersistentLifecycle = class extends Resource {
|
|
|
1312
1318
|
await this._stop(this._currentState);
|
|
1313
1319
|
} catch (err) {
|
|
1314
1320
|
log2.catch(err, void 0, {
|
|
1315
|
-
F:
|
|
1321
|
+
F: __dxlog_file5,
|
|
1316
1322
|
L: 113,
|
|
1317
1323
|
S: this,
|
|
1318
1324
|
C: (f, a) => f(...a)
|
|
@@ -1330,13 +1336,6 @@ var PersistentLifecycle = class extends Resource {
|
|
|
1330
1336
|
}
|
|
1331
1337
|
this._restartTask.schedule();
|
|
1332
1338
|
}
|
|
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
1339
|
};
|
|
1341
1340
|
_ts_decorate([
|
|
1342
1341
|
synchronized
|
|
@@ -1347,7 +1346,7 @@ _ts_decorate([
|
|
|
1347
1346
|
|
|
1348
1347
|
// src/push-iterable.ts
|
|
1349
1348
|
import { invariant as invariant2 } from "@dxos/invariant";
|
|
1350
|
-
var
|
|
1349
|
+
var __dxlog_file6 = "/__w/dxos/dxos/packages/common/async/src/push-iterable.ts";
|
|
1351
1350
|
var makePushIterable = () => {
|
|
1352
1351
|
const buf = [];
|
|
1353
1352
|
const trigger2 = new Trigger({
|
|
@@ -1362,7 +1361,7 @@ var makePushIterable = () => {
|
|
|
1362
1361
|
}
|
|
1363
1362
|
const item = buf.shift();
|
|
1364
1363
|
invariant2(item, void 0, {
|
|
1365
|
-
F:
|
|
1364
|
+
F: __dxlog_file6,
|
|
1366
1365
|
L: 42,
|
|
1367
1366
|
S: this,
|
|
1368
1367
|
A: [
|
|
@@ -1455,19 +1454,6 @@ var streamToArray = (stream) => {
|
|
|
1455
1454
|
|
|
1456
1455
|
// src/test-stream.ts
|
|
1457
1456
|
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
1457
|
var TestStream = class extends Duplex {
|
|
1472
1458
|
static async assertConnectivity(stream1, stream2, { timeout: timeout2 = 200 } = {}) {
|
|
1473
1459
|
stream1.push("ping");
|
|
@@ -1481,6 +1467,8 @@ var TestStream = class extends Duplex {
|
|
|
1481
1467
|
})
|
|
1482
1468
|
]);
|
|
1483
1469
|
}
|
|
1470
|
+
_received = Buffer.alloc(0);
|
|
1471
|
+
_onWrite = new Event();
|
|
1484
1472
|
_write(chunk, encoding, callback) {
|
|
1485
1473
|
this._received = Buffer.concat([
|
|
1486
1474
|
this._received,
|
|
@@ -1495,9 +1483,6 @@ var TestStream = class extends Duplex {
|
|
|
1495
1483
|
const dataBuffer = Buffer.isBuffer(data) ? data : Buffer.from(data);
|
|
1496
1484
|
return asyncTimeout(this._onWrite.waitForCondition(() => this._received.equals(dataBuffer)), timeout2);
|
|
1497
1485
|
}
|
|
1498
|
-
constructor(...args) {
|
|
1499
|
-
super(...args), _define_property9(this, "_received", Buffer.alloc(0)), _define_property9(this, "_onWrite", new Event());
|
|
1500
|
-
}
|
|
1501
1486
|
};
|
|
1502
1487
|
|
|
1503
1488
|
// src/testing.ts
|
|
@@ -1560,20 +1545,14 @@ var untilError = (cb) => {
|
|
|
1560
1545
|
};
|
|
1561
1546
|
|
|
1562
1547
|
// 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
1548
|
var Timer = class {
|
|
1549
|
+
_callback;
|
|
1550
|
+
_state = new Event();
|
|
1551
|
+
_timer;
|
|
1552
|
+
_count = 0;
|
|
1553
|
+
constructor(_callback) {
|
|
1554
|
+
this._callback = _callback;
|
|
1555
|
+
}
|
|
1577
1556
|
get state() {
|
|
1578
1557
|
return this._state;
|
|
1579
1558
|
}
|
|
@@ -1613,33 +1592,29 @@ var Timer = class {
|
|
|
1613
1592
|
this._state.emit(false);
|
|
1614
1593
|
return this;
|
|
1615
1594
|
}
|
|
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
1595
|
};
|
|
1626
1596
|
|
|
1627
1597
|
// 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
1598
|
var TIME_PERIOD = 1e3;
|
|
1642
1599
|
var UpdateScheduler = class {
|
|
1600
|
+
_ctx;
|
|
1601
|
+
_callback;
|
|
1602
|
+
_params;
|
|
1603
|
+
/**
|
|
1604
|
+
* Promise that resolves when the callback is done.
|
|
1605
|
+
* Never rejects.
|
|
1606
|
+
*/
|
|
1607
|
+
_promise = null;
|
|
1608
|
+
_scheduled = false;
|
|
1609
|
+
_lastUpdateTime = -TIME_PERIOD;
|
|
1610
|
+
constructor(_ctx, _callback, _params = {}) {
|
|
1611
|
+
this._ctx = _ctx;
|
|
1612
|
+
this._callback = _callback;
|
|
1613
|
+
this._params = _params;
|
|
1614
|
+
_ctx.onDispose(async () => {
|
|
1615
|
+
await this._promise;
|
|
1616
|
+
});
|
|
1617
|
+
}
|
|
1643
1618
|
trigger() {
|
|
1644
1619
|
if (this._scheduled) {
|
|
1645
1620
|
return;
|
|
@@ -1648,13 +1623,13 @@ var UpdateScheduler = class {
|
|
|
1648
1623
|
await this._promise;
|
|
1649
1624
|
if (this._params.maxFrequency) {
|
|
1650
1625
|
const now = performance.now();
|
|
1651
|
-
const
|
|
1652
|
-
if (
|
|
1626
|
+
const delay2 = this._lastUpdateTime + TIME_PERIOD / this._params.maxFrequency - now;
|
|
1627
|
+
if (delay2 > 0) {
|
|
1653
1628
|
await new Promise((resolve) => {
|
|
1654
1629
|
const timeoutId = setTimeout(() => {
|
|
1655
1630
|
clearContext();
|
|
1656
1631
|
resolve();
|
|
1657
|
-
},
|
|
1632
|
+
}, delay2);
|
|
1658
1633
|
const clearContext = this._ctx.onDispose(() => {
|
|
1659
1634
|
clearTimeout(timeoutId);
|
|
1660
1635
|
resolve();
|
|
@@ -1696,25 +1671,9 @@ var UpdateScheduler = class {
|
|
|
1696
1671
|
this._promise = this._callback();
|
|
1697
1672
|
await this._promise;
|
|
1698
1673
|
}
|
|
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
1674
|
};
|
|
1717
1675
|
export {
|
|
1676
|
+
AsyncTask,
|
|
1718
1677
|
CancellableObservableProvider,
|
|
1719
1678
|
DeferredTask,
|
|
1720
1679
|
Event,
|
|
@@ -1741,6 +1700,7 @@ export {
|
|
|
1741
1700
|
combine,
|
|
1742
1701
|
debounce,
|
|
1743
1702
|
debounceAndThrottle,
|
|
1703
|
+
delay,
|
|
1744
1704
|
dumpLeaks,
|
|
1745
1705
|
interval,
|
|
1746
1706
|
latch,
|