@bbki.ng/site 1.8.10 → 2.0.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/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # @bbki.ng/site
2
2
 
3
+ ## 2.0.0
4
+
5
+ ## 1.8.11
6
+
3
7
  ## 1.8.10
4
8
 
5
9
  ## 1.8.9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbki.ng/site",
3
- "version": "1.8.10",
3
+ "version": "2.0.0",
4
4
  "description": "code behind bbki.ng",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -3,3 +3,46 @@ export enum PluginEvent {
3
3
  INSTALL = "installed",
4
4
  UNINSTALL = "uninstalled",
5
5
  }
6
+
7
+ type EventHandler<T> = (data: T) => void;
8
+
9
+ type HandlerListenerPair = {
10
+ handler: EventHandler<any>;
11
+ listener: EventListener;
12
+ }
13
+
14
+ export class PluginEventMgr {
15
+ private static listeners: Map<PluginEvent, HandlerListenerPair[]> = new Map();
16
+
17
+ public static dispatchEvent<T>(evt: PluginEvent, data: T) {
18
+ window.dispatchEvent(new CustomEvent(evt, { detail: data }));
19
+ }
20
+
21
+ public static addEventListener<T>(evt: PluginEvent, cb: (data: T) => void) {
22
+ const listener = (e: any) => {
23
+ cb((e as CustomEvent<T>).detail);
24
+ }
25
+
26
+ if (!PluginEventMgr.listeners.has(evt)) {
27
+ PluginEventMgr.listeners.set(evt, []);
28
+ }
29
+
30
+ PluginEventMgr.listeners.get(evt)?.push({ handler: cb, listener });
31
+
32
+ window.addEventListener(evt, listener);
33
+ }
34
+
35
+ public static removeEventListener<T>(evt: PluginEvent, cb: (data: T) => void) {
36
+ const listeners = PluginEventMgr.listeners.get(evt);
37
+ if (!listeners) {
38
+ return;
39
+ }
40
+
41
+ const targetPair = listeners.find((l) => l.handler === cb);
42
+ if (!targetPair) {
43
+ return;
44
+ }
45
+
46
+ window.removeEventListener(evt, targetPair.listener);
47
+ }
48
+ }
@@ -1,7 +1,7 @@
1
1
  import { Plugin, PluginConfig } from "./Plugin";
2
2
  import { Dependencies } from "@/plugin/Dependencies";
3
3
  import { PluginManagerPayload } from "@/plugin/PluginManagerPayload";
4
- import { PluginEvent } from "@/plugin/PluginEvent";
4
+ import { PluginEvent, PluginEventMgr } from "@/plugin/PluginEvent";
5
5
 
6
6
  export class PluginManager {
7
7
  private readonly dependencies: Dependencies;
@@ -48,19 +48,15 @@ export class PluginManager {
48
48
  static instance: PluginManager;
49
49
 
50
50
  static dispatchEvent<T>(evt: PluginEvent, data: T) {
51
- window.dispatchEvent(new CustomEvent(evt, { detail: data }));
51
+ PluginEventMgr.dispatchEvent(evt, data);
52
52
  }
53
53
 
54
54
  static addEventListener<T>(evt: PluginEvent, cb: (data: T) => void) {
55
- window.addEventListener(evt, (e) => {
56
- cb((e as CustomEvent<T>).detail);
57
- });
55
+ PluginEventMgr.addEventListener(evt, cb);
58
56
  }
59
57
 
60
58
  static removeEventListener<T>(evt: PluginEvent, cb: (data: T) => void) {
61
- window.removeEventListener(evt, (e) => {
62
- cb((e as CustomEvent<T>).detail);
63
- });
59
+ PluginEventMgr.removeEventListener(evt, cb);
64
60
  }
65
61
 
66
62
  static async init(dependencies: Dependencies) {