@yhwh-script/event-bux 1.0.2 → 1.0.4
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 +20 -16
- package/eventbus.png +0 -0
- package/index.js +20 -25
- 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,25 @@ classDiagram
|
|
|
17
17
|
EventBus: -WeakMap listeners
|
|
18
18
|
```
|
|
19
19
|
|
|
20
|
-
##
|
|
20
|
+
## How-To
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
For instance:
|
|
23
23
|
|
|
24
24
|
```javascript
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
25
|
+
<script type="module">
|
|
26
|
+
import * as bus from '@yhwh-script/event-bux';
|
|
27
|
+
window.bus = bus;
|
|
28
|
+
</script>
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
After importing `@yhwh-script/event-bux`, it is recommended to add it to the `window` object.
|
|
32
|
+
|
|
33
|
+
You can use your own import or stick to other `@yhwh-script` examples
|
|
34
|
+
|
|
35
|
+
Then use `bus.addEventListener(type, listener)` and `bus.dispatchEvent(type, event)` in your WebComponents. You are not bound to event bubbling or capturing (which are the standard event propagation mechanisms), but you can send events even between WebComponents! Check it out!
|
|
36
|
+
|
|
37
|
+

|
|
38
|
+
|
|
39
|
+
## Contribute
|
|
40
|
+
|
|
41
|
+
Welcome!
|
package/eventbus.png
ADDED
|
Binary file
|
package/index.js
CHANGED
|
@@ -1,52 +1,47 @@
|
|
|
1
|
-
export const name =
|
|
2
|
-
|
|
1
|
+
export const name = "eventbux"; // The module name, can be made globally available via the window object, which should be on top.
|
|
3
2
|
const listeners = new WeakMap(); // WeakMap to store listeners per object. Keep it private unless you know what you do.
|
|
4
|
-
// Should auto-cleanup listeners when object is GC'd
|
|
5
|
-
|
|
3
|
+
// Should auto-cleanup listeners when object is GC'd (TODO Test!)
|
|
4
|
+
// This module implements EventTarget.
|
|
5
|
+
export const addEventListener = function (type, listener, context = undefined) { // (TODO check integration into options)
|
|
6
6
|
if (context && typeof context === 'object') { // context is well defined, should be a WebComponent
|
|
7
7
|
if (!listeners.has(context)) { // context is yet unknown to the listeners
|
|
8
8
|
listeners.set(context, new Map()); // will be stored here
|
|
9
9
|
}
|
|
10
10
|
const contextListeners = listeners.get(context);
|
|
11
|
-
if (!contextListeners.has(
|
|
12
|
-
contextListeners.set(
|
|
11
|
+
if (!contextListeners.has(type)) { // eventName is not yet registered
|
|
12
|
+
contextListeners.set(type, []);
|
|
13
13
|
} // next push the handler to the WeakMap for the given context
|
|
14
|
-
contextListeners.get(eventName).push(
|
|
14
|
+
contextListeners.get(eventName).push(listener);
|
|
15
15
|
} else { // default
|
|
16
|
-
if (context) {
|
|
17
|
-
throw new Error("Syntax error.")
|
|
16
|
+
if (context) { // illegal context
|
|
17
|
+
throw new Error("Syntax error.")
|
|
18
18
|
}
|
|
19
|
-
window.addEventListener(
|
|
19
|
+
window.addEventListener(type, listener);
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
export const removeEventListener = function (
|
|
23
|
+
export const removeEventListener = function (type, listener, context = undefined) {
|
|
24
24
|
if (context && typeof context === 'object') {
|
|
25
25
|
const contextListeners = listeners.get(context);
|
|
26
|
-
if (contextListeners && contextListeners.has(
|
|
27
|
-
const handlers = contextListeners.get(
|
|
28
|
-
const index = handlers.indexOf(
|
|
26
|
+
if (contextListeners && contextListeners.has(type)) {
|
|
27
|
+
const handlers = contextListeners.get(type);
|
|
28
|
+
const index = handlers.indexOf(listener);
|
|
29
29
|
if (index > -1) {
|
|
30
30
|
handlers.splice(index, 1);
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
} else { // default
|
|
34
|
-
window.removeEventListener(
|
|
34
|
+
window.removeEventListener(type);
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
|
|
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
|
|
38
|
+
export const dispatchEvent = function (event, context = undefined) { // TODO check if context == event.target, if yes, remove context? What if CustomEvent is used?
|
|
44
39
|
if (context && listeners.has(context)) {
|
|
45
40
|
const contextListeners = listeners.get(context);
|
|
46
|
-
if (contextListeners.has(
|
|
47
|
-
contextListeners.get(
|
|
41
|
+
if (contextListeners.has(event.type)) { // Call context-specific listeners
|
|
42
|
+
contextListeners.get(event.type).forEach(handler => handler(event));
|
|
48
43
|
}
|
|
49
|
-
} else {
|
|
50
|
-
window.dispatchEvent(
|
|
44
|
+
} else { // default
|
|
45
|
+
window.dispatchEvent(event);
|
|
51
46
|
}
|
|
52
47
|
}
|
package/package.json
CHANGED