Skip to content
This repository was archived by the owner on Nov 30, 2019. It is now read-only.

Commit 9a692db

Browse files
committed
feat(EventManager): Added Event and EventManager
The EventManager will handle all the events which will happen in the client and the server
1 parent c302b42 commit 9a692db

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

src/event/Event.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* The basic structure of an event
3+
*
4+
* @export
5+
* @abstract
6+
* @class Event
7+
* @since 0.0.1
8+
* @version 0.0.1
9+
* @author Yannick Fricke <yannickfricke@googlemail.com>
10+
* @license MIT
11+
* @copyright MedjaiBot https://github.com/MedjaiBot/server
12+
*/
13+
export abstract class Event {
14+
/**
15+
* The name of the event
16+
*
17+
* @type {string}
18+
* @memberof Event
19+
*/
20+
public name: string;
21+
22+
/**
23+
* Creates an instance of Event.
24+
* @param {string} name The name of the event which will
25+
* be used internally
26+
* @memberof Event
27+
*/
28+
constructor(
29+
name: string,
30+
) {
31+
// Sets the name property to the given name
32+
this.name = name;
33+
}
34+
}

src/event/EventManager.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { injectable } from 'inversify';
2+
import { Event } from './Event';
3+
4+
/**
5+
* The EventManager handles all events
6+
*
7+
* @export
8+
* @class EventManager
9+
* @since 0.0.1
10+
* @version 0.0.1
11+
* @author Yannick Fricke <yannickfricke@googlemail.com>
12+
* @license MIT
13+
*/
14+
@injectable()
15+
export class EventManager {
16+
/**
17+
* The registered events
18+
*
19+
* @private
20+
* @type {Map<string, object[]>}
21+
* @memberof EventManager
22+
*/
23+
public registeredEvents: Map<string, any[]>;
24+
25+
/**
26+
* Creates an instance of EventManager.
27+
* @memberof EventManager
28+
*/
29+
constructor() {
30+
this.registeredEvents = new Map();
31+
}
32+
public async measureEventByName(
33+
eventName: string,
34+
callArgument: any,
35+
) {
36+
const startTime = Date.now();
37+
38+
await this.broadcast(eventName, callArgument);
39+
40+
return Date.now() - startTime;
41+
}
42+
43+
/**
44+
* Measures an event and returns it execution time
45+
*
46+
* @param event The event to measure
47+
* @returns The measured time
48+
*/
49+
public async measureEvent(
50+
event: Event,
51+
): Promise<number> {
52+
const startTime = Date.now();
53+
54+
await this.broadcast(event.name, event);
55+
56+
return Date.now() - startTime;
57+
}
58+
59+
/**
60+
* Broadcasts an event to the subscribers
61+
*
62+
* @param {string} eventName
63+
* @param {...any[]} args
64+
* @memberof EventManager
65+
*/
66+
public async broadcast(eventName: string, ...args: any[]) {
67+
await this.publishToSubscribers(eventName, args);
68+
}
69+
70+
public async broadcastEvent(event: Event) {
71+
await this.broadcast(event.name, event);
72+
}
73+
74+
public async publishToSubscribers(
75+
eventName: string,
76+
args: any,
77+
) {
78+
if (!this.registeredEvents.has(eventName)) {
79+
return;
80+
}
81+
82+
const subscribers = this.registeredEvents.get(eventName) as any[];
83+
84+
subscribers.forEach(async (subscriber) => {
85+
await subscriber(...args);
86+
});
87+
}
88+
89+
/**
90+
* Registers an event with the given subscriber
91+
*
92+
* @param {string} eventName The name of the event
93+
* @param {object} subscriber The subscriber to add
94+
* @memberof EventManager
95+
*/
96+
public registerEvent(eventName: string, subscriber: object): void {
97+
let existingEntries = [];
98+
99+
if (this.registeredEvents.has(eventName)) {
100+
existingEntries = this.registeredEvents.get(eventName) as any[];
101+
}
102+
103+
existingEntries.push(subscriber);
104+
105+
this.registeredEvents.set(eventName, existingEntries);
106+
}
107+
}

0 commit comments

Comments
 (0)