@rotorsoft/act 0.15.0 → 0.16.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/README.md +12 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/@types/act.d.ts +52 -36
- package/dist/@types/act.d.ts.map +1 -1
- package/dist/@types/types/reaction.d.ts +17 -1
- package/dist/@types/types/reaction.d.ts.map +1 -1
- package/dist/index.cjs +84 -40
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +84 -40
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/@types/act.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Actor, Committed, Drain, DrainOptions, Lease, Query, Registry, Schema, SchemaRegister, Schemas, Snapshot, State, Target } from "./types/index.js";
|
|
1
|
+
import type { Actor, Committed, Drain, DrainOptions, Lease, Query, Registry, Schema, SchemaRegister, Schemas, SettleOptions, Snapshot, State, Target } from "./types/index.js";
|
|
2
2
|
/**
|
|
3
3
|
* @category Orchestrator
|
|
4
4
|
* @see Store
|
|
@@ -31,7 +31,9 @@ export declare class Act<TSchemaReg extends SchemaRegister<TActions>, TEvents ex
|
|
|
31
31
|
private _emitter;
|
|
32
32
|
private _drain_locked;
|
|
33
33
|
private _drain_lag2lead_ratio;
|
|
34
|
-
private
|
|
34
|
+
private _correlation_timer;
|
|
35
|
+
private _settle_timer;
|
|
36
|
+
private _settling;
|
|
35
37
|
/**
|
|
36
38
|
* Emit a lifecycle event (internal use, but can be used for custom listeners).
|
|
37
39
|
*
|
|
@@ -44,6 +46,7 @@ export declare class Act<TSchemaReg extends SchemaRegister<TActions>, TEvents ex
|
|
|
44
46
|
emit(event: "blocked", args: Array<Lease & {
|
|
45
47
|
error: string;
|
|
46
48
|
}>): boolean;
|
|
49
|
+
emit(event: "settled", args: Drain<TEvents>): boolean;
|
|
47
50
|
/**
|
|
48
51
|
* Register a listener for a lifecycle event ("committed", "acked", or "blocked").
|
|
49
52
|
*
|
|
@@ -56,6 +59,7 @@ export declare class Act<TSchemaReg extends SchemaRegister<TActions>, TEvents ex
|
|
|
56
59
|
on(event: "blocked", listener: (args: Array<Lease & {
|
|
57
60
|
error: string;
|
|
58
61
|
}>) => void): this;
|
|
62
|
+
on(event: "settled", listener: (args: Drain<TEvents>) => void): this;
|
|
59
63
|
/**
|
|
60
64
|
* Remove a listener for a lifecycle event.
|
|
61
65
|
*
|
|
@@ -68,6 +72,7 @@ export declare class Act<TSchemaReg extends SchemaRegister<TActions>, TEvents ex
|
|
|
68
72
|
off(event: "blocked", listener: (args: Array<Lease & {
|
|
69
73
|
error: string;
|
|
70
74
|
}>) => void): this;
|
|
75
|
+
off(event: "settled", listener: (args: Drain<TEvents>) => void): this;
|
|
71
76
|
/**
|
|
72
77
|
* Create a new Act orchestrator.
|
|
73
78
|
*
|
|
@@ -300,7 +305,7 @@ export declare class Act<TSchemaReg extends SchemaRegister<TActions>, TEvents ex
|
|
|
300
305
|
/**
|
|
301
306
|
* Processes pending reactions by draining uncommitted events from the event store.
|
|
302
307
|
*
|
|
303
|
-
*
|
|
308
|
+
* Runs a single drain cycle:
|
|
304
309
|
* 1. Polls the store for streams with uncommitted events
|
|
305
310
|
* 2. Leases streams to prevent concurrent processing
|
|
306
311
|
* 3. Fetches events for each leased stream
|
|
@@ -310,7 +315,8 @@ export declare class Act<TSchemaReg extends SchemaRegister<TActions>, TEvents ex
|
|
|
310
315
|
* Drain uses a dual-frontier strategy to balance processing of new streams (lagging)
|
|
311
316
|
* vs active streams (leading). The ratio adapts based on event pressure.
|
|
312
317
|
*
|
|
313
|
-
* Call
|
|
318
|
+
* Call `correlate()` before `drain()` to discover target streams. For a higher-level
|
|
319
|
+
* API that handles debouncing, correlation, and signaling automatically, use {@link settle}.
|
|
314
320
|
*
|
|
315
321
|
* @param options - Drain configuration options
|
|
316
322
|
* @param options.streamLimit - Maximum number of streams to process per cycle (default: 10)
|
|
@@ -318,46 +324,20 @@ export declare class Act<TSchemaReg extends SchemaRegister<TActions>, TEvents ex
|
|
|
318
324
|
* @param options.leaseMillis - Lease duration in milliseconds (default: 10000)
|
|
319
325
|
* @returns Drain statistics with fetched, leased, acked, and blocked counts
|
|
320
326
|
*
|
|
321
|
-
* @example
|
|
327
|
+
* @example In tests and scripts
|
|
322
328
|
* ```typescript
|
|
323
|
-
* // Process reactions after each action
|
|
324
329
|
* await app.do("createUser", target, payload);
|
|
330
|
+
* await app.correlate();
|
|
325
331
|
* await app.drain();
|
|
326
332
|
* ```
|
|
327
333
|
*
|
|
328
|
-
* @example
|
|
329
|
-
* ```typescript
|
|
330
|
-
* setInterval(async () => {
|
|
331
|
-
* try {
|
|
332
|
-
* const result = await app.drain({
|
|
333
|
-
* streamLimit: 20,
|
|
334
|
-
* eventLimit: 50
|
|
335
|
-
* });
|
|
336
|
-
* if (result.acked.length) {
|
|
337
|
-
* console.log(`Processed ${result.acked.length} streams`);
|
|
338
|
-
* }
|
|
339
|
-
* } catch (error) {
|
|
340
|
-
* console.error("Drain error:", error);
|
|
341
|
-
* }
|
|
342
|
-
* }, 5000); // Every 5 seconds
|
|
343
|
-
* ```
|
|
344
|
-
*
|
|
345
|
-
* @example With lifecycle listeners
|
|
334
|
+
* @example In production, prefer settle()
|
|
346
335
|
* ```typescript
|
|
347
|
-
* app.
|
|
348
|
-
*
|
|
349
|
-
* });
|
|
350
|
-
*
|
|
351
|
-
* app.on("blocked", (blocked) => {
|
|
352
|
-
* console.error(`Blocked ${blocked.length} streams due to errors`);
|
|
353
|
-
* blocked.forEach(({ stream, error }) => {
|
|
354
|
-
* console.error(`Stream ${stream}: ${error}`);
|
|
355
|
-
* });
|
|
356
|
-
* });
|
|
357
|
-
*
|
|
358
|
-
* await app.drain();
|
|
336
|
+
* await app.do("CreateItem", target, input);
|
|
337
|
+
* app.settle(); // debounced correlate→drain, emits "settled"
|
|
359
338
|
* ```
|
|
360
339
|
*
|
|
340
|
+
* @see {@link settle} for debounced correlate→drain with lifecycle events
|
|
361
341
|
* @see {@link correlate} for dynamic stream discovery
|
|
362
342
|
* @see {@link start_correlations} for automatic correlation
|
|
363
343
|
*/
|
|
@@ -485,5 +465,41 @@ export declare class Act<TSchemaReg extends SchemaRegister<TActions>, TEvents ex
|
|
|
485
465
|
* @see {@link start_correlations}
|
|
486
466
|
*/
|
|
487
467
|
stop_correlations(): void;
|
|
468
|
+
/**
|
|
469
|
+
* Cancels any pending or active settle cycle.
|
|
470
|
+
*
|
|
471
|
+
* @see {@link settle}
|
|
472
|
+
*/
|
|
473
|
+
stop_settling(): void;
|
|
474
|
+
/**
|
|
475
|
+
* Debounced, non-blocking correlate→drain cycle.
|
|
476
|
+
*
|
|
477
|
+
* Call this after `app.do()` to schedule a background drain. Multiple rapid
|
|
478
|
+
* calls within the debounce window are coalesced into a single cycle. Runs
|
|
479
|
+
* correlate→drain in a loop until the system reaches a consistent state,
|
|
480
|
+
* then emits the `"settled"` lifecycle event.
|
|
481
|
+
*
|
|
482
|
+
* @param options - Settle configuration options
|
|
483
|
+
* @param options.debounceMs - Debounce window in milliseconds (default: 10)
|
|
484
|
+
* @param options.correlate - Query filter for correlation scans (default: `{ after: -1, limit: 100 }`)
|
|
485
|
+
* @param options.maxPasses - Maximum correlate→drain loops (default: 5)
|
|
486
|
+
* @param options.streamLimit - Maximum streams per drain cycle (default: 10)
|
|
487
|
+
* @param options.eventLimit - Maximum events per stream (default: 10)
|
|
488
|
+
* @param options.leaseMillis - Lease duration in milliseconds (default: 10000)
|
|
489
|
+
*
|
|
490
|
+
* @example API mutations
|
|
491
|
+
* ```typescript
|
|
492
|
+
* await app.do("CreateItem", target, input);
|
|
493
|
+
* app.settle(); // non-blocking, returns immediately
|
|
494
|
+
*
|
|
495
|
+
* app.on("settled", (drain) => {
|
|
496
|
+
* // notify SSE clients, invalidate caches, etc.
|
|
497
|
+
* });
|
|
498
|
+
* ```
|
|
499
|
+
*
|
|
500
|
+
* @see {@link drain} for single synchronous drain cycles
|
|
501
|
+
* @see {@link correlate} for manual correlation
|
|
502
|
+
*/
|
|
503
|
+
settle(options?: SettleOptions): void;
|
|
488
504
|
}
|
|
489
505
|
//# sourceMappingURL=act.d.ts.map
|
package/dist/@types/act.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"act.d.ts","sourceRoot":"","sources":["../../src/act.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,KAAK,EACL,SAAS,EACT,KAAK,EACL,YAAY,EACZ,KAAK,EACL,KAAK,EAEL,QAAQ,EACR,MAAM,EACN,cAAc,EACd,OAAO,EACP,QAAQ,EACR,KAAK,EACL,MAAM,EACP,MAAM,kBAAkB,CAAC;AAI1B;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,qBAAa,GAAG,CACd,UAAU,SAAS,cAAc,CAAC,QAAQ,CAAC,EAC3C,OAAO,SAAS,OAAO,EACvB,QAAQ,SAAS,OAAO,EACxB,SAAS,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,EAChE,MAAM,SAAS,KAAK,GAAG,KAAK;
|
|
1
|
+
{"version":3,"file":"act.d.ts","sourceRoot":"","sources":["../../src/act.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,KAAK,EACL,SAAS,EACT,KAAK,EACL,YAAY,EACZ,KAAK,EACL,KAAK,EAEL,QAAQ,EACR,MAAM,EACN,cAAc,EACd,OAAO,EACP,aAAa,EACb,QAAQ,EACR,KAAK,EACL,MAAM,EACP,MAAM,kBAAkB,CAAC;AAI1B;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,qBAAa,GAAG,CACd,UAAU,SAAS,cAAc,CAAC,QAAQ,CAAC,EAC3C,OAAO,SAAS,OAAO,EACvB,QAAQ,SAAS,OAAO,EACxB,SAAS,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CA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UG;YACW,MAAM;IA4CpB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAsCG;IACG,KAAK,CAAC,EACV,WAAgB,EAChB,UAAe,EACf,WAAoB,GACrB,GAAE,YAAiB,GAAG,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAmH9C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4CG;IACG,SAAS,CACb,KAAK,GAAE,KAAgC,GACtC,OAAO,CAAC;QAAE,MAAM,EAAE,KAAK,EAAE,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;IAwChD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAsDG;IACH,kBAAkB,CAChB,KAAK,GAAE,KAAU,EACjB,SAAS,SAAS,EAClB,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,KAAK,EAAE,KAAK,IAAI,GACnC,OAAO;IAkBV;;;;;;;;;;;;;;;;OAgBG;IACH,iBAAiB;IAOjB;;;;OAIG;IACH,aAAa;IAOb;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4BG;IACH,MAAM,CAAC,OAAO,GAAE,aAAkB,GAAG,IAAI;CA8B1C"}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @category Types
|
|
5
5
|
* Types for reactions, leases, and fetch results in the Act Framework.
|
|
6
6
|
*/
|
|
7
|
-
import type { Actor, Committed, Dispatcher, Schema, Schemas, Snapshot } from "./action.js";
|
|
7
|
+
import type { Actor, Committed, Dispatcher, Query, Schema, Schemas, Snapshot } from "./action.js";
|
|
8
8
|
/**
|
|
9
9
|
* Reaction handler function that processes committed events.
|
|
10
10
|
*
|
|
@@ -228,4 +228,20 @@ export type Drain<TEvents extends Schemas> = {
|
|
|
228
228
|
readonly error: string;
|
|
229
229
|
}>;
|
|
230
230
|
};
|
|
231
|
+
/**
|
|
232
|
+
* Options for the debounced settle cycle.
|
|
233
|
+
*
|
|
234
|
+
* Extends {@link DrainOptions} with parameters that control the debounce
|
|
235
|
+
* window, the correlation query, and the maximum number of correlate→drain
|
|
236
|
+
* passes.
|
|
237
|
+
*
|
|
238
|
+
* @property debounceMs - Debounce window in milliseconds (default: 10)
|
|
239
|
+
* @property correlate - Query filter for correlation scans (default: `{ after: -1, limit: 100 }`)
|
|
240
|
+
* @property maxPasses - Maximum correlate→drain loops (default: 5)
|
|
241
|
+
*/
|
|
242
|
+
export type SettleOptions = DrainOptions & {
|
|
243
|
+
readonly debounceMs?: number;
|
|
244
|
+
readonly correlate?: Query;
|
|
245
|
+
readonly maxPasses?: number;
|
|
246
|
+
};
|
|
231
247
|
//# sourceMappingURL=reaction.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reaction.d.ts","sourceRoot":"","sources":["../../../src/types/reaction.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,KAAK,EACV,KAAK,EACL,SAAS,EACT,UAAU,EACV,MAAM,EACN,OAAO,EACP,QAAQ,EACT,MAAM,aAAa,CAAC;AAErB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,MAAM,eAAe,CACzB,OAAO,SAAS,OAAO,EACvB,IAAI,SAAS,MAAM,OAAO,EAC1B,QAAQ,SAAS,OAAO,GAAG,OAAO,EAClC,MAAM,SAAS,KAAK,GAAG,KAAK,IAC1B,CACF,KAAK,EAAE,SAAS,CAAC,OAAO,EAAE,IAAI,CAAC,EAC/B,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,UAAU,CAAC,QAAQ,EAAE,MAAM,CAAC,KAC9B,OAAO,CAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC,CAAC;AAE/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,MAAM,MAAM,gBAAgB,CAC1B,OAAO,SAAS,OAAO,EACvB,IAAI,S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AC,SAAS,CAAC,EAAE,KAAK,CAAC;IAC3B,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAC7B,CAAC"}
|
package/dist/index.cjs
CHANGED
|
@@ -764,13 +764,16 @@ var Act = class {
|
|
|
764
764
|
dispose(() => {
|
|
765
765
|
this._emitter.removeAllListeners();
|
|
766
766
|
this.stop_correlations();
|
|
767
|
+
this.stop_settling();
|
|
767
768
|
return Promise.resolve();
|
|
768
769
|
});
|
|
769
770
|
}
|
|
770
771
|
_emitter = new import_events.default();
|
|
771
772
|
_drain_locked = false;
|
|
772
773
|
_drain_lag2lead_ratio = 0.5;
|
|
773
|
-
|
|
774
|
+
_correlation_timer = void 0;
|
|
775
|
+
_settle_timer = void 0;
|
|
776
|
+
_settling = false;
|
|
774
777
|
emit(event, args) {
|
|
775
778
|
return this._emitter.emit(event, args);
|
|
776
779
|
}
|
|
@@ -1020,7 +1023,7 @@ var Act = class {
|
|
|
1020
1023
|
/**
|
|
1021
1024
|
* Processes pending reactions by draining uncommitted events from the event store.
|
|
1022
1025
|
*
|
|
1023
|
-
*
|
|
1026
|
+
* Runs a single drain cycle:
|
|
1024
1027
|
* 1. Polls the store for streams with uncommitted events
|
|
1025
1028
|
* 2. Leases streams to prevent concurrent processing
|
|
1026
1029
|
* 3. Fetches events for each leased stream
|
|
@@ -1030,7 +1033,8 @@ var Act = class {
|
|
|
1030
1033
|
* Drain uses a dual-frontier strategy to balance processing of new streams (lagging)
|
|
1031
1034
|
* vs active streams (leading). The ratio adapts based on event pressure.
|
|
1032
1035
|
*
|
|
1033
|
-
* Call
|
|
1036
|
+
* Call `correlate()` before `drain()` to discover target streams. For a higher-level
|
|
1037
|
+
* API that handles debouncing, correlation, and signaling automatically, use {@link settle}.
|
|
1034
1038
|
*
|
|
1035
1039
|
* @param options - Drain configuration options
|
|
1036
1040
|
* @param options.streamLimit - Maximum number of streams to process per cycle (default: 10)
|
|
@@ -1038,46 +1042,20 @@ var Act = class {
|
|
|
1038
1042
|
* @param options.leaseMillis - Lease duration in milliseconds (default: 10000)
|
|
1039
1043
|
* @returns Drain statistics with fetched, leased, acked, and blocked counts
|
|
1040
1044
|
*
|
|
1041
|
-
* @example
|
|
1045
|
+
* @example In tests and scripts
|
|
1042
1046
|
* ```typescript
|
|
1043
|
-
* // Process reactions after each action
|
|
1044
1047
|
* await app.do("createUser", target, payload);
|
|
1048
|
+
* await app.correlate();
|
|
1045
1049
|
* await app.drain();
|
|
1046
1050
|
* ```
|
|
1047
1051
|
*
|
|
1048
|
-
* @example
|
|
1052
|
+
* @example In production, prefer settle()
|
|
1049
1053
|
* ```typescript
|
|
1050
|
-
*
|
|
1051
|
-
*
|
|
1052
|
-
* const result = await app.drain({
|
|
1053
|
-
* streamLimit: 20,
|
|
1054
|
-
* eventLimit: 50
|
|
1055
|
-
* });
|
|
1056
|
-
* if (result.acked.length) {
|
|
1057
|
-
* console.log(`Processed ${result.acked.length} streams`);
|
|
1058
|
-
* }
|
|
1059
|
-
* } catch (error) {
|
|
1060
|
-
* console.error("Drain error:", error);
|
|
1061
|
-
* }
|
|
1062
|
-
* }, 5000); // Every 5 seconds
|
|
1063
|
-
* ```
|
|
1064
|
-
*
|
|
1065
|
-
* @example With lifecycle listeners
|
|
1066
|
-
* ```typescript
|
|
1067
|
-
* app.on("acked", (leases) => {
|
|
1068
|
-
* console.log(`Acknowledged ${leases.length} streams`);
|
|
1069
|
-
* });
|
|
1070
|
-
*
|
|
1071
|
-
* app.on("blocked", (blocked) => {
|
|
1072
|
-
* console.error(`Blocked ${blocked.length} streams due to errors`);
|
|
1073
|
-
* blocked.forEach(({ stream, error }) => {
|
|
1074
|
-
* console.error(`Stream ${stream}: ${error}`);
|
|
1075
|
-
* });
|
|
1076
|
-
* });
|
|
1077
|
-
*
|
|
1078
|
-
* await app.drain();
|
|
1054
|
+
* await app.do("CreateItem", target, input);
|
|
1055
|
+
* app.settle(); // debounced correlate→drain, emits "settled"
|
|
1079
1056
|
* ```
|
|
1080
1057
|
*
|
|
1058
|
+
* @see {@link settle} for debounced correlate→drain with lifecycle events
|
|
1081
1059
|
* @see {@link correlate} for dynamic stream discovery
|
|
1082
1060
|
* @see {@link start_correlations} for automatic correlation
|
|
1083
1061
|
*/
|
|
@@ -1306,10 +1284,10 @@ var Act = class {
|
|
|
1306
1284
|
* @see {@link stop_correlations} to stop the worker
|
|
1307
1285
|
*/
|
|
1308
1286
|
start_correlations(query = {}, frequency = 1e4, callback) {
|
|
1309
|
-
if (this.
|
|
1287
|
+
if (this._correlation_timer) return false;
|
|
1310
1288
|
const limit = query.limit || 100;
|
|
1311
1289
|
let after = query.after || -1;
|
|
1312
|
-
this.
|
|
1290
|
+
this._correlation_timer = setInterval(
|
|
1313
1291
|
() => this.correlate({ ...query, after, limit }).then((result) => {
|
|
1314
1292
|
after = result.last_id;
|
|
1315
1293
|
if (callback && result.leased.length) callback(result.leased);
|
|
@@ -1336,11 +1314,77 @@ var Act = class {
|
|
|
1336
1314
|
* @see {@link start_correlations}
|
|
1337
1315
|
*/
|
|
1338
1316
|
stop_correlations() {
|
|
1339
|
-
if (this.
|
|
1340
|
-
clearInterval(this.
|
|
1341
|
-
this.
|
|
1317
|
+
if (this._correlation_timer) {
|
|
1318
|
+
clearInterval(this._correlation_timer);
|
|
1319
|
+
this._correlation_timer = void 0;
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
/**
|
|
1323
|
+
* Cancels any pending or active settle cycle.
|
|
1324
|
+
*
|
|
1325
|
+
* @see {@link settle}
|
|
1326
|
+
*/
|
|
1327
|
+
stop_settling() {
|
|
1328
|
+
if (this._settle_timer) {
|
|
1329
|
+
clearTimeout(this._settle_timer);
|
|
1330
|
+
this._settle_timer = void 0;
|
|
1342
1331
|
}
|
|
1343
1332
|
}
|
|
1333
|
+
/**
|
|
1334
|
+
* Debounced, non-blocking correlate→drain cycle.
|
|
1335
|
+
*
|
|
1336
|
+
* Call this after `app.do()` to schedule a background drain. Multiple rapid
|
|
1337
|
+
* calls within the debounce window are coalesced into a single cycle. Runs
|
|
1338
|
+
* correlate→drain in a loop until the system reaches a consistent state,
|
|
1339
|
+
* then emits the `"settled"` lifecycle event.
|
|
1340
|
+
*
|
|
1341
|
+
* @param options - Settle configuration options
|
|
1342
|
+
* @param options.debounceMs - Debounce window in milliseconds (default: 10)
|
|
1343
|
+
* @param options.correlate - Query filter for correlation scans (default: `{ after: -1, limit: 100 }`)
|
|
1344
|
+
* @param options.maxPasses - Maximum correlate→drain loops (default: 5)
|
|
1345
|
+
* @param options.streamLimit - Maximum streams per drain cycle (default: 10)
|
|
1346
|
+
* @param options.eventLimit - Maximum events per stream (default: 10)
|
|
1347
|
+
* @param options.leaseMillis - Lease duration in milliseconds (default: 10000)
|
|
1348
|
+
*
|
|
1349
|
+
* @example API mutations
|
|
1350
|
+
* ```typescript
|
|
1351
|
+
* await app.do("CreateItem", target, input);
|
|
1352
|
+
* app.settle(); // non-blocking, returns immediately
|
|
1353
|
+
*
|
|
1354
|
+
* app.on("settled", (drain) => {
|
|
1355
|
+
* // notify SSE clients, invalidate caches, etc.
|
|
1356
|
+
* });
|
|
1357
|
+
* ```
|
|
1358
|
+
*
|
|
1359
|
+
* @see {@link drain} for single synchronous drain cycles
|
|
1360
|
+
* @see {@link correlate} for manual correlation
|
|
1361
|
+
*/
|
|
1362
|
+
settle(options = {}) {
|
|
1363
|
+
const {
|
|
1364
|
+
debounceMs = 10,
|
|
1365
|
+
correlate: correlateQuery = { after: -1, limit: 100 },
|
|
1366
|
+
maxPasses = 5,
|
|
1367
|
+
...drainOptions
|
|
1368
|
+
} = options;
|
|
1369
|
+
if (this._settle_timer) clearTimeout(this._settle_timer);
|
|
1370
|
+
this._settle_timer = setTimeout(() => {
|
|
1371
|
+
this._settle_timer = void 0;
|
|
1372
|
+
if (this._settling) return;
|
|
1373
|
+
this._settling = true;
|
|
1374
|
+
(async () => {
|
|
1375
|
+
let lastDrain;
|
|
1376
|
+
for (let i = 0; i < maxPasses; i++) {
|
|
1377
|
+
const { leased } = await this.correlate(correlateQuery);
|
|
1378
|
+
if (leased.length === 0 && i > 0) break;
|
|
1379
|
+
lastDrain = await this.drain(drainOptions);
|
|
1380
|
+
if (!lastDrain.acked.length && !lastDrain.blocked.length) break;
|
|
1381
|
+
}
|
|
1382
|
+
if (lastDrain) this.emit("settled", lastDrain);
|
|
1383
|
+
})().catch((err) => logger.error(err)).finally(() => {
|
|
1384
|
+
this._settling = false;
|
|
1385
|
+
});
|
|
1386
|
+
}, debounceMs);
|
|
1387
|
+
}
|
|
1344
1388
|
};
|
|
1345
1389
|
|
|
1346
1390
|
// src/merge.ts
|