@rotorsoft/act 0.14.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-builder.d.ts +60 -20
- package/dist/@types/act-builder.d.ts.map +1 -1
- package/dist/@types/act.d.ts +75 -57
- package/dist/@types/act.d.ts.map +1 -1
- package/dist/@types/event-sourcing.d.ts +12 -12
- package/dist/@types/event-sourcing.d.ts.map +1 -1
- package/dist/@types/merge.d.ts +1 -1
- package/dist/@types/merge.d.ts.map +1 -1
- package/dist/@types/projection-builder.d.ts +22 -22
- package/dist/@types/projection-builder.d.ts.map +1 -1
- package/dist/@types/slice-builder.d.ts +31 -27
- package/dist/@types/slice-builder.d.ts.map +1 -1
- package/dist/@types/state-builder.d.ts +35 -33
- package/dist/@types/state-builder.d.ts.map +1 -1
- package/dist/@types/types/action.d.ts +75 -66
- package/dist/@types/types/action.d.ts.map +1 -1
- package/dist/@types/types/errors.d.ts +15 -14
- package/dist/@types/types/errors.d.ts.map +1 -1
- package/dist/@types/types/reaction.d.ts +41 -22
- package/dist/@types/types/reaction.d.ts.map +1 -1
- package/dist/@types/types/registry.d.ts +15 -15
- package/dist/@types/types/registry.d.ts.map +1 -1
- package/dist/@types/types/schemas.d.ts +7 -7
- package/dist/@types/types/schemas.d.ts.map +1 -1
- package/dist/index.cjs +117 -48
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +117 -48
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -14,27 +14,27 @@ import type { Committed, EventRegister, ReactionResolver, Schema, Schemas } from
|
|
|
14
14
|
* A self-contained projection grouping read-model update handlers.
|
|
15
15
|
* Projections are composed into an Act orchestrator via `act().withProjection(projection)`.
|
|
16
16
|
*
|
|
17
|
-
* @template
|
|
17
|
+
* @template TEvents - Event schemas handled by this projection
|
|
18
18
|
*/
|
|
19
|
-
export type Projection<
|
|
19
|
+
export type Projection<TEvents extends Schemas> = {
|
|
20
20
|
readonly _tag: "Projection";
|
|
21
|
-
readonly events: EventRegister<
|
|
21
|
+
readonly events: EventRegister<TEvents>;
|
|
22
22
|
};
|
|
23
23
|
/** Helper: a single-key record mapping an event name to its Zod schema. */
|
|
24
|
-
type EventEntry<
|
|
25
|
-
[P in
|
|
24
|
+
type EventEntry<TKey extends string = string, TData extends Schema = Schema> = {
|
|
25
|
+
[P in TKey]: ZodType<TData>;
|
|
26
26
|
};
|
|
27
27
|
/** Infer the handler-result type after registering one event. */
|
|
28
|
-
type DoResult<
|
|
29
|
-
[P in
|
|
28
|
+
type DoResult<TEvents extends Schemas, TKey extends string, TData extends Schema> = ProjectionBuilder<TEvents & {
|
|
29
|
+
[P in TKey]: TData;
|
|
30
30
|
}> & {
|
|
31
|
-
to: (resolver: ReactionResolver<
|
|
32
|
-
[P in
|
|
33
|
-
},
|
|
34
|
-
[P in
|
|
31
|
+
to: (resolver: ReactionResolver<TEvents & {
|
|
32
|
+
[P in TKey]: TData;
|
|
33
|
+
}, TKey> | string) => ProjectionBuilder<TEvents & {
|
|
34
|
+
[P in TKey]: TData;
|
|
35
35
|
}>;
|
|
36
|
-
void: () => ProjectionBuilder<
|
|
37
|
-
[P in
|
|
36
|
+
void: () => ProjectionBuilder<TEvents & {
|
|
37
|
+
[P in TKey]: TData;
|
|
38
38
|
}>;
|
|
39
39
|
};
|
|
40
40
|
/**
|
|
@@ -48,9 +48,9 @@ type DoResult<E extends Schemas, K extends string, D extends Schema> = Projectio
|
|
|
48
48
|
* handlers inherit that resolver. Per-handler `.to()` or `.void()` can
|
|
49
49
|
* still override it.
|
|
50
50
|
*
|
|
51
|
-
* @template
|
|
51
|
+
* @template TEvents - Event schemas
|
|
52
52
|
*/
|
|
53
|
-
export type ProjectionBuilder<
|
|
53
|
+
export type ProjectionBuilder<TEvents extends Schemas> = {
|
|
54
54
|
/**
|
|
55
55
|
* Begins defining a projection handler for a specific event.
|
|
56
56
|
*
|
|
@@ -58,19 +58,19 @@ export type ProjectionBuilder<E extends Schemas> = {
|
|
|
58
58
|
* when the variable name matches the event name. The key becomes the
|
|
59
59
|
* event name, the value the Zod schema.
|
|
60
60
|
*/
|
|
61
|
-
on: <
|
|
62
|
-
do: (handler: (event: Committed<
|
|
63
|
-
[P in
|
|
64
|
-
},
|
|
61
|
+
on: <TKey extends string, TData extends Schema>(entry: EventEntry<TKey, TData>) => {
|
|
62
|
+
do: (handler: (event: Committed<TEvents & {
|
|
63
|
+
[P in TKey]: TData;
|
|
64
|
+
}, TKey>, stream: string) => Promise<void>) => DoResult<TEvents, TKey, TData>;
|
|
65
65
|
};
|
|
66
66
|
/**
|
|
67
67
|
* Builds and returns the Projection data structure.
|
|
68
68
|
*/
|
|
69
|
-
build: () => Projection<
|
|
69
|
+
build: () => Projection<TEvents>;
|
|
70
70
|
/**
|
|
71
71
|
* The registered event schemas and their reaction maps.
|
|
72
72
|
*/
|
|
73
|
-
readonly events: EventRegister<
|
|
73
|
+
readonly events: EventRegister<TEvents>;
|
|
74
74
|
};
|
|
75
75
|
/**
|
|
76
76
|
* Creates a new projection builder for composing read-model update handlers.
|
|
@@ -114,6 +114,6 @@ export type ProjectionBuilder<E extends Schemas> = {
|
|
|
114
114
|
* @see {@link ProjectionBuilder} for builder methods
|
|
115
115
|
* @see {@link Projection} for the output type
|
|
116
116
|
*/
|
|
117
|
-
export declare function projection<
|
|
117
|
+
export declare function projection<TEvents extends Schemas = {}>(target?: string, events?: EventRegister<TEvents>): ProjectionBuilder<TEvents>;
|
|
118
118
|
export {};
|
|
119
119
|
//# 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,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,KAAK;KAAE,CAAC,CAAC;CACjE,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,iBAAiB,CAAC,OAAO,SAAS,OAAO,IAAI;IACvD;;;;;;OAMG;IACH,EAAE,EAAE,CAAC,IAAI,SAAS,MAAM,EAAE,KAAK,SAAS,MAAM,EAC5C,KAAK,EAAE,UAAU,CAAC,IAAI,EAAE,KAAK,CAAC,KAC3B;QACH,EAAE,EAAE,CACF,OAAO,EAAE,CACP,KAAK,EAAE,SAAS,CAAC,OAAO,GAAG;aAAG,CAAC,IAAI,IAAI,GAAG,KAAK;SAAE,EAAE,IAAI,CAAC,EACxD,MAAM,EAAE,MAAM,KACX,OAAO,CAAC,IAAI,CAAC,KACf,QAAQ,CAAC,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;KACrC,CAAC;IACF;;OAEG;IACH,KAAK,EAAE,MAAM,UAAU,CAAC,OAAO,CAAC,CAAC;IACjC;;OAEG;IACH,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;CACzC,CAAC;AAIF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,wBAAgB,UAAU,CAAC,OAAO,SAAS,OAAO,GAAG,EAAE,EACrD,MAAM,CAAC,EAAE,MAAM,EACf,MAAM,GAAE,aAAa,CAAC,OAAO,CAAgC,GAC5D,iBAAiB,CAAC,OAAO,CAAC,CAkF5B"}
|
|
@@ -1,24 +1,27 @@
|
|
|
1
1
|
import type { Projection } from "./projection-builder.js";
|
|
2
|
-
import type { Committed, Dispatcher, EventRegister, ReactionOptions, ReactionResolver, Schema, SchemaRegister, Schemas, Snapshot, State } from "./types/index.js";
|
|
2
|
+
import type { Actor, Committed, Dispatcher, EventRegister, ReactionOptions, ReactionResolver, Schema, SchemaRegister, Schemas, Snapshot, State } from "./types/index.js";
|
|
3
3
|
/**
|
|
4
4
|
* A self-contained functional slice grouping partial states with their
|
|
5
5
|
* scoped reactions. Slices are composed into an Act orchestrator via
|
|
6
6
|
* `act().withSlice(slice)`.
|
|
7
7
|
*
|
|
8
|
-
* @template
|
|
9
|
-
* @template
|
|
10
|
-
* @template
|
|
11
|
-
* @template
|
|
8
|
+
* @template TSchemaReg - Schema register for states
|
|
9
|
+
* @template TEvents - Event schemas from this slice's states
|
|
10
|
+
* @template TActions - Action schemas from this slice's states
|
|
11
|
+
* @template TStateMap - Map of state names to state schemas
|
|
12
|
+
* @template TActor - Actor type extending base Actor
|
|
12
13
|
*/
|
|
13
|
-
export type Slice<
|
|
14
|
+
export type Slice<TSchemaReg extends SchemaRegister<TActions>, TEvents extends Schemas, TActions extends Schemas, TStateMap extends Record<string, Schema> = {}, TActor extends Actor = Actor> = {
|
|
14
15
|
readonly _tag: "Slice";
|
|
15
16
|
readonly states: Map<string, State<any, any, any>>;
|
|
16
|
-
readonly events: EventRegister<
|
|
17
|
+
readonly events: EventRegister<TEvents>;
|
|
17
18
|
readonly projections: ReadonlyArray<Projection<any>>;
|
|
18
19
|
/** @internal phantom field for type-level state schema tracking */
|
|
19
|
-
readonly _S?:
|
|
20
|
+
readonly _S?: TSchemaReg;
|
|
20
21
|
/** @internal phantom field for type-level state name tracking */
|
|
21
|
-
readonly _M?:
|
|
22
|
+
readonly _M?: TStateMap;
|
|
23
|
+
/** @internal phantom field for type-level actor tracking */
|
|
24
|
+
readonly _TActor?: TActor;
|
|
22
25
|
};
|
|
23
26
|
/**
|
|
24
27
|
* Fluent builder interface for composing functional slices.
|
|
@@ -26,12 +29,13 @@ export type Slice<S extends SchemaRegister<A>, E extends Schemas, A extends Sche
|
|
|
26
29
|
* Provides a chainable API for registering states and projections,
|
|
27
30
|
* and defining reactions scoped to the slice's own events.
|
|
28
31
|
*
|
|
29
|
-
* @template
|
|
30
|
-
* @template
|
|
31
|
-
* @template
|
|
32
|
-
* @template
|
|
32
|
+
* @template TSchemaReg - Schema register for states
|
|
33
|
+
* @template TEvents - Event schemas
|
|
34
|
+
* @template TActions - Action schemas
|
|
35
|
+
* @template TStateMap - Map of state names to state schemas
|
|
36
|
+
* @template TActor - Actor type extending base Actor
|
|
33
37
|
*/
|
|
34
|
-
export type SliceBuilder<
|
|
38
|
+
export type SliceBuilder<TSchemaReg extends SchemaRegister<TActions>, TEvents extends Schemas, TActions extends Schemas, TStateMap extends Record<string, Schema> = {}, TActor extends Actor = Actor> = {
|
|
35
39
|
/**
|
|
36
40
|
* Registers a state definition with the slice.
|
|
37
41
|
*
|
|
@@ -39,35 +43,35 @@ export type SliceBuilder<S extends SchemaRegister<A>, E extends Schemas, A exten
|
|
|
39
43
|
* dispatch. Duplicate registrations (same state in multiple slices)
|
|
40
44
|
* are handled automatically at composition time.
|
|
41
45
|
*/
|
|
42
|
-
withState: <
|
|
43
|
-
[K in keyof
|
|
44
|
-
},
|
|
45
|
-
[K in
|
|
46
|
-
}>;
|
|
46
|
+
withState: <TNewState extends Schema, TNewEvents extends Schemas, TNewActions extends Schemas, TNewName extends string = string>(state: State<TNewState, TNewEvents, TNewActions, TNewName>) => SliceBuilder<TSchemaReg & {
|
|
47
|
+
[K in keyof TNewActions]: TNewState;
|
|
48
|
+
}, TEvents & TNewEvents, TActions & TNewActions, TStateMap & {
|
|
49
|
+
[K in TNewName]: TNewState;
|
|
50
|
+
}, TActor>;
|
|
47
51
|
/**
|
|
48
52
|
* Embeds a built Projection within this slice. The projection's events
|
|
49
53
|
* must be a subset of events from states already registered via
|
|
50
54
|
* `.withState()`. Projection handlers preserve their `(event, stream)`
|
|
51
55
|
* signature and do not receive a Dispatcher.
|
|
52
56
|
*/
|
|
53
|
-
withProjection: <
|
|
57
|
+
withProjection: <TNewEvents extends Schemas>(projection: [Exclude<keyof TNewEvents, keyof TEvents>] extends [never] ? Projection<TNewEvents> : never) => SliceBuilder<TSchemaReg, TEvents, TActions, TStateMap, TActor>;
|
|
54
58
|
/**
|
|
55
59
|
* Begins defining a reaction scoped to this slice's events.
|
|
56
60
|
*/
|
|
57
|
-
on: <
|
|
58
|
-
do: (handler: (event: Committed<
|
|
59
|
-
to: (resolver: ReactionResolver<
|
|
60
|
-
void: () => SliceBuilder<
|
|
61
|
+
on: <TKey extends keyof TEvents>(event: TKey) => {
|
|
62
|
+
do: (handler: (event: Committed<TEvents, TKey>, stream: string, app: Dispatcher<TActions, TActor>) => Promise<Snapshot<Schema, TEvents> | void>, options?: Partial<ReactionOptions>) => SliceBuilder<TSchemaReg, TEvents, TActions, TStateMap, TActor> & {
|
|
63
|
+
to: (resolver: ReactionResolver<TEvents, TKey> | string) => SliceBuilder<TSchemaReg, TEvents, TActions, TStateMap, TActor>;
|
|
64
|
+
void: () => SliceBuilder<TSchemaReg, TEvents, TActions, TStateMap, TActor>;
|
|
61
65
|
};
|
|
62
66
|
};
|
|
63
67
|
/**
|
|
64
68
|
* Builds and returns the Slice data structure.
|
|
65
69
|
*/
|
|
66
|
-
build: () => Slice<
|
|
70
|
+
build: () => Slice<TSchemaReg, TEvents, TActions, TStateMap, TActor>;
|
|
67
71
|
/**
|
|
68
72
|
* The registered event schemas and their reaction maps.
|
|
69
73
|
*/
|
|
70
|
-
readonly events: EventRegister<
|
|
74
|
+
readonly events: EventRegister<TEvents>;
|
|
71
75
|
};
|
|
72
76
|
/**
|
|
73
77
|
* Creates a new slice builder for composing partial states with scoped reactions.
|
|
@@ -102,5 +106,5 @@ export type SliceBuilder<S extends SchemaRegister<A>, E extends Schemas, A exten
|
|
|
102
106
|
* @see {@link SliceBuilder} for builder methods
|
|
103
107
|
* @see {@link Slice} for the output type
|
|
104
108
|
*/
|
|
105
|
-
export declare function slice<
|
|
109
|
+
export declare function slice<TSchemaReg extends SchemaRegister<TActions> = {}, TEvents extends Schemas = {}, TActions extends Schemas = {}, TStateMap extends Record<string, Schema> = {}, TActor extends Actor = Actor>(states?: Map<string, State<any, any, any>>, actions?: Record<string, any>, events?: EventRegister<TEvents>, projections?: Projection<any>[]): SliceBuilder<TSchemaReg, TEvents, TActions, TStateMap, TActor>;
|
|
106
110
|
//# sourceMappingURL=slice-builder.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"slice-builder.d.ts","sourceRoot":"","sources":["../../src/slice-builder.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,KAAK,EACV,SAAS,EACT,UAAU,EACV,aAAa,EAGb,eAAe,EACf,gBAAgB,EAChB,MAAM,EACN,cAAc,EACd,OAAO,EACP,QAAQ,EACR,KAAK,EACN,MAAM,kBAAkB,CAAC;AAE1B
|
|
1
|
+
{"version":3,"file":"slice-builder.d.ts","sourceRoot":"","sources":["../../src/slice-builder.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,KAAK,EACV,KAAK,EACL,SAAS,EACT,UAAU,EACV,aAAa,EAGb,eAAe,EACf,gBAAgB,EAChB,MAAM,EACN,cAAc,EACd,OAAO,EACP,QAAQ,EACR,KAAK,EACN,MAAM,kBAAkB,CAAC;AAE1B;;;;;;;;;;GAUG;AACH,MAAM,MAAM,KAAK,CACf,UAAU,SAAS,cAAc,CAAC,QAAQ,CAAC,EAC3C,OAAO,SAAS,OAAO,EACvB,QAAQ,SAAS,OAAO,EAExB,SAAS,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,EAAE,EAC7C,MAAM,SAAS,KAAK,GAAG,KAAK,IAC1B;IACF,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;IACnD,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;IACxC,QAAQ,CAAC,WAAW,EAAE,aAAa,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC;IACrD,mEAAmE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AM,EAAE,KAAK,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAa,EACrD,OAAO,GAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAM,EACjC,MAAM,GAAE,aAAa,CAAC,OAAO,CAAgC,EAC7D,WAAW,GAAE,UAAU,CAAC,GAAG,CAAC,EAAO,GAClC,YAAY,CAAC,UAAU,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,CAAC,CA0FhE"}
|
|
@@ -12,12 +12,13 @@ import { ActionHandler, Invariant, PatchHandlers, Schema, Schemas, Snapshot, Sta
|
|
|
12
12
|
* Provides a fluent API to configure the initial state, event types,
|
|
13
13
|
* and event handlers (reducers) before moving to action configuration.
|
|
14
14
|
*
|
|
15
|
-
* @template
|
|
15
|
+
* @template TState - State schema type
|
|
16
|
+
* @template TName - State name literal type
|
|
16
17
|
*
|
|
17
18
|
* @see {@link state} for usage examples
|
|
18
19
|
* @see {@link ActionBuilder} for action configuration
|
|
19
20
|
*/
|
|
20
|
-
export type StateBuilder<
|
|
21
|
+
export type StateBuilder<TState extends Schema, TName extends string = string> = {
|
|
21
22
|
/**
|
|
22
23
|
* Defines the initial state for new state instances.
|
|
23
24
|
*
|
|
@@ -37,14 +38,14 @@ export type StateBuilder<S extends Schema, N extends string = string> = {
|
|
|
37
38
|
* .init((data) => ({ ...data, createdAt: new Date() }))
|
|
38
39
|
* ```
|
|
39
40
|
*/
|
|
40
|
-
init: (init: () => Readonly<
|
|
41
|
+
init: (init: () => Readonly<TState>) => {
|
|
41
42
|
/**
|
|
42
43
|
* Declares the event types that this state can emit.
|
|
43
44
|
*
|
|
44
45
|
* Events represent facts that have happened - they should be named in past tense.
|
|
45
46
|
* Each event is defined with a Zod schema for type safety and runtime validation.
|
|
46
47
|
*
|
|
47
|
-
* @template
|
|
48
|
+
* @template TEvents - Event schemas type
|
|
48
49
|
* @param events - Object mapping event names to Zod schemas
|
|
49
50
|
* @returns An ActionBuilder (with optional `.patch()` to override specific reducers)
|
|
50
51
|
*
|
|
@@ -57,7 +58,7 @@ export type StateBuilder<S extends Schema, N extends string = string> = {
|
|
|
57
58
|
* })
|
|
58
59
|
* ```
|
|
59
60
|
*/
|
|
60
|
-
emits: <
|
|
61
|
+
emits: <TEvents extends Schemas>(events: ZodTypes<TEvents>) => ActionBuilder<TState, TEvents, {}, TName> & {
|
|
61
62
|
/**
|
|
62
63
|
* Overrides specific event reducers. Events without a custom patch
|
|
63
64
|
* default to passthrough: `({ data }) => data` (event data merges
|
|
@@ -78,17 +79,17 @@ export type StateBuilder<S extends Schema, N extends string = string> = {
|
|
|
78
79
|
* // TicketClosed and TicketResolved use passthrough
|
|
79
80
|
* ```
|
|
80
81
|
*/
|
|
81
|
-
patch: (patch: Partial<PatchHandlers<
|
|
82
|
+
patch: (patch: Partial<PatchHandlers<TState, TEvents>>) => ActionBuilder<TState, TEvents, {}, TName>;
|
|
82
83
|
};
|
|
83
84
|
};
|
|
84
85
|
};
|
|
85
86
|
/** Helper: a single-key record mapping a state name to its Zod schema. */
|
|
86
|
-
type StateEntry<
|
|
87
|
-
[P in
|
|
87
|
+
type StateEntry<TKey extends string = string, TState extends Schema = Schema> = {
|
|
88
|
+
[P in TKey]: ZodType<TState>;
|
|
88
89
|
};
|
|
89
90
|
/** Helper: a single-key record mapping an action name to its Zod schema. */
|
|
90
|
-
type ActionEntry<
|
|
91
|
-
[P in
|
|
91
|
+
type ActionEntry<TKey extends string = string, TNewActions extends Schema = Schema> = {
|
|
92
|
+
[P in TKey]: ZodType<TNewActions>;
|
|
92
93
|
};
|
|
93
94
|
/**
|
|
94
95
|
* Builder interface for defining actions (commands) on a state.
|
|
@@ -96,13 +97,14 @@ type ActionEntry<K extends string = string, AX extends Schema = Schema> = {
|
|
|
96
97
|
* Actions represent user/system intents to modify state. Each action is validated
|
|
97
98
|
* against a schema, can have business rule invariants, and must emit one or more events.
|
|
98
99
|
*
|
|
99
|
-
* @template
|
|
100
|
-
* @template
|
|
101
|
-
* @template
|
|
100
|
+
* @template TState - State schema type
|
|
101
|
+
* @template TEvents - Event schemas type
|
|
102
|
+
* @template TActions - Action schemas type
|
|
103
|
+
* @template TName - State name literal type
|
|
102
104
|
*
|
|
103
105
|
* @see {@link state} for complete usage examples
|
|
104
106
|
*/
|
|
105
|
-
export type ActionBuilder<
|
|
107
|
+
export type ActionBuilder<TState extends Schema, TEvents extends Schemas, TActions extends Schemas, TName extends string = string> = {
|
|
106
108
|
/**
|
|
107
109
|
* Defines an action (command) that can be executed on this state.
|
|
108
110
|
*
|
|
@@ -114,8 +116,8 @@ export type ActionBuilder<S extends Schema, E extends Schemas, A extends Schemas
|
|
|
114
116
|
* when the variable name matches the action name. The key becomes the
|
|
115
117
|
* action name, the value the Zod schema.
|
|
116
118
|
*
|
|
117
|
-
* @template
|
|
118
|
-
* @template
|
|
119
|
+
* @template TKey - Action name (string literal type)
|
|
120
|
+
* @template TNewActions - Action payload schema type
|
|
119
121
|
* @param entry - Single-key record `{ ActionName: schema }`
|
|
120
122
|
* @returns An object with `.given()` and `.emit()` for further configuration
|
|
121
123
|
*
|
|
@@ -142,7 +144,7 @@ export type ActionBuilder<S extends Schema, E extends Schemas, A extends Schemas
|
|
|
142
144
|
* .emit((action) => ["TicketOpened", { title: action.title }])
|
|
143
145
|
* ```
|
|
144
146
|
*/
|
|
145
|
-
on: <
|
|
147
|
+
on: <TKey extends string, TNewActions extends Schema>(entry: ActionEntry<TKey, TNewActions>) => {
|
|
146
148
|
/**
|
|
147
149
|
* Adds business rule invariants that must hold before the action can execute.
|
|
148
150
|
*
|
|
@@ -161,7 +163,7 @@ export type ActionBuilder<S extends Schema, E extends Schemas, A extends Schemas
|
|
|
161
163
|
* ])
|
|
162
164
|
* ```
|
|
163
165
|
*/
|
|
164
|
-
given: (rules: Invariant<
|
|
166
|
+
given: (rules: Invariant<TState>[]) => {
|
|
165
167
|
/**
|
|
166
168
|
* Defines the action handler that emits events.
|
|
167
169
|
*
|
|
@@ -188,11 +190,11 @@ export type ActionBuilder<S extends Schema, E extends Schemas, A extends Schemas
|
|
|
188
190
|
* .emit("TicketAssigned")
|
|
189
191
|
* ```
|
|
190
192
|
*/
|
|
191
|
-
emit: (handler: ActionHandler<
|
|
192
|
-
[P in
|
|
193
|
-
},
|
|
194
|
-
[P in
|
|
195
|
-
},
|
|
193
|
+
emit: (handler: ActionHandler<TState, TEvents, {
|
|
194
|
+
[P in TKey]: TNewActions;
|
|
195
|
+
}, TKey> | (keyof TEvents & string)) => ActionBuilder<TState, TEvents, TActions & {
|
|
196
|
+
[P in TKey]: TNewActions;
|
|
197
|
+
}, TName>;
|
|
196
198
|
};
|
|
197
199
|
/**
|
|
198
200
|
* Defines the action handler that emits events.
|
|
@@ -225,11 +227,11 @@ export type ActionBuilder<S extends Schema, E extends Schemas, A extends Schemas
|
|
|
225
227
|
* ])
|
|
226
228
|
* ```
|
|
227
229
|
*/
|
|
228
|
-
emit: (handler: ActionHandler<
|
|
229
|
-
[P in
|
|
230
|
-
},
|
|
231
|
-
[P in
|
|
232
|
-
},
|
|
230
|
+
emit: (handler: ActionHandler<TState, TEvents, {
|
|
231
|
+
[P in TKey]: TNewActions;
|
|
232
|
+
}, TKey> | (keyof TEvents & string)) => ActionBuilder<TState, TEvents, TActions & {
|
|
233
|
+
[P in TKey]: TNewActions;
|
|
234
|
+
}, TName>;
|
|
233
235
|
};
|
|
234
236
|
/**
|
|
235
237
|
* Defines a snapshotting strategy to optimize state reconstruction.
|
|
@@ -262,7 +264,7 @@ export type ActionBuilder<S extends Schema, E extends Schemas, A extends Schemas
|
|
|
262
264
|
* })
|
|
263
265
|
* ```
|
|
264
266
|
*/
|
|
265
|
-
snap: (snap: (snapshot: Snapshot<
|
|
267
|
+
snap: (snap: (snapshot: Snapshot<TState, TEvents>) => boolean) => ActionBuilder<TState, TEvents, TActions, TName>;
|
|
266
268
|
/**
|
|
267
269
|
* Finalizes and builds the state definition.
|
|
268
270
|
*
|
|
@@ -279,10 +281,10 @@ export type ActionBuilder<S extends Schema, E extends Schemas, A extends Schemas
|
|
|
279
281
|
* .patch({ Incremented: ({ data }, state) => ({ count: state.count + data.amount }) })
|
|
280
282
|
* .on({ increment: z.object({ by: z.number() }) })
|
|
281
283
|
* .emit((action) => ["Incremented", { amount: action.by }])
|
|
282
|
-
* .build(); // Returns State<
|
|
284
|
+
* .build(); // Returns State<TState, TEvents, TActions, TName>
|
|
283
285
|
* ```
|
|
284
286
|
*/
|
|
285
|
-
build: () => State<
|
|
287
|
+
build: () => State<TState, TEvents, TActions, TName>;
|
|
286
288
|
};
|
|
287
289
|
/**
|
|
288
290
|
* Creates a new state definition with event sourcing capabilities.
|
|
@@ -300,7 +302,7 @@ export type ActionBuilder<S extends Schema, E extends Schemas, A extends Schemas
|
|
|
300
302
|
* 5. Business rules (invariants) via `.given()`
|
|
301
303
|
* 6. Snapshotting strategy via `.snap()`
|
|
302
304
|
*
|
|
303
|
-
* @template
|
|
305
|
+
* @template TState - Zod schema type defining the shape of the state
|
|
304
306
|
* @param entry - Single-key record mapping state name to Zod schema (e.g., `{ Counter: z.object({ count: z.number() }) }`)
|
|
305
307
|
* @returns A StateBuilder instance for fluent API configuration
|
|
306
308
|
*
|
|
@@ -401,6 +403,6 @@ export type ActionBuilder<S extends Schema, E extends Schemas, A extends Schemas
|
|
|
401
403
|
* @see {@link https://rotorsoft.github.io/act-root/docs/intro | Getting Started Guide}
|
|
402
404
|
* @see {@link https://rotorsoft.github.io/act-root/docs/examples/calculator | Calculator Example}
|
|
403
405
|
*/
|
|
404
|
-
export declare function state<
|
|
406
|
+
export declare function state<TName extends string, TState extends Schema>(entry: StateEntry<TName, TState>): StateBuilder<TState, TName>;
|
|
405
407
|
export {};
|
|
406
408
|
//# sourceMappingURL=state-builder.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"state-builder.d.ts","sourceRoot":"","sources":["../../src/state-builder.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,O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