forked from snatalenko/node-cqrs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterfaces.d.ts
More file actions
188 lines (142 loc) · 5.16 KB
/
interfaces.d.ts
File metadata and controls
188 lines (142 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
declare type Identifier = string | number;
declare interface IMessage {
type: string;
aggregateId?: Identifier;
aggregateVersion?: number;
sagaId?: Identifier;
sagaVersion?: number;
payload?: any;
context?: any;
}
declare type ICommand = IMessage;
declare type IEvent = IMessage;
declare type IEventStream = ReadonlyArray<Readonly<IEvent>>;
declare interface IEventStore extends IObservable {
getNewId(): Promise<Identifier>;
commit(events: IEventStream): Promise<IEventStream>;
getAllEvents(eventTypes: string[], filter?: EventFilter): Promise<IEventStream>;
getAggregateEvents(aggregateId: Identifier): Promise<IEventStream>;
getSagaEvents(sagaId: Identifier, filter: EventFilter): Promise<IEventStream>;
registerSagaStarters(eventTypes: string[]): void;
once(messageType: string, handler?: IMessageHandler, filter?: function(IEvent): boolean):
Promise<IEvent>;
snapshotsSupported?: boolean;
}
declare interface ICommandBus extends IObservable {
send(commandType: string, aggregateId: Identifier, options: { payload?: object, context?: object }):
Promise<IEventStream>;
sendRaw(ICommand):
Promise<IEventStream>;
}
// region Aggregate
declare interface IAggregateState extends Object {
mutate?(event: IEvent): void;
[key: string]: any;
}
declare interface IAggregate {
readonly id: Identifier;
readonly version: number;
readonly changes: IEventStream;
readonly state?: IAggregateState;
handle(command: ICommand): any;
mutate(event: IEvent): void;
emit(eventType: string, payload?: any): void;
emitRaw(IEvent): void;
readonly snapshotVersion?: number;
readonly shouldTakeSnapshot?: boolean;
takeSnapshot?(): void;
makeSnapshot?(): IEvent;
restoreSnapshot?(snapshotEvent: IEvent): void;
}
declare type TAggregateParams = { id: Identifier, events?: IEventStream, state?: IAggregateState };
declare type IAggregateFactory = (options: TAggregateParams) => IAggregate;
declare interface IAggregateConstructor {
new(options: TAggregateParams): IAggregate;
readonly handles: string[];
}
declare interface ICommandHandler extends IObserver {
subscribe(commandBus: ICommandBus): void;
}
// endregion Aggregate
// region Saga
declare interface ISaga {
readonly id: Identifier;
readonly version: number;
readonly uncommittedMessages: ICommand[];
readonly restored?: boolean;
apply(event: IEvent): ICommand[];
enqueue(commandType: string, aggregateId: Identifier, payload: any): void;
enqueueRaw(command: ICommand): void;
resetUncommittedMessages(): void;
onError(err: Error, params: { event: IEvent, command: ICommand }): void;
}
declare type TSagaParams = { id: Identifier, events?: IEventStream };
declare type ISagaFactory = (options: TSagaParams) => ISaga;
declare interface ISagaConstructor {
new(options: TSagaParams): ISaga;
readonly startsWith: string[];
readonly handles: string[];
}
declare interface IEventReceptor extends IObserver {
subscribe(eventStore: IEventStore): void;
}
// endregion Saga
// region Projection
declare interface IProjection extends IObserver {
readonly view: object;
subscribe(eventStore: IEventStore): void;
project(event: IEvent, options?: { nowait: boolean }): Promise<void>;
}
declare type ViewUpdateCallback = function(any): any;
declare interface IProjectionView<TRecord> {
get(key: any): Promise<TRecord>;
}
declare interface IConcurrentView<TRecord> extends IProjectionView<TRecord> {
ready: boolean;
lock(): Promise<void>;
unlock(): Promise<void>;
once(eventType: "ready"): Promise<void>;
}
// endregion Projection
// region Observable / Observer
declare type IMessageHandler = (message: IMessage) => void;
declare interface IObservable {
on(type: string, handler: IMessageHandler): void;
queue?(name: string): IObservable;
}
declare type TSubscribeOptions = {
messageTypes?: string[],
masterHandler?: IMessageHandler,
queueName?: string
}
declare interface IObserver {
static subscribe(obervable: IObservable, observer: IObserver, options: TSubscribeOptions): void;
readonly handles?: string[];
subscribe(obervable: IObservable, messageTypes?: string[], masterHandler?: IMessageHandler | string): void;
}
// endregion
declare type EventFilter = { afterEvent?: IEvent; beforeEvent?: IEvent; };
declare interface IEventEmitter extends IObservable {
on?(messageType: string, handler: IMessageHandler): void;
off?(messageType: string, handler: IMessageHandler): void;
}
declare interface IEventStorage extends IEventEmitter {
getNewId(): Identifier | Promise<Identifier>;
commitEvents(events: ReadonlyArray<IEvent>): Promise<any>;
getAggregateEvents(aggregateId: Identifier, options: { snapshot: IEvent }): Promise<IEventStream>;
getSagaEvents(sagaId: Identifier, filter: EventFilter): Promise<IEventStream>;
getEvents(eventTypes: string[], filter: EventFilter): Promise<IEventStream>;
}
declare interface IAggregateSnapshotStorage {
getAggregateSnapshot(aggregateId: Identifier): Promise<IEvent>;
saveAggregateSnapshot(IEvent): Promise<void>;
}
declare interface IMessageBus extends IEventEmitter {
send(command: ICommand): Promise<any>;
publish(event: IEvent): Promise<any>;
}
// endregion
declare interface IConstructor<T> {
new(...args: any[]): T;
}
declare type TOF = ((...args: any[]) => object) | IConstructor<object>;