@rotorsoft/act 0.25.2 → 0.26.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 +2 -0
- package/dist/.tsbuildinfo +1 -1
- package/dist/@types/act-builder.d.ts +2 -2
- package/dist/@types/act-builder.d.ts.map +1 -1
- package/dist/@types/act.d.ts +18 -2
- package/dist/@types/act.d.ts.map +1 -1
- package/dist/@types/projection-builder.d.ts +38 -53
- package/dist/@types/projection-builder.d.ts.map +1 -1
- package/dist/@types/types/reaction.d.ts +42 -0
- package/dist/@types/types/reaction.d.ts.map +1 -1
- package/dist/index.cjs +85 -20
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +85 -20
- 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, IAct, Lease, Query, Registry, Schema, SchemaRegister, Schemas, SettleOptions, Snapshot, State, Target } from "./types/index.js";
|
|
1
|
+
import type { Actor, BatchHandler, Committed, Drain, DrainOptions, IAct, Lease, Query, Registry, Schema, SchemaRegister, Schemas, SettleOptions, Snapshot, State, Target } from "./types/index.js";
|
|
2
2
|
/**
|
|
3
3
|
* @category Orchestrator
|
|
4
4
|
* @see Store
|
|
@@ -89,7 +89,9 @@ export declare class Act<TSchemaReg extends SchemaRegister<TActions>, TEvents ex
|
|
|
89
89
|
*/
|
|
90
90
|
/** Static resolver targets collected at build time */
|
|
91
91
|
private readonly _static_targets;
|
|
92
|
-
|
|
92
|
+
/** Batch handlers for static-target projections (target → handler) */
|
|
93
|
+
private readonly _batch_handlers;
|
|
94
|
+
constructor(registry: Registry<TSchemaReg, TEvents, TActions>, _states?: Map<string, State<any, any, any>>, batchHandlers?: Map<string, BatchHandler<any>>);
|
|
93
95
|
/**
|
|
94
96
|
* Executes an action on a state instance, committing resulting events.
|
|
95
97
|
*
|
|
@@ -312,6 +314,20 @@ export declare class Act<TSchemaReg extends SchemaRegister<TActions>, TEvents ex
|
|
|
312
314
|
* @returns The lease with results
|
|
313
315
|
*/
|
|
314
316
|
private handle;
|
|
317
|
+
/**
|
|
318
|
+
* Handles a batch of events for a projection with a batch handler.
|
|
319
|
+
*
|
|
320
|
+
* Called by `drain()` when a leased stream is a static-target projection
|
|
321
|
+
* with a registered batch handler. All events are passed to the handler
|
|
322
|
+
* in a single call, enabling bulk DB operations.
|
|
323
|
+
*
|
|
324
|
+
* @internal
|
|
325
|
+
* @param lease The lease to handle
|
|
326
|
+
* @param payloads The reactions to handle
|
|
327
|
+
* @param batchHandler The batch handler for this projection
|
|
328
|
+
* @returns The lease with results
|
|
329
|
+
*/
|
|
330
|
+
private handleBatch;
|
|
315
331
|
/**
|
|
316
332
|
* Processes pending reactions by draining uncommitted events from the event store.
|
|
317
333
|
*
|
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,IAAI,EACJ,KAAK,EACL,KAAK,EAEL,QAAQ,EACR,MAAM,EACN,cAAc,EACd,OAAO,EACP,aAAa,EACb,QAAQ,EACR,KAAK,EACL,MAAM,EACP,MAAM,kBAAkB,CAAC;AAK1B;;;;;;;;;;;;;;;;;;;;;;;;;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,CAC5B,YAAW,IAAI,CAAC,OAAO,EAAE,QAAQ,EAAE,MAAM,CAAC;
|
|
1
|
+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,KAAK,CAAC,OAAO,CAAC,CAAC;IAgI9C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4CG;IACH;;;;;;OAMG;YACW,iBAAiB;IAczB,SAAS,CACb,KAAK,GAAE,KAAgC,GACtC,OAAO,CAAC;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;IAgEnD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAsDG;IACH,kBAAkB,CAChB,KAAK,GAAE,KAAU,EACjB,SAAS,SAAS,EAClB,QAAQ,CAAC,EAAE,CAAC,UAAU,EAAE,MAAM,KAAK,IAAI,GACtC,OAAO;IAgBV;;;;;;;;;;;;;;;;OAgBG;IACH,iBAAiB;IAOjB;;;;OAIG;IACH,aAAa;IAOb;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4BG;IACH,MAAM,CAAC,OAAO,GAAE,aAAkB,GAAG,IAAI;CAkC1C"}
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* actions, and are pure side-effect handlers routed to a named stream.
|
|
10
10
|
*/
|
|
11
11
|
import type { ZodType } from "zod";
|
|
12
|
-
import type { Committed, EventRegister, ReactionResolver, Schema, Schemas } from "./types/index.js";
|
|
12
|
+
import type { BatchHandler, Committed, EventRegister, ReactionResolver, Schema, Schemas } from "./types/index.js";
|
|
13
13
|
/**
|
|
14
14
|
* A self-contained projection grouping read-model update handlers.
|
|
15
15
|
* Projections are composed into an Act orchestrator via `act().withProjection(projection)`.
|
|
@@ -19,38 +19,37 @@ import type { Committed, EventRegister, ReactionResolver, Schema, Schemas } from
|
|
|
19
19
|
export type Projection<TEvents extends Schemas> = {
|
|
20
20
|
readonly _tag: "Projection";
|
|
21
21
|
readonly events: EventRegister<TEvents>;
|
|
22
|
+
readonly target?: string;
|
|
23
|
+
readonly batchHandler?: BatchHandler<TEvents>;
|
|
22
24
|
};
|
|
23
25
|
/** Helper: a single-key record mapping an event name to its Zod schema. */
|
|
24
26
|
type EventEntry<TKey extends string = string, TData extends Schema = Schema> = {
|
|
25
27
|
[P in TKey]: ZodType<TData>;
|
|
26
28
|
};
|
|
27
29
|
/** Infer the handler-result type after registering one event. */
|
|
28
|
-
type DoResult<TEvents extends Schemas, TKey extends string, TData extends Schema> = ProjectionBuilder<TEvents & {
|
|
30
|
+
type DoResult<TEvents extends Schemas, TKey extends string, TData extends Schema, TTarget extends string | undefined = undefined> = ProjectionBuilder<TEvents & {
|
|
29
31
|
[P in TKey]: TData;
|
|
30
|
-
}> & {
|
|
32
|
+
}, TTarget> & {
|
|
31
33
|
to: (resolver: ReactionResolver<TEvents & {
|
|
32
34
|
[P in TKey]: TData;
|
|
33
35
|
}, TKey> | string) => ProjectionBuilder<TEvents & {
|
|
34
36
|
[P in TKey]: TData;
|
|
35
|
-
}>;
|
|
37
|
+
}, TTarget>;
|
|
36
38
|
void: () => ProjectionBuilder<TEvents & {
|
|
37
39
|
[P in TKey]: TData;
|
|
38
|
-
}>;
|
|
40
|
+
}, TTarget>;
|
|
39
41
|
};
|
|
40
42
|
/**
|
|
41
43
|
* Fluent builder interface for composing projections.
|
|
42
44
|
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
* When a default target is provided via `projection("target")`, all
|
|
48
|
-
* handlers inherit that resolver. Per-handler `.to()` or `.void()` can
|
|
49
|
-
* still override it.
|
|
45
|
+
* When a static target is provided via `projection("target")`, the builder
|
|
46
|
+
* exposes a `.batch()` method for registering a batch handler that processes
|
|
47
|
+
* all events in a single call.
|
|
50
48
|
*
|
|
51
49
|
* @template TEvents - Event schemas
|
|
50
|
+
* @template TTarget - Static target string or undefined
|
|
52
51
|
*/
|
|
53
|
-
export type ProjectionBuilder<TEvents extends Schemas> = {
|
|
52
|
+
export type ProjectionBuilder<TEvents extends Schemas, TTarget extends string | undefined = undefined> = {
|
|
54
53
|
/**
|
|
55
54
|
* Begins defining a projection handler for a specific event.
|
|
56
55
|
*
|
|
@@ -61,7 +60,7 @@ export type ProjectionBuilder<TEvents extends Schemas> = {
|
|
|
61
60
|
on: <TKey extends string, TData extends Schema>(entry: EventEntry<TKey, TData>) => {
|
|
62
61
|
do: (handler: (event: Committed<TEvents & {
|
|
63
62
|
[P in TKey]: TData;
|
|
64
|
-
}, TKey>, stream: string) => Promise<void>) => DoResult<TEvents, TKey, TData>;
|
|
63
|
+
}, TKey>, stream: string) => Promise<void>) => DoResult<TEvents, TKey, TData, TTarget>;
|
|
65
64
|
};
|
|
66
65
|
/**
|
|
67
66
|
* Builds and returns the Projection data structure.
|
|
@@ -71,49 +70,35 @@ export type ProjectionBuilder<TEvents extends Schemas> = {
|
|
|
71
70
|
* The registered event schemas and their reaction maps.
|
|
72
71
|
*/
|
|
73
72
|
readonly events: EventRegister<TEvents>;
|
|
74
|
-
}
|
|
73
|
+
} & (TTarget extends string ? {
|
|
74
|
+
/**
|
|
75
|
+
* Registers a batch handler that processes all events in a single call.
|
|
76
|
+
*
|
|
77
|
+
* Only available on projections with a static target (`projection("target")`).
|
|
78
|
+
* The handler receives a discriminated union of all declared events,
|
|
79
|
+
* enabling bulk DB operations in a single transaction.
|
|
80
|
+
*
|
|
81
|
+
* When defined, the batch handler is always called — even for a single event.
|
|
82
|
+
* Individual `.do()` handlers serve as fallback for projections without `.batch()`.
|
|
83
|
+
*/
|
|
84
|
+
batch: (handler: BatchHandler<TEvents>) => {
|
|
85
|
+
build: () => Projection<TEvents>;
|
|
86
|
+
};
|
|
87
|
+
} : {});
|
|
75
88
|
/**
|
|
76
|
-
* Creates a new projection builder
|
|
77
|
-
*
|
|
78
|
-
* Projections enable separation of read-model concerns from command handling.
|
|
79
|
-
* Each `.on({ Event }).do(handler)` call registers a handler that updates
|
|
80
|
-
* a projection (database table, cache, etc.) in response to events.
|
|
81
|
-
*
|
|
82
|
-
* Pass a target stream name to `projection("target")` so every handler
|
|
83
|
-
* inherits that resolver automatically. Omit it and use per-handler
|
|
84
|
-
* `.to()` / `.void()` when handlers route to different streams.
|
|
89
|
+
* Creates a new projection builder with a static target stream.
|
|
85
90
|
*
|
|
86
|
-
*
|
|
91
|
+
* All handlers inherit the target resolver automatically. Enables `.batch()`
|
|
92
|
+
* for bulk event processing in a single transaction.
|
|
87
93
|
*
|
|
88
|
-
* @
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
*
|
|
93
|
-
* await db.insert(tickets).values({ id: stream, ...data });
|
|
94
|
-
* })
|
|
95
|
-
* .on({ TicketClosed })
|
|
96
|
-
* .do(async ({ stream, data }) => {
|
|
97
|
-
* await db.update(tickets).set(data).where(eq(tickets.id, stream));
|
|
98
|
-
* })
|
|
99
|
-
* .build();
|
|
100
|
-
* ```
|
|
101
|
-
*
|
|
102
|
-
* @example Per-handler routing
|
|
103
|
-
* ```typescript
|
|
104
|
-
* const MultiProjection = projection()
|
|
105
|
-
* .on({ OrderPlaced })
|
|
106
|
-
* .do(async (event) => { ... })
|
|
107
|
-
* .to("orders")
|
|
108
|
-
* .on({ PaymentReceived })
|
|
109
|
-
* .do(async (event) => { ... })
|
|
110
|
-
* .to("payments")
|
|
111
|
-
* .build();
|
|
112
|
-
* ```
|
|
94
|
+
* @param target - Static target stream for all handlers
|
|
95
|
+
*/
|
|
96
|
+
export declare function projection<TEvents extends Schemas = {}>(target: string, events?: EventRegister<TEvents>): ProjectionBuilder<TEvents, string>;
|
|
97
|
+
/**
|
|
98
|
+
* Creates a new projection builder without a default target.
|
|
113
99
|
*
|
|
114
|
-
*
|
|
115
|
-
* @see {@link Projection} for the output type
|
|
100
|
+
* Use per-handler `.to()` / `.void()` to route events.
|
|
116
101
|
*/
|
|
117
|
-
export declare function projection<TEvents extends Schemas = {}>(target?:
|
|
102
|
+
export declare function projection<TEvents extends Schemas = {}>(target?: undefined, events?: EventRegister<TEvents>): ProjectionBuilder<TEvents, undefined>;
|
|
118
103
|
export {};
|
|
119
104
|
//# sourceMappingURL=projection-builder.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"projection-builder.d.ts","sourceRoot":"","sources":["../../src/projection-builder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AAEnC,OAAO,KAAK,EACV,SAAS,EACT,aAAa,EAGb,gBAAgB,EAChB,MAAM,EACN,OAAO,EACR,MAAM,kBAAkB,CAAC;AAE1B;;;;;GAKG;AACH,MAAM,MAAM,UAAU,CAAC,OAAO,SAAS,OAAO,IAAI;IAChD,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAC5B,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"projection-builder.d.ts","sourceRoot":"","sources":["../../src/projection-builder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AAEnC,OAAO,KAAK,EACV,YAAY,EACZ,SAAS,EACT,aAAa,EAGb,gBAAgB,EAChB,MAAM,EACN,OAAO,EACR,MAAM,kBAAkB,CAAC;AAE1B;;;;;GAKG;AACH,MAAM,MAAM,UAAU,CAAC,OAAO,SAAS,OAAO,IAAI;IAChD,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAC5B,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;IACxC,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,YAAY,CAAC,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC;CAC/C,CAAC;AAEF,2EAA2E;AAC3E,KAAK,UAAU,CAAC,IAAI,SAAS,MAAM,GAAG,MAAM,EAAE,KAAK,SAAS,MAAM,GAAG,MAAM,IAAI;KAC5E,CAAC,IAAI,IAAI,GAAG,OAAO,CAAC,KAAK,CAA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C,CAAC;IACjC;;OAEG;IACH,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;CACzC,GAAG,CAAC,OAAO,SAAS,MAAM,GACvB;IACE;;;;;;;;;OASG;IACH,KAAK,EAAE,CAAC,OAAO,EAAE,YAAY,CAAC,OAAO,CAAC,KAAK;QACzC,KAAK,EAAE,MAAM,UAAU,CAAC,OAAO,CAAC,CAAC;KAClC,CAAC;CACH,GAED,EAAE,CAAC,CAAC;AAyJR;;;;;;;GAOG;AACH,wBAAgB,UAAU,CAAC,OAAO,SAAS,OAAO,GAAG,EAAE,EACrD,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,aAAa,CAAC,OAAO,CAAC,GAC9B,iBAAiB,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;AACtC;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,OAAO,SAAS,OAAO,GAAG,EAAE,EACrD,MAAM,CAAC,EAAE,SAAS,EAClB,MAAM,CAAC,EAAE,aAAa,CAAC,OAAO,CAAC,GAC9B,iBAAiB,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC"}
|
|
@@ -100,6 +100,48 @@ export type ReactionOptions = {
|
|
|
100
100
|
readonly blockOnError: boolean;
|
|
101
101
|
readonly maxRetries: number;
|
|
102
102
|
};
|
|
103
|
+
/**
|
|
104
|
+
* Distributive mapped type that produces a proper discriminated union of
|
|
105
|
+
* committed events. Unlike `Committed<TEvents, keyof TEvents>` (where
|
|
106
|
+
* `name` and `data` are independent unions), each variant correlates
|
|
107
|
+
* `name` with its corresponding `data` — enabling `switch (event.name)`
|
|
108
|
+
* to narrow both fields correctly.
|
|
109
|
+
*
|
|
110
|
+
* @template TEvents - Event schemas
|
|
111
|
+
*
|
|
112
|
+
* @example Exhaustive switch
|
|
113
|
+
* ```typescript
|
|
114
|
+
* for (const event of events) {
|
|
115
|
+
* switch (event.name) {
|
|
116
|
+
* case "TicketOpened":
|
|
117
|
+
* event.data; // typed as TicketOpened's schema
|
|
118
|
+
* break;
|
|
119
|
+
* case "TicketClosed":
|
|
120
|
+
* event.data; // typed as TicketClosed's schema
|
|
121
|
+
* break;
|
|
122
|
+
* default:
|
|
123
|
+
* const _: never = event; // compile error if a case is missing
|
|
124
|
+
* }
|
|
125
|
+
* }
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
export type BatchEvent<TEvents extends Schemas> = {
|
|
129
|
+
[K in keyof TEvents]: Committed<TEvents, K>;
|
|
130
|
+
}[keyof TEvents];
|
|
131
|
+
/**
|
|
132
|
+
* Batch handler for projections that processes multiple events in a single call.
|
|
133
|
+
*
|
|
134
|
+
* Receives the full ordered array of all event types declared on the projection,
|
|
135
|
+
* enabling bulk DB operations (batch INSERT/UPDATE) in a single transaction.
|
|
136
|
+
* The handler is always called when defined — even for a single event.
|
|
137
|
+
*
|
|
138
|
+
* @template TEvents - Event schemas (all events declared on the projection)
|
|
139
|
+
* @param events - Ordered array of committed events (discriminated union)
|
|
140
|
+
* @param stream - The target stream name
|
|
141
|
+
*
|
|
142
|
+
* @see {@link BatchEvent} for the discriminated union type
|
|
143
|
+
*/
|
|
144
|
+
export type BatchHandler<TEvents extends Schemas> = (events: ReadonlyArray<BatchEvent<TEvents>>, stream: string) => Promise<void>;
|
|
103
145
|
/**
|
|
104
146
|
* Defines a reaction to an event.
|
|
105
147
|
* @template TEvents - Event schemas.
|
|
@@ -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,IAAI,EACJ,KAAK,EACL,MAAM,EACN,OAAO,EACP,QAAQ,EACT,MAAM,aAAa,CAAC;AAErB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,MAAM,eAAe,CACzB,OAAO,SAAS,OAAO,EACvB,IAAI,SAAS,MAAM,OAAO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,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;CAC/B,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,KAAK,CAAC,OAAO,SAAS,OAAO,IAAI;IAC3C,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;IACjC,QAAQ,CAAC,MAAM,EAAE,KAAK,EAAE,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,KAAK,EAAE,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC,KAAK,GAAG;QAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CAC7D,CAAC;AAEF;;;;;;;;;;GAUG;AACH,MAAM,MAAM,aAAa,GAAG,YAAY,GAAG;IACzC,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,SAAS,CAAC,EAAE,KAAK,CAAC;IAC3B,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAC7B,CAAC"}
|
package/dist/index.cjs
CHANGED
|
@@ -904,9 +904,10 @@ async function action(me, action2, target, payload, reactingTo, skipValidation =
|
|
|
904
904
|
var logger3 = log();
|
|
905
905
|
var tracer = build_tracer(config().logLevel);
|
|
906
906
|
var Act = class {
|
|
907
|
-
constructor(registry, _states = /* @__PURE__ */ new Map()) {
|
|
907
|
+
constructor(registry, _states = /* @__PURE__ */ new Map(), batchHandlers = /* @__PURE__ */ new Map()) {
|
|
908
908
|
this.registry = registry;
|
|
909
909
|
this._states = _states;
|
|
910
|
+
this._batch_handlers = batchHandlers;
|
|
910
911
|
const statics = [];
|
|
911
912
|
for (const [name, register] of Object.entries(this.registry.events)) {
|
|
912
913
|
if (register.reactions.size > 0) {
|
|
@@ -964,6 +965,8 @@ var Act = class {
|
|
|
964
965
|
*/
|
|
965
966
|
/** Static resolver targets collected at build time */
|
|
966
967
|
_static_targets;
|
|
968
|
+
/** Batch handlers for static-target projections (target → handler) */
|
|
969
|
+
_batch_handlers;
|
|
967
970
|
/**
|
|
968
971
|
* Executes an action on a state instance, committing resulting events.
|
|
969
972
|
*
|
|
@@ -1205,6 +1208,41 @@ var Act = class {
|
|
|
1205
1208
|
}
|
|
1206
1209
|
return { lease, handled, at };
|
|
1207
1210
|
}
|
|
1211
|
+
/**
|
|
1212
|
+
* Handles a batch of events for a projection with a batch handler.
|
|
1213
|
+
*
|
|
1214
|
+
* Called by `drain()` when a leased stream is a static-target projection
|
|
1215
|
+
* with a registered batch handler. All events are passed to the handler
|
|
1216
|
+
* in a single call, enabling bulk DB operations.
|
|
1217
|
+
*
|
|
1218
|
+
* @internal
|
|
1219
|
+
* @param lease The lease to handle
|
|
1220
|
+
* @param payloads The reactions to handle
|
|
1221
|
+
* @param batchHandler The batch handler for this projection
|
|
1222
|
+
* @returns The lease with results
|
|
1223
|
+
*/
|
|
1224
|
+
async handleBatch(lease, payloads, batchHandler) {
|
|
1225
|
+
const stream = lease.stream;
|
|
1226
|
+
const events = payloads.map((p) => p.event);
|
|
1227
|
+
const at = events.at(-1).id;
|
|
1228
|
+
lease.retry > 0 && logger3.warn(`Retrying batch ${stream}@${events[0].id} (${lease.retry}).`);
|
|
1229
|
+
try {
|
|
1230
|
+
await batchHandler(events, stream);
|
|
1231
|
+
return { lease, handled: events.length, at };
|
|
1232
|
+
} catch (error) {
|
|
1233
|
+
logger3.error(error);
|
|
1234
|
+
const { options } = payloads[0];
|
|
1235
|
+
const block = lease.retry >= options.maxRetries && options.blockOnError;
|
|
1236
|
+
block && logger3.error(`Blocking ${stream} after ${lease.retry} retries.`);
|
|
1237
|
+
return {
|
|
1238
|
+
lease,
|
|
1239
|
+
handled: 0,
|
|
1240
|
+
at: lease.at,
|
|
1241
|
+
error: error.message,
|
|
1242
|
+
block
|
|
1243
|
+
};
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1208
1246
|
/**
|
|
1209
1247
|
* Processes pending reactions by draining uncommitted events from the event store.
|
|
1210
1248
|
*
|
|
@@ -1299,10 +1337,12 @@ var Act = class {
|
|
|
1299
1337
|
leased.map((lease) => {
|
|
1300
1338
|
const streamFetch = fetched.find((f) => f.stream === lease.stream);
|
|
1301
1339
|
const at = streamFetch?.events.at(-1)?.id || fetch_window_at;
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1340
|
+
const payloads = payloadsMap.get(lease.stream);
|
|
1341
|
+
const batchHandler = this._batch_handlers.get(lease.stream);
|
|
1342
|
+
if (batchHandler && payloads.length > 0) {
|
|
1343
|
+
return this.handleBatch({ ...lease, at }, payloads, batchHandler);
|
|
1344
|
+
}
|
|
1345
|
+
return this.handle({ ...lease, at }, payloads);
|
|
1306
1346
|
})
|
|
1307
1347
|
);
|
|
1308
1348
|
const [lagging_handled, leading_handled] = handled.reduce(
|
|
@@ -1743,14 +1783,15 @@ var _void_ = () => void 0;
|
|
|
1743
1783
|
function act(states = /* @__PURE__ */ new Map(), registry = {
|
|
1744
1784
|
actions: {},
|
|
1745
1785
|
events: {}
|
|
1746
|
-
}, pendingProjections = []) {
|
|
1786
|
+
}, pendingProjections = [], batchHandlers = /* @__PURE__ */ new Map()) {
|
|
1747
1787
|
const builder = {
|
|
1748
1788
|
withState: (state2) => {
|
|
1749
1789
|
registerState(state2, states, registry.actions, registry.events);
|
|
1750
1790
|
return act(
|
|
1751
1791
|
states,
|
|
1752
1792
|
registry,
|
|
1753
|
-
pendingProjections
|
|
1793
|
+
pendingProjections,
|
|
1794
|
+
batchHandlers
|
|
1754
1795
|
);
|
|
1755
1796
|
},
|
|
1756
1797
|
withSlice: (input) => {
|
|
@@ -1767,22 +1808,28 @@ function act(states = /* @__PURE__ */ new Map(), registry = {
|
|
|
1767
1808
|
return act(
|
|
1768
1809
|
states,
|
|
1769
1810
|
registry,
|
|
1770
|
-
pendingProjections
|
|
1811
|
+
pendingProjections,
|
|
1812
|
+
batchHandlers
|
|
1771
1813
|
);
|
|
1772
1814
|
},
|
|
1773
1815
|
withProjection: (proj) => {
|
|
1774
1816
|
mergeProjection(proj, registry.events);
|
|
1817
|
+
if (proj.batchHandler && proj.target) {
|
|
1818
|
+
batchHandlers.set(proj.target, proj.batchHandler);
|
|
1819
|
+
}
|
|
1775
1820
|
return act(
|
|
1776
1821
|
states,
|
|
1777
1822
|
registry,
|
|
1778
|
-
pendingProjections
|
|
1823
|
+
pendingProjections,
|
|
1824
|
+
batchHandlers
|
|
1779
1825
|
);
|
|
1780
1826
|
},
|
|
1781
1827
|
withActor: () => {
|
|
1782
1828
|
return act(
|
|
1783
1829
|
states,
|
|
1784
1830
|
registry,
|
|
1785
|
-
pendingProjections
|
|
1831
|
+
pendingProjections,
|
|
1832
|
+
batchHandlers
|
|
1786
1833
|
);
|
|
1787
1834
|
},
|
|
1788
1835
|
on: (event) => ({
|
|
@@ -1819,10 +1866,14 @@ function act(states = /* @__PURE__ */ new Map(), registry = {
|
|
|
1819
1866
|
build: () => {
|
|
1820
1867
|
for (const proj of pendingProjections) {
|
|
1821
1868
|
mergeProjection(proj, registry.events);
|
|
1869
|
+
if (proj.batchHandler && proj.target) {
|
|
1870
|
+
batchHandlers.set(proj.target, proj.batchHandler);
|
|
1871
|
+
}
|
|
1822
1872
|
}
|
|
1823
1873
|
return new Act(
|
|
1824
1874
|
registry,
|
|
1825
|
-
states
|
|
1875
|
+
states,
|
|
1876
|
+
batchHandlers
|
|
1826
1877
|
);
|
|
1827
1878
|
},
|
|
1828
1879
|
events: registry.events
|
|
@@ -1831,9 +1882,9 @@ function act(states = /* @__PURE__ */ new Map(), registry = {
|
|
|
1831
1882
|
}
|
|
1832
1883
|
|
|
1833
1884
|
// src/projection-builder.ts
|
|
1834
|
-
function
|
|
1835
|
-
const defaultResolver = target ? { target } : void 0;
|
|
1836
|
-
const
|
|
1885
|
+
function _projection(target, events) {
|
|
1886
|
+
const defaultResolver = typeof target === "string" ? { target } : void 0;
|
|
1887
|
+
const base = {
|
|
1837
1888
|
on: (entry) => {
|
|
1838
1889
|
const keys = Object.keys(entry);
|
|
1839
1890
|
if (keys.length !== 1) throw new Error(".on() requires exactly one key");
|
|
@@ -1858,10 +1909,7 @@ function projection(target, events = {}) {
|
|
|
1858
1909
|
const register = events[event];
|
|
1859
1910
|
const name = handler.name || `${event}_${register.reactions.size}`;
|
|
1860
1911
|
register.reactions.set(name, reaction);
|
|
1861
|
-
const nextBuilder =
|
|
1862
|
-
target,
|
|
1863
|
-
events
|
|
1864
|
-
);
|
|
1912
|
+
const nextBuilder = _projection(target, events);
|
|
1865
1913
|
return {
|
|
1866
1914
|
...nextBuilder,
|
|
1867
1915
|
to(resolver) {
|
|
@@ -1884,11 +1932,28 @@ function projection(target, events = {}) {
|
|
|
1884
1932
|
},
|
|
1885
1933
|
build: () => ({
|
|
1886
1934
|
_tag: "Projection",
|
|
1887
|
-
events
|
|
1935
|
+
events,
|
|
1936
|
+
...target !== void 0 && { target }
|
|
1888
1937
|
}),
|
|
1889
1938
|
events
|
|
1890
1939
|
};
|
|
1891
|
-
|
|
1940
|
+
if (typeof target === "string") {
|
|
1941
|
+
return {
|
|
1942
|
+
...base,
|
|
1943
|
+
batch: (handler) => ({
|
|
1944
|
+
build: () => ({
|
|
1945
|
+
_tag: "Projection",
|
|
1946
|
+
events,
|
|
1947
|
+
target,
|
|
1948
|
+
batchHandler: handler
|
|
1949
|
+
})
|
|
1950
|
+
})
|
|
1951
|
+
};
|
|
1952
|
+
}
|
|
1953
|
+
return base;
|
|
1954
|
+
}
|
|
1955
|
+
function projection(target, events = {}) {
|
|
1956
|
+
return _projection(target, events);
|
|
1892
1957
|
}
|
|
1893
1958
|
|
|
1894
1959
|
// src/slice-builder.ts
|