@vanillaspa/event-bus 1.2.0 → 1.3.0

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/index.js CHANGED
@@ -1,63 +1,58 @@
1
1
  export const name = "eventbus";
2
- const worker = new ServiceWorker(new URL('./eventbusWorker.js', import.meta.url), { type: 'module' });
3
- worker.port.start();
2
+ const channel = new BroadcastChannel();
3
+ const contextListeners = new WeakMap(); // context - Map<type, listener[]>
4
4
 
5
- const contextListeners = new WeakMap(); // WeakMap to store listeners per object. Keep it private unless you know what you do.
6
-
7
- worker.port.onmessage = ({ data }) => {
5
+ channel.onmessage = ({ data }) => {
8
6
  const { type, detail } = data;
9
- dispatchEvent(new CustomEvent(type, { detail }));
7
+ dispatchLocally(new CustomEvent(type, { detail }));
10
8
  }
11
9
 
12
- worker.port.onmessageerror = (e) => {
10
+ channel.onmessageerror = (e) => {
13
11
  console.error('eventbus deserialization error:', e);
14
12
  }
15
13
 
14
+ function dispatchLocally(event, context = undefined) {
15
+ if (!context) context = event instanceof CustomEvent ? event.detail?.target : event.target;
16
+
17
+ if (context && contextListeners.has(context)) {
18
+ const byType = contextListeners.get(context);
19
+ byType.get(event.type)?.forEach(handler => handler(event));
20
+ } else {
21
+ if (typeof window !== 'undefined') window.dispatchEvent(event);
22
+ }
23
+ }
24
+
16
25
  export function addEventListener(type, listener, context = undefined) {
17
26
  if (context && typeof context === 'object') { // context is well defined, should be a WebComponent
18
- if (!contextListeners.has(context)) { // context is yet unknown to the listeners
19
- contextListeners.set(context, new Map()); // will be stored here
27
+ if (!contextListeners.has(context)) {
28
+ contextListeners.set(context, new Map());
20
29
  }
21
30
  const byType = contextListeners.get(context);
22
31
  if (!byType.has(type)) byType.set(type, []);
23
32
  byType.get(type).push(listener);
24
33
  } else {
25
34
  if (context) throw new Error("Syntax error: context must be an object.");
26
- window.addEventListener(type, listener);
35
+ if (typeof window !== 'undefined') window.addEventListener(type, listener);
27
36
  }
28
-
29
- worker.port.postMessage({ action: 'addEventListener', type });
30
37
  }
31
38
 
32
- export function removeEventListener(type, listener, context = undefined, options) {
39
+ export function removeEventListener(type, listener, context = undefined) {
33
40
  if (context && typeof context === 'object') {
34
41
  const byType = contextListeners.get(context);
35
42
  if (!byType?.has(type)) return;
36
-
37
43
  const handlers = byType.get(type)
38
44
  const index = handlers.indexOf(listener);
39
45
  if (index > -1) handlers.splice(index, 1);
40
- if (handlers.length === 0) {
41
- byType.delete(type);
42
- worker.port.postMessage({ action: 'removeEventListener', type });
43
- }
46
+ if (handlers.length === 0) byType.delete(type);
44
47
  } else {
45
- window.removeEventListener(type, listener);
46
- worker.port.postMessage({ action: 'removeEventListener', type });
48
+ if (typeof window !== 'undefined') window.removeEventListener(type, listener);
47
49
  }
48
50
  }
49
51
 
50
52
  export function dispatchEvent(event) {
51
- const context = event instanceof CustomEvent ? event.detail?.target : event.target;
52
- if (context && contextListeners.has(context)) {
53
- const byType = contextListeners.get(context);
54
- byType.get(event.type)?.forEach(handler => handler(event));
55
- } else {
56
- if (typeof window !== 'undefined') window.dispatchEvent(event);
57
- worker.port.postMessage({
58
- action: 'dispatchEvent',
59
- type: event.type,
60
- detail: event instanceof CustomEvent ? event.detail : {}
61
- });
62
- }
53
+ dispatchLocally(event, context);
54
+ channel.postMessage({
55
+ type: event.type,
56
+ detail: event instanceof CustomEvent ? event.detail : {}
57
+ });
63
58
  }
package/package.json CHANGED
@@ -6,12 +6,14 @@
6
6
  "description": "An event-bus for vanilla SPA. You can use it with web-components.",
7
7
  "homepage": "https://github.com/vanillaspa/event-bus#readme",
8
8
  "keywords": [
9
+ "BroadcastChannel",
10
+ "CustomElement",
9
11
  "EventBus",
10
12
  "JavaScript",
11
13
  "Module",
12
14
  "Vanilla",
13
- "ServiceWorker",
14
15
  "SPA",
16
+ "WeakMap",
15
17
  "WebComponents"
16
18
  ],
17
19
  "license": "Unlicense",
@@ -25,5 +27,5 @@
25
27
  "url": "git+https://github.com/vanillaspa/event-bus.git"
26
28
  },
27
29
  "type": "module",
28
- "version": "1.2.0"
30
+ "version": "1.3.0"
29
31
  }
package/eventbusWorker.js DELETED
@@ -1,31 +0,0 @@
1
- const listeners = new Map();
2
-
3
- onconnect = function ({ ports }) {
4
- const port = ports[0];
5
-
6
- port.onmessage = ({ data }) => {
7
- const { action, type, detail } = data;
8
- switch (action) {
9
- case 'addEventListener':
10
- if (!listeners.has(type)) listeners.set(type, new Set());
11
- listeners.get(type).add(port);
12
- break;
13
- case 'removeEventListener':
14
- listeners.get(type)?.delete(port);
15
- break;
16
- case 'dispatchEvent':
17
- listeners.get(type)?.forEach(p => {
18
- if (p !== port) p.postMessage({ type, detail });
19
- });
20
- break;
21
- }
22
- };
23
-
24
- port.onmessageerror = () => {
25
- for (const ports of listeners.values()) {
26
- ports.delete(port);
27
- }
28
- };
29
-
30
- port.start();
31
- }