-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.js
47 lines (38 loc) · 1007 Bytes
/
event.js
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
import {dlog} from './op.app.js'
import { _exists, _default } from "./functions";
/**
* Simple event reactor
* kudos to
*/
export class Event{
constructor(name){
this.name = name;
this.callbacks = [];
}
registerCallback (callback) {
this.callbacks.push(callback);
}
}
export class Reactor{
constructor(){
this.events = {};
}
registerEvent(eventName) {
var event = new Event(eventName);
this.events[eventName] = event;
}
dispatchEvent(eventName, eventArgs) {
this.events[eventName].callbacks.forEach(function (callback) {
callback(eventArgs);
});
};
addEventListener (eventName, callback) {
dlog( Object.keys( this.events ) );
if( _exists(this.events[eventName])){
this.events[eventName].registerCallback(callback);
}
else{
dlog(`Event not found: [${eventName}] `, this.events );
}
};
}