@d1g1tal/subscribr 4.0.2 → 4.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.
- package/dist/subscribr.d.ts +23 -28
- package/dist/subscribr.js.map +3 -3
- package/package.json +12 -18
- package/src/context-event-handler.ts +5 -5
- package/src/subscribr.ts +13 -18
- package/src/subscription.ts +5 -5
package/dist/subscribr.d.ts
CHANGED
|
@@ -6,22 +6,22 @@ declare class ContextEventHandler {
|
|
|
6
6
|
private readonly context;
|
|
7
7
|
private readonly eventListener;
|
|
8
8
|
/**
|
|
9
|
-
* @param
|
|
10
|
-
* @param
|
|
9
|
+
* @param context The context to bind to the event handler.
|
|
10
|
+
* @param eventListener The event handler to call when the event is published.
|
|
11
11
|
*/
|
|
12
12
|
constructor(context: unknown, eventListener: ContextEventListener);
|
|
13
13
|
/**
|
|
14
14
|
* Call the event handler for the provided event.
|
|
15
15
|
*
|
|
16
|
-
* @param
|
|
17
|
-
* @param
|
|
16
|
+
* @param event The event to handle
|
|
17
|
+
* @param data The value to be passed to the event handler as a parameter.
|
|
18
18
|
*/
|
|
19
19
|
handle(event: Event, data?: unknown): void;
|
|
20
20
|
/**
|
|
21
21
|
* A String value that is used in the creation of the default string
|
|
22
22
|
* description of an object. Called by the built-in method {@link Object.prototype.toString}.
|
|
23
23
|
*
|
|
24
|
-
* @returns
|
|
24
|
+
* @returns The default string description of this object.
|
|
25
25
|
*/
|
|
26
26
|
get [Symbol.toStringTag](): string;
|
|
27
27
|
}
|
|
@@ -31,77 +31,72 @@ declare class Subscription {
|
|
|
31
31
|
private readonly _eventName;
|
|
32
32
|
private readonly _contextEventHandler;
|
|
33
33
|
/**
|
|
34
|
-
* @param
|
|
35
|
-
* @param
|
|
34
|
+
* @param eventName The event name.
|
|
35
|
+
* @param contextEventHandler The context event handler.
|
|
36
36
|
*/
|
|
37
37
|
constructor(eventName: string, contextEventHandler: ContextEventHandler);
|
|
38
38
|
/**
|
|
39
39
|
* Gets the event name for the subscription.
|
|
40
40
|
*
|
|
41
|
-
* @returns
|
|
41
|
+
* @returns The event name.
|
|
42
42
|
*/
|
|
43
43
|
get eventName(): string;
|
|
44
44
|
/**
|
|
45
45
|
* Gets the context event handler.
|
|
46
46
|
*
|
|
47
|
-
* @returns
|
|
47
|
+
* @returns The context event handler
|
|
48
48
|
*/
|
|
49
49
|
get contextEventHandler(): ContextEventHandler;
|
|
50
50
|
/**
|
|
51
51
|
* A String value that is used in the creation of the default string
|
|
52
52
|
* description of an object. Called by the built-in method {@link Object.prototype.toString}.
|
|
53
53
|
*
|
|
54
|
-
* @returns
|
|
54
|
+
* @returns The default string description of this object.
|
|
55
55
|
*/
|
|
56
56
|
get [Symbol.toStringTag](): string;
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
/**
|
|
60
|
-
* A class that allows objects to subscribe to events and be notified when the event is published.
|
|
61
|
-
*
|
|
62
|
-
* @class
|
|
63
|
-
* @exports Subscribr
|
|
64
|
-
*/
|
|
59
|
+
/** A class that allows objects to subscribe to events and be notified when the event is published. */
|
|
65
60
|
declare class Subscribr {
|
|
66
61
|
private readonly subscribers;
|
|
67
62
|
constructor();
|
|
68
63
|
/**
|
|
69
64
|
* Subscribe to an event
|
|
70
65
|
*
|
|
71
|
-
* @param
|
|
72
|
-
* @param
|
|
73
|
-
* @param
|
|
74
|
-
* @returns
|
|
66
|
+
* @param eventName The event name to subscribe to.
|
|
67
|
+
* @param eventHandler The event handler to call when the event is published.
|
|
68
|
+
* @param context The context to bind to the event handler.
|
|
69
|
+
* @returns An object used to check if the subscription still exists and to unsubscribe from the event.
|
|
75
70
|
*/
|
|
76
71
|
subscribe(eventName: string, eventHandler: ContextEventListener, context?: unknown): Subscription;
|
|
77
72
|
/**
|
|
78
73
|
* Unsubscribe from the event
|
|
79
74
|
*
|
|
80
|
-
* @param
|
|
81
|
-
* @returns
|
|
75
|
+
* @param subscription The subscription to unsubscribe.
|
|
76
|
+
* @returns true if eventListener has been removed successfully. false if the value is not found or if the value is not an object.
|
|
82
77
|
*/
|
|
83
78
|
unsubscribe({ eventName, contextEventHandler }: Subscription): boolean;
|
|
84
79
|
/**
|
|
85
80
|
* Publish an event
|
|
86
81
|
*
|
|
87
82
|
* @template T
|
|
88
|
-
* @param
|
|
89
|
-
* @param
|
|
90
|
-
* @param
|
|
83
|
+
* @param eventName The name of the event.
|
|
84
|
+
* @param event The event to be handled.
|
|
85
|
+
* @param data The value to be passed to the event handler as a parameter.
|
|
91
86
|
*/
|
|
92
87
|
publish<T>(eventName: string, event?: Event, data?: T): void;
|
|
93
88
|
/**
|
|
94
89
|
* Check if the event and handler are subscribed.
|
|
95
90
|
*
|
|
96
|
-
* @param
|
|
97
|
-
* @returns
|
|
91
|
+
* @param subscription The subscription object.
|
|
92
|
+
* @returns true if the event name and handler are subscribed, false otherwise.
|
|
98
93
|
*/
|
|
99
94
|
isSubscribed({ eventName, contextEventHandler }: Subscription): boolean;
|
|
100
95
|
/**
|
|
101
96
|
* A String value that is used in the creation of the default string
|
|
102
97
|
* description of an object. Called by the built-in method {@link Object.prototype.toString}.
|
|
103
98
|
*
|
|
104
|
-
* @returns
|
|
99
|
+
* @returns The default string description of this object.
|
|
105
100
|
*/
|
|
106
101
|
get [Symbol.toStringTag](): string;
|
|
107
102
|
}
|
package/dist/subscribr.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../node_modules/.pnpm/@d1g1tal+collections@2.0.
|
|
4
|
-
"sourcesContent": ["/** A {@link Map} that can contain multiple, unique, values for the same key. */\nexport class SetMultiMap<K, V> extends Map<K, Set<V>>{\n\t/**\n\t * Adds a new element with a specified key and value to the SetMultiMap.\n\t * If an element with the same key already exists, the value will be added to the underlying {@link Set}.\n\t * If the value already exists in the {@link Set}, it will not be added again.\n\t *\n\t * @param {K} key The key to set.\n\t * @param {V} value The value to add to the SetMultiMap\n\t * @returns {SetMultiMap<K, V>} The SetMultiMap with the updated key and value.\n\t */\n\t// @ts-expect-error I am overriding the set method from the Map class\n\toverride set(key: K, value: V): SetMultiMap<K, V> {\n\t\tsuper.set(key, (super.get(key) ?? new Set()).add(value));\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Checks if a specific key has a specific value.\n\t *\n\t * @param {K} key The key to check.\n\t * @param {V} value The value to check.\n\t * @returns {boolean} True if the key has the value, false otherwise.\n\t */\n\thasValue(key: K, value: V): boolean {\n\t\tconst values = super.get(key);\n\n\t\treturn values ? values.has(value) : false;\n\t}\n\n\tfind(key: K, iterator: (value: V) => boolean): V | undefined {\n\t\tconst values = this.get(key);\n\n\t\tif (values !== undefined) {\n\t\t\treturn Array.from(values).find(iterator);\n\t\t}\n\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Removes a specific value from a specific key.\n\t *\n\t * @param {K} key The key to remove the value from.\n\t * @param {V | undefined} value The value to remove.\n\t * @returns {boolean} True if the value was removed, false otherwise.\n\t */\n\tdeleteValue(key: K, value: V | undefined): boolean {\n\t\tif (value === undefined) { return this.delete(key) }\n\n\t\tconst values = super.get(key);\n\t\tif (values) {\n\t\t\tconst deleted = values.delete(value);\n\n\t\t\tif (values.size === 0) {\n\t\t\t\tsuper.delete(key);\n\t\t\t}\n\n\t\t\treturn deleted;\n\t\t}\n\n\t\treturn false;\n\t}\n\n\toverride get [Symbol.toStringTag](): string {\n\t\treturn 'SetMultiMap';\n\t}\n}", "import type { ContextEventListener } from './@types';\n\n/** A wrapper for an event handler that binds a context to the event handler. */\nexport class ContextEventHandler {\n\tprivate readonly context: unknown;\n\tprivate readonly eventListener: ContextEventListener;\n\n\t/**\n\t * @param
|
|
5
|
-
"mappings": "AACO,IAAMA,EAAN,cAAgC,GAAc,CAW3C,IAAIC,EAAQC,EAA6B,CACjD,aAAM,IAAID,GAAM,MAAM,IAAIA,CAAG,GAAK,IAAI,KAAO,IAAIC,CAAK,CAAC,EAEhD,IACR,CASA,SAASD,EAAQC,EAAmB,CACnC,IAAMC,EAAS,MAAM,IAAIF,CAAG,EAE5B,OAAOE,EAASA,EAAO,IAAID,CAAK,EAAI,EACrC,CAEA,KAAKD,EAAQG,EAAgD,CAC5D,IAAMD,EAAS,KAAK,IAAIF,CAAG,EAE3B,GAAIE,IAAW,OACd,OAAO,MAAM,KAAKA,CAAM,EAAE,KAAKC,CAAQ,CAIzC,CASA,YAAYH,EAAQC,EAA+B,CAClD,GAAIA,IAAU,OAAa,OAAO,KAAK,OAAOD,CAAG,EAEjD,IAAME,EAAS,MAAM,IAAIF,CAAG,EAC5B,GAAIE,EAAQ,CACX,IAAME,EAAUF,EAAO,OAAOD,CAAK,EAEnC,OAAIC,EAAO,OAAS,GACnB,MAAM,OAAOF,CAAG,EAGVI,CACR,CAEA,MAAO,EACR,CAEA,IAAc,OAAO,WAAW,GAAY,CAC3C,MAAO,aACR,CACD,ECjEO,IAAMC,EAAN,KAA0B,CACf,QACA,cAMjB,YAAYC,EAAkBC,EAAqC,CAClE,KAAK,QAAUD,EACf,KAAK,cAAgBC,CACtB,CAQA,OAAOC,EAAcC,EAAsB,CAC1C,KAAK,cAAc,KAAK,KAAK,QAASD,EAAOC,CAAI,CAClD,CAQA,IAAK,OAAO,WAAW,GAAY,CAClC,MAAO,qBACR,CACD,EChCO,IAAMC,EAAN,KAAmB,CACR,WACA,qBAMjB,YAAYC,EAAmBC,EAA0C,CACxE,KAAK,WAAaD,EAClB,KAAK,qBAAuBC,CAC7B,CAOA,IAAI,WAAoB,CACvB,OAAO,KAAK,UACb,CAOA,IAAI,qBAA2C,CAC9C,OAAO,KAAK,oBACb,CAQA,IAAK,OAAO,WAAW,GAAY,CAClC,MAAO,cACR,CACD,
|
|
3
|
+
"sources": ["../node_modules/.pnpm/@d1g1tal+collections@2.0.3_typescript@5.9.2/node_modules/@d1g1tal/collections/src/set-multi-map.ts", "../src/context-event-handler.ts", "../src/subscription.ts", "../src/subscribr.ts"],
|
|
4
|
+
"sourcesContent": ["/** A {@link Map} that can contain multiple, unique, values for the same key. */\nexport class SetMultiMap<K, V> extends Map<K, Set<V>>{\n\t/**\n\t * Adds a new element with a specified key and value to the SetMultiMap.\n\t * If an element with the same key already exists, the value will be added to the underlying {@link Set}.\n\t * If the value already exists in the {@link Set}, it will not be added again.\n\t *\n\t * @param {K} key The key to set.\n\t * @param {V} value The value to add to the SetMultiMap\n\t * @returns {SetMultiMap<K, V>} The SetMultiMap with the updated key and value.\n\t */\n\t// @ts-expect-error I am overriding the set method from the Map class\n\toverride set(key: K, value: V): SetMultiMap<K, V> {\n\t\tsuper.set(key, (super.get(key) ?? new Set()).add(value));\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Checks if a specific key has a specific value.\n\t *\n\t * @param {K} key The key to check.\n\t * @param {V} value The value to check.\n\t * @returns {boolean} True if the key has the value, false otherwise.\n\t */\n\thasValue(key: K, value: V): boolean {\n\t\tconst values = super.get(key);\n\n\t\treturn values ? values.has(value) : false;\n\t}\n\n\tfind(key: K, iterator: (value: V) => boolean): V | undefined {\n\t\tconst values = this.get(key);\n\n\t\tif (values !== undefined) {\n\t\t\treturn Array.from(values).find(iterator);\n\t\t}\n\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Removes a specific value from a specific key.\n\t *\n\t * @param {K} key The key to remove the value from.\n\t * @param {V | undefined} value The value to remove.\n\t * @returns {boolean} True if the value was removed, false otherwise.\n\t */\n\tdeleteValue(key: K, value: V | undefined): boolean {\n\t\tif (value === undefined) { return this.delete(key) }\n\n\t\tconst values = super.get(key);\n\t\tif (values) {\n\t\t\tconst deleted = values.delete(value);\n\n\t\t\tif (values.size === 0) {\n\t\t\t\tsuper.delete(key);\n\t\t\t}\n\n\t\t\treturn deleted;\n\t\t}\n\n\t\treturn false;\n\t}\n\n\toverride get [Symbol.toStringTag](): string {\n\t\treturn 'SetMultiMap';\n\t}\n}", "import type { ContextEventListener } from './@types';\n\n/** A wrapper for an event handler that binds a context to the event handler. */\nexport class ContextEventHandler {\n\tprivate readonly context: unknown;\n\tprivate readonly eventListener: ContextEventListener;\n\n\t/**\n\t * @param context The context to bind to the event handler.\n\t * @param eventListener The event handler to call when the event is published.\n\t */\n\tconstructor(context: unknown, eventListener: ContextEventListener) {\n\t\tthis.context = context;\n\t\tthis.eventListener = eventListener;\n\t}\n\n\t/**\n\t * Call the event handler for the provided event.\n\t *\n\t * @param 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: Event, data?: unknown): void {\n\t\tthis.eventListener.call(this.context, event, data);\n\t}\n\n\t/**\n\t * A String value that is used in the creation of the default string\n\t * description of an object. Called by the built-in method {@link Object.prototype.toString}.\n\t *\n\t * @returns The default string description of this object.\n\t */\n\tget [Symbol.toStringTag](): string {\n\t\treturn 'ContextEventHandler';\n\t}\n}", "import type { ContextEventHandler } from './context-event-handler';\n\n/** Represents a subscription to an event. */\nexport class Subscription {\n\tprivate readonly _eventName: string;\n\tprivate readonly _contextEventHandler: ContextEventHandler;\n\n\t/**\n\t * @param eventName The event name.\n\t * @param contextEventHandler The context event handler.\n\t */\n\tconstructor(eventName: string, contextEventHandler: 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 The event name.\n\t */\n\tget eventName(): string {\n\t\treturn this._eventName;\n\t}\n\n\t/**\n\t * Gets the context event handler.\n\t *\n\t * @returns The context event handler\n\t */\n\tget contextEventHandler(): ContextEventHandler {\n\t\treturn this._contextEventHandler;\n\t}\n\n\t/**\n\t * A String value that is used in the creation of the default string\n\t * description of an object. Called by the built-in method {@link Object.prototype.toString}.\n\t *\n\t * @returns The default string description of this object.\n\t */\n\tget [Symbol.toStringTag](): string {\n\t\treturn 'Subscription';\n\t}\n}", "import { SetMultiMap } from '@d1g1tal/collections/src';\nimport { ContextEventHandler } from './context-event-handler';\nimport { Subscription } from './subscription';\nimport type { ContextEventListener } from './@types';\n\n/** A class that allows objects to subscribe to events and be notified when the event is published. */\nexport class Subscribr {\n\tprivate readonly subscribers: SetMultiMap<string, ContextEventHandler>;\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 eventName The event name to subscribe to.\n\t * @param 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 An object used to check if the subscription still exists and to unsubscribe from the event.\n\t */\n\tsubscribe(eventName: string, eventHandler: ContextEventListener, context: unknown = eventHandler): Subscription {\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 The subscription to unsubscribe.\n\t * @returns 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 }: Subscription): boolean {\n\t\tconst contextEventHandlers = this.subscribers.get(eventName) ?? new Set();\n\t\tconst removed = contextEventHandlers.delete(contextEventHandler);\n\n\t\tif (removed && contextEventHandlers.size === 0) {\tthis.subscribers.delete(eventName) }\n\n\t\treturn removed;\n\t}\n\n\t/**\n\t * Publish an event\n\t *\n\t * @template T\n\t * @param eventName The name of the event.\n\t * @param 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<T>(eventName: string, event: Event = new CustomEvent(eventName), data?: T): void {\n\t\tthis.subscribers.get(eventName)?.forEach((contextEventHandler: 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 The subscription object.\n\t * @returns true if the event name and handler are subscribed, false otherwise.\n\t */\n\tisSubscribed({ eventName, contextEventHandler }: Subscription): boolean {\n\t\treturn this.subscribers.get(eventName)?.has(contextEventHandler) ?? false;\n\t}\n\n\t/**\n\t * A String value that is used in the creation of the default string\n\t * description of an object. Called by the built-in method {@link Object.prototype.toString}.\n\t *\n\t * @returns The default string description of this object.\n\t */\n\tget [Symbol.toStringTag](): string {\n\t\treturn 'Subscribr';\n\t}\n}"],
|
|
5
|
+
"mappings": "AACO,IAAMA,EAAN,cAAgC,GAAc,CAW3C,IAAIC,EAAQC,EAA6B,CACjD,aAAM,IAAID,GAAM,MAAM,IAAIA,CAAG,GAAK,IAAI,KAAO,IAAIC,CAAK,CAAC,EAEhD,IACR,CASA,SAASD,EAAQC,EAAmB,CACnC,IAAMC,EAAS,MAAM,IAAIF,CAAG,EAE5B,OAAOE,EAASA,EAAO,IAAID,CAAK,EAAI,EACrC,CAEA,KAAKD,EAAQG,EAAgD,CAC5D,IAAMD,EAAS,KAAK,IAAIF,CAAG,EAE3B,GAAIE,IAAW,OACd,OAAO,MAAM,KAAKA,CAAM,EAAE,KAAKC,CAAQ,CAIzC,CASA,YAAYH,EAAQC,EAA+B,CAClD,GAAIA,IAAU,OAAa,OAAO,KAAK,OAAOD,CAAG,EAEjD,IAAME,EAAS,MAAM,IAAIF,CAAG,EAC5B,GAAIE,EAAQ,CACX,IAAME,EAAUF,EAAO,OAAOD,CAAK,EAEnC,OAAIC,EAAO,OAAS,GACnB,MAAM,OAAOF,CAAG,EAGVI,CACR,CAEA,MAAO,EACR,CAEA,IAAc,OAAO,WAAW,GAAY,CAC3C,MAAO,aACR,CACD,ECjEO,IAAMC,EAAN,KAA0B,CACf,QACA,cAMjB,YAAYC,EAAkBC,EAAqC,CAClE,KAAK,QAAUD,EACf,KAAK,cAAgBC,CACtB,CAQA,OAAOC,EAAcC,EAAsB,CAC1C,KAAK,cAAc,KAAK,KAAK,QAASD,EAAOC,CAAI,CAClD,CAQA,IAAK,OAAO,WAAW,GAAY,CAClC,MAAO,qBACR,CACD,EChCO,IAAMC,EAAN,KAAmB,CACR,WACA,qBAMjB,YAAYC,EAAmBC,EAA0C,CACxE,KAAK,WAAaD,EAClB,KAAK,qBAAuBC,CAC7B,CAOA,IAAI,WAAoB,CACvB,OAAO,KAAK,UACb,CAOA,IAAI,qBAA2C,CAC9C,OAAO,KAAK,oBACb,CAQA,IAAK,OAAO,WAAW,GAAY,CAClC,MAAO,cACR,CACD,ECrCO,IAAMC,EAAN,KAAgB,CACL,YAEjB,aAAc,CACb,KAAK,YAAc,IAAIC,CACxB,CAUA,UAAUC,EAAmBC,EAAoCC,EAAmBD,EAA4B,CAC/G,IAAME,EAAsB,IAAIC,EAAoBF,EAASD,CAAY,EACzE,YAAK,YAAY,IAAID,EAAWG,CAAmB,EAE5C,IAAIE,EAAaL,EAAWG,CAAmB,CACvD,CAQA,YAAY,CAAE,UAAAH,EAAW,oBAAAG,CAAoB,EAA0B,CACtE,IAAMG,EAAuB,KAAK,YAAY,IAAIN,CAAS,GAAK,IAAI,IAC9DO,EAAUD,EAAqB,OAAOH,CAAmB,EAE/D,OAAII,GAAWD,EAAqB,OAAS,GAAK,KAAK,YAAY,OAAON,CAAS,EAE5EO,CACR,CAUA,QAAWP,EAAmBQ,EAAe,IAAI,YAAYR,CAAS,EAAGS,EAAgB,CACxF,KAAK,YAAY,IAAIT,CAAS,GAAG,QAASG,GAA6CA,EAAoB,OAAOK,EAAOC,CAAI,CAAC,CAC/H,CAQA,aAAa,CAAE,UAAAT,EAAW,oBAAAG,CAAoB,EAA0B,CACvE,OAAO,KAAK,YAAY,IAAIH,CAAS,GAAG,IAAIG,CAAmB,GAAK,EACrE,CAQA,IAAK,OAAO,WAAW,GAAY,CAClC,MAAO,WACR,CACD",
|
|
6
6
|
"names": ["SetMultiMap", "key", "value", "values", "iterator", "deleted", "ContextEventHandler", "context", "eventListener", "event", "data", "Subscription", "eventName", "contextEventHandler", "Subscribr", "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": "4.0.
|
|
3
|
+
"version": "4.0.3",
|
|
4
4
|
"description": "JavaScript Publish/Subscribe Library",
|
|
5
5
|
"author": "Jason DiMeo",
|
|
6
6
|
"license": "ISC",
|
|
@@ -36,32 +36,25 @@
|
|
|
36
36
|
"url": "git+https://github.com/D1g1talEntr0py/subscribr.git"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@d1g1tal/collections": "^2.0.
|
|
39
|
+
"@d1g1tal/collections": "^2.0.3"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@d1g1tal/chrysalis": "^2.5.0",
|
|
43
|
-
"@eslint/compat": "^1.3.
|
|
44
|
-
"@eslint/js": "^9.
|
|
45
|
-
"@types/node": "^24.
|
|
46
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
47
|
-
"@typescript-eslint/parser": "^8.
|
|
43
|
+
"@eslint/compat": "^1.3.2",
|
|
44
|
+
"@eslint/js": "^9.34.0",
|
|
45
|
+
"@types/node": "^24.3.1",
|
|
46
|
+
"@typescript-eslint/eslint-plugin": "^8.42.0",
|
|
47
|
+
"@typescript-eslint/parser": "^8.42.0",
|
|
48
48
|
"@vitest/coverage-v8": "^3.2.4",
|
|
49
|
-
"eslint": "^9.
|
|
49
|
+
"eslint": "^9.34.0",
|
|
50
50
|
"eslint-plugin-compat": "^6.0.2",
|
|
51
|
-
"eslint-plugin-jsdoc": "^
|
|
52
|
-
"
|
|
53
|
-
"typescript": "^5.8.3",
|
|
54
|
-
"typescript-eslint": "^8.37.0",
|
|
51
|
+
"eslint-plugin-jsdoc": "^54.3.1",
|
|
52
|
+
"typescript-eslint": "^8.42.0",
|
|
55
53
|
"vitest": "^3.2.4"
|
|
56
54
|
},
|
|
57
55
|
"peerDependencies": {
|
|
58
56
|
"typescript": "^5.0.0"
|
|
59
57
|
},
|
|
60
|
-
"peerDependenciesMeta": {
|
|
61
|
-
"typescript": {
|
|
62
|
-
"optional": true
|
|
63
|
-
}
|
|
64
|
-
},
|
|
65
58
|
"engines": {
|
|
66
59
|
"node": ">=20.15.1"
|
|
67
60
|
},
|
|
@@ -74,7 +67,8 @@
|
|
|
74
67
|
"build:watch": "tsbuild --watch",
|
|
75
68
|
"type-check": "tsbuild --typeCheck",
|
|
76
69
|
"lint": "eslint ./src",
|
|
77
|
-
"test": "vitest run
|
|
70
|
+
"test": "vitest run",
|
|
71
|
+
"test:coverage": "vitest run --coverage",
|
|
78
72
|
"test:watch": "vitest",
|
|
79
73
|
"prepublish": "pnpm lint && pnpm test && pnpm -s build"
|
|
80
74
|
}
|
|
@@ -6,8 +6,8 @@ export class ContextEventHandler {
|
|
|
6
6
|
private readonly eventListener: ContextEventListener;
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
* @param
|
|
10
|
-
* @param
|
|
9
|
+
* @param context The context to bind to the event handler.
|
|
10
|
+
* @param eventListener The event handler to call when the event is published.
|
|
11
11
|
*/
|
|
12
12
|
constructor(context: unknown, eventListener: ContextEventListener) {
|
|
13
13
|
this.context = context;
|
|
@@ -17,8 +17,8 @@ export class ContextEventHandler {
|
|
|
17
17
|
/**
|
|
18
18
|
* Call the event handler for the provided event.
|
|
19
19
|
*
|
|
20
|
-
* @param
|
|
21
|
-
* @param
|
|
20
|
+
* @param event The event to handle
|
|
21
|
+
* @param data The value to be passed to the event handler as a parameter.
|
|
22
22
|
*/
|
|
23
23
|
handle(event: Event, data?: unknown): void {
|
|
24
24
|
this.eventListener.call(this.context, event, data);
|
|
@@ -28,7 +28,7 @@ export class ContextEventHandler {
|
|
|
28
28
|
* A String value that is used in the creation of the default string
|
|
29
29
|
* description of an object. Called by the built-in method {@link Object.prototype.toString}.
|
|
30
30
|
*
|
|
31
|
-
* @returns
|
|
31
|
+
* @returns The default string description of this object.
|
|
32
32
|
*/
|
|
33
33
|
get [Symbol.toStringTag](): string {
|
|
34
34
|
return 'ContextEventHandler';
|
package/src/subscribr.ts
CHANGED
|
@@ -3,12 +3,7 @@ import { ContextEventHandler } from './context-event-handler';
|
|
|
3
3
|
import { Subscription } from './subscription';
|
|
4
4
|
import type { ContextEventListener } from './@types';
|
|
5
5
|
|
|
6
|
-
/**
|
|
7
|
-
* A class that allows objects to subscribe to events and be notified when the event is published.
|
|
8
|
-
*
|
|
9
|
-
* @class
|
|
10
|
-
* @exports Subscribr
|
|
11
|
-
*/
|
|
6
|
+
/** A class that allows objects to subscribe to events and be notified when the event is published. */
|
|
12
7
|
export class Subscribr {
|
|
13
8
|
private readonly subscribers: SetMultiMap<string, ContextEventHandler>;
|
|
14
9
|
|
|
@@ -19,10 +14,10 @@ export class Subscribr {
|
|
|
19
14
|
/**
|
|
20
15
|
* Subscribe to an event
|
|
21
16
|
*
|
|
22
|
-
* @param
|
|
23
|
-
* @param
|
|
24
|
-
* @param
|
|
25
|
-
* @returns
|
|
17
|
+
* @param eventName The event name to subscribe to.
|
|
18
|
+
* @param eventHandler The event handler to call when the event is published.
|
|
19
|
+
* @param context The context to bind to the event handler.
|
|
20
|
+
* @returns An object used to check if the subscription still exists and to unsubscribe from the event.
|
|
26
21
|
*/
|
|
27
22
|
subscribe(eventName: string, eventHandler: ContextEventListener, context: unknown = eventHandler): Subscription {
|
|
28
23
|
const contextEventHandler = new ContextEventHandler(context, eventHandler);
|
|
@@ -34,8 +29,8 @@ export class Subscribr {
|
|
|
34
29
|
/**
|
|
35
30
|
* Unsubscribe from the event
|
|
36
31
|
*
|
|
37
|
-
* @param
|
|
38
|
-
* @returns
|
|
32
|
+
* @param subscription The subscription to unsubscribe.
|
|
33
|
+
* @returns true if eventListener has been removed successfully. false if the value is not found or if the value is not an object.
|
|
39
34
|
*/
|
|
40
35
|
unsubscribe({ eventName, contextEventHandler }: Subscription): boolean {
|
|
41
36
|
const contextEventHandlers = this.subscribers.get(eventName) ?? new Set();
|
|
@@ -50,9 +45,9 @@ export class Subscribr {
|
|
|
50
45
|
* Publish an event
|
|
51
46
|
*
|
|
52
47
|
* @template T
|
|
53
|
-
* @param
|
|
54
|
-
* @param
|
|
55
|
-
* @param
|
|
48
|
+
* @param eventName The name of the event.
|
|
49
|
+
* @param event The event to be handled.
|
|
50
|
+
* @param data The value to be passed to the event handler as a parameter.
|
|
56
51
|
*/
|
|
57
52
|
publish<T>(eventName: string, event: Event = new CustomEvent(eventName), data?: T): void {
|
|
58
53
|
this.subscribers.get(eventName)?.forEach((contextEventHandler: ContextEventHandler) => contextEventHandler.handle(event, data));
|
|
@@ -61,8 +56,8 @@ export class Subscribr {
|
|
|
61
56
|
/**
|
|
62
57
|
* Check if the event and handler are subscribed.
|
|
63
58
|
*
|
|
64
|
-
* @param
|
|
65
|
-
* @returns
|
|
59
|
+
* @param subscription The subscription object.
|
|
60
|
+
* @returns true if the event name and handler are subscribed, false otherwise.
|
|
66
61
|
*/
|
|
67
62
|
isSubscribed({ eventName, contextEventHandler }: Subscription): boolean {
|
|
68
63
|
return this.subscribers.get(eventName)?.has(contextEventHandler) ?? false;
|
|
@@ -72,7 +67,7 @@ export class Subscribr {
|
|
|
72
67
|
* A String value that is used in the creation of the default string
|
|
73
68
|
* description of an object. Called by the built-in method {@link Object.prototype.toString}.
|
|
74
69
|
*
|
|
75
|
-
* @returns
|
|
70
|
+
* @returns The default string description of this object.
|
|
76
71
|
*/
|
|
77
72
|
get [Symbol.toStringTag](): string {
|
|
78
73
|
return 'Subscribr';
|
package/src/subscription.ts
CHANGED
|
@@ -6,8 +6,8 @@ export class Subscription {
|
|
|
6
6
|
private readonly _contextEventHandler: ContextEventHandler;
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
* @param
|
|
10
|
-
* @param
|
|
9
|
+
* @param eventName The event name.
|
|
10
|
+
* @param contextEventHandler The context event handler.
|
|
11
11
|
*/
|
|
12
12
|
constructor(eventName: string, contextEventHandler: ContextEventHandler) {
|
|
13
13
|
this._eventName = eventName;
|
|
@@ -17,7 +17,7 @@ export class Subscription {
|
|
|
17
17
|
/**
|
|
18
18
|
* Gets the event name for the subscription.
|
|
19
19
|
*
|
|
20
|
-
* @returns
|
|
20
|
+
* @returns The event name.
|
|
21
21
|
*/
|
|
22
22
|
get eventName(): string {
|
|
23
23
|
return this._eventName;
|
|
@@ -26,7 +26,7 @@ export class Subscription {
|
|
|
26
26
|
/**
|
|
27
27
|
* Gets the context event handler.
|
|
28
28
|
*
|
|
29
|
-
* @returns
|
|
29
|
+
* @returns The context event handler
|
|
30
30
|
*/
|
|
31
31
|
get contextEventHandler(): ContextEventHandler {
|
|
32
32
|
return this._contextEventHandler;
|
|
@@ -36,7 +36,7 @@ export class Subscription {
|
|
|
36
36
|
* A String value that is used in the creation of the default string
|
|
37
37
|
* description of an object. Called by the built-in method {@link Object.prototype.toString}.
|
|
38
38
|
*
|
|
39
|
-
* @returns
|
|
39
|
+
* @returns The default string description of this object.
|
|
40
40
|
*/
|
|
41
41
|
get [Symbol.toStringTag](): string {
|
|
42
42
|
return 'Subscription';
|