@context-action/core 0.7.4 โ 0.7.5
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/LICENSE +201 -0
- package/dist/index.cjs +29 -58
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +29 -58
- package/dist/index.js.map +1 -1
- package/package.json +17 -19
package/dist/index.js
CHANGED
|
@@ -50,8 +50,7 @@ async function executeSequential(context, createController) {
|
|
|
50
50
|
try {
|
|
51
51
|
if (context.aborted) break;
|
|
52
52
|
if (registration.config.condition) try {
|
|
53
|
-
|
|
54
|
-
if (!shouldExecute) {
|
|
53
|
+
if (!registration.config.condition(context.payload)) {
|
|
55
54
|
i++;
|
|
56
55
|
continue;
|
|
57
56
|
}
|
|
@@ -75,7 +74,6 @@ async function executeSequential(context, createController) {
|
|
|
75
74
|
timestamp: handlerError.timestamp,
|
|
76
75
|
severity: "non-blocking"
|
|
77
76
|
});
|
|
78
|
-
return void 0;
|
|
79
77
|
});
|
|
80
78
|
nonBlockingPromises.push(promiseWithErrorHandling);
|
|
81
79
|
} else if (result !== void 0 && !context.terminated) context.results.push(result);
|
|
@@ -113,15 +111,12 @@ async function executeSequential(context, createController) {
|
|
|
113
111
|
}
|
|
114
112
|
}
|
|
115
113
|
if (nonBlockingPromises.length > 0) await Promise.allSettled(nonBlockingPromises);
|
|
116
|
-
if (errors.length > 0) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}));
|
|
123
|
-
context.collectedErrors = handlerErrors;
|
|
124
|
-
}
|
|
114
|
+
if (errors.length > 0) context.collectedErrors = errors.map((err) => ({
|
|
115
|
+
handlerId: err.handlerId,
|
|
116
|
+
error: err.error,
|
|
117
|
+
timestamp: err.timestamp,
|
|
118
|
+
severity: "non-blocking"
|
|
119
|
+
}));
|
|
125
120
|
}
|
|
126
121
|
/**
|
|
127
122
|
* Execute handlers in parallel mode (all at once)
|
|
@@ -150,8 +145,7 @@ async function executeParallel(context, createController) {
|
|
|
150
145
|
const controller = createController(registration, _index);
|
|
151
146
|
try {
|
|
152
147
|
if (registration.config.condition) try {
|
|
153
|
-
|
|
154
|
-
if (!shouldExecute) return {
|
|
148
|
+
if (!registration.config.condition(context.payload)) return {
|
|
155
149
|
success: true,
|
|
156
150
|
handlerId: registration.id,
|
|
157
151
|
result: void 0,
|
|
@@ -169,10 +163,8 @@ async function executeParallel(context, createController) {
|
|
|
169
163
|
}
|
|
170
164
|
const result = registration.handler(context.payload, controller);
|
|
171
165
|
let handlerResult;
|
|
172
|
-
if (result instanceof Promise)
|
|
173
|
-
|
|
174
|
-
handlerResult = resolved;
|
|
175
|
-
} else handlerResult = result;
|
|
166
|
+
if (result instanceof Promise) handlerResult = await result;
|
|
167
|
+
else handlerResult = result;
|
|
176
168
|
/** Collect result if handler returned something and pipeline wasn't terminated */
|
|
177
169
|
if (handlerResult !== void 0 && !context.terminated) context.results.push(handlerResult);
|
|
178
170
|
return {
|
|
@@ -195,22 +187,15 @@ async function executeParallel(context, createController) {
|
|
|
195
187
|
const results = await Promise.allSettled(handlerPromises);
|
|
196
188
|
/** Check for any rejected blocking handlers */
|
|
197
189
|
const failures = results.filter((result, index) => {
|
|
198
|
-
if (result.status === "rejected")
|
|
199
|
-
const registration = runnableHandlers[index];
|
|
200
|
-
return registration?.config.blocking ?? false;
|
|
201
|
-
}
|
|
190
|
+
if (result.status === "rejected") return runnableHandlers[index]?.config.blocking ?? false;
|
|
202
191
|
return false;
|
|
203
192
|
});
|
|
204
|
-
if (failures.length > 0)
|
|
205
|
-
const firstFailure = failures[0];
|
|
206
|
-
throw firstFailure.reason;
|
|
207
|
-
}
|
|
193
|
+
if (failures.length > 0) throw failures[0].reason;
|
|
208
194
|
/** Check if any handler terminated the pipeline */
|
|
209
195
|
const terminatedResults = results.filter((result) => result.status === "fulfilled" && result.value.terminated);
|
|
210
196
|
if (terminatedResults.length > 0) {
|
|
211
197
|
context.terminated = true;
|
|
212
|
-
|
|
213
|
-
context.terminationResult = firstTerminated.value.result;
|
|
198
|
+
context.terminationResult = terminatedResults[0].value.result;
|
|
214
199
|
}
|
|
215
200
|
}
|
|
216
201
|
/**
|
|
@@ -242,8 +227,7 @@ async function executeRace(context, createController) {
|
|
|
242
227
|
const controller = createController(registration, _index);
|
|
243
228
|
try {
|
|
244
229
|
if (registration.config.condition) try {
|
|
245
|
-
|
|
246
|
-
if (!shouldExecute) return {
|
|
230
|
+
if (!registration.config.condition(context.payload)) return {
|
|
247
231
|
success: true,
|
|
248
232
|
handlerId: registration.id,
|
|
249
233
|
registration,
|
|
@@ -263,10 +247,8 @@ async function executeRace(context, createController) {
|
|
|
263
247
|
}
|
|
264
248
|
const result = registration.handler(context.payload, controller);
|
|
265
249
|
let handlerResult;
|
|
266
|
-
if (result instanceof Promise)
|
|
267
|
-
|
|
268
|
-
handlerResult = resolved;
|
|
269
|
-
} else handlerResult = result;
|
|
250
|
+
if (result instanceof Promise) handlerResult = await result;
|
|
251
|
+
else handlerResult = result;
|
|
270
252
|
return {
|
|
271
253
|
success: true,
|
|
272
254
|
handlerId: registration.id,
|
|
@@ -689,8 +671,7 @@ var OperationQueue = class {
|
|
|
689
671
|
* ๐ ์์
์๋ฃ ์ ํธ - ๋๊ธฐ ์ค์ธ ํ๋ก์ธ์ค๋ค์๊ฒ ์๋ฆผ
|
|
690
672
|
*/
|
|
691
673
|
notifyOperationComplete() {
|
|
692
|
-
|
|
693
|
-
resolvers.forEach((resolve) => resolve());
|
|
674
|
+
this.pendingResolvers.splice(0).forEach((resolve) => resolve());
|
|
694
675
|
}
|
|
695
676
|
/**
|
|
696
677
|
* ๐ ์๋ก์ด ์์
์ถ๊ฐ ์ ํธ - processQueue์์ ํธ์ถ
|
|
@@ -747,8 +728,7 @@ var OperationQueue = class {
|
|
|
747
728
|
});
|
|
748
729
|
this.queue = [];
|
|
749
730
|
this.processingPromise = null;
|
|
750
|
-
|
|
751
|
-
resolvers.forEach((resolve) => resolve());
|
|
731
|
+
this.pendingResolvers.splice(0).forEach((resolve) => resolve());
|
|
752
732
|
}
|
|
753
733
|
/**
|
|
754
734
|
* ํ ํฌ๊ธฐ ์กฐํ
|
|
@@ -823,8 +803,7 @@ var ActionRegister = class {
|
|
|
823
803
|
*/
|
|
824
804
|
register(action, handler, config = {}) {
|
|
825
805
|
const handlerId = config.id || this.generateHandlerId(action);
|
|
826
|
-
|
|
827
|
-
return unregisterFn;
|
|
806
|
+
return this._performRegistrationSync(action, handler, config, handlerId);
|
|
828
807
|
}
|
|
829
808
|
/**
|
|
830
809
|
* ๐ Unified logging method with cached debug mode check
|
|
@@ -1041,12 +1020,10 @@ var ActionRegister = class {
|
|
|
1041
1020
|
}
|
|
1042
1021
|
}
|
|
1043
1022
|
if (debounceMs !== void 0) {
|
|
1044
|
-
|
|
1045
|
-
if (!shouldProceed) return;
|
|
1023
|
+
if (!await this.actionGuard.debounce(actionKey, debounceMs)) return;
|
|
1046
1024
|
}
|
|
1047
1025
|
if (throttleMs !== void 0) {
|
|
1048
|
-
|
|
1049
|
-
if (!shouldProceed) return;
|
|
1026
|
+
if (!this.actionGuard.throttle(actionKey, throttleMs)) return;
|
|
1050
1027
|
}
|
|
1051
1028
|
const currentExecutionMode = options?.executionMode || this.actionExecutionModes.get(action) || this.executionMode;
|
|
1052
1029
|
const context = {
|
|
@@ -1177,8 +1154,7 @@ var ActionRegister = class {
|
|
|
1177
1154
|
let errors = [];
|
|
1178
1155
|
try {
|
|
1179
1156
|
await this.executePipeline(context, autoAbortController, options?.autoAbort);
|
|
1180
|
-
|
|
1181
|
-
errors = contextWithErrors.collectedErrors || [];
|
|
1157
|
+
errors = context.collectedErrors || [];
|
|
1182
1158
|
const executedCount = Math.min(context.currentIndex + (context.aborted ? 0 : 1), filteredHandlers.length);
|
|
1183
1159
|
for (let i = 0; i < executedCount; i++) {
|
|
1184
1160
|
const handler = filteredHandlers[i];
|
|
@@ -1187,8 +1163,7 @@ var ActionRegister = class {
|
|
|
1187
1163
|
if (handlerResult) handlerResult.executed = true;
|
|
1188
1164
|
}
|
|
1189
1165
|
} catch (error) {
|
|
1190
|
-
|
|
1191
|
-
errors = contextWithErrors.collectedErrors || [];
|
|
1166
|
+
errors = context.collectedErrors || [];
|
|
1192
1167
|
executionError = error instanceof Error ? error : new Error(String(error));
|
|
1193
1168
|
errors.push({
|
|
1194
1169
|
handlerId: "pipeline",
|
|
@@ -1264,8 +1239,7 @@ var ActionRegister = class {
|
|
|
1264
1239
|
}
|
|
1265
1240
|
}
|
|
1266
1241
|
if (debounceMs !== void 0) {
|
|
1267
|
-
|
|
1268
|
-
if (!shouldProceed) return {
|
|
1242
|
+
if (!await this.actionGuard.debounce(actionKey, debounceMs)) return {
|
|
1269
1243
|
success: false,
|
|
1270
1244
|
aborted: true,
|
|
1271
1245
|
abortReason: "Debounced execution",
|
|
@@ -1287,8 +1261,7 @@ var ActionRegister = class {
|
|
|
1287
1261
|
};
|
|
1288
1262
|
}
|
|
1289
1263
|
if (throttleMs !== void 0) {
|
|
1290
|
-
|
|
1291
|
-
if (!shouldProceed) return {
|
|
1264
|
+
if (!this.actionGuard.throttle(actionKey, throttleMs)) return {
|
|
1292
1265
|
success: false,
|
|
1293
1266
|
aborted: true,
|
|
1294
1267
|
abortReason: "Throttled execution",
|
|
@@ -1376,7 +1349,7 @@ var ActionRegister = class {
|
|
|
1376
1349
|
if (!filterOptions) return handlers;
|
|
1377
1350
|
const handlerIdSet = filterOptions.handlerIds ? new Set(filterOptions.handlerIds) : null;
|
|
1378
1351
|
const excludeIdSet = filterOptions.excludeHandlerIds ? new Set(filterOptions.excludeHandlerIds) : null;
|
|
1379
|
-
|
|
1352
|
+
return handlers.filter((registration) => {
|
|
1380
1353
|
const config = registration.config;
|
|
1381
1354
|
if (handlerIdSet && !handlerIdSet.has(config.id)) return false;
|
|
1382
1355
|
if (excludeIdSet && excludeIdSet.has(config.id)) return false;
|
|
@@ -1388,15 +1361,14 @@ var ActionRegister = class {
|
|
|
1388
1361
|
if (filterOptions.custom && !filterOptions.custom(config)) return false;
|
|
1389
1362
|
return true;
|
|
1390
1363
|
});
|
|
1391
|
-
return filtered;
|
|
1392
1364
|
}
|
|
1393
1365
|
processResults(context, resultOptions) {
|
|
1394
1366
|
const results = context.results;
|
|
1395
1367
|
if (context.terminated && context.terminationResult !== void 0) return context.terminationResult;
|
|
1396
1368
|
if (!resultOptions) return results.length > 0 ? results[results.length - 1] : void 0;
|
|
1397
|
-
if (!resultOptions.collect && !resultOptions.strategy) return
|
|
1369
|
+
if (!resultOptions.collect && !resultOptions.strategy) return;
|
|
1398
1370
|
const limitedResults = resultOptions.maxResults ? results.slice(0, resultOptions.maxResults) : results;
|
|
1399
|
-
if (limitedResults.length === 0) return
|
|
1371
|
+
if (limitedResults.length === 0) return;
|
|
1400
1372
|
switch (resultOptions.strategy) {
|
|
1401
1373
|
case "first": return limitedResults[0];
|
|
1402
1374
|
case "last": return limitedResults[limitedResults.length - 1];
|
|
@@ -1562,13 +1534,12 @@ var ActionRegister = class {
|
|
|
1562
1534
|
priority,
|
|
1563
1535
|
handlers: handlers.map((h) => ({ id: h.config.id }))
|
|
1564
1536
|
}));
|
|
1565
|
-
const executionStats = void 0;
|
|
1566
1537
|
return {
|
|
1567
1538
|
action,
|
|
1568
1539
|
handlerCount: pipeline.length,
|
|
1569
1540
|
totalHandlers: pipeline.length,
|
|
1570
1541
|
handlersByPriority,
|
|
1571
|
-
executionStats,
|
|
1542
|
+
executionStats: void 0,
|
|
1572
1543
|
lastRegistered: this.lastRegisteredTimestamps.get(action)
|
|
1573
1544
|
};
|
|
1574
1545
|
}
|