@d1g1tal/subscribr 3.0.0 → 3.0.1
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/dist/browser/subscribr.js +63 -0
- package/dist/browser/subscribr.min.js.map +2 -2
- package/dist/esm/subscribr.js +63 -0
- package/dist/esm/subscribr.min.js.map +2 -2
- package/package.json +11 -9
- package/src/context-event-handler.d.ts +22 -22
- package/src/subscribr.js +1 -1
- package/src/subscription.js +1 -2
|
@@ -25,6 +25,13 @@ var Subscribr = (() => {
|
|
|
25
25
|
|
|
26
26
|
// node_modules/@d1g1tal/collections/src/set-multi-map.js
|
|
27
27
|
var SetMultiMap = class extends Map {
|
|
28
|
+
/**
|
|
29
|
+
* Adds a new element with a specified key and value to the SetMultiMap. If an element with the same key already exists, the value will be added to the underlying {@link Set}.
|
|
30
|
+
*
|
|
31
|
+
* @param {*} key The key to set.
|
|
32
|
+
* @param {*} value The value to add to the SetMultiMap
|
|
33
|
+
* @returns {SetMultiMap} The SetMultiMap with the updated key and value.
|
|
34
|
+
*/
|
|
28
35
|
set(key, value) {
|
|
29
36
|
super.set(key, (super.get(key) ?? /* @__PURE__ */ new Set()).add(value));
|
|
30
37
|
return this;
|
|
@@ -38,10 +45,20 @@ var Subscribr = (() => {
|
|
|
38
45
|
var ContextEventHandler = class {
|
|
39
46
|
#context;
|
|
40
47
|
#eventHandler;
|
|
48
|
+
/**
|
|
49
|
+
* @param {*} context The context to bind to the event handler.
|
|
50
|
+
* @param {function(*): void} eventHandler The event handler to call when the event is published.
|
|
51
|
+
*/
|
|
41
52
|
constructor(context, eventHandler) {
|
|
42
53
|
this.#context = context;
|
|
43
54
|
this.#eventHandler = eventHandler;
|
|
44
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Call the event handler for the provided event.
|
|
58
|
+
*
|
|
59
|
+
* @param {Event} event The event to handle
|
|
60
|
+
* @param {*} [data] The value to be passed to the event handler as a parameter.
|
|
61
|
+
*/
|
|
45
62
|
handle(event, data) {
|
|
46
63
|
this.#eventHandler.call(this.#context, event, data);
|
|
47
64
|
}
|
|
@@ -54,13 +71,27 @@ var Subscribr = (() => {
|
|
|
54
71
|
var Subscription = class {
|
|
55
72
|
#eventName;
|
|
56
73
|
#contextEventHandler;
|
|
74
|
+
/**
|
|
75
|
+
* @param {string} eventName The event name.
|
|
76
|
+
* @param {ContextEventHandler} contextEventHandler Then context event handler.
|
|
77
|
+
*/
|
|
57
78
|
constructor(eventName, contextEventHandler) {
|
|
58
79
|
this.#eventName = eventName;
|
|
59
80
|
this.#contextEventHandler = contextEventHandler;
|
|
60
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* Gets the event name for the subscription.
|
|
84
|
+
*
|
|
85
|
+
* @returns {string} The event name.
|
|
86
|
+
*/
|
|
61
87
|
get eventName() {
|
|
62
88
|
return this.#eventName;
|
|
63
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Gets the context event handler.
|
|
92
|
+
*
|
|
93
|
+
* @returns {ContextEventHandler} The context event handler
|
|
94
|
+
*/
|
|
64
95
|
get contextEventHandler() {
|
|
65
96
|
return this.#contextEventHandler;
|
|
66
97
|
}
|
|
@@ -71,15 +102,32 @@ var Subscribr = (() => {
|
|
|
71
102
|
|
|
72
103
|
// src/subscribr.js
|
|
73
104
|
var Subscribr = class {
|
|
105
|
+
/** @type {SetMultiMap<string, ContextEventHandler>} */
|
|
74
106
|
#subscribers;
|
|
75
107
|
constructor() {
|
|
76
108
|
this.#subscribers = new SetMultiMap();
|
|
77
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Subscribe to an event
|
|
112
|
+
*
|
|
113
|
+
* @param {string} eventName The event name to subscribe to.
|
|
114
|
+
* @param {function(Event, *): void} eventHandler The event handler to call when the event is published.
|
|
115
|
+
* @param {*} [context] The context to bind to the event handler.
|
|
116
|
+
* @returns {Subscription} An object used to check if the subscription still exists and to unsubscribe from the event.
|
|
117
|
+
*/
|
|
78
118
|
subscribe(eventName, eventHandler, context = eventHandler) {
|
|
79
119
|
const contextEventHandler = new ContextEventHandler(context, eventHandler);
|
|
80
120
|
this.#subscribers.set(eventName, contextEventHandler);
|
|
81
121
|
return new Subscription(eventName, contextEventHandler);
|
|
82
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Unsubscribe from the event
|
|
125
|
+
*
|
|
126
|
+
* @param {Subscription} subscription The subscription to unsubscribe.
|
|
127
|
+
* @param {string} subscription.eventName The event name to subscribe to.
|
|
128
|
+
* @param {ContextEventHandler} subscription.contextEventHandler The event handler to call when the event is published.
|
|
129
|
+
* @returns {boolean} true if eventListener has been removed successfully. false if the value is not found or if the value is not an object.
|
|
130
|
+
*/
|
|
83
131
|
unsubscribe({ eventName, contextEventHandler }) {
|
|
84
132
|
const contextEventHandlers = this.#subscribers.get(eventName);
|
|
85
133
|
const removed = contextEventHandlers?.delete(contextEventHandler);
|
|
@@ -88,12 +136,27 @@ var Subscribr = (() => {
|
|
|
88
136
|
}
|
|
89
137
|
return removed;
|
|
90
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* Publish an event
|
|
141
|
+
*
|
|
142
|
+
* @param {string} eventName The name of the event.
|
|
143
|
+
* @param {Event} event The event to be handled.
|
|
144
|
+
* @param {*} [data] The value to be passed to the event handler as a parameter.
|
|
145
|
+
*/
|
|
91
146
|
publish(eventName, event = new CustomEvent(eventName), data) {
|
|
92
147
|
if (data == null && !(event instanceof Event)) {
|
|
93
148
|
[data, event] = [event, new CustomEvent(eventName)];
|
|
94
149
|
}
|
|
95
150
|
this.#subscribers.get(eventName)?.forEach((contextEventHandler) => contextEventHandler.handle(event, data));
|
|
96
151
|
}
|
|
152
|
+
/**
|
|
153
|
+
* Check if the event and handler are subscribed.
|
|
154
|
+
*
|
|
155
|
+
* @param {Subscription} subscription The subscription object.
|
|
156
|
+
* @param {string} subscription.eventName The name of the event to check.
|
|
157
|
+
* @param {ContextEventHandler} subscription.contextEventHandler The event handler to check.
|
|
158
|
+
* @returns {boolean} true if the event name and handler are subscribed, false otherwise.
|
|
159
|
+
*/
|
|
97
160
|
isSubscribed({ eventName, contextEventHandler }) {
|
|
98
161
|
return this.#subscribers.get(eventName)?.has(contextEventHandler);
|
|
99
162
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/subscribr.js", "../../node_modules/@d1g1tal/collections/src/set-multi-map.js", "../../src/context-event-handler.js", "../../src/subscription.js"],
|
|
4
|
-
"sourcesContent": ["import SetMultiMap from '@d1g1tal/collections/set-multi-map.js';\nimport ContextEventHandler from './context-event-handler.js';\nimport Subscription from './subscription.js';\n\nexport default class Subscribr {\n\t/** @type {SetMultiMap<string, ContextEventHandler>} */\n\t#subscribers;\n\n\tconstructor() {\n\t\tthis.#subscribers = new SetMultiMap();\n\t}\n\n\t/**\n\t * Subscribe to an event\n\t *\n\t * @param {string} eventName The event name to subscribe to.\n\t * @param {function(Event, *): void} eventHandler The event handler to call when the event is published.\n\t * @param {*} [context] The context to bind to the event handler.\n\t * @returns {Subscription} An object used to check if the subscription still exists and to unsubscribe from the event.\n\t */\n\tsubscribe(eventName, eventHandler, context = eventHandler) {\n\t\tconst contextEventHandler = new ContextEventHandler(context, eventHandler);\n\t\tthis.#subscribers.set(eventName, contextEventHandler);\n\n\t\treturn new Subscription(eventName, contextEventHandler);\n\t}\n\n\t/**\n\t * Unsubscribe from the event\n\t *\n\t * @param {Subscription} subscription The subscription to unsubscribe.\n\t * @param {string} subscription.eventName The event name to subscribe to.\n\t * @param {ContextEventHandler} subscription.contextEventHandler The event handler to call when the event is published.\n\t * @returns {boolean} true if eventListener has been removed successfully. false if the value is not found or if the value is not an object.\n\t */\n\tunsubscribe({ eventName, contextEventHandler }) {\n\t\tconst contextEventHandlers = this.#subscribers.get(eventName);\n\t\tconst removed = contextEventHandlers?.delete(contextEventHandler);\n\n\t\tif (removed && contextEventHandlers.size == 0) {\n\t\t\tthis.#subscribers.delete(eventName);\n\t\t}\n\n\t\treturn removed;\n\t}\n\n\t/**\n\t * Publish an event\n\t *\n\t * @param {string} eventName The name of the event.\n\t * @param {Event} event The event to be handled.\n\t * @param {*} [data] The value to be passed to the event handler as a parameter.\n\t */\n\tpublish(eventName, event = new CustomEvent(eventName), data) {\n\t\tif (data == null && !(event instanceof Event)) {\n\t\t\t[data, event] = [event, new CustomEvent(eventName)];\n\t\t}\n\t\tthis.#subscribers.get(eventName)?.forEach((contextEventHandler) => contextEventHandler.handle(event, data));\n\t}\n\n\t/**\n\t * Check if the event and handler are subscribed.\n\t *\n\t * @param {Subscription} subscription The subscription object.\n\t * @param {string} subscription.eventName The name of the event to check.\n\t * @param {ContextEventHandler} subscription.contextEventHandler The event handler to check.\n\t * @returns {boolean} true if the event name and handler are subscribed, false otherwise.\n\t */\n\tisSubscribed({ eventName, contextEventHandler }) {\n\t\treturn this.#subscribers.get(eventName)?.has(contextEventHandler);\n\t}\n\n\tget [Symbol.toStringTag]() {\n\t\treturn 'Subscribr';\n\t}\n}", "/**\n *\n * @typedef {Map<*, *>} SetMultiMap\n * @extends Map\n */\nexport default class SetMultiMap extends Map {\n\t/**\n\t * Adds a new element with a specified key and value to the SetMultiMap. If an element with the same key already exists, the value will be added to the underlying {@link Set}.\n\t *\n\t * @param {*} key The key to set.\n\t * @param {*} value The value to add to the SetMultiMap\n\t * @returns {SetMultiMap} The SetMultiMap with the updated key and value.\n\t */\n\tset(key, value) {\n\t\tsuper.set(key, (super.get(key) ?? new Set()).add(value));\n\n\t\treturn this;\n\t}\n\n\t[Symbol.toStringTag]() {\n\t\treturn 'SetMultiMap';\n\t}\n}", "export default class ContextEventHandler {\n\t#context;\n\t#eventHandler;\n\n\t/**\n\t * @param {*} context The context to bind to the event handler.\n\t * @param {function(*): void} eventHandler The event handler to call when the event is published.\n\t */\n\tconstructor(context, eventHandler) {\n\t\tthis.#context = context;\n\t\tthis.#eventHandler = eventHandler;\n\t}\n\n\t/**\n\t * Call the event handler for the provided event.\n\t *\n\t * @param {Event} event The event to handle\n\t * @param {*} [data] The value to be passed to the event handler as a parameter.\n\t */\n\thandle(event, data) {\n\t\tthis.#eventHandler.call(this.#context, event, data);\n\t}\n\n\tget [Symbol.toStringTag]() {\n\t\treturn 'ContextEventHandler';\n\t}\n}", "
|
|
5
|
-
"mappings": "gbAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,ICKA,IAAqBC,EAArB,cAAyC,GAAI,CAQ5C,IAAIC,EAAKC,EAAO,CACf,aAAM,IAAID,GAAM,MAAM,IAAIA,CAAG,GAAK,IAAI,KAAO,IAAIC,CAAK,CAAC,EAEhD,IACR,CAEA,CAAC,OAAO,
|
|
4
|
+
"sourcesContent": ["import SetMultiMap from '@d1g1tal/collections/set-multi-map.js';\nimport ContextEventHandler from './context-event-handler.js';\nimport Subscription from './subscription.js';\n\nexport default class Subscribr {\n\t/** @type {SetMultiMap<string, ContextEventHandler>} */\n\t#subscribers;\n\n\tconstructor() {\n\t\tthis.#subscribers = new SetMultiMap();\n\t}\n\n\t/**\n\t * Subscribe to an event\n\t *\n\t * @param {string} eventName The event name to subscribe to.\n\t * @param {function(Event, *): void} eventHandler The event handler to call when the event is published.\n\t * @param {*} [context] The context to bind to the event handler.\n\t * @returns {Subscription} An object used to check if the subscription still exists and to unsubscribe from the event.\n\t */\n\tsubscribe(eventName, eventHandler, context = eventHandler) {\n\t\tconst contextEventHandler = new ContextEventHandler(context, eventHandler);\n\t\tthis.#subscribers.set(eventName, contextEventHandler);\n\n\t\treturn new Subscription(eventName, contextEventHandler);\n\t}\n\n\t/**\n\t * Unsubscribe from the event\n\t *\n\t * @param {Subscription} subscription The subscription to unsubscribe.\n\t * @param {string} subscription.eventName The event name to subscribe to.\n\t * @param {ContextEventHandler} subscription.contextEventHandler The event handler to call when the event is published.\n\t * @returns {boolean} true if eventListener has been removed successfully. false if the value is not found or if the value is not an object.\n\t */\n\tunsubscribe({ eventName, contextEventHandler }) {\n\t\tconst contextEventHandlers = this.#subscribers.get(eventName);\n\t\tconst removed = contextEventHandlers?.delete(contextEventHandler);\n\n\t\tif (removed && contextEventHandlers.size == 0) {\n\t\t\tthis.#subscribers.delete(eventName);\n\t\t}\n\n\t\treturn removed;\n\t}\n\n\t/**\n\t * Publish an event\n\t *\n\t * @param {string} eventName The name of the event.\n\t * @param {Event} event The event to be handled.\n\t * @param {*} [data] The value to be passed to the event handler as a parameter.\n\t */\n\tpublish(eventName, event = new CustomEvent(eventName), data) {\n\t\tif (data == null && !(event instanceof Event)) {\n\t\t\t// Swap the event and data parameters because only data was passed without an event object\n\t\t\t[data, event] = [event, new CustomEvent(eventName)];\n\t\t}\n\t\tthis.#subscribers.get(eventName)?.forEach((contextEventHandler) => contextEventHandler.handle(event, data));\n\t}\n\n\t/**\n\t * Check if the event and handler are subscribed.\n\t *\n\t * @param {Subscription} subscription The subscription object.\n\t * @param {string} subscription.eventName The name of the event to check.\n\t * @param {ContextEventHandler} subscription.contextEventHandler The event handler to check.\n\t * @returns {boolean} true if the event name and handler are subscribed, false otherwise.\n\t */\n\tisSubscribed({ eventName, contextEventHandler }) {\n\t\treturn this.#subscribers.get(eventName)?.has(contextEventHandler);\n\t}\n\n\tget [Symbol.toStringTag]() {\n\t\treturn 'Subscribr';\n\t}\n}", "/**\n *\n * @typedef {Map<*, *>} SetMultiMap\n * @extends Map\n */\nexport default class SetMultiMap extends Map {\n\t/**\n\t * Adds a new element with a specified key and value to the SetMultiMap. If an element with the same key already exists, the value will be added to the underlying {@link Set}.\n\t *\n\t * @param {*} key The key to set.\n\t * @param {*} value The value to add to the SetMultiMap\n\t * @returns {SetMultiMap} The SetMultiMap with the updated key and value.\n\t */\n\tset(key, value) {\n\t\tsuper.set(key, (super.get(key) ?? new Set()).add(value));\n\n\t\treturn this;\n\t}\n\n\t[Symbol.toStringTag]() {\n\t\treturn 'SetMultiMap';\n\t}\n}", "export default class ContextEventHandler {\n\t#context;\n\t#eventHandler;\n\n\t/**\n\t * @param {*} context The context to bind to the event handler.\n\t * @param {function(*): void} eventHandler The event handler to call when the event is published.\n\t */\n\tconstructor(context, eventHandler) {\n\t\tthis.#context = context;\n\t\tthis.#eventHandler = eventHandler;\n\t}\n\n\t/**\n\t * Call the event handler for the provided event.\n\t *\n\t * @param {Event} event The event to handle\n\t * @param {*} [data] The value to be passed to the event handler as a parameter.\n\t */\n\thandle(event, data) {\n\t\tthis.#eventHandler.call(this.#context, event, data);\n\t}\n\n\tget [Symbol.toStringTag]() {\n\t\treturn 'ContextEventHandler';\n\t}\n}", "/** @typedef { import('./context-event-handler.js').default } ContextEventHandler */\n\nexport default class Subscription {\n\t#eventName;\n\t#contextEventHandler;\n\n\t/**\n\t * @param {string} eventName The event name.\n\t * @param {ContextEventHandler} contextEventHandler Then context event handler.\n\t */\n\tconstructor(eventName, contextEventHandler) {\n\t\tthis.#eventName = eventName;\n\t\tthis.#contextEventHandler = contextEventHandler;\n\t}\n\n\t/**\n\t * Gets the event name for the subscription.\n\t *\n\t * @returns {string} The event name.\n\t */\n\tget eventName() {\n\t\treturn this.#eventName;\n\t}\n\n\t/**\n\t * Gets the context event handler.\n\t *\n\t * @returns {ContextEventHandler} The context event handler\n\t */\n\tget contextEventHandler() {\n\t\treturn this.#contextEventHandler;\n\t}\n\n\tget [Symbol.toStringTag]() {\n\t\treturn 'Subscription';\n\t}\n}"],
|
|
5
|
+
"mappings": "gbAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,ICKA,IAAqBC,EAArB,cAAyC,GAAI,CAQ5C,IAAIC,EAAKC,EAAO,CACf,aAAM,IAAID,GAAM,MAAM,IAAIA,CAAG,GAAK,IAAI,KAAO,IAAIC,CAAK,CAAC,EAEhD,IACR,CAEA,CAAC,OAAO,WAAW,GAAI,CACtB,MAAO,aACR,CACD,ECtBA,IAAqBC,EAArB,KAAyC,CACxCC,GACAC,GAMA,YAAYC,EAASC,EAAc,CAClC,KAAKH,GAAWE,EAChB,KAAKD,GAAgBE,CACtB,CAQA,OAAOC,EAAOC,EAAM,CACnB,KAAKJ,GAAc,KAAK,KAAKD,GAAUI,EAAOC,CAAI,CACnD,CAEA,IAAK,OAAO,WAAW,GAAI,CAC1B,MAAO,qBACR,CACD,ECxBA,IAAqBC,EAArB,KAAkC,CACjCC,GACAC,GAMA,YAAYC,EAAWC,EAAqB,CAC3C,KAAKH,GAAaE,EAClB,KAAKD,GAAuBE,CAC7B,CAOA,IAAI,WAAY,CACf,OAAO,KAAKH,EACb,CAOA,IAAI,qBAAsB,CACzB,OAAO,KAAKC,EACb,CAEA,IAAK,OAAO,WAAW,GAAI,CAC1B,MAAO,cACR,CACD,EHhCA,IAAqBG,EAArB,KAA+B,CAE9BC,GAEA,aAAc,CACb,KAAKA,GAAe,IAAIC,CACzB,CAUA,UAAUC,EAAWC,EAAcC,EAAUD,EAAc,CAC1D,IAAME,EAAsB,IAAIC,EAAoBF,EAASD,CAAY,EACzE,YAAKH,GAAa,IAAIE,EAAWG,CAAmB,EAE7C,IAAIE,EAAaL,EAAWG,CAAmB,CACvD,CAUA,YAAY,CAAE,UAAAH,EAAW,oBAAAG,CAAoB,EAAG,CAC/C,IAAMG,EAAuB,KAAKR,GAAa,IAAIE,CAAS,EACtDO,EAAUD,GAAsB,OAAOH,CAAmB,EAEhE,OAAII,GAAWD,EAAqB,MAAQ,GAC3C,KAAKR,GAAa,OAAOE,CAAS,EAG5BO,CACR,CASA,QAAQP,EAAWQ,EAAQ,IAAI,YAAYR,CAAS,EAAGS,EAAM,CACxDA,GAAQ,MAAQ,EAAED,aAAiB,SAEtC,CAACC,EAAMD,CAAK,EAAI,CAACA,EAAO,IAAI,YAAYR,CAAS,CAAC,GAEnD,KAAKF,GAAa,IAAIE,CAAS,GAAG,QAASG,GAAwBA,EAAoB,OAAOK,EAAOC,CAAI,CAAC,CAC3G,CAUA,aAAa,CAAE,UAAAT,EAAW,oBAAAG,CAAoB,EAAG,CAChD,OAAO,KAAKL,GAAa,IAAIE,CAAS,GAAG,IAAIG,CAAmB,CACjE,CAEA,IAAK,OAAO,WAAW,GAAI,CAC1B,MAAO,WACR,CACD",
|
|
6
6
|
"names": ["subscribr_exports", "__export", "Subscribr", "SetMultiMap", "key", "value", "ContextEventHandler", "#context", "#eventHandler", "context", "eventHandler", "event", "data", "Subscription", "#eventName", "#contextEventHandler", "eventName", "contextEventHandler", "Subscribr", "#subscribers", "SetMultiMap", "eventName", "eventHandler", "context", "contextEventHandler", "ContextEventHandler", "Subscription", "contextEventHandlers", "removed", "event", "data"]
|
|
7
7
|
}
|
package/dist/esm/subscribr.js
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
// node_modules/@d1g1tal/collections/src/set-multi-map.js
|
|
2
2
|
var SetMultiMap = class extends Map {
|
|
3
|
+
/**
|
|
4
|
+
* Adds a new element with a specified key and value to the SetMultiMap. If an element with the same key already exists, the value will be added to the underlying {@link Set}.
|
|
5
|
+
*
|
|
6
|
+
* @param {*} key The key to set.
|
|
7
|
+
* @param {*} value The value to add to the SetMultiMap
|
|
8
|
+
* @returns {SetMultiMap} The SetMultiMap with the updated key and value.
|
|
9
|
+
*/
|
|
3
10
|
set(key, value) {
|
|
4
11
|
super.set(key, (super.get(key) ?? /* @__PURE__ */ new Set()).add(value));
|
|
5
12
|
return this;
|
|
@@ -13,10 +20,20 @@ var SetMultiMap = class extends Map {
|
|
|
13
20
|
var ContextEventHandler = class {
|
|
14
21
|
#context;
|
|
15
22
|
#eventHandler;
|
|
23
|
+
/**
|
|
24
|
+
* @param {*} context The context to bind to the event handler.
|
|
25
|
+
* @param {function(*): void} eventHandler The event handler to call when the event is published.
|
|
26
|
+
*/
|
|
16
27
|
constructor(context, eventHandler) {
|
|
17
28
|
this.#context = context;
|
|
18
29
|
this.#eventHandler = eventHandler;
|
|
19
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Call the event handler for the provided event.
|
|
33
|
+
*
|
|
34
|
+
* @param {Event} event The event to handle
|
|
35
|
+
* @param {*} [data] The value to be passed to the event handler as a parameter.
|
|
36
|
+
*/
|
|
20
37
|
handle(event, data) {
|
|
21
38
|
this.#eventHandler.call(this.#context, event, data);
|
|
22
39
|
}
|
|
@@ -29,13 +46,27 @@ var ContextEventHandler = class {
|
|
|
29
46
|
var Subscription = class {
|
|
30
47
|
#eventName;
|
|
31
48
|
#contextEventHandler;
|
|
49
|
+
/**
|
|
50
|
+
* @param {string} eventName The event name.
|
|
51
|
+
* @param {ContextEventHandler} contextEventHandler Then context event handler.
|
|
52
|
+
*/
|
|
32
53
|
constructor(eventName, contextEventHandler) {
|
|
33
54
|
this.#eventName = eventName;
|
|
34
55
|
this.#contextEventHandler = contextEventHandler;
|
|
35
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Gets the event name for the subscription.
|
|
59
|
+
*
|
|
60
|
+
* @returns {string} The event name.
|
|
61
|
+
*/
|
|
36
62
|
get eventName() {
|
|
37
63
|
return this.#eventName;
|
|
38
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Gets the context event handler.
|
|
67
|
+
*
|
|
68
|
+
* @returns {ContextEventHandler} The context event handler
|
|
69
|
+
*/
|
|
39
70
|
get contextEventHandler() {
|
|
40
71
|
return this.#contextEventHandler;
|
|
41
72
|
}
|
|
@@ -46,15 +77,32 @@ var Subscription = class {
|
|
|
46
77
|
|
|
47
78
|
// src/subscribr.js
|
|
48
79
|
var Subscribr = class {
|
|
80
|
+
/** @type {SetMultiMap<string, ContextEventHandler>} */
|
|
49
81
|
#subscribers;
|
|
50
82
|
constructor() {
|
|
51
83
|
this.#subscribers = new SetMultiMap();
|
|
52
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* Subscribe to an event
|
|
87
|
+
*
|
|
88
|
+
* @param {string} eventName The event name to subscribe to.
|
|
89
|
+
* @param {function(Event, *): void} eventHandler The event handler to call when the event is published.
|
|
90
|
+
* @param {*} [context] The context to bind to the event handler.
|
|
91
|
+
* @returns {Subscription} An object used to check if the subscription still exists and to unsubscribe from the event.
|
|
92
|
+
*/
|
|
53
93
|
subscribe(eventName, eventHandler, context = eventHandler) {
|
|
54
94
|
const contextEventHandler = new ContextEventHandler(context, eventHandler);
|
|
55
95
|
this.#subscribers.set(eventName, contextEventHandler);
|
|
56
96
|
return new Subscription(eventName, contextEventHandler);
|
|
57
97
|
}
|
|
98
|
+
/**
|
|
99
|
+
* Unsubscribe from the event
|
|
100
|
+
*
|
|
101
|
+
* @param {Subscription} subscription The subscription to unsubscribe.
|
|
102
|
+
* @param {string} subscription.eventName The event name to subscribe to.
|
|
103
|
+
* @param {ContextEventHandler} subscription.contextEventHandler The event handler to call when the event is published.
|
|
104
|
+
* @returns {boolean} true if eventListener has been removed successfully. false if the value is not found or if the value is not an object.
|
|
105
|
+
*/
|
|
58
106
|
unsubscribe({ eventName, contextEventHandler }) {
|
|
59
107
|
const contextEventHandlers = this.#subscribers.get(eventName);
|
|
60
108
|
const removed = contextEventHandlers?.delete(contextEventHandler);
|
|
@@ -63,12 +111,27 @@ var Subscribr = class {
|
|
|
63
111
|
}
|
|
64
112
|
return removed;
|
|
65
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* Publish an event
|
|
116
|
+
*
|
|
117
|
+
* @param {string} eventName The name of the event.
|
|
118
|
+
* @param {Event} event The event to be handled.
|
|
119
|
+
* @param {*} [data] The value to be passed to the event handler as a parameter.
|
|
120
|
+
*/
|
|
66
121
|
publish(eventName, event = new CustomEvent(eventName), data) {
|
|
67
122
|
if (data == null && !(event instanceof Event)) {
|
|
68
123
|
[data, event] = [event, new CustomEvent(eventName)];
|
|
69
124
|
}
|
|
70
125
|
this.#subscribers.get(eventName)?.forEach((contextEventHandler) => contextEventHandler.handle(event, data));
|
|
71
126
|
}
|
|
127
|
+
/**
|
|
128
|
+
* Check if the event and handler are subscribed.
|
|
129
|
+
*
|
|
130
|
+
* @param {Subscription} subscription The subscription object.
|
|
131
|
+
* @param {string} subscription.eventName The name of the event to check.
|
|
132
|
+
* @param {ContextEventHandler} subscription.contextEventHandler The event handler to check.
|
|
133
|
+
* @returns {boolean} true if the event name and handler are subscribed, false otherwise.
|
|
134
|
+
*/
|
|
72
135
|
isSubscribed({ eventName, contextEventHandler }) {
|
|
73
136
|
return this.#subscribers.get(eventName)?.has(contextEventHandler);
|
|
74
137
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../node_modules/@d1g1tal/collections/src/set-multi-map.js", "../../src/context-event-handler.js", "../../src/subscription.js", "../../src/subscribr.js"],
|
|
4
|
-
"sourcesContent": ["/**\n *\n * @typedef {Map<*, *>} SetMultiMap\n * @extends Map\n */\nexport default class SetMultiMap extends Map {\n\t/**\n\t * Adds a new element with a specified key and value to the SetMultiMap. If an element with the same key already exists, the value will be added to the underlying {@link Set}.\n\t *\n\t * @param {*} key The key to set.\n\t * @param {*} value The value to add to the SetMultiMap\n\t * @returns {SetMultiMap} The SetMultiMap with the updated key and value.\n\t */\n\tset(key, value) {\n\t\tsuper.set(key, (super.get(key) ?? new Set()).add(value));\n\n\t\treturn this;\n\t}\n\n\t[Symbol.toStringTag]() {\n\t\treturn 'SetMultiMap';\n\t}\n}", "export default class ContextEventHandler {\n\t#context;\n\t#eventHandler;\n\n\t/**\n\t * @param {*} context The context to bind to the event handler.\n\t * @param {function(*): void} eventHandler The event handler to call when the event is published.\n\t */\n\tconstructor(context, eventHandler) {\n\t\tthis.#context = context;\n\t\tthis.#eventHandler = eventHandler;\n\t}\n\n\t/**\n\t * Call the event handler for the provided event.\n\t *\n\t * @param {Event} event The event to handle\n\t * @param {*} [data] The value to be passed to the event handler as a parameter.\n\t */\n\thandle(event, data) {\n\t\tthis.#eventHandler.call(this.#context, event, data);\n\t}\n\n\tget [Symbol.toStringTag]() {\n\t\treturn 'ContextEventHandler';\n\t}\n}", "
|
|
5
|
-
"mappings": "AAKA,IAAqBA,EAArB,cAAyC,GAAI,CAQ5C,IAAIC,EAAKC,EAAO,CACf,aAAM,IAAID,GAAM,MAAM,IAAIA,CAAG,GAAK,IAAI,KAAO,IAAIC,CAAK,CAAC,EAEhD,IACR,CAEA,CAAC,OAAO,
|
|
4
|
+
"sourcesContent": ["/**\n *\n * @typedef {Map<*, *>} SetMultiMap\n * @extends Map\n */\nexport default class SetMultiMap extends Map {\n\t/**\n\t * Adds a new element with a specified key and value to the SetMultiMap. If an element with the same key already exists, the value will be added to the underlying {@link Set}.\n\t *\n\t * @param {*} key The key to set.\n\t * @param {*} value The value to add to the SetMultiMap\n\t * @returns {SetMultiMap} The SetMultiMap with the updated key and value.\n\t */\n\tset(key, value) {\n\t\tsuper.set(key, (super.get(key) ?? new Set()).add(value));\n\n\t\treturn this;\n\t}\n\n\t[Symbol.toStringTag]() {\n\t\treturn 'SetMultiMap';\n\t}\n}", "export default class ContextEventHandler {\n\t#context;\n\t#eventHandler;\n\n\t/**\n\t * @param {*} context The context to bind to the event handler.\n\t * @param {function(*): void} eventHandler The event handler to call when the event is published.\n\t */\n\tconstructor(context, eventHandler) {\n\t\tthis.#context = context;\n\t\tthis.#eventHandler = eventHandler;\n\t}\n\n\t/**\n\t * Call the event handler for the provided event.\n\t *\n\t * @param {Event} event The event to handle\n\t * @param {*} [data] The value to be passed to the event handler as a parameter.\n\t */\n\thandle(event, data) {\n\t\tthis.#eventHandler.call(this.#context, event, data);\n\t}\n\n\tget [Symbol.toStringTag]() {\n\t\treturn 'ContextEventHandler';\n\t}\n}", "/** @typedef { import('./context-event-handler.js').default } ContextEventHandler */\n\nexport default class Subscription {\n\t#eventName;\n\t#contextEventHandler;\n\n\t/**\n\t * @param {string} eventName The event name.\n\t * @param {ContextEventHandler} contextEventHandler Then context event handler.\n\t */\n\tconstructor(eventName, contextEventHandler) {\n\t\tthis.#eventName = eventName;\n\t\tthis.#contextEventHandler = contextEventHandler;\n\t}\n\n\t/**\n\t * Gets the event name for the subscription.\n\t *\n\t * @returns {string} The event name.\n\t */\n\tget eventName() {\n\t\treturn this.#eventName;\n\t}\n\n\t/**\n\t * Gets the context event handler.\n\t *\n\t * @returns {ContextEventHandler} The context event handler\n\t */\n\tget contextEventHandler() {\n\t\treturn this.#contextEventHandler;\n\t}\n\n\tget [Symbol.toStringTag]() {\n\t\treturn 'Subscription';\n\t}\n}", "import SetMultiMap from '@d1g1tal/collections/set-multi-map.js';\nimport ContextEventHandler from './context-event-handler.js';\nimport Subscription from './subscription.js';\n\nexport default class Subscribr {\n\t/** @type {SetMultiMap<string, ContextEventHandler>} */\n\t#subscribers;\n\n\tconstructor() {\n\t\tthis.#subscribers = new SetMultiMap();\n\t}\n\n\t/**\n\t * Subscribe to an event\n\t *\n\t * @param {string} eventName The event name to subscribe to.\n\t * @param {function(Event, *): void} eventHandler The event handler to call when the event is published.\n\t * @param {*} [context] The context to bind to the event handler.\n\t * @returns {Subscription} An object used to check if the subscription still exists and to unsubscribe from the event.\n\t */\n\tsubscribe(eventName, eventHandler, context = eventHandler) {\n\t\tconst contextEventHandler = new ContextEventHandler(context, eventHandler);\n\t\tthis.#subscribers.set(eventName, contextEventHandler);\n\n\t\treturn new Subscription(eventName, contextEventHandler);\n\t}\n\n\t/**\n\t * Unsubscribe from the event\n\t *\n\t * @param {Subscription} subscription The subscription to unsubscribe.\n\t * @param {string} subscription.eventName The event name to subscribe to.\n\t * @param {ContextEventHandler} subscription.contextEventHandler The event handler to call when the event is published.\n\t * @returns {boolean} true if eventListener has been removed successfully. false if the value is not found or if the value is not an object.\n\t */\n\tunsubscribe({ eventName, contextEventHandler }) {\n\t\tconst contextEventHandlers = this.#subscribers.get(eventName);\n\t\tconst removed = contextEventHandlers?.delete(contextEventHandler);\n\n\t\tif (removed && contextEventHandlers.size == 0) {\n\t\t\tthis.#subscribers.delete(eventName);\n\t\t}\n\n\t\treturn removed;\n\t}\n\n\t/**\n\t * Publish an event\n\t *\n\t * @param {string} eventName The name of the event.\n\t * @param {Event} event The event to be handled.\n\t * @param {*} [data] The value to be passed to the event handler as a parameter.\n\t */\n\tpublish(eventName, event = new CustomEvent(eventName), data) {\n\t\tif (data == null && !(event instanceof Event)) {\n\t\t\t// Swap the event and data parameters because only data was passed without an event object\n\t\t\t[data, event] = [event, new CustomEvent(eventName)];\n\t\t}\n\t\tthis.#subscribers.get(eventName)?.forEach((contextEventHandler) => contextEventHandler.handle(event, data));\n\t}\n\n\t/**\n\t * Check if the event and handler are subscribed.\n\t *\n\t * @param {Subscription} subscription The subscription object.\n\t * @param {string} subscription.eventName The name of the event to check.\n\t * @param {ContextEventHandler} subscription.contextEventHandler The event handler to check.\n\t * @returns {boolean} true if the event name and handler are subscribed, false otherwise.\n\t */\n\tisSubscribed({ eventName, contextEventHandler }) {\n\t\treturn this.#subscribers.get(eventName)?.has(contextEventHandler);\n\t}\n\n\tget [Symbol.toStringTag]() {\n\t\treturn 'Subscribr';\n\t}\n}"],
|
|
5
|
+
"mappings": "AAKA,IAAqBA,EAArB,cAAyC,GAAI,CAQ5C,IAAIC,EAAKC,EAAO,CACf,aAAM,IAAID,GAAM,MAAM,IAAIA,CAAG,GAAK,IAAI,KAAO,IAAIC,CAAK,CAAC,EAEhD,IACR,CAEA,CAAC,OAAO,WAAW,GAAI,CACtB,MAAO,aACR,CACD,ECtBA,IAAqBC,EAArB,KAAyC,CACxCC,GACAC,GAMA,YAAYC,EAASC,EAAc,CAClC,KAAKH,GAAWE,EAChB,KAAKD,GAAgBE,CACtB,CAQA,OAAOC,EAAOC,EAAM,CACnB,KAAKJ,GAAc,KAAK,KAAKD,GAAUI,EAAOC,CAAI,CACnD,CAEA,IAAK,OAAO,WAAW,GAAI,CAC1B,MAAO,qBACR,CACD,ECxBA,IAAqBC,EAArB,KAAkC,CACjCC,GACAC,GAMA,YAAYC,EAAWC,EAAqB,CAC3C,KAAKH,GAAaE,EAClB,KAAKD,GAAuBE,CAC7B,CAOA,IAAI,WAAY,CACf,OAAO,KAAKH,EACb,CAOA,IAAI,qBAAsB,CACzB,OAAO,KAAKC,EACb,CAEA,IAAK,OAAO,WAAW,GAAI,CAC1B,MAAO,cACR,CACD,EChCA,IAAqBG,EAArB,KAA+B,CAE9BC,GAEA,aAAc,CACb,KAAKA,GAAe,IAAIC,CACzB,CAUA,UAAUC,EAAWC,EAAcC,EAAUD,EAAc,CAC1D,IAAME,EAAsB,IAAIC,EAAoBF,EAASD,CAAY,EACzE,YAAKH,GAAa,IAAIE,EAAWG,CAAmB,EAE7C,IAAIE,EAAaL,EAAWG,CAAmB,CACvD,CAUA,YAAY,CAAE,UAAAH,EAAW,oBAAAG,CAAoB,EAAG,CAC/C,IAAMG,EAAuB,KAAKR,GAAa,IAAIE,CAAS,EACtDO,EAAUD,GAAsB,OAAOH,CAAmB,EAEhE,OAAII,GAAWD,EAAqB,MAAQ,GAC3C,KAAKR,GAAa,OAAOE,CAAS,EAG5BO,CACR,CASA,QAAQP,EAAWQ,EAAQ,IAAI,YAAYR,CAAS,EAAGS,EAAM,CACxDA,GAAQ,MAAQ,EAAED,aAAiB,SAEtC,CAACC,EAAMD,CAAK,EAAI,CAACA,EAAO,IAAI,YAAYR,CAAS,CAAC,GAEnD,KAAKF,GAAa,IAAIE,CAAS,GAAG,QAASG,GAAwBA,EAAoB,OAAOK,EAAOC,CAAI,CAAC,CAC3G,CAUA,aAAa,CAAE,UAAAT,EAAW,oBAAAG,CAAoB,EAAG,CAChD,OAAO,KAAKL,GAAa,IAAIE,CAAS,GAAG,IAAIG,CAAmB,CACjE,CAEA,IAAK,OAAO,WAAW,GAAI,CAC1B,MAAO,WACR,CACD",
|
|
6
6
|
"names": ["SetMultiMap", "key", "value", "ContextEventHandler", "#context", "#eventHandler", "context", "eventHandler", "event", "data", "Subscription", "#eventName", "#contextEventHandler", "eventName", "contextEventHandler", "Subscribr", "#subscribers", "SetMultiMap", "eventName", "eventHandler", "context", "contextEventHandler", "ContextEventHandler", "Subscription", "contextEventHandlers", "removed", "event", "data"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@d1g1tal/subscribr",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"description": "JavaScript Publish/Subscribe Library",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "index.d.js",
|
|
@@ -43,14 +43,14 @@
|
|
|
43
43
|
},
|
|
44
44
|
"homepage": "https://github.com/D1g1talEntr0py/subscribr#readme",
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@d1g1tal/chrysalis": "^1.
|
|
46
|
+
"@d1g1tal/chrysalis": "^1.2.3",
|
|
47
47
|
"@skypack/package-check": "^0.2.2",
|
|
48
|
-
"esbuild": "^0.
|
|
49
|
-
"eslint": "^8.
|
|
50
|
-
"eslint-plugin-compat": "^4.
|
|
51
|
-
"eslint-plugin-jsdoc": "^
|
|
52
|
-
"jest": "^29.
|
|
53
|
-
"rimraf": "^
|
|
48
|
+
"esbuild": "^0.17.18",
|
|
49
|
+
"eslint": "^8.39.0",
|
|
50
|
+
"eslint-plugin-compat": "^4.1.4",
|
|
51
|
+
"eslint-plugin-jsdoc": "^43.1.1",
|
|
52
|
+
"jest": "^29.5.0",
|
|
53
|
+
"rimraf": "^5.0.0"
|
|
54
54
|
},
|
|
55
55
|
"jest": {
|
|
56
56
|
"verbose": true,
|
|
@@ -65,7 +65,9 @@
|
|
|
65
65
|
]
|
|
66
66
|
},
|
|
67
67
|
"browserslist": [
|
|
68
|
-
"defaults"
|
|
68
|
+
"defaults",
|
|
69
|
+
"not ios_saf < 15",
|
|
70
|
+
"not op_mini all"
|
|
69
71
|
],
|
|
70
72
|
"dependencies": {
|
|
71
73
|
"@d1g1tal/collections": "^0.0.4"
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
export default class ContextEventHandler {
|
|
2
|
-
/**
|
|
3
|
-
* @param {*} context The context to bind to the event handler.
|
|
4
|
-
* @param {function(*): void} eventHandler The event handler to call when the event is published.
|
|
5
|
-
*/
|
|
6
|
-
constructor(context: any, eventHandler: (arg0: any) => void);
|
|
7
|
-
/**
|
|
8
|
-
* Call the event handler for the provided event.
|
|
9
|
-
*
|
|
10
|
-
* @param {Event} event The event to handle
|
|
11
|
-
* @param {*} [data] The value to be passed to the event handler as a parameter.
|
|
12
|
-
*/
|
|
13
|
-
handle(event: Event, data?: any): void;
|
|
14
|
-
/**
|
|
15
|
-
* Get the event handler
|
|
16
|
-
*
|
|
17
|
-
* @returns {function(*)} The event handler
|
|
18
|
-
*/
|
|
19
|
-
get eventHandler(): (arg0: any) => any;
|
|
20
|
-
get [Symbol.toStringTag](): string;
|
|
21
|
-
#private;
|
|
22
|
-
}
|
|
1
|
+
export default class ContextEventHandler {
|
|
2
|
+
/**
|
|
3
|
+
* @param {*} context The context to bind to the event handler.
|
|
4
|
+
* @param {function(*): void} eventHandler The event handler to call when the event is published.
|
|
5
|
+
*/
|
|
6
|
+
constructor(context: any, eventHandler: (arg0: any) => void);
|
|
7
|
+
/**
|
|
8
|
+
* Call the event handler for the provided event.
|
|
9
|
+
*
|
|
10
|
+
* @param {Event} event The event to handle
|
|
11
|
+
* @param {*} [data] The value to be passed to the event handler as a parameter.
|
|
12
|
+
*/
|
|
13
|
+
handle(event: Event, data?: any): void;
|
|
14
|
+
/**
|
|
15
|
+
* Get the event handler
|
|
16
|
+
*
|
|
17
|
+
* @returns {function(*)} The event handler
|
|
18
|
+
*/
|
|
19
|
+
get eventHandler(): (arg0: any) => any;
|
|
20
|
+
get [Symbol.toStringTag](): string;
|
|
21
|
+
#private;
|
|
22
|
+
}
|
package/src/subscribr.js
CHANGED
|
@@ -53,7 +53,7 @@ export default class Subscribr {
|
|
|
53
53
|
*/
|
|
54
54
|
publish(eventName, event = new CustomEvent(eventName), data) {
|
|
55
55
|
if (data == null && !(event instanceof Event)) {
|
|
56
|
-
// Swap the event and data parameters
|
|
56
|
+
// Swap the event and data parameters because only data was passed without an event object
|
|
57
57
|
[data, event] = [event, new CustomEvent(eventName)];
|
|
58
58
|
}
|
|
59
59
|
this.#subscribers.get(eventName)?.forEach((contextEventHandler) => contextEventHandler.handle(event, data));
|
package/src/subscription.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
/** @typedef {import('./context-event-handler.js').default} ContextEventHandler */
|
|
1
|
+
/** @typedef { import('./context-event-handler.js').default } ContextEventHandler */
|
|
3
2
|
|
|
4
3
|
export default class Subscription {
|
|
5
4
|
#eventName;
|