@dxos/async 0.8.4-main.7ace549 → 0.8.4-main.8342792842

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