@dxos/async 0.8.4-main.fffef41 → 0.8.4-staging.60fe92afc8

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.
Files changed (42) hide show
  1. package/LICENSE +102 -5
  2. package/dist/lib/browser/index.mjs +111 -88
  3. package/dist/lib/browser/index.mjs.map +3 -3
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/node-esm/index.mjs +111 -88
  6. package/dist/lib/node-esm/index.mjs.map +3 -3
  7. package/dist/lib/node-esm/meta.json +1 -1
  8. package/dist/types/src/callback.d.ts.map +1 -1
  9. package/dist/types/src/chain.d.ts.map +1 -1
  10. package/dist/types/src/cleanup.d.ts +2 -2
  11. package/dist/types/src/cleanup.d.ts.map +1 -1
  12. package/dist/types/src/debounce.d.ts +15 -10
  13. package/dist/types/src/debounce.d.ts.map +1 -1
  14. package/dist/types/src/errors.d.ts.map +1 -1
  15. package/dist/types/src/event-emitter.d.ts.map +1 -1
  16. package/dist/types/src/events.d.ts.map +1 -1
  17. package/dist/types/src/mutex.d.ts.map +1 -1
  18. package/dist/types/src/observable-value.d.ts.map +1 -1
  19. package/dist/types/src/observable.d.ts.map +1 -1
  20. package/dist/types/src/persistent-lifecycle.d.ts +3 -2
  21. package/dist/types/src/persistent-lifecycle.d.ts.map +1 -1
  22. package/dist/types/src/stream-to-array.d.ts.map +1 -1
  23. package/dist/types/src/task-scheduling.d.ts +29 -1
  24. package/dist/types/src/task-scheduling.d.ts.map +1 -1
  25. package/dist/types/src/test-stream.d.ts.map +1 -1
  26. package/dist/types/src/testing.d.ts.map +1 -1
  27. package/dist/types/src/timeout.d.ts +1 -1
  28. package/dist/types/src/timeout.d.ts.map +1 -1
  29. package/dist/types/src/timer.d.ts.map +1 -1
  30. package/dist/types/src/track-leaks.d.ts.map +1 -1
  31. package/dist/types/src/trigger.d.ts.map +1 -1
  32. package/dist/types/src/update-scheduler.d.ts.map +1 -1
  33. package/dist/types/tsconfig.tsbuildinfo +1 -1
  34. package/package.json +12 -11
  35. package/src/cleanup.ts +7 -4
  36. package/src/debounce.ts +19 -14
  37. package/src/event-emitter.test.ts +0 -1
  38. package/src/observable-value.ts +4 -2
  39. package/src/persistent-lifecycle.test.ts +36 -0
  40. package/src/persistent-lifecycle.ts +33 -6
  41. package/src/task-scheduling.ts +95 -1
  42. package/src/timeout.ts +6 -9
package/LICENSE CHANGED
@@ -1,8 +1,105 @@
1
- MIT License
2
- Copyright (c) 2022 DXOS
1
+ # Functional Source License, Version 1.1, ALv2 Future License
3
2
 
4
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
3
+ ## Abbreviation
5
4
 
6
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
5
+ FSL-1.1-Apache-2.0
7
6
 
