comes 0.0.1 → 0.0.2
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 +69 -12
- package/build/index.js +79 -17
- package/package.json +1 -1
package/build/index.d.ts
CHANGED
|
@@ -30,11 +30,35 @@ export type ES_ValueType = {
|
|
|
30
30
|
* If some event/value was emitted before the first listener registered, so
|
|
31
31
|
* this function will not be called.
|
|
32
32
|
*
|
|
33
|
-
* @param id The address name
|
|
33
|
+
* @param id The address name of the event
|
|
34
34
|
* @param args Extra parameters to the function
|
|
35
|
-
* @returns
|
|
35
|
+
* @returns void
|
|
36
36
|
*/
|
|
37
37
|
loader?: (id: string, ...args: any[]) => void;
|
|
38
|
+
/**
|
|
39
|
+
* Error handler of the loader. If the execution of the loader throw
|
|
40
|
+
* an Error/Exception then this function will be called with the
|
|
41
|
+
* exception.
|
|
42
|
+
*
|
|
43
|
+
* This function can be async, and will be awaited.
|
|
44
|
+
*
|
|
45
|
+
* If this functions returns a value (not undefined), then the loader will
|
|
46
|
+
* resolve successfully with that value.
|
|
47
|
+
* If this function does not return a value, then loader will reject with the
|
|
48
|
+
* original exception.
|
|
49
|
+
* If this function throws an exception, then the loader will reject with that exception,
|
|
50
|
+
* and the original exception of the loader will be in the "loaderEx" field of that exception.
|
|
51
|
+
*
|
|
52
|
+
* @param id The address name of the event
|
|
53
|
+
* @param ex Exception thrown by the loader
|
|
54
|
+
* @returns void
|
|
55
|
+
*/
|
|
56
|
+
loaderCatch?: (id: string, ex: Error | any) => any;
|
|
57
|
+
/**
|
|
58
|
+
* Promise resolved when loader ends execution.
|
|
59
|
+
* If some error occurss then the "loaderCatch" function
|
|
60
|
+
* will be called.
|
|
61
|
+
*/
|
|
38
62
|
loaderProm?: Promise<any>;
|
|
39
63
|
};
|
|
40
64
|
/**
|
|
@@ -50,12 +74,12 @@ export declare class EventSystem {
|
|
|
50
74
|
};
|
|
51
75
|
/**
|
|
52
76
|
* Gets a reference to {@link ES_ValueType} of the address informed.
|
|
53
|
-
* @param id The address name
|
|
77
|
+
* @param id The address name of the event
|
|
54
78
|
*/
|
|
55
79
|
get(id: string): ES_ValueType;
|
|
56
80
|
/**
|
|
57
81
|
* Sends the value to the listeners of the event address.
|
|
58
|
-
* @param id The address name
|
|
82
|
+
* @param id The address name of the event
|
|
59
83
|
* @param event The value to send to listeners
|
|
60
84
|
* @returns The value informed
|
|
61
85
|
*/
|
|
@@ -67,25 +91,58 @@ export declare class EventSystem {
|
|
|
67
91
|
*
|
|
68
92
|
* The listener will be called with the last value available in the cache.
|
|
69
93
|
*
|
|
70
|
-
* @param id The address name
|
|
94
|
+
* @param id The address name of the event
|
|
71
95
|
* @param listener The listener, will be called every time a new value is emitted to this address
|
|
72
96
|
* @returns An unregister function. Use this function to remove the listener
|
|
73
97
|
*/
|
|
74
98
|
listen(id: string, listener: (event: any) => void): () => void;
|
|
99
|
+
/**
|
|
100
|
+
* Remove the listener from the list of this event address.
|
|
101
|
+
*
|
|
102
|
+
* If you need to remove a listener from the event address inside the exection of
|
|
103
|
+
* the listener, then use this function like so:
|
|
104
|
+
* ```ts
|
|
105
|
+
* const listener = () => {
|
|
106
|
+
* es.unlisten(ES_EVENT_NAME, listener);
|
|
107
|
+
* // then do what you need...
|
|
108
|
+
* // ...
|
|
109
|
+
* };
|
|
110
|
+
* es.listen(ES_EVENT_NAME, listener);
|
|
111
|
+
* ```
|
|
112
|
+
* This is because in some cases the return value of the {@link EventSystem.listen listen}
|
|
113
|
+
* will not be ready to unregister the listener (the variable will not be set yet).
|
|
114
|
+
*
|
|
115
|
+
* @param id The address name of the event
|
|
116
|
+
* @param listener Listener to remove
|
|
117
|
+
*/
|
|
75
118
|
unlisten(id: string, listener: (event: any) => void): void;
|
|
76
119
|
/**
|
|
77
|
-
* Configure a loader to execute when the first listener is registered and no value exists yet.
|
|
120
|
+
* Configure a {@link ES_ValueType.loader loader} to execute when the first listener is registered and no value exists yet.
|
|
121
|
+
*
|
|
122
|
+
* Remember to configure a Error Handler ({@link ES_ValueType.loaderCatch loaderCatch}) before configuring
|
|
123
|
+
* the {@link ES_ValueType.loader loader}, or use the 3o argumento to
|
|
124
|
+
* configure the {@link ES_ValueType.loaderCatch loaderCatch}.
|
|
78
125
|
*
|
|
79
|
-
*
|
|
126
|
+
* If any listener is registered, and no value was emitted yet, then the loader will
|
|
127
|
+
* be called by this method to fetch the first value of the event address.
|
|
128
|
+
*
|
|
129
|
+
* @param id The address name of the event
|
|
80
130
|
* @param loader The function to execute when the first liteners is registered
|
|
81
131
|
* and no value exists yet.
|
|
132
|
+
* @param loaderCatch The error handler for exceptions thrown by the {@link ES_ValueType.loader loader}
|
|
82
133
|
*/
|
|
83
|
-
setLoader(id: string, loader: ES_ValueType['loader']): void;
|
|
134
|
+
setLoader(id: string, loader: ES_ValueType['loader'], loaderCatch?: ES_ValueType['loaderCatch']): void;
|
|
135
|
+
setLoaderCatch(id: string, loaderCatch: ES_ValueType['loaderCatch']): void;
|
|
84
136
|
/**
|
|
85
|
-
* Executes the loader for an address.
|
|
86
|
-
*
|
|
87
|
-
* @
|
|
88
|
-
*
|
|
137
|
+
* Executes the {@link ES_ValueType.loader loader} for an address.
|
|
138
|
+
*
|
|
139
|
+
* If some error hapens in the {@link ES_ValueType.loader loader} function, then the {@link ES_ValueType.loaderCatch loaderCatch} will be
|
|
140
|
+
* called with the error (if configured).
|
|
141
|
+
* Check the {@link ES_ValueType.loaderCatch loaderCatch} docs to understand the behavior.
|
|
142
|
+
*
|
|
143
|
+
* @param id The address name of the event
|
|
144
|
+
* @param args Arguments to pass to the {@link ES_ValueType.loader loader}
|
|
145
|
+
* @returns Promise that will resolve when the {@link ES_ValueType.loader loader} resolves
|
|
89
146
|
*/
|
|
90
147
|
load<T>(id: string, ...args: any[]): Promise<() => T>;
|
|
91
148
|
}
|
package/build/index.js
CHANGED
|
@@ -30,7 +30,7 @@ class EventSystem {
|
|
|
30
30
|
}
|
|
31
31
|
/**
|
|
32
32
|
* Gets a reference to {@link ES_ValueType} of the address informed.
|
|
33
|
-
* @param id The address name
|
|
33
|
+
* @param id The address name of the event
|
|
34
34
|
*/
|
|
35
35
|
get(id) {
|
|
36
36
|
if (!this.data[id])
|
|
@@ -39,7 +39,7 @@ class EventSystem {
|
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
41
41
|
* Sends the value to the listeners of the event address.
|
|
42
|
-
* @param id The address name
|
|
42
|
+
* @param id The address name of the event
|
|
43
43
|
* @param event The value to send to listeners
|
|
44
44
|
* @returns The value informed
|
|
45
45
|
*/
|
|
@@ -58,7 +58,7 @@ class EventSystem {
|
|
|
58
58
|
*
|
|
59
59
|
* The listener will be called with the last value available in the cache.
|
|
60
60
|
*
|
|
61
|
-
* @param id The address name
|
|
61
|
+
* @param id The address name of the event
|
|
62
62
|
* @param listener The listener, will be called every time a new value is emitted to this address
|
|
63
63
|
* @returns An unregister function. Use this function to remove the listener
|
|
64
64
|
*/
|
|
@@ -68,40 +68,102 @@ class EventSystem {
|
|
|
68
68
|
if (esData.date)
|
|
69
69
|
listener(esData.last);
|
|
70
70
|
else if (esData.loader)
|
|
71
|
-
|
|
71
|
+
esData.loaderProm = this.load(id);
|
|
72
72
|
return () => this.unlisten(id, listener);
|
|
73
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Remove the listener from the list of this event address.
|
|
76
|
+
*
|
|
77
|
+
* If you need to remove a listener from the event address inside the exection of
|
|
78
|
+
* the listener, then use this function like so:
|
|
79
|
+
* ```ts
|
|
80
|
+
* const listener = () => {
|
|
81
|
+
* es.unlisten(ES_EVENT_NAME, listener);
|
|
82
|
+
* // then do what you need...
|
|
83
|
+
* // ...
|
|
84
|
+
* };
|
|
85
|
+
* es.listen(ES_EVENT_NAME, listener);
|
|
86
|
+
* ```
|
|
87
|
+
* This is because in some cases the return value of the {@link EventSystem.listen listen}
|
|
88
|
+
* will not be ready to unregister the listener (the variable will not be set yet).
|
|
89
|
+
*
|
|
90
|
+
* @param id The address name of the event
|
|
91
|
+
* @param listener Listener to remove
|
|
92
|
+
*/
|
|
74
93
|
unlisten(id, listener) {
|
|
75
94
|
let esData = this.get(id);
|
|
76
95
|
deleteFromArray(listener, esData.listeners);
|
|
77
96
|
}
|
|
78
97
|
/**
|
|
79
|
-
* Configure a loader to execute when the first listener is registered and no value exists yet.
|
|
98
|
+
* Configure a {@link ES_ValueType.loader loader} to execute when the first listener is registered and no value exists yet.
|
|
80
99
|
*
|
|
81
|
-
* @
|
|
100
|
+
* Remember to configure a Error Handler ({@link ES_ValueType.loaderCatch loaderCatch}) before configuring
|
|
101
|
+
* the {@link ES_ValueType.loader loader}, or use the 3o argumento to
|
|
102
|
+
* configure the {@link ES_ValueType.loaderCatch loaderCatch}.
|
|
103
|
+
*
|
|
104
|
+
* If any listener is registered, and no value was emitted yet, then the loader will
|
|
105
|
+
* be called by this method to fetch the first value of the event address.
|
|
106
|
+
*
|
|
107
|
+
* @param id The address name of the event
|
|
82
108
|
* @param loader The function to execute when the first liteners is registered
|
|
83
109
|
* and no value exists yet.
|
|
110
|
+
* @param loaderCatch The error handler for exceptions thrown by the {@link ES_ValueType.loader loader}
|
|
84
111
|
*/
|
|
85
|
-
setLoader(id, loader) {
|
|
112
|
+
setLoader(id, loader, loaderCatch) {
|
|
86
113
|
let esData = this.get(id);
|
|
87
114
|
esData.loader = loader;
|
|
115
|
+
if (loaderCatch)
|
|
116
|
+
esData.loaderCatch = loaderCatch;
|
|
88
117
|
if (!esData.date && esData.listeners.length)
|
|
89
|
-
this.load(id);
|
|
118
|
+
esData.loaderProm = this.load(id);
|
|
119
|
+
}
|
|
120
|
+
setLoaderCatch(id, loaderCatch) {
|
|
121
|
+
let esData = this.get(id);
|
|
122
|
+
esData.loaderCatch = loaderCatch;
|
|
90
123
|
}
|
|
91
124
|
/**
|
|
92
|
-
* Executes the loader for an address.
|
|
93
|
-
*
|
|
94
|
-
* @
|
|
95
|
-
*
|
|
125
|
+
* Executes the {@link ES_ValueType.loader loader} for an address.
|
|
126
|
+
*
|
|
127
|
+
* If some error hapens in the {@link ES_ValueType.loader loader} function, then the {@link ES_ValueType.loaderCatch loaderCatch} will be
|
|
128
|
+
* called with the error (if configured).
|
|
129
|
+
* Check the {@link ES_ValueType.loaderCatch loaderCatch} docs to understand the behavior.
|
|
130
|
+
*
|
|
131
|
+
* @param id The address name of the event
|
|
132
|
+
* @param args Arguments to pass to the {@link ES_ValueType.loader loader}
|
|
133
|
+
* @returns Promise that will resolve when the {@link ES_ValueType.loader loader} resolves
|
|
96
134
|
*/
|
|
97
135
|
async load(id, ...args) {
|
|
98
136
|
let esData = this.get(id);
|
|
99
|
-
if (!esData.loader)
|
|
100
|
-
return esData.last;
|
|
101
|
-
esData.loaderProm = new Promise(async (res) => {
|
|
102
|
-
await esData.loader(id, ...args);
|
|
137
|
+
if (!esData.loader) {
|
|
103
138
|
esData.loaderProm = undefined;
|
|
104
|
-
|
|
139
|
+
return esData.last;
|
|
140
|
+
}
|
|
141
|
+
esData.loaderProm = new Promise(async (res, rej) => {
|
|
142
|
+
try {
|
|
143
|
+
await esData.loader(id, ...args);
|
|
144
|
+
esData.loaderProm = undefined;
|
|
145
|
+
res(esData.last);
|
|
146
|
+
}
|
|
147
|
+
catch (ex) {
|
|
148
|
+
if (esData.loaderCatch) {
|
|
149
|
+
try {
|
|
150
|
+
const catchRes = await esData.loaderCatch(id, ex);
|
|
151
|
+
if (catchRes !== undefined) {
|
|
152
|
+
this.emit(id, catchRes);
|
|
153
|
+
res(catchRes);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
catch (ex2) {
|
|
157
|
+
try {
|
|
158
|
+
ex2.loaderEx = ex;
|
|
159
|
+
}
|
|
160
|
+
catch (ex3) { /* do nothing */ }
|
|
161
|
+
rej(ex2);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
else
|
|
165
|
+
rej(ex);
|
|
166
|
+
}
|
|
105
167
|
});
|
|
106
168
|
return esData.loaderProm;
|
|
107
169
|
}
|