-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
163 lines (133 loc) · 4.1 KB
/
index.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
class Event {
constructor() {
this.subscriptions = {};
this.sub = this.subscribe;
this.unsub = this.unsubscribe;
}
/**
* Publish an event to all relevant subscribers
* @param event
* @param payload
*/
publish(event, payload) {
let eventSegments = event.split('/');
let eventStack = '';
let notify = [];
if(this.subscriptions[event]) notify = this.subscriptions[event];
// check for wildcards
eventSegments.forEach((item,index) => {
eventStack += item;
// Matches all wildcard
if(this.subscriptions[eventStack + '*']) {
notify = [...notify, ...this.subscriptions[eventStack + '*']];
}
eventStack += '/';
// Matches wildcard one level down
if(this.subscriptions[eventStack + '*'] && eventSegments[index + 1] && ! eventSegments[index + 2]) {
notify = [...notify, ...this.subscriptions[eventStack + '*']];
}
// Matches wildcard all levels down
if(this.subscriptions[eventStack + '**'] && eventSegments[index+1]) {
notify = [...notify, ...this.subscriptions[eventStack + '**']]
}
});
notify.forEach((item, index) => {
item.callback(payload, event);
});
}
/**
* Subscribes to an event
* @param event
* @param callback
* @returns {*}
*/
subscribe(event, callback) {
if(Array.isArray(event)) {
let eventIds = [];
event.forEach(item => {
eventIds.push(this._subscribeSingle(item, callback));
});
return eventIds;
} else {
return this._subscribeSingle(event, callback);
}
}
/**
* Unsubscribes one more event listeners
* @param event
*/
unsubscribe(event) {
if(Array.isArray(event)) {
event.forEach(eventId => this._unsubscribeSingle(eventId))
} else {
this._unsubscribeSingle(event);
}
}
/**
* Clears all subscriptions
*/
clearAll() {
this.subscriptions = [];
}
/**
* Subscribes a single event
* @param event
* @param callback
* @returns {*}
* @private
*/
_subscribeSingle(event, callback) {
if(!this.subscriptions[event]) {
this.subscriptions[event] = [];
}
const subscriptionId = this._generateId();
this.subscriptions[event].push({callback, id: subscriptionId});
return subscriptionId;
}
/**
* Unsubscribes a single event
* @param eventId
* @private
*/
_unsubscribeSingle(eventId) {
Object.keys(this.subscriptions).forEach(event => {
let subscriptions = this.subscriptions[event];
if(subscriptions) {
subscriptions.forEach((item, key) => {
if(item.id === eventId) subscriptions.splice(key, 1);
});
}
if(!subscriptions.length) {
delete this.subscriptions[event];
} else {
this.subscriptions[event] = subscriptions;
}
});
}
/**
* Generates an ID
* @returns {string}
* @private
*/
_generateId() {
const possibleChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let text = [];
for(let i=0; i < 10; i++) {
text.push(possibleChars.charAt(Math.floor(Math.random() * possibleChars.length)));
}
return text.join('');
}
}
const Eventsy = new Event();
export const useListener = (listener, callback, updateOn = []) => {
const React = require('react');
return React.useEffect(() => {
const subscribedListener = Eventsy.sub(listener, callback);
return () => Eventsy.unsubscribe(subscribedListener);
}, updateOn);
};
export const eventSystemReduxMiddleware = store => next => action => {
Eventsy.publish(action.type, action.payload);
return next(action);
};
export default Eventsy;