@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  ## Getting started
2
2
 
3
- To install EventBux module into your project, simply run
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
- ## Recommendation
20
+ ## How-To
21
21
 
22
- After importing this module in your code, it is recommended to add it to the window object, like in `@yhwh-script/components`
22
+ For instance:
23
23
 
24
24
  ```javascript
25
- Promise.all([
26
- import('@yhwh-script/event-bux')
27
- ]).then((importedModules) => {
28
- importedModules.forEach((module) => {
29
- if (!module.name) {
30
- throw new Error('Missing name in imported module.');
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
+
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
+ ![EventBus schema](eventbus.png)
38
+
39
+ ## Contribute
40
+
41
+ Welcome!
package/eventbus.png ADDED
Binary file
package/index.js CHANGED
@@ -1,52 +1,47 @@
1
- export const name = 'bus'; // implements EventTarget
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
- export const addEventListener = function (eventName, handler, context = undefined) {
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(eventName)) { // eventName is not yet registered
12
- contextListeners.set(eventName, []);
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(handler);
14
+ contextListeners.get(eventName).push(listener);
15
15
  } else { // default
16
- if (context) {
17
- throw new Error("Syntax error.") // illegal context
16
+ if (context) { // illegal context
17
+ throw new Error("Syntax error.")
18
18
  }
19
- window.addEventListener(eventName, handler);
19
+ window.addEventListener(type, listener);
20
20
  }
21
21
  }
22
22
 
23
- export const removeEventListener = function (eventName, handler, context = undefined) {
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(eventName)) {
27
- const handlers = contextListeners.get(eventName);
28
- const index = handlers.indexOf(handler);
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(eventName);
34
+ window.removeEventListener(type);
35
35
  }
36
36
  }
37
37
 
38
- export const dispatchEvent = function (customEvent, context = undefined) {
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(eventName)) {
47
- contextListeners.get(eventName).forEach(handler => handler(event));
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(customEvent);
44
+ } else { // default
45
+ window.dispatchEvent(event);
51
46
  }
52
47
  }
package/package.json CHANGED
@@ -22,5 +22,5 @@
22
22
  "url": "git+https://github.com/yhwh-script/event-bux.git"
23
23
  },
24
24
  "type": "module",
25
- "version": "1.0.2"
25
+ "version": "1.0.4"
26
26
  }