Skip to content

Commit b61522b

Browse files
committed
feat(Logger): create EventManager
1 parent dc5afc2 commit b61522b

File tree

7 files changed

+134
-0
lines changed

7 files changed

+134
-0
lines changed

src/Library/EventManager/Event.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export class Event<T> {
2+
constructor(private event: string, private target: any, private payload: any) { }
3+
4+
getEvent(): string {
5+
return this.event;
6+
}
7+
8+
getTarget(): T {
9+
return this.target;
10+
}
11+
12+
getPayload(): any {
13+
return this.payload;
14+
}
15+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { EventEmitter } from 'events';
2+
import { Event } from './Event';
3+
import { SelfDestructingCallbackInterface } from './EventManagerTypes';
4+
5+
export class EventManager extends EventEmitter {
6+
protected sharedEventManager: EventManager;
7+
8+
private hooks: { [eventName: string]: Array<Function> } = {};
9+
10+
public constructor (sharedEventManager: EventManager = null) {
11+
super();
12+
13+
this.sharedEventManager = sharedEventManager;
14+
}
15+
16+
async trigger(eventName: string, target: any, payload?: any): Promise<boolean> {
17+
if (!this.hooks[eventName]) {
18+
return false;
19+
}
20+
21+
const callbacks = this.hooks[eventName];
22+
23+
let remaining = callbacks.length;
24+
25+
for (let i = 0; i < remaining; i++) {
26+
const callback = callbacks[i] as SelfDestructingCallbackInterface;
27+
28+
await callback(new Event<typeof target>(eventName, target, payload));
29+
30+
if (callback._isSelfDestructingCallback) {
31+
callbacks.splice(i, 1);
32+
33+
remaining--;
34+
i--;
35+
}
36+
}
37+
38+
if (!remaining) {
39+
delete this.hooks[eventName];
40+
}
41+
42+
return true;
43+
}
44+
45+
has (event: string, callback: Function): boolean {
46+
return this.hooks[event] && this.hooks[event].indexOf(callback) > -1;
47+
}
48+
49+
attachOnce(eventName: string, callback: Function, index?: number) {
50+
(callback as SelfDestructingCallbackInterface)._isSelfDestructingCallback = true;
51+
52+
this.attach(eventName, callback, index);
53+
}
54+
55+
attach(event: string, callback: Function, index?: number): this {
56+
this.hooks[event] = this.hooks[event] || [];
57+
58+
if (index) {
59+
this.hooks[event].splice(index, 0, callback);
60+
} else {
61+
this.hooks[event].push(callback);
62+
}
63+
64+
return this;
65+
}
66+
67+
attachAt(index: number, event: string, callback: Function): this {
68+
return this.attach(event, callback, index);
69+
}
70+
71+
detach(event: string, callback: Function): this {
72+
if (!this.hooks[event]) {
73+
return this;
74+
}
75+
76+
const hookIndex: number = this.hooks[event].indexOf(callback);
77+
78+
if (hookIndex === -1) {
79+
return this;
80+
}
81+
82+
this.hooks[event].splice(hookIndex, 1);
83+
84+
if (this.hooks[event].length === 0) {
85+
delete this.hooks[event];
86+
}
87+
88+
return this;
89+
}
90+
91+
getSharedEventManager(): EventManager {
92+
return this.sharedEventManager;
93+
}
94+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { ServiceManager } from '../ServiceManager';
2+
import { SharedEventManager } from './SharedEventManager';
3+
import { EventManager } from './EventManager';
4+
5+
export const EventManagerFactory = (sm: ServiceManager) => {
6+
return new EventManager(sm.has(SharedEventManager) ? sm.get(SharedEventManager) : null);
7+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Event } from './Event';
2+
3+
export interface SelfDestructingCallbackInterface extends Function {
4+
(event: Event<any>): Promise<void>;
5+
_isSelfDestructingCallback?: boolean;
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export enum ModuleManagerEvents {
2+
OnBootstrap = 'OnBootstrap',
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { EventManager } from './EventManager';
2+
3+
export class SharedEventManager extends EventManager {
4+
}

src/Library/EventManager/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export * from './Event';
2+
export * from './EventManager';
3+
export * from './EventManagerFactory';
4+
export * from './SharedEventManager';
5+
export * from './EventManagerTypes';

0 commit comments

Comments
 (0)