@dxos/async 0.8.4-staging.ac66bdf99f → 0.9.0

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 (32) hide show
  1. package/LICENSE +102 -5
  2. package/dist/lib/browser/index.mjs +28 -85
  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 +28 -85
  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.map +1 -1
  11. package/dist/types/src/debounce.d.ts.map +1 -1
  12. package/dist/types/src/errors.d.ts.map +1 -1
  13. package/dist/types/src/event-emitter.d.ts.map +1 -1
  14. package/dist/types/src/events.d.ts.map +1 -1
  15. package/dist/types/src/mutex.d.ts.map +1 -1
  16. package/dist/types/src/observable-value.d.ts.map +1 -1
  17. package/dist/types/src/observable.d.ts.map +1 -1
  18. package/dist/types/src/persistent-lifecycle.d.ts +1 -0
  19. package/dist/types/src/persistent-lifecycle.d.ts.map +1 -1
  20. package/dist/types/src/stream-to-array.d.ts.map +1 -1
  21. package/dist/types/src/task-scheduling.d.ts.map +1 -1
  22. package/dist/types/src/test-stream.d.ts.map +1 -1
  23. package/dist/types/src/testing.d.ts.map +1 -1
  24. package/dist/types/src/timeout.d.ts.map +1 -1
  25. package/dist/types/src/timer.d.ts.map +1 -1
  26. package/dist/types/src/track-leaks.d.ts.map +1 -1
  27. package/dist/types/src/trigger.d.ts.map +1 -1
  28. package/dist/types/src/update-scheduler.d.ts.map +1 -1
  29. package/dist/types/tsconfig.tsbuildinfo +1 -1
  30. package/package.json +8 -11
  31. package/src/persistent-lifecycle.test.ts +36 -0
  32. package/src/persistent-lifecycle.ts +31 -4
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.
@@ -215,7 +215,6 @@ var waitForEvent = (eventEmitter, eventName, test, timeout2, error) => {
215
215
 
216
216
  // src/events.ts
217
217
  import { Context } from "@dxos/context";
218
- var __dxlog_file = "/__w/dxos/dxos/packages/common/async/src/events.ts";
219
218
  var DO_NOT_ERROR_ON_ASYNC_CALLBACK = true;
220
219
  var Event = class _Event {
221
220
  /**
@@ -270,10 +269,7 @@ var Event = class _Event {
270
269
  _ctx,
271
270
  _callback
272
271
  ] : [
273
- new Context(void 0, {
274
- F: __dxlog_file,
275
- L: 132
276
- }),
272
+ new Context(),
277
273
  _ctx
278
274
  ];
279
275
  const weak = !!options?.weak;
@@ -303,10 +299,7 @@ var Event = class _Event {
303
299
  _ctx,
304
300
  _callback
305
301
  ] : [
306
- new Context(void 0, {
307
- F: __dxlog_file,
308
- L: 169
309
- }),
302
+ new Context(),
310
303
  _ctx
311
304
  ];
312
305
  const listener = new EventListener(this, callback, ctx, true, false);
@@ -640,7 +633,6 @@ import PushStream from "zen-push";
640
633
 
641
634
  // src/trigger.ts
642
635
  import { invariant } from "@dxos/invariant";
643
- var __dxlog_file2 = "/__w/dxos/dxos/packages/common/async/src/trigger.ts";
644
636
  var trigger = (timeout2) => {
645
637
  let callback;
646
638
  const promise = new Promise((resolve, reject) => {
@@ -732,15 +724,7 @@ var Trigger = class {
732
724
  }
733
725
  };
734
726
  var latch = ({ count = 1, timeout: timeout2 } = {}) => {
735
- invariant(count >= 0, void 0, {
736
- F: __dxlog_file2,
737
- L: 139,
738
- S: void 0,
739
- A: [
740
- "count >= 0",
741
- ""
742
- ]
743
- });
727
+ invariant(count >= 0);
744
728
  let t;
745
729
  let doResolve;
746
730
  let doReject;
@@ -943,7 +927,6 @@ import { StackTrace as StackTrace2 } from "@dxos/debug";
943
927
  // src/track-leaks.ts
944
928
  import { StackTrace } from "@dxos/debug";
945
929
  import { log } from "@dxos/log";
946
- var __dxlog_file3 = "/__w/dxos/dxos/packages/common/async/src/track-leaks.ts";
947
930
  var enabled = typeof process !== "undefined" && !!process.env.DX_TRACK_LEAKS;
948
931
  var openResources = /* @__PURE__ */ new Set();
949
932
  var handleSymbol = /* @__PURE__ */ Symbol("checkLeaksHandle");
@@ -993,31 +976,11 @@ var dumpLeaks = () => {
993
976
  if (!enabled) {
994
977
  return;
995
978
  }
996
- log.info(`Leaked resources ${openResources.size}:`, void 0, {
997
- F: __dxlog_file3,
998
- L: 82,
999
- S: void 0,
1000
- C: (f, a) => f(...a)
1001
- });
979
+ log.info(`Leaked resources ${openResources.size}:`);
1002
980
  for (const resource of openResources) {
1003
- log.info(`- ${resource.name} at`, void 0, {
1004
- F: __dxlog_file3,
1005
- L: 84,
1006
- S: void 0,
1007
- C: (f, a) => f(...a)
1008
- });
1009
- log.info(resource.openStack.getStack(1), void 0, {
1010
- F: __dxlog_file3,
1011
- L: 85,
1012
- S: void 0,
1013
- C: (f, a) => f(...a)
1014
- });
1015
- log.info("\n", void 0, {
1016
- F: __dxlog_file3,
1017
- L: 86,
1018
- S: void 0,
1019
- C: (f, a) => f(...a)
1020
- });
981
+ log.info(`- ${resource.name} at`);
982
+ log.info(resource.openStack.getStack(1));
983
+ log.info("\n");
1021
984
  }
1022
985
  };
1023
986
  if (enabled) {
@@ -1025,7 +988,6 @@ if (enabled) {
1025
988
  }
1026
989
 
1027
990
  // src/task-scheduling.ts
1028
- var __dxlog_file4 = "/__w/dxos/dxos/packages/common/async/src/task-scheduling.ts";
1029
991
  var DeferredTask = class {
1030
992
  _ctx;
1031
993
  _callback;
@@ -1092,10 +1054,7 @@ var AsyncTask = class {
1092
1054
  * When the context is disposed, the task is cancelled and cannot be scheduled again.
1093
1055
  */
1094
1056
  open() {
1095
- this.#ctx = new Context2(void 0, {
1096
- F: __dxlog_file4,
1097
- L: 102
1098
- });
1057
+ this.#ctx = new Context2();
1099
1058
  }
1100
1059
  /**
1101
1060
  * Closes the task and waits for it to finish if it is running.
@@ -1236,9 +1195,9 @@ function _ts_decorate(decorators, target, key, desc) {
1236
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;
1237
1196
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1238
1197
  }
1239
- var __dxlog_file5 = "/__w/dxos/dxos/packages/common/async/src/persistent-lifecycle.ts";
1240
1198
  var INIT_RESTART_DELAY = 100;
1241
1199
  var DEFAULT_MAX_RESTART_DELAY = 5e3;
1200
+ var STABLE_CONNECTION_THRESHOLD = 5e3;
1242
1201
  var PersistentLifecycle = class extends Resource {
1243
1202
  _start;
1244
1203
  _stop;
@@ -1247,6 +1206,7 @@ var PersistentLifecycle = class extends Resource {
1247
1206
  _currentState = void 0;
1248
1207
  _restartTask = void 0;
1249
1208
  _restartAfter = 0;
1209
+ _connectedAt = void 0;
1250
1210
  constructor({ start, stop, onRestart, maxRestartDelay = DEFAULT_MAX_RESTART_DELAY }) {
1251
1211
  super();
1252
1212
  this._start = start;
@@ -1262,29 +1222,27 @@ var PersistentLifecycle = class extends Resource {
1262
1222
  try {
1263
1223
  await this._restart();
1264
1224
  } catch (err) {
1225
+ if (this._ctx?.disposed) {
1226
+ return;
1227
+ }
1265
1228
  log2.warn("Restart failed", {
1266
1229
  err
1267
- }, {
1268
- F: __dxlog_file5,
1269
- L: 72,
1270
- S: this,
1271
- C: (f, a) => f(...a)
1272
1230
  });
1273
1231
  this._restartTask?.schedule();
1274
1232
  }
1275
1233
  });
1276
- 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
+ }
1277
1241
  log2.warn("Start failed", {
1278
1242
  err
1279
- }, {
1280
- F: __dxlog_file5,
1281
- L: 78,
1282
- S: this,
1283
- C: (f, a) => f(...a)
1284
1243
  });
1285
1244
  this._restartTask?.schedule();
1286
- return void 0;
1287
- });
1245
+ }
1288
1246
  }
1289
1247
  async _close() {
1290
1248
  await this._restartTask?.join();
@@ -1294,12 +1252,11 @@ var PersistentLifecycle = class extends Resource {
1294
1252
  async _restart() {
1295
1253
  log2(`restarting in ${this._restartAfter}ms`, {
1296
1254
  state: this._lifecycleState
1297
- }, {
1298
- F: __dxlog_file5,
1299
- L: 91,
1300
- S: this,
1301
- C: (f, a) => f(...a)
1302
1255
  });
1256
+ if (this._connectedAt !== void 0 && Date.now() - this._connectedAt >= STABLE_CONNECTION_THRESHOLD) {
1257
+ this._restartAfter = 0;
1258
+ }
1259
+ this._connectedAt = void 0;
1303
1260
  await this._stopCurrentState();
1304
1261
  if (this._lifecycleState !== LifecycleState.OPEN) {
1305
1262
  return;
@@ -1309,7 +1266,7 @@ var PersistentLifecycle = class extends Resource {
1309
1266
  await warnAfterTimeout2(5e3, "Connection establishment takes too long", async () => {
1310
1267
  this._currentState = await this._start();
1311
1268
  });
1312
- this._restartAfter = 0;
1269
+ this._connectedAt = Date.now();
1313
1270
  await this._onRestart?.();
1314
1271
  }
1315
1272
  async _stopCurrentState() {
@@ -1317,12 +1274,7 @@ var PersistentLifecycle = class extends Resource {
1317
1274
  try {
1318
1275
  await this._stop(this._currentState);
1319
1276
  } catch (err) {
1320
- log2.catch(err, void 0, {
1321
- F: __dxlog_file5,
1322
- L: 113,
1323
- S: this,
1324
- C: (f, a) => f(...a)
1325
- });
1277
+ log2.catch(err);
1326
1278
  }
1327
1279
  this._currentState = void 0;
1328
1280
  }
@@ -1346,7 +1298,6 @@ _ts_decorate([
1346
1298
 
1347
1299
  // src/push-iterable.ts
1348
1300
  import { invariant as invariant2 } from "@dxos/invariant";
1349
- var __dxlog_file6 = "/__w/dxos/dxos/packages/common/async/src/push-iterable.ts";
1350
1301
  var makePushIterable = () => {
1351
1302
  const buf = [];
1352
1303
  const trigger2 = new Trigger({
@@ -1360,15 +1311,7 @@ var makePushIterable = () => {
1360
1311
  await trigger2.wait();
1361
1312
  }
1362
1313
  const item = buf.shift();
1363
- invariant2(item, void 0, {
1364
- F: __dxlog_file6,
1365
- L: 42,
1366
- S: this,
1367
- A: [
1368
- "item",
1369
- ""
1370
- ]
1371
- });
1314
+ invariant2(item);
1372
1315
  switch (item.kind) {
1373
1316
  case "next":
1374
1317
  return {