comes 0.0.1
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/LICENSE +23 -0
- package/README.md +3 -0
- package/build/index.d.ts +95 -0
- package/build/index.js +113 -0
- package/package.json +30 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
Copyright (c) 2023, Rafael Cheruti
|
|
2
|
+
All rights reserved.
|
|
3
|
+
|
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
|
6
|
+
|
|
7
|
+
* Redistributions of source code must retain the above copyright notice, this
|
|
8
|
+
list of conditions and the following disclaimer.
|
|
9
|
+
|
|
10
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
|
11
|
+
this list of conditions and the following disclaimer in the documentation
|
|
12
|
+
and/or other materials provided with the distribution.
|
|
13
|
+
|
|
14
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
15
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
16
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
17
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
18
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
19
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
20
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
21
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
22
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
23
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Function to exclude an item from the array.
|
|
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;
|
|
7
|
+
/**
|
|
8
|
+
* Type that holds the value for an event address.
|
|
9
|
+
*/
|
|
10
|
+
export type ES_ValueType = {
|
|
11
|
+
/**
|
|
12
|
+
* The last value emitted.
|
|
13
|
+
*/
|
|
14
|
+
last: any;
|
|
15
|
+
/**
|
|
16
|
+
* Listeners of this event.
|
|
17
|
+
*/
|
|
18
|
+
listeners: ((event: any) => void)[];
|
|
19
|
+
/**
|
|
20
|
+
* Last time an event was emitted.
|
|
21
|
+
*
|
|
22
|
+
* If this value is "null" or "undefined" so no event/value was emitted yet.
|
|
23
|
+
* In this case, the "last" field will be "null" or "undefined" also.
|
|
24
|
+
*/
|
|
25
|
+
date?: Date;
|
|
26
|
+
/**
|
|
27
|
+
* Loader to run when the first listeners is registered and no event was
|
|
28
|
+
* emitted yet.
|
|
29
|
+
*
|
|
30
|
+
* If some event/value was emitted before the first listener registered, so
|
|
31
|
+
* this function will not be called.
|
|
32
|
+
*
|
|
33
|
+
* @param id The address name
|
|
34
|
+
* @param args Extra parameters to the function
|
|
35
|
+
* @returns
|
|
36
|
+
*/
|
|
37
|
+
loader?: (id: string, ...args: any[]) => void;
|
|
38
|
+
loaderProm?: Promise<any>;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Class that creates the field through which communication will occur.
|
|
42
|
+
*/
|
|
43
|
+
export declare class EventSystem {
|
|
44
|
+
/**
|
|
45
|
+
* Each key is an event address and the value is an {@link ES_ValueType} that
|
|
46
|
+
* holds the last value for the address.
|
|
47
|
+
*/
|
|
48
|
+
data: {
|
|
49
|
+
[id: string]: ES_ValueType;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Gets a reference to {@link ES_ValueType} of the address informed.
|
|
53
|
+
* @param id The address name
|
|
54
|
+
*/
|
|
55
|
+
get(id: string): ES_ValueType;
|
|
56
|
+
/**
|
|
57
|
+
* Sends the value to the listeners of the event address.
|
|
58
|
+
* @param id The address name
|
|
59
|
+
* @param event The value to send to listeners
|
|
60
|
+
* @returns The value informed
|
|
61
|
+
*/
|
|
62
|
+
emit<T>(id: string, event: T): T;
|
|
63
|
+
/**
|
|
64
|
+
* Register a listener for the address.
|
|
65
|
+
* The listener will receive the last value emitted.
|
|
66
|
+
* The listener can call {@link get} to get the {@link ES_ValueType} of the type in the start of the function.
|
|
67
|
+
*
|
|
68
|
+
* The listener will be called with the last value available in the cache.
|
|
69
|
+
*
|
|
70
|
+
* @param id The address name
|
|
71
|
+
* @param listener The listener, will be called every time a new value is emitted to this address
|
|
72
|
+
* @returns An unregister function. Use this function to remove the listener
|
|
73
|
+
*/
|
|
74
|
+
listen(id: string, listener: (event: any) => void): () => void;
|
|
75
|
+
unlisten(id: string, listener: (event: any) => void): void;
|
|
76
|
+
/**
|
|
77
|
+
* Configure a loader to execute when the first listener is registered and no value exists yet.
|
|
78
|
+
*
|
|
79
|
+
* @param id The address name
|
|
80
|
+
* @param loader The function to execute when the first liteners is registered
|
|
81
|
+
* and no value exists yet.
|
|
82
|
+
*/
|
|
83
|
+
setLoader(id: string, loader: ES_ValueType['loader']): void;
|
|
84
|
+
/**
|
|
85
|
+
* Executes the loader for an address.
|
|
86
|
+
* @param id The address name
|
|
87
|
+
* @param args Arguments to pass to the loader
|
|
88
|
+
* @returns Promise that will resolve when the loader resolves
|
|
89
|
+
*/
|
|
90
|
+
load<T>(id: string, ...args: any[]): Promise<() => T>;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* This "es" is the default EventSystem created.
|
|
94
|
+
*/
|
|
95
|
+
export declare const es: EventSystem;
|
package/build/index.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.es = exports.EventSystem = void 0;
|
|
4
|
+
exports.deleteFromArray = deleteFromArray;
|
|
5
|
+
// -------
|
|
6
|
+
/**
|
|
7
|
+
* Function to exclude an item from the array.
|
|
8
|
+
* @param item Item that will be removed
|
|
9
|
+
* @param list Array where to find the item
|
|
10
|
+
*/
|
|
11
|
+
function deleteFromArray(item, list) {
|
|
12
|
+
for (let i = list.length; i > -1; i--) {
|
|
13
|
+
let it = list[i];
|
|
14
|
+
if (it === item) {
|
|
15
|
+
list.splice(i, 1);
|
|
16
|
+
break;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Class that creates the field through which communication will occur.
|
|
22
|
+
*/
|
|
23
|
+
class EventSystem {
|
|
24
|
+
constructor() {
|
|
25
|
+
/**
|
|
26
|
+
* Each key is an event address and the value is an {@link ES_ValueType} that
|
|
27
|
+
* holds the last value for the address.
|
|
28
|
+
*/
|
|
29
|
+
this.data = {};
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Gets a reference to {@link ES_ValueType} of the address informed.
|
|
33
|
+
* @param id The address name
|
|
34
|
+
*/
|
|
35
|
+
get(id) {
|
|
36
|
+
if (!this.data[id])
|
|
37
|
+
this.data[id] = { last: null, listeners: [] };
|
|
38
|
+
return this.data[id];
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Sends the value to the listeners of the event address.
|
|
42
|
+
* @param id The address name
|
|
43
|
+
* @param event The value to send to listeners
|
|
44
|
+
* @returns The value informed
|
|
45
|
+
*/
|
|
46
|
+
emit(id, event) {
|
|
47
|
+
let esData = this.get(id);
|
|
48
|
+
esData.last = event;
|
|
49
|
+
esData.date = new Date();
|
|
50
|
+
for (let func of esData.listeners)
|
|
51
|
+
func(event);
|
|
52
|
+
return event;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Register a listener for the address.
|
|
56
|
+
* The listener will receive the last value emitted.
|
|
57
|
+
* The listener can call {@link get} to get the {@link ES_ValueType} of the type in the start of the function.
|
|
58
|
+
*
|
|
59
|
+
* The listener will be called with the last value available in the cache.
|
|
60
|
+
*
|
|
61
|
+
* @param id The address name
|
|
62
|
+
* @param listener The listener, will be called every time a new value is emitted to this address
|
|
63
|
+
* @returns An unregister function. Use this function to remove the listener
|
|
64
|
+
*/
|
|
65
|
+
listen(id, listener) {
|
|
66
|
+
let esData = this.get(id);
|
|
67
|
+
esData.listeners.push(listener);
|
|
68
|
+
if (esData.date)
|
|
69
|
+
listener(esData.last);
|
|
70
|
+
else if (esData.loader)
|
|
71
|
+
exports.es.load(id);
|
|
72
|
+
return () => this.unlisten(id, listener);
|
|
73
|
+
}
|
|
74
|
+
unlisten(id, listener) {
|
|
75
|
+
let esData = this.get(id);
|
|
76
|
+
deleteFromArray(listener, esData.listeners);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Configure a loader to execute when the first listener is registered and no value exists yet.
|
|
80
|
+
*
|
|
81
|
+
* @param id The address name
|
|
82
|
+
* @param loader The function to execute when the first liteners is registered
|
|
83
|
+
* and no value exists yet.
|
|
84
|
+
*/
|
|
85
|
+
setLoader(id, loader) {
|
|
86
|
+
let esData = this.get(id);
|
|
87
|
+
esData.loader = loader;
|
|
88
|
+
if (!esData.date && esData.listeners.length)
|
|
89
|
+
this.load(id);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Executes the loader for an address.
|
|
93
|
+
* @param id The address name
|
|
94
|
+
* @param args Arguments to pass to the loader
|
|
95
|
+
* @returns Promise that will resolve when the loader resolves
|
|
96
|
+
*/
|
|
97
|
+
async load(id, ...args) {
|
|
98
|
+
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);
|
|
103
|
+
esData.loaderProm = undefined;
|
|
104
|
+
res(esData.last);
|
|
105
|
+
});
|
|
106
|
+
return esData.loaderProm;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
exports.EventSystem = EventSystem;
|
|
110
|
+
/**
|
|
111
|
+
* This "es" is the default EventSystem created.
|
|
112
|
+
*/
|
|
113
|
+
exports.es = new EventSystem();
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "comes",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Simple Event System Communication",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"event",
|
|
7
|
+
"event-system",
|
|
8
|
+
"communication"
|
|
9
|
+
],
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git://github.com/rcheruti/comes.git"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/rcheruti/comes#readme",
|
|
15
|
+
"main": "build/index.js",
|
|
16
|
+
"types": "build/index.d.ts",
|
|
17
|
+
"scripts": {
|
|
18
|
+
"dev": "ts-node src/index.ts",
|
|
19
|
+
"build": "rimraf build && tsc",
|
|
20
|
+
"test": "vitest"
|
|
21
|
+
},
|
|
22
|
+
"author": "RCC",
|
|
23
|
+
"license": "ISC",
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@vitest/coverage-v8": "2.1.3",
|
|
26
|
+
"rimraf": "^6.0.1",
|
|
27
|
+
"typescript": "5.6.3",
|
|
28
|
+
"vitest": "2.1.3"
|
|
29
|
+
}
|
|
30
|
+
}
|