@dxos/async 0.8.4-main.fffef41 → 0.8.4-staging.ac66bdf99f

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;
@@ -1072,6 +1075,82 @@ var DeferredTask = class {
1072
1075
  await this._currentTask;
1073
1076
  }
1074
1077
  };
1078
+ var AsyncTask = class {
1079
+ #callback;
1080
+ #ctx = void 0;
1081
+ #scheduled = false;
1082
+ #currentTask = null;
1083
+ #nextTask = new Trigger();
1084
+ constructor(callback) {
1085
+ this.#callback = callback;
1086
+ }
1087
+ get scheduled() {
1088
+ return this.#scheduled;
1089
+ }
1090
+ /**
1091
+ * Context of the resource that owns the task.
1092
+ * When the context is disposed, the task is cancelled and cannot be scheduled again.
1093
+ */
1094
+ open() {
1095
+ this.#ctx = new Context2(void 0, {
1096
+ F: __dxlog_file4,
1097
+ L: 102
1098
+ });
1099
+ }
1100
+ /**
1101
+ * Closes the task and waits for it to finish if it is running.
1102
+ */
1103
+ async close() {
1104
+ await this.#ctx?.dispose();
1105
+ await this.join();
1106
+ this.#ctx = void 0;
1107
+ }
1108
+ [Symbol.asyncDispose]() {
1109
+ return this.close();
1110
+ }
1111
+ /**
1112
+ * Schedule the task to run asynchronously.
1113
+ */
1114
+ // TODO(dmaretskyi): Add scheduleAt. Where the earlier time will override the later one.
1115
+ schedule() {
1116
+ if (!this.#ctx || this.#ctx.disposed) {
1117
+ throw new Error("AsyncTask not open");
1118
+ }
1119
+ if (this.#scheduled) {
1120
+ return;
1121
+ }
1122
+ scheduleTask(this.#ctx, async () => {
1123
+ await this.#currentTask;
1124
+ if (!this.#ctx || this.#ctx.disposed) {
1125
+ return;
1126
+ }
1127
+ this.#scheduled = false;
1128
+ const completionTrigger = this.#nextTask;
1129
+ this.#nextTask = new Trigger();
1130
+ this.#currentTask = runInContextAsync(this.#ctx, () => this.#callback()).then(() => {
1131
+ completionTrigger.wake();
1132
+ });
1133
+ });
1134
+ this.#scheduled = true;
1135
+ }
1136
+ /**
1137
+ * Schedule the task to run and wait for it to finish.
1138
+ */
1139
+ async runBlocking() {
1140
+ if (this.#ctx?.disposed) {
1141
+ throw new ContextDisposedError2();
1142
+ }
1143
+ this.schedule();
1144
+ await this.#nextTask.wait();
1145
+ }
1146
+ /**
1147
+ * Waits for the current task to finish if it is running.
1148
+ * Does not schedule a new task.
1149
+ */
1150
+ async join() {
1151
+ await this.#currentTask;
1152
+ }
1153
+ };
1075
1154
  var runInContext = (ctx, fn) => {
1076
1155
  try {
1077
1156
  fn();
@@ -1157,7 +1236,7 @@ function _ts_decorate(decorators, target, key, desc) {
1157
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;
1158
1237
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1159
1238
  }
1160
- 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";
1161
1240
  var INIT_RESTART_DELAY = 100;
1162
1241
  var DEFAULT_MAX_RESTART_DELAY = 5e3;
1163
1242
  var PersistentLifecycle = class extends Resource {
@@ -1186,7 +1265,7 @@ var PersistentLifecycle = class extends Resource {
1186
1265
  log2.warn("Restart failed", {
1187
1266
  err
1188
1267
  }, {
1189
- F: __dxlog_file4,
1268
+ F: __dxlog_file5,
1190
1269
  L: 72,
1191
1270
  S: this,
1192
1271
  C: (f, a) => f(...a)
@@ -1198,7 +1277,7 @@ var PersistentLifecycle = class extends Resource {
1198
1277
  log2.warn("Start failed", {
1199
1278
  err
1200
1279
  }, {
1201
- F: __dxlog_file4,
1280
+ F: __dxlog_file5,
1202
1281
  L: 78,
1203
1282
  S: this,
1204
1283
  C: (f, a) => f(...a)
@@ -1216,7 +1295,7 @@ var PersistentLifecycle = class extends Resource {
1216
1295
  log2(`restarting in ${this._restartAfter}ms`, {
1217
1296
  state: this._lifecycleState
1218
1297
  }, {
1219
- F: __dxlog_file4,
1298
+ F: __dxlog_file5,
1220
1299
  L: 91,
1221
1300
  S: this,
1222
1301
  C: (f, a) => f(...a)
@@ -1239,7 +1318,7 @@ var PersistentLifecycle = class extends Resource {
1239
1318
  await this._stop(this._currentState);
1240
1319
  } catch (err) {
1241
1320
  log2.catch(err, void 0, {
1242
- F: __dxlog_file4,
1321
+ F: __dxlog_file5,
1243
1322
  L: 113,
1244
1323
  S: this,
1245
1324
  C: (f, a) => f(...a)
@@ -1267,7 +1346,7 @@ _ts_decorate([
1267
1346
 
1268
1347
  // src/push-iterable.ts
1269
1348
  import { invariant as invariant2 } from "@dxos/invariant";
1270
- 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";
1271
1350
  var makePushIterable = () => {
1272
1351
  const buf = [];
1273
1352
  const trigger2 = new Trigger({
@@ -1282,7 +1361,7 @@ var makePushIterable = () => {
1282
1361
  }
1283
1362
  const item = buf.shift();
1284
1363
  invariant2(item, void 0, {
1285
- F: __dxlog_file5,
1364
+ F: __dxlog_file6,
1286
1365
  L: 42,
1287
1366
  S: this,
1288
1367
  A: [
@@ -1594,6 +1673,7 @@ var UpdateScheduler = class {
1594
1673
  }
1595
1674
  };
1596
1675
  export {
1676
+ AsyncTask,
1597
1677
  CancellableObservableProvider,
1598
1678
  DeferredTask,
1599
1679
  Event,