comes 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.d.ts +45 -11
- package/build/index.js +68 -8
- package/package.json +1 -1
package/build/index.d.ts
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* @param item Item that will be removed
|
|
4
|
-
* @param list Array where to find the item
|
|
5
|
-
*/
|
|
6
|
-
export declare function deleteFromArray(item: any, list: any[]): void;
|
|
1
|
+
export type EventListenerType<T = any> = (event: T) => void;
|
|
2
|
+
export type EventInterceptorType<T = any> = (id: string, event: T, es: EventSystem) => T;
|
|
7
3
|
/**
|
|
8
4
|
* Type that holds the value for an event address.
|
|
9
5
|
*/
|
|
@@ -15,7 +11,7 @@ export type ES_ValueType = {
|
|
|
15
11
|
/**
|
|
16
12
|
* Listeners of this event.
|
|
17
13
|
*/
|
|
18
|
-
listeners:
|
|
14
|
+
listeners: EventListenerType[];
|
|
19
15
|
/**
|
|
20
16
|
* Last time an event was emitted.
|
|
21
17
|
*
|
|
@@ -72,6 +68,19 @@ export declare class EventSystem {
|
|
|
72
68
|
data: {
|
|
73
69
|
[id: string]: ES_ValueType;
|
|
74
70
|
};
|
|
71
|
+
/**
|
|
72
|
+
* Interceptors to transform data.
|
|
73
|
+
* Interceptors are called in the installation order.
|
|
74
|
+
* The special empty string event address will be called for all events sent.
|
|
75
|
+
*
|
|
76
|
+
* The interceptors exists to transform, monitor, prepare or validate (or many other uses)
|
|
77
|
+
* the event sent to an address. The sequence of interceptors will be called like a
|
|
78
|
+
* "chain of responsibility" pattern, if any exception is thrown the following interceptors
|
|
79
|
+
* will not be executed, and the listeners not called.
|
|
80
|
+
*/
|
|
81
|
+
inters: {
|
|
82
|
+
[id: string]: EventInterceptorType[];
|
|
83
|
+
};
|
|
75
84
|
/**
|
|
76
85
|
* Gets a reference to {@link ES_ValueType} of the address informed.
|
|
77
86
|
* @param id The address name of the event
|
|
@@ -81,9 +90,8 @@ export declare class EventSystem {
|
|
|
81
90
|
* Sends the value to the listeners of the event address.
|
|
82
91
|
* @param id The address name of the event
|
|
83
92
|
* @param event The value to send to listeners
|
|
84
|
-
* @returns The value informed
|
|
85
93
|
*/
|
|
86
|
-
|
|
94
|
+
send<T>(id: string, event: T): Promise<T>;
|
|
87
95
|
/**
|
|
88
96
|
* Register a listener for the address.
|
|
89
97
|
* The listener will receive the last value emitted.
|
|
@@ -95,7 +103,7 @@ export declare class EventSystem {
|
|
|
95
103
|
* @param listener The listener, will be called every time a new value is emitted to this address
|
|
96
104
|
* @returns An unregister function. Use this function to remove the listener
|
|
97
105
|
*/
|
|
98
|
-
listen(id: string, listener:
|
|
106
|
+
listen(id: string, listener: EventListenerType): () => void;
|
|
99
107
|
/**
|
|
100
108
|
* Remove the listener from the list of this event address.
|
|
101
109
|
*
|
|
@@ -115,7 +123,33 @@ export declare class EventSystem {
|
|
|
115
123
|
* @param id The address name of the event
|
|
116
124
|
* @param listener Listener to remove
|
|
117
125
|
*/
|
|
118
|
-
unlisten(id: string, listener:
|
|
126
|
+
unlisten(id: string, listener: EventListenerType): void;
|
|
127
|
+
/**
|
|
128
|
+
* Add an interceptor to transform data.
|
|
129
|
+
* Interceptors are called in the installation order.
|
|
130
|
+
* The special empty string event address will be called for all events sent.
|
|
131
|
+
*
|
|
132
|
+
* The interceptors exists to transform, monitor, prepare or validate (or many other uses)
|
|
133
|
+
* the event sent to an address. The sequence of interceptors will be called like a
|
|
134
|
+
* "chain of responsibility" pattern, if any exception is thrown the following interceptors
|
|
135
|
+
* will not be executed, and the listeners not called.
|
|
136
|
+
*
|
|
137
|
+
* Interceptors will be executed synchronous in the call of the {@link send} method.
|
|
138
|
+
* So any exception thrown will be thrown also in the {@link send} call.
|
|
139
|
+
*
|
|
140
|
+
* @param id The address name of the event
|
|
141
|
+
* @param listener Interceptor to add
|
|
142
|
+
* @returns An unregister function. Use this function to remove the interceptor
|
|
143
|
+
*/
|
|
144
|
+
addInter(id: string, listener: EventInterceptorType): () => void;
|
|
145
|
+
/**
|
|
146
|
+
* Remove an interceptor.
|
|
147
|
+
*
|
|
148
|
+
* @param id The address name of the event
|
|
149
|
+
* @param listener Interceptor to add
|
|
150
|
+
* @returns An unregister function. Use this function to remove the interceptor
|
|
151
|
+
*/
|
|
152
|
+
removeInter(id: string, listener: EventInterceptorType): void;
|
|
119
153
|
/**
|
|
120
154
|
* Configure a {@link ES_ValueType.loader loader} to execute when the first listener is registered and no value exists yet.
|
|
121
155
|
*
|
package/build/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.es = exports.EventSystem = void 0;
|
|
4
|
-
exports.deleteFromArray = deleteFromArray;
|
|
5
4
|
// -------
|
|
6
5
|
/**
|
|
7
6
|
* Function to exclude an item from the array.
|
|
@@ -27,6 +26,17 @@ class EventSystem {
|
|
|
27
26
|
* holds the last value for the address.
|
|
28
27
|
*/
|
|
29
28
|
this.data = {};
|
|
29
|
+
/**
|
|
30
|
+
* Interceptors to transform data.
|
|
31
|
+
* Interceptors are called in the installation order.
|
|
32
|
+
* The special empty string event address will be called for all events sent.
|
|
33
|
+
*
|
|
34
|
+
* The interceptors exists to transform, monitor, prepare or validate (or many other uses)
|
|
35
|
+
* the event sent to an address. The sequence of interceptors will be called like a
|
|
36
|
+
* "chain of responsibility" pattern, if any exception is thrown the following interceptors
|
|
37
|
+
* will not be executed, and the listeners not called.
|
|
38
|
+
*/
|
|
39
|
+
this.inters = {};
|
|
30
40
|
}
|
|
31
41
|
/**
|
|
32
42
|
* Gets a reference to {@link ES_ValueType} of the address informed.
|
|
@@ -41,15 +51,32 @@ class EventSystem {
|
|
|
41
51
|
* Sends the value to the listeners of the event address.
|
|
42
52
|
* @param id The address name of the event
|
|
43
53
|
* @param event The value to send to listeners
|
|
44
|
-
* @returns The value informed
|
|
45
54
|
*/
|
|
46
|
-
|
|
55
|
+
async send(id, event) {
|
|
56
|
+
let inters = [...(this.inters[''] || []), ...(this.inters[id] || [])];
|
|
57
|
+
let newValue = event;
|
|
58
|
+
try {
|
|
59
|
+
for (let inte of inters) {
|
|
60
|
+
newValue = await inte(id, newValue, this);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
catch (err) {
|
|
64
|
+
console.error(`error on interceptor of ${id}`, err);
|
|
65
|
+
throw err;
|
|
66
|
+
}
|
|
47
67
|
let esData = this.get(id);
|
|
48
|
-
esData.last =
|
|
68
|
+
esData.last = newValue;
|
|
49
69
|
esData.date = new Date();
|
|
50
|
-
for (let func of esData.listeners)
|
|
51
|
-
|
|
52
|
-
|
|
70
|
+
for (let func of esData.listeners) {
|
|
71
|
+
try {
|
|
72
|
+
await func(newValue);
|
|
73
|
+
}
|
|
74
|
+
catch (err) {
|
|
75
|
+
console.error(`Error on listener of ${id}, ${func}`, err);
|
|
76
|
+
throw err;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return newValue;
|
|
53
80
|
}
|
|
54
81
|
/**
|
|
55
82
|
* Register a listener for the address.
|
|
@@ -94,6 +121,39 @@ class EventSystem {
|
|
|
94
121
|
let esData = this.get(id);
|
|
95
122
|
deleteFromArray(listener, esData.listeners);
|
|
96
123
|
}
|
|
124
|
+
/**
|
|
125
|
+
* Add an interceptor to transform data.
|
|
126
|
+
* Interceptors are called in the installation order.
|
|
127
|
+
* The special empty string event address will be called for all events sent.
|
|
128
|
+
*
|
|
129
|
+
* The interceptors exists to transform, monitor, prepare or validate (or many other uses)
|
|
130
|
+
* the event sent to an address. The sequence of interceptors will be called like a
|
|
131
|
+
* "chain of responsibility" pattern, if any exception is thrown the following interceptors
|
|
132
|
+
* will not be executed, and the listeners not called.
|
|
133
|
+
*
|
|
134
|
+
* Interceptors will be executed synchronous in the call of the {@link send} method.
|
|
135
|
+
* So any exception thrown will be thrown also in the {@link send} call.
|
|
136
|
+
*
|
|
137
|
+
* @param id The address name of the event
|
|
138
|
+
* @param listener Interceptor to add
|
|
139
|
+
* @returns An unregister function. Use this function to remove the interceptor
|
|
140
|
+
*/
|
|
141
|
+
addInter(id, listener) {
|
|
142
|
+
if (!this.inters[id])
|
|
143
|
+
this.inters[id] = [];
|
|
144
|
+
this.inters[id].push(listener);
|
|
145
|
+
return () => this.removeInter(id, listener);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Remove an interceptor.
|
|
149
|
+
*
|
|
150
|
+
* @param id The address name of the event
|
|
151
|
+
* @param listener Interceptor to add
|
|
152
|
+
* @returns An unregister function. Use this function to remove the interceptor
|
|
153
|
+
*/
|
|
154
|
+
removeInter(id, listener) {
|
|
155
|
+
deleteFromArray(listener, this.inters[id]);
|
|
156
|
+
}
|
|
97
157
|
/**
|
|
98
158
|
* Configure a {@link ES_ValueType.loader loader} to execute when the first listener is registered and no value exists yet.
|
|
99
159
|
*
|
|
@@ -149,7 +209,7 @@ class EventSystem {
|
|
|
149
209
|
try {
|
|
150
210
|
const catchRes = await esData.loaderCatch(id, ex);
|
|
151
211
|
if (catchRes !== undefined) {
|
|
152
|
-
this.
|
|
212
|
+
this.send(id, catchRes);
|
|
153
213
|
res(catchRes);
|
|
154
214
|
}
|
|
155
215
|
}
|