@dxos/async 0.8.4-main.fbb7a13 → 0.8.4-main.fcfe5033a5

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.
@@ -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);
@@ -607,7 +609,7 @@ var MutexGuard = class {
607
609
  this.release();
608
610
  }
609
611
  };
610
- var classMutexSymbol = Symbol("class-mutex");
612
+ var classMutexSymbol = /* @__PURE__ */ Symbol("class-mutex");
611
613
  var FORCE_DISABLE_WARNING = false;
612
614
  var enableWarning = !FORCE_DISABLE_WARNING && globalThis.mochaExecutor;
613
615
  var synchronized = (target, propertyName, descriptor) => {
@@ -935,7 +937,7 @@ import { warnAfterTimeout as warnAfterTimeout2 } from "@dxos/debug";
935
937
  import { log as log2 } from "@dxos/log";
936
938
 
937
939
  // src/task-scheduling.ts
938
- import { ContextDisposedError as ContextDisposedError2 } from "@dxos/context";
940
+ import { Context as Context2, ContextDisposedError as ContextDisposedError2 } from "@dxos/context";
939
941
  import { StackTrace as StackTrace2 } from "@dxos/debug";
940
942
 
941
943
  // src/track-leaks.ts
@@ -944,7 +946,7 @@ import { log } from "@dxos/log";
944
946
  var __dxlog_file3 = "/__w/dxos/dxos/packages/common/async/src/track-leaks.ts";
945
947
  var enabled = typeof process !== "undefined" && !!process.env.DX_TRACK_LEAKS;
946
948
  var openResources = /* @__PURE__ */ new Set();
947
- var handleSymbol = Symbol("checkLeaksHandle");
949
+ var handleSymbol = /* @__PURE__ */ Symbol("checkLeaksHandle");
948
950
  var trackResource = (resourceProvider) => {
949
951
  if (!enabled) {
950
952
  return () => {
@@ -1023,6 +1025,7 @@ if (enabled) {
1023
1025
  }
1024
1026
 
1025
1027
  // src/task-scheduling.ts
1028
+ var __dxlog_file4 = "/__w/dxos/dxos/packages/common/async/src/task-scheduling.ts";
1026
1029
  var DeferredTask = class {
1027
1030
  _ctx;
1028
1031
  _callback;
@@ -1088,16 +1091,19 @@ var AsyncTask = class {
1088
1091
  * Context of the resource that owns the task.
1089
1092
  * When the context is disposed, the task is cancelled and cannot be scheduled again.
1090
1093
  */
1091
- // TODO(dmaretskyi): We don't really need to pass ctx in here, since close will also signal dispose.
1092
- open(ctx) {
1093
- this.#ctx = ctx;
1094
+ open() {
1095
+ this.#ctx = new Context2(void 0, {
1096
+ F: __dxlog_file4,
1097
+ L: 102
1098
+ });
1094
1099
  }
1095
1100
  /**
1096
1101
  * Closes the task and waits for it to finish if it is running.
1097
1102
  */
1098
1103
  async close() {
1099
- this.#ctx = void 0;
1104
+ await this.#ctx?.dispose();
1100
1105
  await this.join();
1106
+ this.#ctx = void 0;
1101
1107
  }
1102
1108
  [Symbol.asyncDispose]() {
1103
1109
  return this.close();
@@ -1105,6 +1111,7 @@ var AsyncTask = class {
1105
1111
  /**
1106
1112
  * Schedule the task to run asynchronously.
1107
1113
  */
1114
+ // TODO(dmaretskyi): Add scheduleAt. Where the earlier time will override the later one.
1108
1115
  schedule() {
1109
1116
  if (!this.#ctx || this.#ctx.disposed) {
1110
1117
  throw new Error("AsyncTask not open");
@@ -1229,7 +1236,7 @@ function _ts_decorate(decorators, target, key, desc) {
1229
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;
1230
1237
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1231
1238
  }
1232
- var __dxlog_file4 = "/__w/dxos/dxos/packages/common/async/src/persistent-lifecycle.ts";
1239
+ var __dxlog_file5 = "/__w/dxos/dxos/packages/common/async/src/persistent-lifecycle.ts";
1233
1240
  var INIT_RESTART_DELAY = 100;
1234
1241
  var DEFAULT_MAX_RESTART_DELAY = 5e3;
1235
1242
  var PersistentLifecycle = class extends Resource {
@@ -1258,7 +1265,7 @@ var PersistentLifecycle = class extends Resource {
1258
1265
  log2.warn("Restart failed", {
1259
1266
  err
1260
1267
  }, {
1261
- F: __dxlog_file4,
1268
+ F: __dxlog_file5,
1262
1269
  L: 72,
1263
1270
  S: this,
1264
1271
  C: (f, a) => f(...a)
@@ -1270,7 +1277,7 @@ var PersistentLifecycle = class extends Resource {
1270
1277
  log2.warn("Start failed", {
1271
1278
  err
1272
1279
  }, {
1273
- F: __dxlog_file4,
1280
+ F: __dxlog_file5,
1274
1281
  L: 78,
1275
1282
  S: this,
1276
1283
  C: (f, a) => f(...a)
@@ -1288,7 +1295,7 @@ var PersistentLifecycle = class extends Resource {
1288
1295
  log2(`restarting in ${this._restartAfter}ms`, {
1289
1296
  state: this._lifecycleState
1290
1297
  }, {
1291
- F: __dxlog_file4,
1298
+ F: __dxlog_file5,
1292
1299
  L: 91,
1293
1300
  S: this,
1294
1301
  C: (f, a) => f(...a)
@@ -1311,7 +1318,7 @@ var PersistentLifecycle = class extends Resource {
1311
1318
  await this._stop(this._currentState);
1312
1319
  } catch (err) {
1313
1320
  log2.catch(err, void 0, {
1314
- F: __dxlog_file4,
1321
+ F: __dxlog_file5,
1315
1322
  L: 113,
1316
1323
  S: this,
1317
1324
  C: (f, a) => f(...a)
@@ -1339,7 +1346,7 @@ _ts_decorate([
1339
1346
 
1340
1347
  // src/push-iterable.ts
1341
1348
  import { invariant as invariant2 } from "@dxos/invariant";
1342
- var __dxlog_file5 = "/__w/dxos/dxos/packages/common/async/src/push-iterable.ts";
1349
+ var __dxlog_file6 = "/__w/dxos/dxos/packages/common/async/src/push-iterable.ts";
1343
1350
  var makePushIterable = () => {
1344
1351
  const buf = [];
1345
1352
  const trigger2 = new Trigger({
@@ -1354,7 +1361,7 @@ var makePushIterable = () => {
1354
1361
  }
1355
1362
  const item = buf.shift();
1356
1363
  invariant2(item, void 0, {
1357
- F: __dxlog_file5,
1364
+ F: __dxlog_file6,
1358
1365
  L: 42,
1359
1366
  S: this,
1360
1367
  A: [