@dxos/async 0.8.4-main.2e9d522 → 0.8.4-main.3c1ae3b

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.
@@ -38,9 +38,7 @@ var addEventListener = (target, type, listener, options) => {
38
38
  return () => target.removeEventListener(type, listener, options);
39
39
  };
40
40
  var SubscriptionList = class {
41
- constructor() {
42
- this._cleanups = [];
43
- }
41
+ _cleanups = [];
44
42
  add(cb) {
45
43
  this._cleanups.push(cb);
46
44
  return this;
@@ -51,6 +49,7 @@ var SubscriptionList = class {
51
49
  }
52
50
  };
53
51
  var SubscriptionSet = class {
52
+ _cleanupMap;
54
53
  constructor(keyProjection) {
55
54
  this._cleanupMap = new ComplexMap(keyProjection);
56
55
  }
@@ -65,29 +64,55 @@ var SubscriptionSet = class {
65
64
  };
66
65
 
67
66
  // src/debounce.ts
68
- var debounce = (cb, delay = 100) => {
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) => {
69
84
  let t;
70
85
  return (...args) => {
71
86
  clearTimeout(t);
72
- t = setTimeout(() => cb(...args), delay);
87
+ t = setTimeout(() => cb(...args), delay2);
73
88
  };
74
89
  };
75
- var throttle = (cb, delay = 100) => {
90
+ var throttle = (cb, delay2 = 100) => {
76
91
  let lastCall = 0;
77
92
  return (...args) => {
78
93
  const now = Date.now();
79
- if (now - lastCall >= delay) {
94
+ if (now - lastCall >= delay2) {
80
95
  cb(...args);
81
96
  lastCall = now;
82
97
  }
83
98
  };
84
99
  };
85
- var debounceAndThrottle = (cb, delay = 100) => {
86
- const debounced = debounce(cb, delay);
87
- const throttled = throttle(cb, delay);
100
+ var debounceAndThrottle = (cb, delay2 = 100) => {
101
+ let timeout2;
102
+ let lastCall = 0;
88
103
  return (...args) => {
89
- debounced(...args);
90
- throttled(...args);
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
+ }
91
116
  };
92
117
  };
93
118
 
@@ -191,10 +216,6 @@ import { Context } from "@dxos/context";
191
216
  var __dxlog_file = "/__w/dxos/dxos/packages/common/async/src/events.ts";
192
217
  var DO_NOT_ERROR_ON_ASYNC_CALLBACK = true;
193
218
  var Event = class _Event {
194
- constructor() {
195
- this._listeners = /* @__PURE__ */ new Set();
196
- this._effects = /* @__PURE__ */ new Set();
197
- }
198
219
  /**
199
220
  * Wrap objects that have on/off style event emitters.
200
221
  */
@@ -207,6 +228,8 @@ var Event = class _Event {
207
228
  });
208
229
  return event;
209
230
  }
231
+ _listeners = /* @__PURE__ */ new Set();
232
+ _effects = /* @__PURE__ */ new Set();
210
233
  /**
211
234
  * Emit an event.
212
235
  * In most cases should only be called by the class or entity containing the event.
@@ -450,11 +473,15 @@ var Event = class _Event {
450
473
  }
451
474
  };
452
475
  var EventListener = class {
476
+ ctx;
477
+ once;
478
+ weak;
479
+ callback;
480
+ _clearDispose = void 0;
453
481
  constructor(event, listener, ctx, once, weak) {
454
482
  this.ctx = ctx;
455
483
  this.once = once;
456
484
  this.weak = weak;
457
- this._clearDispose = void 0;
458
485
  this._clearDispose = ctx.onDispose(() => {
459
486
  event._removeListener(this);
460
487
  });
@@ -518,11 +545,9 @@ var weakListeners = () => {
518
545
  import "@dxos/util";
519
546
  import { warnAfterTimeout } from "@dxos/debug";
520
547
  var Mutex = class {
521
- constructor() {
522
- this._queue = Promise.resolve();
523
- this._queueLength = 0;
524
- this._tag = null;
525
- }
548
+ _queue = Promise.resolve();
549
+ _queueLength = 0;
550
+ _tag = null;
526
551
  get tag() {
527
552
  return this._tag;
528
553
  }
@@ -568,6 +593,7 @@ var Mutex = class {
568
593
  }
569
594
  };
570
595
  var MutexGuard = class {
596
+ _release;
571
597
  constructor(_release) {
572
598
  this._release = _release;
573
599
  }
@@ -628,18 +654,22 @@ var trigger = (timeout2) => {
628
654
  resolver
629
655
  ];
630
656
  };
631
- var TriggerState = /* @__PURE__ */ function(TriggerState2) {
657
+ var TriggerState = /* @__PURE__ */ (function(TriggerState2) {
632
658
  TriggerState2["WAITING"] = "WAITING";
633
659
  TriggerState2["RESOLVED"] = "RESOLVED";
634
660
  TriggerState2["REJECTED"] = "REJECTED";
635
661
  return TriggerState2;
636
- }({});
662
+ })({});
637
663
  var Trigger = class {
664
+ _options;
665
+ _promise;
666
+ _resolve;
667
+ _reject;
668
+ _state = "WAITING";
638
669
  constructor(_options = {
639
670
  autoReset: false
640
671
  }) {
641
672
  this._options = _options;
642
- this._state = "WAITING";
643
673
  this.reset();
644
674
  }
645
675
  get state() {
@@ -748,20 +778,12 @@ var latch = ({ count = 1, timeout: timeout2 } = {}) => {
748
778
 
749
779
  // src/observable.ts
750
780
  var MulticastObservable = class _MulticastObservable extends Observable {
781
+ _value;
782
+ _observers = /* @__PURE__ */ new Set();
783
+ _observable;
784
+ _completed = new Trigger();
751
785
  constructor(subscriber, _value) {
752
- super((observer) => this._subscribe(observer)), this._value = _value, this._observers = /* @__PURE__ */ new Set(), this._completed = new Trigger(), this._handlers = {
753
- next: (value) => {
754
- this._value = value;
755
- this._observers.forEach((observer) => observer.next?.(value));
756
- },
757
- error: (err) => {
758
- this._observers.forEach((observer) => observer.error?.(err));
759
- },
760
- complete: () => {
761
- this._completed.wake();
762
- this._observers.forEach((observer) => observer.complete?.());
763
- }
764
- };
786
+ super((observer) => this._subscribe(observer)), this._value = _value;
765
787
  this._observable = typeof subscriber === "function" ? new Observable(subscriber) : subscriber;
766
788
  this._observable.subscribe(this._handlers);
767
789
  }
@@ -845,18 +867,30 @@ var MulticastObservable = class _MulticastObservable extends Observable {
845
867
  this._observers.delete(observer);
846
868
  };
847
869
  }
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
+ };
848
883
  };
849
884
  var EMPTY_OBSERVABLE = MulticastObservable.of(null);
850
885
 
851
886
  // src/observable-value.ts
852
887
  import { createSetDispatch } from "@dxos/util";
853
888
  var ObservableProvider = class {
854
- constructor() {
855
- this._handlers = /* @__PURE__ */ new Set();
856
- this._proxy = createSetDispatch({
857
- handlers: this._handlers
858
- });
859
- }
889
+ _handlers = /* @__PURE__ */ new Set();
890
+ _proxy = createSetDispatch({
891
+ handlers: this._handlers
892
+ });
893
+ _value;
860
894
  /**
861
895
  * Proxy used to dispatch callbacks to each subscription.
862
896
  */
@@ -877,8 +911,10 @@ var ObservableProvider = class {
877
911
  }
878
912
  };
879
913
  var CancellableObservableProvider = class extends ObservableProvider {
914
+ _handleCancel;
915
+ _cancelled = false;
880
916
  constructor(_handleCancel) {
881
- super(), this._handleCancel = _handleCancel, this._cancelled = false;
917
+ super(), this._handleCancel = _handleCancel;
882
918
  }
883
919
  get cancelled() {
884
920
  return this._cancelled;
@@ -988,12 +1024,14 @@ if (enabled) {
988
1024
 
989
1025
  // src/task-scheduling.ts
990
1026
  var DeferredTask = class {
1027
+ _ctx;
1028
+ _callback;
1029
+ _scheduled = false;
1030
+ _currentTask = null;
1031
+ _nextTask = new Trigger();
991
1032
  constructor(_ctx, _callback) {
992
1033
  this._ctx = _ctx;
993
1034
  this._callback = _callback;
994
- this._scheduled = false;
995
- this._currentTask = null;
996
- this._nextTask = new Trigger();
997
1035
  }
998
1036
  get scheduled() {
999
1037
  return this._scheduled;
@@ -1123,11 +1161,15 @@ var __dxlog_file4 = "/__w/dxos/dxos/packages/common/async/src/persistent-lifecyc
1123
1161
  var INIT_RESTART_DELAY = 100;
1124
1162
  var DEFAULT_MAX_RESTART_DELAY = 5e3;
1125
1163
  var PersistentLifecycle = class extends Resource {
1164
+ _start;
1165
+ _stop;
1166
+ _onRestart;
1167
+ _maxRestartDelay;
1168
+ _currentState = void 0;
1169
+ _restartTask = void 0;
1170
+ _restartAfter = 0;
1126
1171
  constructor({ start, stop, onRestart, maxRestartDelay = DEFAULT_MAX_RESTART_DELAY }) {
1127
1172
  super();
1128
- this._currentState = void 0;
1129
- this._restartTask = void 0;
1130
- this._restartAfter = 0;
1131
1173
  this._start = start;
1132
1174
  this._stop = stop;
1133
1175
  this._onRestart = onRestart;
@@ -1334,11 +1376,6 @@ var streamToArray = (stream) => {
1334
1376
  // src/test-stream.ts
1335
1377
  import { Duplex } from "@dxos/node-std/stream";
1336
1378
  var TestStream = class extends Duplex {
1337
- constructor() {
1338
- super(...arguments);
1339
- this._received = Buffer.alloc(0);
1340
- this._onWrite = new Event();
1341
- }
1342
1379
  static async assertConnectivity(stream1, stream2, { timeout: timeout2 = 200 } = {}) {
1343
1380
  stream1.push("ping");
1344
1381
  stream2.push("pong");
@@ -1351,6 +1388,8 @@ var TestStream = class extends Duplex {
1351
1388
  })
1352
1389
  ]);
1353
1390
  }
1391
+ _received = Buffer.alloc(0);
1392
+ _onWrite = new Event();
1354
1393
  _write(chunk, encoding, callback) {
1355
1394
  this._received = Buffer.concat([
1356
1395
  this._received,
@@ -1428,10 +1467,12 @@ var untilError = (cb) => {
1428
1467
 
1429
1468
  // src/timer.ts
1430
1469
  var Timer = class {
1470
+ _callback;
1471
+ _state = new Event();
1472
+ _timer;
1473
+ _count = 0;
1431
1474
  constructor(_callback) {
1432
1475
  this._callback = _callback;
1433
- this._state = new Event();
1434
- this._count = 0;
1435
1476
  }
1436
1477
  get state() {
1437
1478
  return this._state;
@@ -1477,13 +1518,20 @@ var Timer = class {
1477
1518
  // src/update-scheduler.ts
1478
1519
  var TIME_PERIOD = 1e3;
1479
1520
  var UpdateScheduler = class {
1521
+ _ctx;
1522
+ _callback;
1523
+ _params;
1524
+ /**
1525
+ * Promise that resolves when the callback is done.
1526
+ * Never rejects.
1527
+ */
1528
+ _promise = null;
1529
+ _scheduled = false;
1530
+ _lastUpdateTime = -TIME_PERIOD;
1480
1531
  constructor(_ctx, _callback, _params = {}) {
1481
1532
  this._ctx = _ctx;
1482
1533
  this._callback = _callback;
1483
1534
  this._params = _params;
1484
- this._promise = null;
1485
- this._scheduled = false;
1486
- this._lastUpdateTime = -TIME_PERIOD;
1487
1535
  _ctx.onDispose(async () => {
1488
1536
  await this._promise;
1489
1537
  });
@@ -1496,13 +1544,13 @@ var UpdateScheduler = class {
1496
1544
  await this._promise;
1497
1545
  if (this._params.maxFrequency) {
1498
1546
  const now = performance.now();
1499
- const delay = this._lastUpdateTime + TIME_PERIOD / this._params.maxFrequency - now;
1500
- if (delay > 0) {
1547
+ const delay2 = this._lastUpdateTime + TIME_PERIOD / this._params.maxFrequency - now;
1548
+ if (delay2 > 0) {
1501
1549
  await new Promise((resolve) => {
1502
1550
  const timeoutId = setTimeout(() => {
1503
1551
  clearContext();
1504
1552
  resolve();
1505
- }, delay);
1553
+ }, delay2);
1506
1554
  const clearContext = this._ctx.onDispose(() => {
1507
1555
  clearTimeout(timeoutId);
1508
1556
  resolve();
@@ -1572,6 +1620,7 @@ export {
1572
1620
  combine,
1573
1621
  debounce,
1574
1622
  debounceAndThrottle,
1623
+ delay,
1575
1624
  dumpLeaks,
1576
1625
  interval,
1577
1626
  latch,