forked from PrestaShopCorp/cqrs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent-bus.ts
133 lines (113 loc) · 3.91 KB
/
event-bus.ts
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
import { Injectable, OnModuleDestroy, Type } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import { Observable, Subscription } from 'rxjs';
import { filter } from 'rxjs/operators';
import { isFunction } from 'util';
import { CommandBus } from './command-bus';
import { EVENTS_HANDLER_METADATA, SAGA_METADATA } from './decorators/constants';
import { InvalidSagaException } from './exceptions';
import { defaultGetEventName } from './helpers/default-get-event-name';
import { DefaultPubSub } from './helpers/default-pubsub';
import {
IEvent,
IEventBus,
IEventHandler,
IEventPublisher,
ISaga,
} from './interfaces';
import { ObservableBus } from './utils';
export type EventHandlerType<EventBase extends IEvent = IEvent> = Type<
IEventHandler<EventBase>
>;
@Injectable()
export class EventBus<EventBase extends IEvent = IEvent>
extends ObservableBus<EventBase>
implements IEventBus<EventBase>, OnModuleDestroy {
protected getEventName: (event: EventBase) => string;
protected readonly subscriptions: Subscription[];
private _publisher: IEventPublisher<EventBase>;
constructor(
private readonly commandBus: CommandBus,
private readonly moduleRef: ModuleRef,
) {
super();
this.subscriptions = [];
this.getEventName = defaultGetEventName;
this.useDefaultPublisher();
}
get publisher(): IEventPublisher<EventBase> {
return this._publisher;
}
set publisher(_publisher: IEventPublisher<EventBase>) {
this._publisher = _publisher;
}
onModuleDestroy() {
this.subscriptions.forEach((subscription) => subscription.unsubscribe());
}
publish<T extends EventBase>(event: T) {
return this._publisher.publish(event);
}
publishAll<T extends EventBase>(events: T[]) {
if (this._publisher.publishAll) {
return this._publisher.publishAll(events);
}
return (events || []).map((event) => this._publisher.publish(event));
}
bind(handler: IEventHandler<EventBase>, name: string) {
const stream$ = name ? this.ofEventName(name) : this.subject$;
const subscription = stream$.subscribe((event) => handler.handle(event));
this.subscriptions.push(subscription);
}
registerSagas(types: Type<unknown>[] = []) {
const sagas = types
.map((target) => {
const metadata = Reflect.getMetadata(SAGA_METADATA, target) || [];
const instance = this.moduleRef.get(target, { strict: false });
if (!instance) {
throw new InvalidSagaException();
}
return metadata.map((key: string) => instance[key]);
})
.reduce((a, b) => a.concat(b), []);
sagas.forEach((saga) => this.registerSaga(saga));
}
register(handlers: EventHandlerType<EventBase>[] = []) {
handlers.forEach((handler) => this.registerHandler(handler));
}
protected registerHandler(handler: EventHandlerType<EventBase>) {
const instance = this.moduleRef.get(handler, { strict: false });
if (!instance) {
return;
}
const eventsNames = this.reflectEventsNames(handler);
eventsNames.map((event) =>
this.bind(instance as IEventHandler<EventBase>, event.name),
);
}
protected ofEventName(name: string) {
return this.subject$.pipe(
filter((event) => this.getEventName(event) === name),
);
}
protected registerSaga(saga: ISaga<EventBase>) {
if (!isFunction(saga)) {
throw new InvalidSagaException();
}
const stream$ = saga(this);
if (!(stream$ instanceof Observable)) {
throw new InvalidSagaException();
}
const subscription = stream$
.pipe(filter((e) => !!e))
.subscribe((command) => this.commandBus.execute(command));
this.subscriptions.push(subscription);
}
private reflectEventsNames(
handler: EventHandlerType<EventBase>,
): FunctionConstructor[] {
return Reflect.getMetadata(EVENTS_HANDLER_METADATA, handler);
}
private useDefaultPublisher() {
this._publisher = new DefaultPubSub<EventBase>(this.subject$);
}
}