@context-action/core 1.1.3 → 1.2.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.
- package/CHANGELOG.md +10 -0
- package/README.md +5 -1
- package/dist/index.cjs +89 -21
- package/dist/index.d.cts +38 -16
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +38 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +89 -20
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1138,6 +1138,8 @@ var ActionRegister = class {
|
|
|
1138
1138
|
this.activeDispatches = /* @__PURE__ */ new Set();
|
|
1139
1139
|
this.activeHandlerPromises = /* @__PURE__ */ new Set();
|
|
1140
1140
|
this.dispatchConstructionDepth = 0;
|
|
1141
|
+
this.dispatchTraceSequence = 0;
|
|
1142
|
+
this.dispatchTraceListeners = /* @__PURE__ */ new Set();
|
|
1141
1143
|
this.actionDispatchers = /* @__PURE__ */ new Map();
|
|
1142
1144
|
this.actionResultDispatchers = /* @__PURE__ */ new Map();
|
|
1143
1145
|
this.name = config.name || "ActionRegister";
|
|
@@ -1301,6 +1303,61 @@ var ActionRegister = class {
|
|
|
1301
1303
|
console[level](`🎯 [${timestamp}] [${this.name}] ${message}`, data || "");
|
|
1302
1304
|
}
|
|
1303
1305
|
}
|
|
1306
|
+
beginDispatchTrace(action, options) {
|
|
1307
|
+
const dispatchId = `${this.name}:${++this.dispatchTraceSequence}`;
|
|
1308
|
+
const startedAt = Date.now();
|
|
1309
|
+
const metadata = options?.trace;
|
|
1310
|
+
let settled = false;
|
|
1311
|
+
const emit = (event) => {
|
|
1312
|
+
for (const listener of this.dispatchTraceListeners) try {
|
|
1313
|
+
listener(event);
|
|
1314
|
+
} catch (error) {
|
|
1315
|
+
this.log("Dispatch trace listener failed", error, "warn");
|
|
1316
|
+
}
|
|
1317
|
+
};
|
|
1318
|
+
emit({
|
|
1319
|
+
dispatchId,
|
|
1320
|
+
action,
|
|
1321
|
+
phase: "started",
|
|
1322
|
+
transactionId: metadata?.transactionId,
|
|
1323
|
+
origin: metadata?.origin,
|
|
1324
|
+
label: metadata?.label,
|
|
1325
|
+
startedAt
|
|
1326
|
+
});
|
|
1327
|
+
return {
|
|
1328
|
+
dispatchId,
|
|
1329
|
+
startedAt,
|
|
1330
|
+
settle: (status) => {
|
|
1331
|
+
if (settled) return;
|
|
1332
|
+
settled = true;
|
|
1333
|
+
emit({
|
|
1334
|
+
dispatchId,
|
|
1335
|
+
action,
|
|
1336
|
+
phase: "settled",
|
|
1337
|
+
status,
|
|
1338
|
+
transactionId: metadata?.transactionId,
|
|
1339
|
+
origin: metadata?.origin,
|
|
1340
|
+
label: metadata?.label,
|
|
1341
|
+
startedAt,
|
|
1342
|
+
endedAt: Date.now()
|
|
1343
|
+
});
|
|
1344
|
+
}
|
|
1345
|
+
};
|
|
1346
|
+
}
|
|
1347
|
+
withDispatchTrace(promise, trace, status) {
|
|
1348
|
+
return promise.then((result) => {
|
|
1349
|
+
trace.settle(status(result));
|
|
1350
|
+
return result;
|
|
1351
|
+
}, (error) => {
|
|
1352
|
+
trace.settle("failed");
|
|
1353
|
+
throw error;
|
|
1354
|
+
});
|
|
1355
|
+
}
|
|
1356
|
+
/** Subscribe to start/settled dispatch lifecycle events. */
|
|
1357
|
+
subscribeDispatchTrace(listener) {
|
|
1358
|
+
this.dispatchTraceListeners.add(listener);
|
|
1359
|
+
return () => this.dispatchTraceListeners.delete(listener);
|
|
1360
|
+
}
|
|
1304
1361
|
assertAcceptingWork() {
|
|
1305
1362
|
if (this.lifecycleState !== "active") throw new ActionRegisterDestroyedError(this.name, this.lifecycleState);
|
|
1306
1363
|
}
|
|
@@ -1440,7 +1497,8 @@ var ActionRegister = class {
|
|
|
1440
1497
|
dispatch(action, ...args) {
|
|
1441
1498
|
this.assertStringActionKey(action);
|
|
1442
1499
|
const [payload, options] = args;
|
|
1443
|
-
|
|
1500
|
+
const trace = this.beginDispatchTrace(String(action), options);
|
|
1501
|
+
if (this.lifecycleState !== "active") return this.withDispatchTrace(this.rejectedLifecyclePromise(), trace, () => "failed");
|
|
1444
1502
|
const timeoutScope = this.createTimeoutScope(action, options);
|
|
1445
1503
|
const dispatchHandlerPromises = /* @__PURE__ */ new Set();
|
|
1446
1504
|
const attemptState = { count: 0 };
|
|
@@ -1448,6 +1506,7 @@ var ActionRegister = class {
|
|
|
1448
1506
|
const hasTimingGuard = plan.debounceMs !== void 0 || plan.throttleMs !== void 0;
|
|
1449
1507
|
const notifiedObservers = /* @__PURE__ */ new Set();
|
|
1450
1508
|
const notifiedObserverOutcomes = /* @__PURE__ */ new Set();
|
|
1509
|
+
let dispatchTraceStatus = "completed";
|
|
1451
1510
|
let terminalErrorReported = false;
|
|
1452
1511
|
const reportTerminalError = (error) => {
|
|
1453
1512
|
if (terminalErrorReported) return;
|
|
@@ -1457,6 +1516,10 @@ var ActionRegister = class {
|
|
|
1457
1516
|
const notifyObservers = async (event) => {
|
|
1458
1517
|
if (notifiedObserverOutcomes.has(event.outcome)) return;
|
|
1459
1518
|
notifiedObserverOutcomes.add(event.outcome);
|
|
1519
|
+
if (event.outcome === "failed") dispatchTraceStatus = "failed";
|
|
1520
|
+
else if (event.outcome === "cancelled") dispatchTraceStatus = "cancelled";
|
|
1521
|
+
else if (event.outcome === "debounced") dispatchTraceStatus = "debounced";
|
|
1522
|
+
else if (event.outcome === "throttled") dispatchTraceStatus = "throttled";
|
|
1460
1523
|
await this.executeObservers(action, plan, event, notifiedObservers);
|
|
1461
1524
|
};
|
|
1462
1525
|
const pipelineOperation = async () => {
|
|
@@ -1620,8 +1683,9 @@ var ActionRegister = class {
|
|
|
1620
1683
|
});
|
|
1621
1684
|
throw error;
|
|
1622
1685
|
});
|
|
1623
|
-
|
|
1624
|
-
|
|
1686
|
+
const tracedPromise = this.withDispatchTrace(observedPromise, trace, () => dispatchTraceStatus);
|
|
1687
|
+
tracedPromise.catch(() => {});
|
|
1688
|
+
return tracedPromise;
|
|
1625
1689
|
}
|
|
1626
1690
|
/** Execute a dispatch operation with an optional whole-action retry policy. */
|
|
1627
1691
|
async executeWithRetry(operation, options, attemptState, shouldRetryResult, canRetry = () => true, telemetry, beforeRetry) {
|
|
@@ -1769,22 +1833,24 @@ var ActionRegister = class {
|
|
|
1769
1833
|
timeoutController?.signal
|
|
1770
1834
|
].filter((candidate) => Boolean(candidate));
|
|
1771
1835
|
let signal = signals[0];
|
|
1772
|
-
if (signals.length > 1)
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1836
|
+
if (signals.length > 1) {
|
|
1837
|
+
if (typeof AbortSignal.any === "function") signal = AbortSignal.any(signals);
|
|
1838
|
+
else {
|
|
1839
|
+
const mergedController = new AbortController();
|
|
1840
|
+
const forwardAbort = (source) => {
|
|
1841
|
+
if (!mergedController.signal.aborted) mergedController.abort(source.reason);
|
|
1842
|
+
};
|
|
1843
|
+
for (const source of signals) {
|
|
1844
|
+
if (source.aborted) {
|
|
1845
|
+
forwardAbort(source);
|
|
1846
|
+
break;
|
|
1847
|
+
}
|
|
1848
|
+
const listener = () => forwardAbort(source);
|
|
1849
|
+
source.addEventListener("abort", listener, { once: true });
|
|
1850
|
+
signalCleanups.push(() => source.removeEventListener("abort", listener));
|
|
1782
1851
|
}
|
|
1783
|
-
|
|
1784
|
-
source.addEventListener("abort", listener, { once: true });
|
|
1785
|
-
signalCleanups.push(() => source.removeEventListener("abort", listener));
|
|
1852
|
+
signal = mergedController.signal;
|
|
1786
1853
|
}
|
|
1787
|
-
signal = mergedController.signal;
|
|
1788
1854
|
}
|
|
1789
1855
|
let timer;
|
|
1790
1856
|
const timeoutPromise = timeoutController && timeout !== void 0 ? new Promise((_, reject) => {
|
|
@@ -2203,7 +2269,8 @@ var ActionRegister = class {
|
|
|
2203
2269
|
dispatchWithResult(action, ...args) {
|
|
2204
2270
|
this.assertStringActionKey(action);
|
|
2205
2271
|
const [payload, options] = args;
|
|
2206
|
-
|
|
2272
|
+
const trace = this.beginDispatchTrace(String(action), options);
|
|
2273
|
+
if (this.lifecycleState !== "active") return this.withDispatchTrace(this.rejectedLifecyclePromise(), trace, () => "failed");
|
|
2207
2274
|
const timeoutScope = this.createTimeoutScope(action, options);
|
|
2208
2275
|
const dispatchStartTime = Date.now();
|
|
2209
2276
|
const dispatchHandlerPromises = /* @__PURE__ */ new Set();
|
|
@@ -2399,8 +2466,9 @@ var ActionRegister = class {
|
|
|
2399
2466
|
});
|
|
2400
2467
|
throw error;
|
|
2401
2468
|
});
|
|
2402
|
-
|
|
2403
|
-
|
|
2469
|
+
const tracedPromise = this.withDispatchTrace(observedPromise, trace, (result) => result.success ? "completed" : result.outcome === "cancelled" ? "cancelled" : "failed");
|
|
2470
|
+
tracedPromise.catch(() => {});
|
|
2471
|
+
return tracedPromise;
|
|
2404
2472
|
}
|
|
2405
2473
|
async _performDispatchWithResult(action, payload, options, validation, plan, executedHandlers, dispatchHandlerPromises) {
|
|
2406
2474
|
const _startTime = Date.now();
|
|
@@ -3048,6 +3116,7 @@ var ActionRegister = class {
|
|
|
3048
3116
|
finalizeDestroy() {
|
|
3049
3117
|
if (this.lifecycleState === "destroyed") return;
|
|
3050
3118
|
this.clearAll();
|
|
3119
|
+
this.dispatchTraceListeners.clear();
|
|
3051
3120
|
this.actionExecutionModes.clear();
|
|
3052
3121
|
this.lifecycleState = "destroyed";
|
|
3053
3122
|
this.log("ActionRegister destroyed");
|