@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.
Files changed (3) hide show
  1. package/README.md +9 -16
  2. package/index.js +16 -22
  3. package/package.json +1 -1
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,14 @@ 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
+ After importing this module in your code, it is recommended to add it to the window object:
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
+ 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
- export const addEventListener = function (eventName, handler, context = undefined) {
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(eventName)) { // eventName is not yet registered
12
- contextListeners.set(eventName, []);
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(handler);
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(eventName, handler);
18
+ window.addEventListener(type, listener);
20
19
  }
21
20
  }
22
21
 
23
- export const removeEventListener = function (eventName, handler, context = undefined) {
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(eventName)) {
27
- const handlers = contextListeners.get(eventName);
28
- const index = handlers.indexOf(handler);
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(eventName);
33
+ window.removeEventListener(type);
35
34
  }
36
35
  }
37
36
 
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
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(eventName)) {
47
- contextListeners.get(eventName).forEach(handler => handler(event));
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(customEvent);
43
+ } else { // default
44
+ window.dispatchEvent(event);
51
45
  }
52
46
  }
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.3"
26
26
  }