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