-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvt.js
executable file
·105 lines (98 loc) · 3.09 KB
/
Evt.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
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
/*
Evt.js - Publish/Subscribe for Javascript
http://evt.github.com
Author: James Filtness 2015 (github: jamesfiltness)
*/
/**
*
* @Evt
* Should be instantiated - i.e. let evts = new Evt()
* Allows for context and arguments to be passed to the subscribing functions
*/
export default class Evt {
/**
*
* @Evt - Creates a new instance of Ok
* @constructor
* @this {Evt}
* Creates an empty evts object where all subscriptions will be held
* Creats a uid which each subscription will be stored with - provides mechanism to unsubscribe
*
*/
constructor() {
this.evts = {};
this.uid = -1;
}
/**
*
* @subscribe - subscribe a callback to an event - return a uid for unsubscribing
* @method
* @this {Evt}
* @evt {string} the event that the callback is subscribing to - if the event does not exist the method defines it
* @fn {function} the callback - to be called when the corresponding event is published
* ctx {object} - an optional 'this' context can be supplied
*
*/
subscribe(evt, fn, ctx) {
if (!this.evts.hasOwnProperty(evt)) {
this.evts[evt] = {};
}
ctx ? this.evts[evt][++this.uid] = { fn: fn, ctx: ctx } : this.evts[evt][++this.uid] = fn;
//return uid for unsubscribing
return this.uid;
}
/**
*
* @unsubscribe - unsubscribe a callback
* @method
* @this {Evt}
* @uid {number | string} if a number the subscribing callback is unsubscribed
* If a string is provided then the corresponding evt itself is removed
*
*/
unsubscribe(uid) {
let key;
if('number' === typeof uid) {
for(key in this.evts) {
if(this.evts[key][uid]) {
this.evts[key][uid] = {};
delete this.evts[key][uid];
}
}
} else if('string' === typeof uid) {
this.evts[uid] = {};
}
}
/**
*
* @publish - call all subscribing callbacks, applying their context if defined and passing along additional params
* @method
* @this {Evt}
* @params - first argument must be the event to be published, additional arguments are fed to the subscribing function as arguments
*
*/
publish() {
let args = Array.from(arguments),
evt = args[0],
key;
//remove the evt argument from the array of supplied args
args.shift();
if (this.evts.hasOwnProperty(evt)) {
for (key in this.evts[evt]) {
'object' === typeof this.evts[evt][key]
? this.evts[evt][key].fn.apply(this.evts[evt][key].ctx, args)
: this.evts[evt][key].apply(null,args);
}
}
}
/**
*
* @purge - reset the Evt instance - remove all events and subscribing functions
* @method
* @this {Evt}
*
*/
purge() {
this.evts = {};
}
}