8
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7
+ ## Notice
8
+
9
+ Copyright 2026 DXOS
10
+
11
+ ## Terms and Conditions
12
+
13
+ ### Licensor ("We")
14
+
15
+ The party offering the Software under these Terms and Conditions.
16
+
17
+ ### The Software
18
+
19
+ The "Software" is each version of the software that we make available under
20
+ these Terms and Conditions, as indicated by our inclusion of these Terms and
21
+ Conditions with the Software.
22
+
23
+ ### License Grant
24
+
25
+ Subject to your compliance with this License Grant and the Patents,
26
+ Redistribution and Trademark clauses below, we hereby grant you the right to
27
+ use, copy, modify, create derivative works, publicly perform, publicly display
28
+ and redistribute the Software for any Permitted Purpose identified below.
29
+
30
+ ### Permitted Purpose
31
+
32
+ A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
33
+ means making the Software available to others in a commercial product or
34
+ service that:
35
+
36
+ 1. substitutes for the Software;
37
+
38
+ 2. substitutes for any other product or service we offer using the Software
39
+ that exists as of the date we make the Software available; or
40
+
41
+ 3. offers the same or substantially similar functionality as the Software.
42
+
43
+ Permitted Purposes specifically include using the Software:
44
+
45
+ 1. for your internal use and access;
46
+
47
+ 2. for non-commercial education;
48
+
49
+ 3. for non-commercial research; and
50
+
51
+ 4. in connection with professional services that you provide to a licensee
52
+ using the Software in accordance with these Terms and Conditions.
53
+
54
+ ### Patents
55
+
56
+ To the extent your use for a Permitted Purpose would necessarily infringe our
57
+ patents, the license grant above includes a license under our patents. If you
58
+ make a claim against any party that the Software infringes or contributes to
59
+ the infringement of any patent, then your patent license to the Software ends
60
+ immediately.
61
+
62
+ ### Redistribution
63
+
64
+ The Terms and Conditions apply to all copies, modifications and derivatives of
65
+ the Software.
66
+
67
+ If you redistribute any copies, modifications or derivatives of the Software,
68
+ you must include a copy of or a link to these Terms and Conditions and not
69
+ remove any copyright notices provided in or with the Software.
70
+
71
+ ### Disclaimer
72
+
73
+ THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
74
+ IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
75
+ PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
76
+
77
+ IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
78
+ SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
79
+ EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
80
+
81
+ ### Trademarks
82
+
83
+ Except for displaying the License Details and identifying us as the origin of
84
+ the Software, you have no right under these Terms and Conditions to use our
85
+ trademarks, trade names, service marks or product names.
86
+
87
+ ## Grant of Future License
88
+
89
+ We hereby irrevocably grant you an additional license to use the Software under
90
+ the Apache License, Version 2.0 that is effective on the second anniversary of
91
+ the date we make the Software available. On or after that date, you may use the
92
+ Software under the Apache License, Version 2.0, in which case the following
93
+ will apply:
94
+
95
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use
96
+ this file except in compliance with the License.
97
+
98
+ You may obtain a copy of the License at
99
+
100
+ http://www.apache.org/licenses/LICENSE-2.0
101
+
102
+ Unless required by applicable law or agreed to in writing, software distributed
103
+ under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
104
+ CONDITIONS OF ANY KIND, either express or implied. See the License for the
105
+ specific language governing permissions and limitations under the License.
@@ -22,7 +22,7 @@ var chain = (chain2) => async (elements) => {
22
22
  import { ComplexMap } from "@dxos/util";
23
23
  var combine = (...cleanupFns) => {
24
24
  return () => {
25
- cleanupFns.flat().forEach((cleanupFn) => cleanupFn());
25
+ cleanupFns.flat().filter((f) => typeof f === "function").forEach((cleanupFn) => cleanupFn());
26
26
  };
27
27
  };
28
28
  var timeout = (cb, ms = 0) => {
@@ -39,8 +39,8 @@ var addEventListener = (target, type, listener, options) => {
39
39
  };
40
40
  var SubscriptionList = class {
41
41
  _cleanups = [];
42
- add(cb) {
43
- this._cleanups.push(cb);
42
+ add(...cb) {
43
+ this._cleanups.push(...cb);
44
44
  return this;
45
45
  }
46
46
  clear() {
@@ -166,6 +166,9 @@ var sleepWithContext = (ctx, ms) => {
166
166
  };
167
167
  var asyncReturn = () => sleep(0);
168
168
  var asyncTimeout = async (promise, timeout2, err) => {
169
+ if (typeof promise === "function") {
170
+ throw new Error("First argument must be a promise.");
171
+ }
169
172
  let timeoutId;
170
173
  const throwable = err === void 0 || typeof err === "string" ? new TimeoutError(timeout2, err) : err;
171
174
  const timeoutPromise = new Promise((resolve, reject) => {
@@ -174,9 +177,8 @@ var asyncTimeout = async (promise, timeout2, err) => {
174
177
  }, timeout2);
175
178
  unrefTimeout(timeoutId);
176
179
  });
177
- const conditionTimeout = typeof promise === "function" ? promiseFromCallback(promise) : promise;
178
180
  return await Promise.race([
179
- conditionTimeout,
181
+ promise,
180
182
  timeoutPromise
181
183
  ]).finally(() => {
182
184
  clearTimeout(timeoutId);
@@ -213,7 +215,6 @@ var waitForEvent = (eventEmitter, eventName, test, timeout2, error) => {
213
215
 
214
216
  // src/events.ts
215
217
  import { Context } from "@dxos/context";
216
- var __dxlog_file = "/__w/dxos/dxos/packages/common/async/src/events.ts";
217
218
  var DO_NOT_ERROR_ON_ASYNC_CALLBACK = true;
218
219
  var Event = class _Event {
219
220
  /**
@@ -268,10 +269,7 @@ var Event = class _Event {
268
269
  _ctx,
269
270
  _callback
270
271
  ] : [
271
- new Context(void 0, {
272
- F: __dxlog_file,
273
- L: 132
274
- }),
272
+ new Context(),
275
273
  _ctx
276
274
  ];
277
275
  const weak = !!options?.weak;
@@ -301,10 +299,7 @@ var Event = class _Event {
301
299
  _ctx,
302
300
  _callback
303
301
  ] : [
304
- new Context(void 0, {
305
- F: __dxlog_file,
306
- L: 169
307
- }),
302
+ new Context(),
308
303
  _ctx
309
304
  ];
310
305
  const listener = new EventListener(this, callback, ctx, true, false);
@@ -607,7 +602,7 @@ var MutexGuard = class {
607
602
  this.release();
608
603
  }
609
604
  };
610
- var classMutexSymbol = Symbol("class-mutex");
605
+ var classMutexSymbol = /* @__PURE__ */ Symbol("class-mutex");
611
606
  var FORCE_DISABLE_WARNING = false;
612
607
  var enableWarning = !FORCE_DISABLE_WARNING && globalThis.mochaExecutor;
613
608
  var synchronized = (target, propertyName, descriptor) => {
@@ -638,7 +633,6 @@ import PushStream from "zen-push";
638
633
 
639
634
  // src/trigger.ts
640
635
  import { invariant } from "@dxos/invariant";
641
- var __dxlog_file2 = "/__w/dxos/dxos/packages/common/async/src/trigger.ts";
642
636
  var trigger = (timeout2) => {
643
637
  let callback;
644
638
  const promise = new Promise((resolve, reject) => {
@@ -730,15 +724,7 @@ var Trigger = class {
730
724
  }
731
725
  };
732
726
  var latch = ({ count = 1, timeout: timeout2 } = {}) => {
733
- invariant(count >= 0, void 0, {
734
- F: __dxlog_file2,
735
- L: 139,
736
- S: void 0,
737
- A: [
738
- "count >= 0",
739
- ""
740
- ]
741
- });
727
+ invariant(count >= 0);
742
728
  let t;
743
729
  let doResolve;
744
730
  let doReject;
@@ -935,16 +921,15 @@ import { warnAfterTimeout as warnAfterTimeout2 } from "@dxos/debug";
935
921
  import { log as log2 } from "@dxos/log";
936
922
 
937
923
  // src/task-scheduling.ts
938
- import { ContextDisposedError as ContextDisposedError2 } from "@dxos/context";
924
+ import { Context as Context2, ContextDisposedError as ContextDisposedError2 } from "@dxos/context";
939
925
  import { StackTrace as StackTrace2 } from "@dxos/debug";
940
926
 
941
927
  // src/track-leaks.ts
942
928
  import { StackTrace } from "@dxos/debug";
943
929
  import { log } from "@dxos/log";
944
- var __dxlog_file3 = "/__w/dxos/dxos/packages/common/async/src/track-leaks.ts";
945
930
  var enabled = typeof process !== "undefined" && !!process.env.DX_TRACK_LEAKS;
946
931
  var openResources = /* @__PURE__ */ new Set();
947
- var handleSymbol = Symbol("checkLeaksHandle");
932
+ var handleSymbol = /* @__PURE__ */ Symbol("checkLeaksHandle");
948
933
  var trackResource = (resourceProvider) => {
949
934
  if (!enabled) {
950
935
  return () => {
@@ -991,31 +976,11 @@ var dumpLeaks = () => {
991
976
  if (!enabled) {
992
977
  return;
993
978
  }
994
- log.info(`Leaked resources ${openResources.size}:`, void 0, {
995
- F: __dxlog_file3,
996
- L: 82,
997
- S: void 0,
998
- C: (f, a) => f(...a)
999
- });
979
+ log.info(`Leaked resources ${openResources.size}:`);
1000
980
  for (const resource of openResources) {
1001
- log.info(`- ${resource.name} at`, void 0, {
1002
- F: __dxlog_file3,
1003
- L: 84,
1004
- S: void 0,
1005
- C: (f, a) => f(...a)
1006
- });
1007
- log.info(resource.openStack.getStack(1), void 0, {
1008
- F: __dxlog_file3,
1009
- L: 85,
1010
- S: void 0,
1011
- C: (f, a) => f(...a)
1012
- });
1013
- log.info("\n", void 0, {
1014
- F: __dxlog_file3,
1015
- L: 86,
1016
- S: void 0,
1017
- C: (f, a) => f(...a)
1018
- });
981
+ log.info(`- ${resource.name} at`);
982
+ log.info(resource.openStack.getStack(1));
983
+ log.info("\n");
1019
984
  }
1020
985
  };
1021
986
  if (enabled) {
@@ -1072,6 +1037,79 @@ var DeferredTask = class {
1072
1037
  await this._currentTask;
1073
1038
  }
1074
1039
  };
1040
+ var AsyncTask = class {
1041
+ #callback;
1042
+ #ctx = void 0;
1043
+ #scheduled = false;
1044
+ #currentTask = null;
1045
+ #nextTask = new Trigger();
1046
+ constructor(callback) {
1047
+ this.#callback = callback;
1048
+ }
1049
+ get scheduled() {
1050
+ return this.#scheduled;
1051
+ }
1052
+ /**
1053
+ * Context of the resource that owns the task.
1054
+ * When the context is disposed, the task is cancelled and cannot be scheduled again.
1055
+ */
1056
+ open() {
1057
+ this.#ctx = new Context2();
1058
+ }
1059
+ /**
1060
+ * Closes the task and waits for it to finish if it is running.
1061
+ */
1062
+ async close() {
1063
+ await this.#ctx?.dispose();
1064
+ await this.join();
1065
+ this.#ctx = void 0;
1066
+ }
1067
+ [Symbol.asyncDispose]() {
1068
+ return this.close();
1069
+ }
1070
+ /**
1071
+ * Schedule the task to run asynchronously.
1072
+ */
1073
+ // TODO(dmaretskyi): Add scheduleAt. Where the earlier time will override the later one.
1074
+ schedule() {
1075
+ if (!this.#ctx || this.#ctx.disposed) {
1076
+ throw new Error("AsyncTask not open");
1077
+ }
1078
+ if (this.#scheduled) {
1079
+ return;
1080
+ }
1081
+ scheduleTask(this.#ctx, async () => {
1082
+ await this.#currentTask;
1083
+ if (!this.#ctx || this.#ctx.disposed) {
1084
+ return;
1085
+ }
1086
+ this.#scheduled = false;
1087
+ const completionTrigger = this.#nextTask;
1088
+ this.#nextTask = new Trigger();
1089
+ this.#currentTask = runInContextAsync(this.#ctx, () => this.#callback()).then(() => {
1090
+ completionTrigger.wake();
1091
+ });
1092
+ });
1093
+ this.#scheduled = true;
1094
+ }
1095
+ /**
1096
+ * Schedule the task to run and wait for it to finish.
1097
+ */
1098
+ async runBlocking() {
1099
+ if (this.#ctx?.disposed) {
1100
+ throw new ContextDisposedError2();
1101
+ }
1102
+ this.schedule();
1103
+ await this.#nextTask.wait();
1104
+ }
1105
+ /**
1106
+ * Waits for the current task to finish if it is running.
1107
+ * Does not schedule a new task.
1108
+ */
1109
+ async join() {
1110
+ await this.#currentTask;
1111
+ }
1112
+ };
1075
1113
  var runInContext = (ctx, fn) => {
1076
1114
  try {
1077
1115
  fn();
@@ -1157,9 +1195,9 @@ function _ts_decorate(decorators, target, key, desc) {
1157
1195
  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;
1158
1196
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1159
1197
  }
1160
- var __dxlog_file4 = "/__w/dxos/dxos/packages/common/async/src/persistent-lifecycle.ts";
1161
1198
  var INIT_RESTART_DELAY = 100;
1162
1199
  var DEFAULT_MAX_RESTART_DELAY = 5e3;
1200
+ var STABLE_CONNECTION_THRESHOLD = 5e3;
1163
1201
  var PersistentLifecycle = class extends Resource {
1164
1202
  _start;
1165
1203
  _stop;
@@ -1168,6 +1206,7 @@ var PersistentLifecycle = class extends Resource {
1168
1206
  _currentState = void 0;
1169
1207
  _restartTask = void 0;
1170
1208
  _restartAfter = 0;
1209
+ _connectedAt = void 0;
1171
1210
  constructor({ start, stop, onRestart, maxRestartDelay = DEFAULT_MAX_RESTART_DELAY }) {
1172
1211
  super();
1173
1212
  this._start = start;
@@ -1183,29 +1222,27 @@ var PersistentLifecycle = class extends Resource {
1183
1222
  try {
1184
1223
  await this._restart();
1185
1224
  } catch (err) {
1225
+ if (this._ctx?.disposed) {
1226
+ return;
1227
+ }
1186
1228
  log2.warn("Restart failed", {
1187
1229
  err
1188
- }, {
1189
- F: __dxlog_file4,
1190
- L: 72,
1191
- S: this,
1192
- C: (f, a) => f(...a)
1193
1230
  });
1194
1231
  this._restartTask?.schedule();
1195
1232
  }
1196
1233
  });
1197
- this._currentState = await this._start().catch((err) => {
1234
+ try {
1235
+ this._currentState = await this._start();
1236
+ this._connectedAt = Date.now();
1237
+ } catch (err) {
1238
+ if (this._ctx?.disposed) {
1239
+ return;
1240
+ }
1198
1241
  log2.warn("Start failed", {
1199
1242
  err
1200
- }, {
1201
- F: __dxlog_file4,
1202
- L: 78,
1203
- S: this,
1204
- C: (f, a) => f(...a)
1205
1243
  });
1206
1244
  this._restartTask?.schedule();
1207
- return void 0;
1208
- });
1245
+ }
1209
1246
  }
1210
1247
  async _close() {
1211
1248
  await this._restartTask?.join();
@@ -1215,12 +1252,11 @@ var PersistentLifecycle = class extends Resource {
1215
1252
  async _restart() {
1216
1253
  log2(`restarting in ${this._restartAfter}ms`, {
1217
1254
  state: this._lifecycleState
1218
- }, {
1219
- F: __dxlog_file4,
1220
- L: 91,
1221
- S: this,
1222
- C: (f, a) => f(...a)
1223
1255
  });
1256
+ if (this._connectedAt !== void 0 && Date.now() - this._connectedAt >= STABLE_CONNECTION_THRESHOLD) {
1257
+ this._restartAfter = 0;
1258
+ }
1259
+ this._connectedAt = void 0;
1224
1260
  await this._stopCurrentState();
1225
1261
  if (this._lifecycleState !== LifecycleState.OPEN) {
1226
1262
  return;
@@ -1230,7 +1266,7 @@ var PersistentLifecycle = class extends Resource {
1230
1266
  await warnAfterTimeout2(5e3, "Connection establishment takes too long", async () => {
1231
1267
  this._currentState = await this._start();
1232
1268
  });
1233
- this._restartAfter = 0;
1269
+ this._connectedAt = Date.now();
1234
1270
  await this._onRestart?.();
1235
1271
  }
1236
1272
  async _stopCurrentState() {
@@ -1238,12 +1274,7 @@ var PersistentLifecycle = class extends Resource {
1238
1274
  try {
1239
1275
  await this._stop(this._currentState);
1240
1276
  } catch (err) {
1241
- log2.catch(err, void 0, {
1242
- F: __dxlog_file4,
1243
- L: 113,
1244
- S: this,
1245
- C: (f, a) => f(...a)
1246
- });
1277
+ log2.catch(err);
1247
1278
  }
1248
1279
  this._currentState = void 0;
1249
1280
  }
@@ -1267,7 +1298,6 @@ _ts_decorate([
1267
1298
 
1268
1299
  // src/push-iterable.ts
1269
1300
  import { invariant as invariant2 } from "@dxos/invariant";
1270
- var __dxlog_file5 = "/__w/dxos/dxos/packages/common/async/src/push-iterable.ts";
1271
1301
  var makePushIterable = () => {
1272
1302
  const buf = [];
1273
1303
  const trigger2 = new Trigger({
@@ -1281,15 +1311,7 @@ var makePushIterable = () => {
1281
1311
  await trigger2.wait();
1282
1312
  }
1283
1313
  const item = buf.shift();
1284
- invariant2(item, void 0, {
1285
- F: __dxlog_file5,
1286
- L: 42,
1287
- S: this,
1288
- A: [
1289
- "item",
1290
- ""
1291
- ]
1292
- });
1314
+ invariant2(item);
1293
1315
  switch (item.kind) {
1294
1316
  case "next":
1295
1317
  return {
@@ -1594,6 +1616,7 @@ var UpdateScheduler = class {
1594
1616
  }
1595
1617
  };
1596
1618
  export {
1619
+ AsyncTask,
1597
1620
  CancellableObservableProvider,
1598
1621
  DeferredTask,
1599
1622
  Event,