@yhwh-script/event-bux 1.0.2 → 1.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/README.md +9 -16
- package/index.js +16 -22
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
## Getting started
|
|
2
2
|
|
|
3
|
-
To install
|
|
3
|
+
To install the eventbus into your project, simply run
|
|
4
4
|
|
|
5
5
|
```bash
|
|
6
6
|
npm install @yhwh-script/event-bux
|
|
@@ -17,21 +17,14 @@ classDiagram
|
|
|
17
17
|
EventBus: -WeakMap listeners
|
|
18
18
|
```
|
|
19
19
|
|
|
20
|
-
##
|
|
20
|
+
## How-To
|
|
21
21
|
|
|
22
|
-
After importing this module in your code, it is recommended to add it to the window object
|
|
22
|
+
After importing this module in your code, it is recommended to add it to the window object:
|
|
23
23
|
|
|
24
24
|
```javascript
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
window[module.name] = module;
|
|
33
|
-
});
|
|
34
|
-
}).finally(() => {
|
|
35
|
-
import('./components')
|
|
36
|
-
});
|
|
37
|
-
```
|
|
25
|
+
<script type="module">
|
|
26
|
+
import * as bus from '@yhwh-script/event-bux';
|
|
27
|
+
window.bus = bus;
|
|
28
|
+
</script>
|
|
29
|
+
```
|
|
30
|
+
Then use `bus.addEventListener(type, listener)` and `bus.dispatchEvent(type, event)` to send event between components.
|
package/index.js
CHANGED
|
@@ -1,52 +1,46 @@
|
|
|
1
|
-
export const name = 'bus'; // implements EventTarget
|
|
2
|
-
|
|
3
1
|
const listeners = new WeakMap(); // WeakMap to store listeners per object. Keep it private unless you know what you do.
|
|
4
2
|
// Should auto-cleanup listeners when object is GC'd
|
|
5
|
-
|
|
3
|
+
// implements EventTarget
|
|
4
|
+
export const addEventListener = function (type, listener, context = undefined) {
|
|
6
5
|
if (context && typeof context === 'object') { // context is well defined, should be a WebComponent
|
|
7
6
|
if (!listeners.has(context)) { // context is yet unknown to the listeners
|
|
8
7
|
listeners.set(context, new Map()); // will be stored here
|
|
9
8
|
}
|
|
10
9
|
const contextListeners = listeners.get(context);
|
|
11
|
-
if (!contextListeners.has(
|
|
12
|
-
contextListeners.set(
|
|
10
|
+
if (!contextListeners.has(type)) { // eventName is not yet registered
|
|
11
|
+
contextListeners.set(type, []);
|
|
13
12
|
} // next push the handler to the WeakMap for the given context
|
|
14
|
-
contextListeners.get(eventName).push(
|
|
13
|
+
contextListeners.get(eventName).push(listener);
|
|
15
14
|
} else { // default
|
|
16
15
|
if (context) {
|
|
17
16
|
throw new Error("Syntax error.") // illegal context
|
|
18
17
|
}
|
|
19
|
-
window.addEventListener(
|
|
18
|
+
window.addEventListener(type, listener);
|
|
20
19
|
}
|
|
21
20
|
}
|
|
22
21
|
|
|
23
|
-
export const removeEventListener = function (
|
|
22
|
+
export const removeEventListener = function (type, listener, context = undefined) {
|
|
24
23
|
if (context && typeof context === 'object') {
|
|
25
24
|
const contextListeners = listeners.get(context);
|
|
26
|
-
if (contextListeners && contextListeners.has(
|
|
27
|
-
const handlers = contextListeners.get(
|
|
28
|
-
const index = handlers.indexOf(
|
|
25
|
+
if (contextListeners && contextListeners.has(type)) {
|
|
26
|
+
const handlers = contextListeners.get(type);
|
|
27
|
+
const index = handlers.indexOf(listener);
|
|
29
28
|
if (index > -1) {
|
|
30
29
|
handlers.splice(index, 1);
|
|
31
30
|
}
|
|
32
31
|
}
|
|
33
32
|
} else { // default
|
|
34
|
-
window.removeEventListener(
|
|
33
|
+
window.removeEventListener(type);
|
|
35
34
|
}
|
|
36
35
|
}
|
|
37
36
|
|
|
38
|
-
export const dispatchEvent = function (
|
|
39
|
-
let eventName = customEvent.type;
|
|
40
|
-
let detail = customEvent.detail;
|
|
41
|
-
const event = { type: eventName, detail, target: context };
|
|
42
|
-
|
|
43
|
-
// Call context-specific listeners
|
|
37
|
+
export const dispatchEvent = function (event, context = undefined) {
|
|
44
38
|
if (context && listeners.has(context)) {
|
|
45
39
|
const contextListeners = listeners.get(context);
|
|
46
|
-
if (contextListeners.has(
|
|
47
|
-
contextListeners.get(
|
|
40
|
+
if (contextListeners.has(event.type)) { // Call context-specific listeners
|
|
41
|
+
contextListeners.get(event.type).forEach(handler => handler(event));
|
|
48
42
|
}
|
|
49
|
-
} else {
|
|
50
|
-
window.dispatchEvent(
|
|
43
|
+
} else { // default
|
|
44
|
+
window.dispatchEvent(event);
|
|
51
45
|
}
|
|
52
46
|
}
|
package/package.json
CHANGED