@d1g1tal/subscribr 2.0.2 → 3.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/README.md +39 -7
- package/dist/browser/subscribr.js +23 -170
- package/dist/browser/subscribr.min.js +1 -1
- package/dist/browser/subscribr.min.js.map +4 -4
- package/dist/browser.zip +0 -0
- package/dist/esm/subscribr.js +23 -170
- package/dist/esm/subscribr.min.js +1 -1
- package/dist/esm/subscribr.min.js.map +4 -4
- package/dist/esm.zip +0 -0
- package/package.json +8 -7
- package/src/context-event-handler.d.ts +13 -3
- package/src/context-event-handler.js +8 -7
- package/src/subscribr.d.ts +15 -5
- package/src/subscribr.js +33 -27
- package/src/subscription.d.ts +9 -20
- package/src/subscription.js +12 -27
package/README.md
CHANGED
|
@@ -3,27 +3,59 @@ JavaScript Publish/Subscribe
|
|
|
3
3
|
|
|
4
4
|
This simple class enables you to subscribe and publish events using the Observer pattern.
|
|
5
5
|
|
|
6
|
+
Basically, you subscribe your event handler using the `Subscribr` instance and are returned a `Subscription` instance.
|
|
7
|
+
|
|
8
|
+
When the event is published, your event handler will be called.
|
|
9
|
+
|
|
6
10
|
### Installation:
|
|
7
11
|
```bash
|
|
8
12
|
npm install @d1g1tal/subscribr --save
|
|
9
13
|
```
|
|
14
|
+
Or Script tags from downloaded script or from a NPM CDN like jsDelivr
|
|
15
|
+
|
|
16
|
+
```html
|
|
17
|
+
<!-- Load as global script -->
|
|
18
|
+
<script src="/app/js/subscribr.min.js"></script>
|
|
19
|
+
|
|
20
|
+
<!-- Load from CDN -->
|
|
21
|
+
<script src="https://cdn.jsdelivr.net/npm/@d1g1tal/subscribr@2/dist/browser/subscribr.min.js"></script>
|
|
22
|
+
```
|
|
10
23
|
|
|
11
24
|
### Usage:
|
|
12
25
|
```javascript
|
|
26
|
+
// ES Module
|
|
27
|
+
import Subscribr from '@d1g1tal/subscribr';
|
|
28
|
+
|
|
13
29
|
const subscribr = new Subscribr();
|
|
14
30
|
const eventName = 'myEvent';
|
|
15
|
-
const eventHandler = (event) => console.log(`Event: '${event.type}' published`);
|
|
16
31
|
|
|
17
32
|
// Subscribr.prototype.subscribe returns a Subscription object.
|
|
18
|
-
const
|
|
33
|
+
const mySubscription = subscribr.subscribe(eventName, (event, data) => console.log(`Event: '${event.type}' published with data: ${data}`));
|
|
34
|
+
|
|
35
|
+
// Publish the event with just the name and a CustomEvent will be created with the eventName. All handlers that have subscribed are called
|
|
36
|
+
subscribr.publish(eventName, { foo: 'bar', zip: 'fizz' }); // Event: 'myEvent' published with data: { foo: 'bar', zip: 'fizz' }
|
|
37
|
+
|
|
38
|
+
// Is the event subscribed?
|
|
39
|
+
const isSubscribed = subscribr.isSubscribed(mySubscription); // true
|
|
40
|
+
|
|
41
|
+
const isUnsubscribed = subscribr.unsubscribe(mySubscription); // true
|
|
42
|
+
|
|
43
|
+
const isSubscribed = subscribr.isSubscribed(mySubscription); // false
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
// Subscribe to a DOM event
|
|
47
|
+
const eventTargetSubscription = subsribr.subscribe('eventTargetChanged', (event, data) => console.log(`Event target changed with data: ${data}`));
|
|
19
48
|
|
|
20
|
-
//
|
|
21
|
-
|
|
49
|
+
// Add some event to an event target (DOMElement)
|
|
50
|
+
new EventTarget().addEventListener('change', function(event) {
|
|
51
|
+
// Publish the event and all handlers that have subscribed are called
|
|
52
|
+
subscribr.publish('eventTargetChanged', event, { value: this.value }); // Event target changed with data: {value: 'new value'}
|
|
53
|
+
});
|
|
22
54
|
|
|
23
55
|
// Is the event subscribed?
|
|
24
|
-
const isSubscribed = subscribr.isSubscribed(
|
|
56
|
+
const isSubscribed = subscribr.isSubscribed(eventTargetSubscription); // true
|
|
25
57
|
|
|
26
|
-
const isUnsubscribed =
|
|
58
|
+
const isUnsubscribed = subscribr.unsubscribe(eventTargetSubscription); // true
|
|
27
59
|
|
|
28
|
-
const isSubscribed = subscribr.isSubscribed(
|
|
60
|
+
const isSubscribed = subscribr.isSubscribed(eventTargetSubscription); // false
|
|
29
61
|
```
|
|
@@ -23,118 +23,6 @@ var Subscribr = (() => {
|
|
|
23
23
|
default: () => Subscribr
|
|
24
24
|
});
|
|
25
25
|
|
|
26
|
-
// node_modules/@d1g1tal/collections/src/list.js
|
|
27
|
-
var List = class {
|
|
28
|
-
#array;
|
|
29
|
-
constructor(iterable = []) {
|
|
30
|
-
this.#array = Array.of(...iterable);
|
|
31
|
-
}
|
|
32
|
-
static from(iterable, mapper, context) {
|
|
33
|
-
return new List(Array.from(iterable, mapper, context));
|
|
34
|
-
}
|
|
35
|
-
static withSize(size) {
|
|
36
|
-
return new List(new Array(size));
|
|
37
|
-
}
|
|
38
|
-
add(element) {
|
|
39
|
-
this.#array.push(element);
|
|
40
|
-
return this;
|
|
41
|
-
}
|
|
42
|
-
addAll(iterable) {
|
|
43
|
-
this.#array.push(...iterable);
|
|
44
|
-
return this;
|
|
45
|
-
}
|
|
46
|
-
clear() {
|
|
47
|
-
this.#array.length = 0;
|
|
48
|
-
}
|
|
49
|
-
concat(elements) {
|
|
50
|
-
return this(this.#array.concat(...elements));
|
|
51
|
-
}
|
|
52
|
-
every(predicate) {
|
|
53
|
-
return this.#array.every((element, index) => predicate(element, index, this));
|
|
54
|
-
}
|
|
55
|
-
some(predicate) {
|
|
56
|
-
return this.#array.some((element, index) => predicate(element, index, this));
|
|
57
|
-
}
|
|
58
|
-
filter(predicate) {
|
|
59
|
-
return new List(this.#array.filter((element, index) => predicate(element, index, this)));
|
|
60
|
-
}
|
|
61
|
-
find(predicate, context) {
|
|
62
|
-
return this.#array.find((element, index) => predicate(element, index, this), context);
|
|
63
|
-
}
|
|
64
|
-
findIndex(predicate, context) {
|
|
65
|
-
return this.#array.findIndex((element, index) => predicate(element, index, this), context);
|
|
66
|
-
}
|
|
67
|
-
map(mapper) {
|
|
68
|
-
return new List(this.#array.map((element, index) => mapper(element, index, this)));
|
|
69
|
-
}
|
|
70
|
-
reduce(reducer, initialValue) {
|
|
71
|
-
return this.#array.reduce(reducer, initialValue);
|
|
72
|
-
}
|
|
73
|
-
sort(comparator) {
|
|
74
|
-
this.#array.sort(comparator);
|
|
75
|
-
return this;
|
|
76
|
-
}
|
|
77
|
-
forEach(consumer) {
|
|
78
|
-
this.#array.forEach((element, index) => consumer(element, index, this));
|
|
79
|
-
}
|
|
80
|
-
get(element, fromIndex = 0) {
|
|
81
|
-
return this.#array.at(this.#array.indexOf(element, fromIndex));
|
|
82
|
-
}
|
|
83
|
-
has(element) {
|
|
84
|
-
return this.#array.includes(element);
|
|
85
|
-
}
|
|
86
|
-
insert(index, element) {
|
|
87
|
-
this.#array.splice(index, 0, element);
|
|
88
|
-
}
|
|
89
|
-
delete(element) {
|
|
90
|
-
return this.#array.splice(this.#array.indexOf(element), 1).length == 1;
|
|
91
|
-
}
|
|
92
|
-
deleteAt(index) {
|
|
93
|
-
return this.#array.splice(index, 1).length == 1;
|
|
94
|
-
}
|
|
95
|
-
keys() {
|
|
96
|
-
return this.#array.keys();
|
|
97
|
-
}
|
|
98
|
-
values() {
|
|
99
|
-
return this[Symbol.iterator]();
|
|
100
|
-
}
|
|
101
|
-
entries() {
|
|
102
|
-
return this.#array.entries();
|
|
103
|
-
}
|
|
104
|
-
isEmpty() {
|
|
105
|
-
return this.#array.length === 0;
|
|
106
|
-
}
|
|
107
|
-
toArray() {
|
|
108
|
-
return [...this.#array];
|
|
109
|
-
}
|
|
110
|
-
toString() {
|
|
111
|
-
return this.#array.toString();
|
|
112
|
-
}
|
|
113
|
-
valueOf() {
|
|
114
|
-
return this.#array.valueOf();
|
|
115
|
-
}
|
|
116
|
-
get size() {
|
|
117
|
-
return this.#array.length;
|
|
118
|
-
}
|
|
119
|
-
[Symbol.iterator]() {
|
|
120
|
-
return this.#array[Symbol.iterator]();
|
|
121
|
-
}
|
|
122
|
-
get [Symbol.toStringTag]() {
|
|
123
|
-
return "List";
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
// node_modules/@d1g1tal/collections/src/multi-map.js
|
|
128
|
-
var MultiMap = class extends Map {
|
|
129
|
-
set(key, value) {
|
|
130
|
-
super.set(key, (this.get(key) ?? new List()).add(value));
|
|
131
|
-
return this;
|
|
132
|
-
}
|
|
133
|
-
[Symbol.toStringTag]() {
|
|
134
|
-
return "MultiMap";
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
|
|
138
26
|
// node_modules/@d1g1tal/collections/src/set-multi-map.js
|
|
139
27
|
var SetMultiMap = class extends Map {
|
|
140
28
|
set(key, value) {
|
|
@@ -146,36 +34,6 @@ var Subscribr = (() => {
|
|
|
146
34
|
}
|
|
147
35
|
};
|
|
148
36
|
|
|
149
|
-
// node_modules/@d1g1tal/collections/src/node.js
|
|
150
|
-
var Node = class {
|
|
151
|
-
#value;
|
|
152
|
-
#next;
|
|
153
|
-
constructor(value) {
|
|
154
|
-
this.#value = value;
|
|
155
|
-
this.#next = null;
|
|
156
|
-
}
|
|
157
|
-
get value() {
|
|
158
|
-
return this.#value;
|
|
159
|
-
}
|
|
160
|
-
get next() {
|
|
161
|
-
return this.#next;
|
|
162
|
-
}
|
|
163
|
-
get [Symbol.toStringTag]() {
|
|
164
|
-
return "Node";
|
|
165
|
-
}
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
// node_modules/@d1g1tal/collections/src/weak-set-multi-map.js
|
|
169
|
-
var WeakSetMultiMap = class extends Map {
|
|
170
|
-
set(key, value) {
|
|
171
|
-
super.set(key, (super.get(key) ?? /* @__PURE__ */ new WeakSet()).add(value));
|
|
172
|
-
return this;
|
|
173
|
-
}
|
|
174
|
-
[Symbol.toStringTag]() {
|
|
175
|
-
return "WeakSetMultiMap";
|
|
176
|
-
}
|
|
177
|
-
};
|
|
178
|
-
|
|
179
37
|
// src/context-event-handler.js
|
|
180
38
|
var ContextEventHandler = class {
|
|
181
39
|
#context;
|
|
@@ -184,11 +42,8 @@ var Subscribr = (() => {
|
|
|
184
42
|
this.#context = context;
|
|
185
43
|
this.#eventHandler = eventHandler;
|
|
186
44
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
}
|
|
190
|
-
get eventHandler() {
|
|
191
|
-
return this.#eventHandler;
|
|
45
|
+
handle(event, data) {
|
|
46
|
+
this.#eventHandler.call(this.#context, event, data);
|
|
192
47
|
}
|
|
193
48
|
get [Symbol.toStringTag]() {
|
|
194
49
|
return "ContextEventHandler";
|
|
@@ -199,23 +54,18 @@ var Subscribr = (() => {
|
|
|
199
54
|
var Subscription = class {
|
|
200
55
|
#eventName;
|
|
201
56
|
#contextEventHandler;
|
|
202
|
-
|
|
203
|
-
constructor(eventName, contextEventHandler, unsubscribe) {
|
|
57
|
+
constructor(eventName, contextEventHandler) {
|
|
204
58
|
this.#eventName = eventName;
|
|
205
59
|
this.#contextEventHandler = contextEventHandler;
|
|
206
|
-
this.#unsubscribe = unsubscribe;
|
|
207
60
|
}
|
|
208
61
|
get eventName() {
|
|
209
62
|
return this.#eventName;
|
|
210
63
|
}
|
|
211
|
-
get
|
|
212
|
-
return this.#contextEventHandler
|
|
64
|
+
get contextEventHandler() {
|
|
65
|
+
return this.#contextEventHandler;
|
|
213
66
|
}
|
|
214
|
-
get
|
|
215
|
-
return
|
|
216
|
-
}
|
|
217
|
-
unsubscribe() {
|
|
218
|
-
return this.#unsubscribe();
|
|
67
|
+
get [Symbol.toStringTag]() {
|
|
68
|
+
return "Subscription";
|
|
219
69
|
}
|
|
220
70
|
};
|
|
221
71
|
|
|
@@ -223,23 +73,14 @@ var Subscribr = (() => {
|
|
|
223
73
|
var Subscribr = class {
|
|
224
74
|
#subscribers;
|
|
225
75
|
constructor() {
|
|
226
|
-
this.#subscribers = new
|
|
76
|
+
this.#subscribers = new SetMultiMap();
|
|
227
77
|
}
|
|
228
|
-
subscribe(eventName, eventHandler, context) {
|
|
78
|
+
subscribe(eventName, eventHandler, context = eventHandler) {
|
|
229
79
|
const contextEventHandler = new ContextEventHandler(context, eventHandler);
|
|
230
80
|
this.#subscribers.set(eventName, contextEventHandler);
|
|
231
|
-
return new Subscription(eventName, contextEventHandler
|
|
232
|
-
}
|
|
233
|
-
publish(eventName, data) {
|
|
234
|
-
this.#subscribers.get(eventName)?.forEach(({ context, eventHandler }) => eventHandler.call(context, data));
|
|
235
|
-
}
|
|
236
|
-
isSubscribed({ eventName, eventHandler: handler }) {
|
|
237
|
-
return this.#subscribers.get(eventName)?.some(({ eventHandler }) => eventHandler === handler);
|
|
81
|
+
return new Subscription(eventName, contextEventHandler);
|
|
238
82
|
}
|
|
239
|
-
|
|
240
|
-
return "Subscribr";
|
|
241
|
-
}
|
|
242
|
-
#unsubscribe(eventName, contextEventHandler) {
|
|
83
|
+
unsubscribe({ eventName, contextEventHandler }) {
|
|
243
84
|
const contextEventHandlers = this.#subscribers.get(eventName);
|
|
244
85
|
const removed = contextEventHandlers?.delete(contextEventHandler);
|
|
245
86
|
if (removed && contextEventHandlers.size == 0) {
|
|
@@ -247,6 +88,18 @@ var Subscribr = (() => {
|
|
|
247
88
|
}
|
|
248
89
|
return removed;
|
|
249
90
|
}
|
|
91
|
+
publish(eventName, event = new CustomEvent(eventName), data) {
|
|
92
|
+
if (data == null && !(event instanceof Event)) {
|
|
93
|
+
[data, event] = [event, new CustomEvent(eventName)];
|
|
94
|
+
}
|
|
95
|
+
this.#subscribers.get(eventName)?.forEach((contextEventHandler) => contextEventHandler.handle(event, data));
|
|
96
|
+
}
|
|
97
|
+
isSubscribed({ eventName, contextEventHandler }) {
|
|
98
|
+
return this.#subscribers.get(eventName)?.has(contextEventHandler);
|
|
99
|
+
}
|
|
100
|
+
get [Symbol.toStringTag]() {
|
|
101
|
+
return "Subscribr";
|
|
102
|
+
}
|
|
250
103
|
};
|
|
251
104
|
return __toCommonJS(subscribr_exports);
|
|
252
105
|
})();
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
var Subscribr=(()=>{var
|
|
1
|
+
var Subscribr=(()=>{var c=Object.defineProperty;var a=Object.getOwnPropertyDescriptor;var h=Object.getOwnPropertyNames;var g=Object.prototype.hasOwnProperty;var d=(s,t)=>{for(var e in t)c(s,e,{get:t[e],enumerable:!0})},b=(s,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of h(t))!g.call(s,r)&&r!==e&&c(s,r,{get:()=>t[r],enumerable:!(n=a(t,r))||n.enumerable});return s};var p=s=>b(c({},"__esModule",{value:!0}),s);var S={};d(S,{default:()=>l});var o=class extends Map{set(t,e){return super.set(t,(super.get(t)??new Set).add(e)),this}[Symbol.toStringTag](){return"SetMultiMap"}};var i=class{#t;#e;constructor(t,e){this.#t=t,this.#e=e}handle(t,e){this.#e.call(this.#t,t,e)}get[Symbol.toStringTag](){return"ContextEventHandler"}};var u=class{#t;#e;constructor(t,e){this.#t=t,this.#e=e}get eventName(){return this.#t}get contextEventHandler(){return this.#e}get[Symbol.toStringTag](){return"Subscription"}};var l=class{#t;constructor(){this.#t=new o}subscribe(t,e,n=e){let r=new i(n,e);return this.#t.set(t,r),new u(t,r)}unsubscribe({eventName:t,contextEventHandler:e}){let n=this.#t.get(t),r=n?.delete(e);return r&&n.size==0&&this.#t.delete(t),r}publish(t,e=new CustomEvent(t),n){n==null&&!(e instanceof Event)&&([n,e]=[e,new CustomEvent(t)]),this.#t.get(t)?.forEach(r=>r.handle(e,n))}isSubscribed({eventName:t,contextEventHandler:e}){return this.#t.get(t)?.has(e)}get[Symbol.toStringTag](){return"Subscribr"}};return p(S);})();
|
|
2
2
|
window.Subscribr = Subscribr.default;
|
|
3
3
|
//# sourceMappingURL=subscribr.min.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../src/subscribr.js", "../../node_modules/@d1g1tal/collections/src/
|
|
4
|
-
"sourcesContent": ["import { MultiMap } from '@d1g1tal/collections';\nimport ContextEventHandler from './context-event-handler.js';\nimport Subscription from './subscription.js';\n\nexport default class Subscribr {\n\t/** @type {MultiMap<string, ContextEventHandler>} */\n\t#subscribers;\n\n\tconstructor() {\n\t\tthis.#subscribers = new MultiMap();\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(*): 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) {\n\t\tconst contextEventHandler = new ContextEventHandler(context, eventHandler);\n\t\tthis.#subscribers.set(eventName, contextEventHandler);\n\n\t\treturn new Subscription(eventName, contextEventHandler, () => this.#unsubscribe(eventName, contextEventHandler));\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 {*} [data] The value to be passed to the event handler as a parameter.\n\t */\n\tpublish(eventName, data) {\n\t\tthis.#subscribers.get(eventName)?.forEach(({context, eventHandler}) => eventHandler.call(context, 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 {function(*)} subscription.eventHandler 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, eventHandler: handler}) {\n\t\treturn this.#subscribers.get(eventName)?.some(({ eventHandler }) => eventHandler === handler);\n\t}\n\n\tget [Symbol.toStringTag]() {\n\t\treturn 'Subscribr';\n\t}\n\n\t/**\n\t * Unsubscribe from the event\n\t *\n\t * @param {string} eventName The event name to subscribe to.\n\t * @param {ContextEventHandler} 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\t#unsubscribe(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/**\n *\n * @template E\n * @typedef {Iterable<E>} List<E>\n */\n\n/**\n * @template E\n * @type {List<E>}\n */\nexport default class List {\n\t/** @type {Array<E>} */\n\t#array;\n\n\t/**\n\t *\n\t * @param {Iterable<E>} [iterable]\n\t */\n\tconstructor(iterable = []) {\n\t\tthis.#array = Array.of(...iterable);\n\t}\n\n\t/**\n\t *\n\t * @param {Iterable<E>} iterable\n\t * @param {function(E, number?)} mapper\n\t * @param {*} context\n\t * @returns {List<E>}\n\t */\n\tstatic from(iterable, mapper, context) {\n\t\treturn new List(Array.from(iterable, mapper, context));\n\t}\n\n\tstatic withSize(size) {\n\t\treturn new List(new Array(size));\n\t}\n\n\t/**\n\t *\n\t * @param {E} element\n\t * @returns {List<E>} The updated list\n\t */\n\tadd(element) {\n\t\tthis.#array.push(element);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t *\n\t * @param {Iterable<E>} iterable\n\t * @returns {List<E>} The updated list\n\t */\n\taddAll(iterable) {\n\t\tthis.#array.push(...iterable);\n\n\t\treturn this;\n\t}\n\n\tclear() {\n\t\tthis.#array.length = 0;\n\t}\n\n\t/**\n\t *\n\t * @param {E} elements\n\t * @returns {List<E>}\n\t */\n\tconcat(elements) {\n\t\treturn this(this.#array.concat(...elements));\n\t}\n\n\t/**\n\t *\n\t * @param {function(E, number?, List<E>?): boolean} predicate\n\t * @returns {boolean}\n\t */\n\tevery(predicate) {\n\t\treturn this.#array.every((element, index) => predicate(element, index, this));\n\t}\n\n\t/**\n\t *\n\t * @param {function(E, number?, List<E>?): boolean} predicate\n\t * @returns {boolean}\n\t */\n\tsome(predicate) {\n\t\treturn this.#array.some((element, index) => predicate(element, index, this));\n\t}\n\n\t/**\n\t *\n\t * @param {function(E, number?, List<E>?): boolean} predicate\n\t * @returns {List<E>}\n\t */\n\tfilter(predicate) {\n\t\treturn new List(this.#array.filter((element, index) => predicate(element, index, this)));\n\t}\n\n\t/**\n\t *\n\t * @param {function(E, number?, List<E>?): boolean} predicate\n\t * @param {*} [context] Optional object to use as this when calling the predicate function\n\t * @returns {E}\n\t */\n\tfind(predicate, context) {\n\t\treturn this.#array.find((element, index) => predicate(element, index, this), context);\n\t}\n\n\t/**\n\t *\n\t * @param {function(E, number?, List<E>?): boolean} predicate\n\t * @param {*} [context] Optional object to use as this when calling the predicate function\n\t * @returns {number}\n\t */\n\tfindIndex(predicate, context) {\n\t\treturn this.#array.findIndex((element, index) => predicate(element, index, this), context);\n\t}\n\n\t/**\n\t *\n\t * @param {function(E, number?, List<E>?)} mapper\n\t * @returns {List<E>}\n\t */\n\tmap(mapper) {\n\t\treturn new List(this.#array.map((element, index) => mapper(element, index, this)));\n\t}\n\n\t/**\n\t * Executes a user-supplied \"reducer\" callback function on each element of the list, in order,\n\t * passing in the return value from the calculation on the preceding element. The final result\n\t * of running the reducer across all elements of the array is a single value.\n\t *\n\t * @param {function(E, E, *)} reducer\n\t * @param {*} [initialValue]\n\t * @returns {*}\n\t */\n\treduce(reducer, initialValue) {\n\t\treturn this.#array.reduce(reducer, initialValue);\n\t}\n\n\t/**\n\t * Sorts the elements of a list in place and returns the reference to the same list, now sorted.\n\t * The default sort order is ascending, built upon converting the elements into strings,\n\t * then comparing their sequences of UTF-16 code units values.\n\t *\n\t * The sort() method preserves empty slots. If the source list is sparse, the empty slots are moved to the end of the list, and always come after all the undefined values.\n\t *\n\t * @example\n\t * // returns\n\t * new List([50, 3, 20, 33, 9, 1]).sort();\n\t * @param {function(E, E): number} [comparator] A function that defines the sort order. If omitted, the list elements are converted to strings, then sorted according to each character's Unicode code point value.\n\t * @returns {List<E>} The reference to the original list, now sorted. Note that the list is sorted in place, and no copy is made.\n\t */\n\tsort(comparator) {\n\t\tthis.#array.sort(comparator);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t *\n\t * @param {function(E, number?, List<E>?): boolean} consumer\n\t */\n\tforEach(consumer) {\n\t\tthis.#array.forEach((element, index) => consumer(element, index, this));\n\t}\n\n\t/**\n\t *\n\t * @param {E} element\n\t * @param {number} [fromIndex]\n\t * @returns {E}\n\t */\n\tget(element, fromIndex = 0) {\n\t\treturn this.#array.at(this.#array.indexOf(element, fromIndex));\n\t}\n\n\t/**\n\t *\n\t * @param {E} element\n\t * @returns {boolean}\n\t */\n\thas(element) {\n\t\treturn this.#array.includes(element);\n\t}\n\n\t/**\n\t * Inserts the entry in a sorted list in the correct position.\n\t * The list must be sorted in ascending order to work.\n\t *\n\t * @param {number} index\n\t * @param {E} element The entry to add to the list.\n\t */\n\tinsert(index, element) {\n\t\tthis.#array.splice(index, 0, element);\n\t}\n\n\t/**\n\t * Removes the element associated to the value and returns a boolean asserting whether an element was successfully removed or not.\n\t *\n\t * @param {E} element The element to remove\n\t * @returns {boolean} true if element was already in {@link List}; otherwise false.\n\t */\n\tdelete(element) {\n\t\treturn this.#array.splice(this.#array.indexOf(element), 1).length == 1;\n\t}\n\n\t/**\n\t * Removes the element at the specified index and returns a boolean asserting whether an element was successfully removed or not.\n\t *\n\t * @param {number} index The element to remove\n\t * @returns {boolean} true if element was already in {@link List}; otherwise false.\n\t */\n\tdeleteAt(index) {\n\t\treturn this.#array.splice(index, 1).length == 1;\n\t}\n\n\tkeys() {\n\t\treturn this.#array.keys();\n\t}\n\n\t/**\n\t *\n\t * @returns {IterableIterator<E>}\n\t */\n\tvalues() {\n\t\treturn this[Symbol.iterator]();\n\t}\n\n\t/**\n\t *\n\t * @returns {IterableIterator<number, E>}\n\t */\n\tentries() {\n\t\treturn this.#array.entries();\n\t}\n\n\t/**\n\t * Checks to see if the list is empty\n\t *\n\t * @returns {boolean} true if the list is empty, false otherwise.\n\t */\n\tisEmpty() {\n\t\treturn this.#array.length === 0;\n\t}\n\n\ttoArray() {\n\t\treturn [...this.#array];\n\t}\n\n\ttoString() {\n\t\treturn this.#array.toString();\n\t}\n\n\tvalueOf() {\n\t\treturn this.#array.valueOf();\n\t}\n\n\tget size() {\n\t\treturn this.#array.length;\n\t}\n\n\t[Symbol.iterator]() {\n\t\treturn this.#array[Symbol.iterator]();\n\t}\n\n\tget [Symbol.toStringTag]() {\n\t\treturn 'List';\n\t}\n}", "import List from './list.js';\n\n/**\n *\n * @template K\n * @template V\n * @typedef {Map<K, V>} MultiMap\n * @extends Map\n */\nexport default class MultiMap extends Map {\n\t/**\n\t * Adds a new element with a specified key and value to the MultiMap.\n\t * If an element with the same key already exists, the value will be added to the underlying {@link List}.\n\t *\n\t * @override\n\t * @template K\n\t * @template V\n\t * @param {K} key The key to set.\n\t * @param {V} value The value to add to the MultiMap\n\t * @returns {MultiMap<K, V>} The MultiMap with the updated key and value.\n\t */\n\tset(key, value) {\n\t\tsuper.set(key, (this.get(key) ?? new List()).add(value));\n\n\t\treturn this;\n\t}\n\n\t[Symbol.toStringTag]() {\n\t\treturn 'MultiMap';\n\t}\n}", "/**\n *\n * @typedef {Map<*, *>} MultiMap\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 MultiMap. 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 MultiMap\n\t * @returns {MultiMap} The MultiMap 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 Node {\n\t#value;\n\t#next;\n\n\tconstructor(value) {\n\t\tthis.#value = value;\n\t\tthis.#next = null;\n\t}\n\n\tget value() {\n\t\treturn this.#value;\n\t}\n\n\tget next() {\n\t\treturn this.#next;\n\t}\n\n\tget [Symbol.toStringTag]() {\n\t\treturn 'Node';\n\t}\n}", "/**\n *\n * @typedef {Map<*, *>} MultiMap\n * @extends Map\n */\nexport default class WeakSetMultiMap extends Map {\n\t/**\n\t * Adds a new element with a specified key and value to the MultiMap. 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 MultiMap\n\t * @returns {MultiMap} The MultiMap with the updated key and value.\n\t */\n\tset(key, value) {\n\t\tsuper.set(key, (super.get(key) ?? new WeakSet()).add(value));\n\n\t\treturn this;\n\t}\n\n\t[Symbol.toStringTag]() {\n\t\treturn 'WeakSetMultiMap';\n\t}\n}", "export default class ContextEventHandler {\n\t#context;\n\t#eventHandler;\n\n\t/**\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\tget context() {\n\t\treturn this.#context;\n\t}\n\n\tget eventHandler() {\n\t\treturn this.#eventHandler;\n\t}\n\n\tget [Symbol.toStringTag]() {\n\t\treturn 'ContextEventHandler';\n\t}\n}", "export default class Subscription {\n\t#eventName;\n\t#contextEventHandler;\n\t#unsubscribe;\n\n\t/**\n\t *\n\t * @param {string} eventName\n\t * @param {import('./context-event-handler.js').default} contextEventHandler\n\t * @param {function(string, function(*): void): boolean} unsubscribe\n\t */\n\tconstructor(eventName, contextEventHandler, unsubscribe) {\n\t\tthis.#eventName = eventName;\n\t\tthis.#contextEventHandler = contextEventHandler;\n\t\tthis.#unsubscribe = unsubscribe;\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 which will be bound to the event handler when it is called.\n\t *\n\t * @returns {*} The event handler context.\n\t */\n\tget context() {\n\t\treturn this.#contextEventHandler.context;\n\t}\n\n\t/**\n\t * Gets the event handler for the subscription.\n\t *\n\t * @returns {function(*): void} The event handler.\n\t */\n\tget eventHandler() {\n\t\treturn this.#contextEventHandler.eventHandler;\n\t}\n\n\t/**\n\t * Unsubscribes from the event.\n\t *\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() {\n\t\treturn this.#unsubscribe();\n\t}\n}"],
|
|
5
|
-
"mappings": "gbAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,
|
|
6
|
-
"names": ["subscribr_exports", "__export", "Subscribr", "
|
|
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}", "// eslint-disable-next-line jsdoc/valid-types\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,cAAe,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,cAAe,CAC1B,MAAO,qBACR,CACD,ECvBA,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,cAAe,CAC1B,MAAO,cACR,CACD,EHjCA,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,SACtC,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,cAAe,CAC1B,MAAO,WACR,CACD",
|
|
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/browser.zip
ADDED
|
Binary file
|
package/dist/esm/subscribr.js
CHANGED
|
@@ -1,115 +1,3 @@
|
|
|
1
|
-
// node_modules/@d1g1tal/collections/src/list.js
|
|
2
|
-
var List = class {
|
|
3
|
-
#array;
|
|
4
|
-
constructor(iterable = []) {
|
|
5
|
-
this.#array = Array.of(...iterable);
|
|
6
|
-
}
|
|
7
|
-
static from(iterable, mapper, context) {
|
|
8
|
-
return new List(Array.from(iterable, mapper, context));
|
|
9
|
-
}
|
|
10
|
-
static withSize(size) {
|
|
11
|
-
return new List(new Array(size));
|
|
12
|
-
}
|
|
13
|
-
add(element) {
|
|
14
|
-
this.#array.push(element);
|
|
15
|
-
return this;
|
|
16
|
-
}
|
|
17
|
-
addAll(iterable) {
|
|
18
|
-
this.#array.push(...iterable);
|
|
19
|
-
return this;
|
|
20
|
-
}
|
|
21
|
-
clear() {
|
|
22
|
-
this.#array.length = 0;
|
|
23
|
-
}
|
|
24
|
-
concat(elements) {
|
|
25
|
-
return this(this.#array.concat(...elements));
|
|
26
|
-
}
|
|
27
|
-
every(predicate) {
|
|
28
|
-
return this.#array.every((element, index) => predicate(element, index, this));
|
|
29
|
-
}
|
|
30
|
-
some(predicate) {
|
|
31
|
-
return this.#array.some((element, index) => predicate(element, index, this));
|
|
32
|
-
}
|
|
33
|
-
filter(predicate) {
|
|
34
|
-
return new List(this.#array.filter((element, index) => predicate(element, index, this)));
|
|
35
|
-
}
|
|
36
|
-
find(predicate, context) {
|
|
37
|
-
return this.#array.find((element, index) => predicate(element, index, this), context);
|
|
38
|
-
}
|
|
39
|
-
findIndex(predicate, context) {
|
|
40
|
-
return this.#array.findIndex((element, index) => predicate(element, index, this), context);
|
|
41
|
-
}
|
|
42
|
-
map(mapper) {
|
|
43
|
-
return new List(this.#array.map((element, index) => mapper(element, index, this)));
|
|
44
|
-
}
|
|
45
|
-
reduce(reducer, initialValue) {
|
|
46
|
-
return this.#array.reduce(reducer, initialValue);
|
|
47
|
-
}
|
|
48
|
-
sort(comparator) {
|
|
49
|
-
this.#array.sort(comparator);
|
|
50
|
-
return this;
|
|
51
|
-
}
|
|
52
|
-
forEach(consumer) {
|
|
53
|
-
this.#array.forEach((element, index) => consumer(element, index, this));
|
|
54
|
-
}
|
|
55
|
-
get(element, fromIndex = 0) {
|
|
56
|
-
return this.#array.at(this.#array.indexOf(element, fromIndex));
|
|
57
|
-
}
|
|
58
|
-
has(element) {
|
|
59
|
-
return this.#array.includes(element);
|
|
60
|
-
}
|
|
61
|
-
insert(index, element) {
|
|
62
|
-
this.#array.splice(index, 0, element);
|
|
63
|
-
}
|
|
64
|
-
delete(element) {
|
|
65
|
-
return this.#array.splice(this.#array.indexOf(element), 1).length == 1;
|
|
66
|
-
}
|
|
67
|
-
deleteAt(index) {
|
|
68
|
-
return this.#array.splice(index, 1).length == 1;
|
|
69
|
-
}
|
|
70
|
-
keys() {
|
|
71
|
-
return this.#array.keys();
|
|
72
|
-
}
|
|
73
|
-
values() {
|
|
74
|
-
return this[Symbol.iterator]();
|
|
75
|
-
}
|
|
76
|
-
entries() {
|
|
77
|
-
return this.#array.entries();
|
|
78
|
-
}
|
|
79
|
-
isEmpty() {
|
|
80
|
-
return this.#array.length === 0;
|
|
81
|
-
}
|
|
82
|
-
toArray() {
|
|
83
|
-
return [...this.#array];
|
|
84
|
-
}
|
|
85
|
-
toString() {
|
|
86
|
-
return this.#array.toString();
|
|
87
|
-
}
|
|
88
|
-
valueOf() {
|
|
89
|
-
return this.#array.valueOf();
|
|
90
|
-
}
|
|
91
|
-
get size() {
|
|
92
|
-
return this.#array.length;
|
|
93
|
-
}
|
|
94
|
-
[Symbol.iterator]() {
|
|
95
|
-
return this.#array[Symbol.iterator]();
|
|
96
|
-
}
|
|
97
|
-
get [Symbol.toStringTag]() {
|
|
98
|
-
return "List";
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
// node_modules/@d1g1tal/collections/src/multi-map.js
|
|
103
|
-
var MultiMap = class extends Map {
|
|
104
|
-
set(key, value) {
|
|
105
|
-
super.set(key, (this.get(key) ?? new List()).add(value));
|
|
106
|
-
return this;
|
|
107
|
-
}
|
|
108
|
-
[Symbol.toStringTag]() {
|
|
109
|
-
return "MultiMap";
|
|
110
|
-
}
|
|
111
|
-
};
|
|
112
|
-
|
|
113
1
|
// node_modules/@d1g1tal/collections/src/set-multi-map.js
|
|
114
2
|
var SetMultiMap = class extends Map {
|
|
115
3
|
set(key, value) {
|
|
@@ -121,36 +9,6 @@ var SetMultiMap = class extends Map {
|
|
|
121
9
|
}
|
|
122
10
|
};
|
|
123
11
|
|
|
124
|
-
// node_modules/@d1g1tal/collections/src/node.js
|
|
125
|
-
var Node = class {
|
|
126
|
-
#value;
|
|
127
|
-
#next;
|
|
128
|
-
constructor(value) {
|
|
129
|
-
this.#value = value;
|
|
130
|
-
this.#next = null;
|
|
131
|
-
}
|
|
132
|
-
get value() {
|
|
133
|
-
return this.#value;
|
|
134
|
-
}
|
|
135
|
-
get next() {
|
|
136
|
-
return this.#next;
|
|
137
|
-
}
|
|
138
|
-
get [Symbol.toStringTag]() {
|
|
139
|
-
return "Node";
|
|
140
|
-
}
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
// node_modules/@d1g1tal/collections/src/weak-set-multi-map.js
|
|
144
|
-
var WeakSetMultiMap = class extends Map {
|
|
145
|
-
set(key, value) {
|
|
146
|
-
super.set(key, (super.get(key) ?? /* @__PURE__ */ new WeakSet()).add(value));
|
|
147
|
-
return this;
|
|
148
|
-
}
|
|
149
|
-
[Symbol.toStringTag]() {
|
|
150
|
-
return "WeakSetMultiMap";
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
|
|
154
12
|
// src/context-event-handler.js
|
|
155
13
|
var ContextEventHandler = class {
|
|
156
14
|
#context;
|
|
@@ -159,11 +17,8 @@ var ContextEventHandler = class {
|
|
|
159
17
|
this.#context = context;
|
|
160
18
|
this.#eventHandler = eventHandler;
|
|
161
19
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
165
|
-
get eventHandler() {
|
|
166
|
-
return this.#eventHandler;
|
|
20
|
+
handle(event, data) {
|
|
21
|
+
this.#eventHandler.call(this.#context, event, data);
|
|
167
22
|
}
|
|
168
23
|
get [Symbol.toStringTag]() {
|
|
169
24
|
return "ContextEventHandler";
|
|
@@ -174,23 +29,18 @@ var ContextEventHandler = class {
|
|
|
174
29
|
var Subscription = class {
|
|
175
30
|
#eventName;
|
|
176
31
|
#contextEventHandler;
|
|
177
|
-
|
|
178
|
-
constructor(eventName, contextEventHandler, unsubscribe) {
|
|
32
|
+
constructor(eventName, contextEventHandler) {
|
|
179
33
|
this.#eventName = eventName;
|
|
180
34
|
this.#contextEventHandler = contextEventHandler;
|
|
181
|
-
this.#unsubscribe = unsubscribe;
|
|
182
35
|
}
|
|
183
36
|
get eventName() {
|
|
184
37
|
return this.#eventName;
|
|
185
38
|
}
|
|
186
|
-
get
|
|
187
|
-
return this.#contextEventHandler
|
|
39
|
+
get contextEventHandler() {
|
|
40
|
+
return this.#contextEventHandler;
|
|
188
41
|
}
|
|
189
|
-
get
|
|
190
|
-
return
|
|
191
|
-
}
|
|
192
|
-
unsubscribe() {
|
|
193
|
-
return this.#unsubscribe();
|
|
42
|
+
get [Symbol.toStringTag]() {
|
|
43
|
+
return "Subscription";
|
|
194
44
|
}
|
|
195
45
|
};
|
|
196
46
|
|
|
@@ -198,23 +48,14 @@ var Subscription = class {
|
|
|
198
48
|
var Subscribr = class {
|
|
199
49
|
#subscribers;
|
|
200
50
|
constructor() {
|
|
201
|
-
this.#subscribers = new
|
|
51
|
+
this.#subscribers = new SetMultiMap();
|
|
202
52
|
}
|
|
203
|
-
subscribe(eventName, eventHandler, context) {
|
|
53
|
+
subscribe(eventName, eventHandler, context = eventHandler) {
|
|
204
54
|
const contextEventHandler = new ContextEventHandler(context, eventHandler);
|
|
205
55
|
this.#subscribers.set(eventName, contextEventHandler);
|
|
206
|
-
return new Subscription(eventName, contextEventHandler
|
|
207
|
-
}
|
|
208
|
-
publish(eventName, data) {
|
|
209
|
-
this.#subscribers.get(eventName)?.forEach(({ context, eventHandler }) => eventHandler.call(context, data));
|
|
210
|
-
}
|
|
211
|
-
isSubscribed({ eventName, eventHandler: handler }) {
|
|
212
|
-
return this.#subscribers.get(eventName)?.some(({ eventHandler }) => eventHandler === handler);
|
|
56
|
+
return new Subscription(eventName, contextEventHandler);
|
|
213
57
|
}
|
|
214
|
-
|
|
215
|
-
return "Subscribr";
|
|
216
|
-
}
|
|
217
|
-
#unsubscribe(eventName, contextEventHandler) {
|
|
58
|
+
unsubscribe({ eventName, contextEventHandler }) {
|
|
218
59
|
const contextEventHandlers = this.#subscribers.get(eventName);
|
|
219
60
|
const removed = contextEventHandlers?.delete(contextEventHandler);
|
|
220
61
|
if (removed && contextEventHandlers.size == 0) {
|
|
@@ -222,6 +63,18 @@ var Subscribr = class {
|
|
|
222
63
|
}
|
|
223
64
|
return removed;
|
|
224
65
|
}
|
|
66
|
+
publish(eventName, event = new CustomEvent(eventName), data) {
|
|
67
|
+
if (data == null && !(event instanceof Event)) {
|
|
68
|
+
[data, event] = [event, new CustomEvent(eventName)];
|
|
69
|
+
}
|
|
70
|
+
this.#subscribers.get(eventName)?.forEach((contextEventHandler) => contextEventHandler.handle(event, data));
|
|
71
|
+
}
|
|
72
|
+
isSubscribed({ eventName, contextEventHandler }) {
|
|
73
|
+
return this.#subscribers.get(eventName)?.has(contextEventHandler);
|
|
74
|
+
}
|
|
75
|
+
get [Symbol.toStringTag]() {
|
|
76
|
+
return "Subscribr";
|
|
77
|
+
}
|
|
225
78
|
};
|
|
226
79
|
export {
|
|
227
80
|
Subscribr as default
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var n=class
|
|
1
|
+
var n=class extends Map{set(t,e){return super.set(t,(super.get(t)??new Set).add(e)),this}[Symbol.toStringTag](){return"SetMultiMap"}};var o=class{#t;#e;constructor(t,e){this.#t=t,this.#e=e}handle(t,e){this.#e.call(this.#t,t,e)}get[Symbol.toStringTag](){return"ContextEventHandler"}};var i=class{#t;#e;constructor(t,e){this.#t=t,this.#e=e}get eventName(){return this.#t}get contextEventHandler(){return this.#e}get[Symbol.toStringTag](){return"Subscription"}};var l=class{#t;constructor(){this.#t=new n}subscribe(t,e,r=e){let s=new o(r,e);return this.#t.set(t,s),new i(t,s)}unsubscribe({eventName:t,contextEventHandler:e}){let r=this.#t.get(t),s=r?.delete(e);return s&&r.size==0&&this.#t.delete(t),s}publish(t,e=new CustomEvent(t),r){r==null&&!(e instanceof Event)&&([r,e]=[e,new CustomEvent(t)]),this.#t.get(t)?.forEach(s=>s.handle(e,r))}isSubscribed({eventName:t,contextEventHandler:e}){return this.#t.get(t)?.has(e)}get[Symbol.toStringTag](){return"Subscribr"}};export{l as default};
|
|
2
2
|
//# sourceMappingURL=subscribr.min.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../node_modules/@d1g1tal/collections/src/
|
|
4
|
-
"sourcesContent": ["\n/**\n *\n * @template E\n * @typedef {Iterable<E>} List<E>\n */\n\n/**\n * @template E\n * @type {List<E>}\n */\nexport default class List {\n\t/** @type {Array<E>} */\n\t#array;\n\n\t/**\n\t *\n\t * @param {Iterable<E>} [iterable]\n\t */\n\tconstructor(iterable = []) {\n\t\tthis.#array = Array.of(...iterable);\n\t}\n\n\t/**\n\t *\n\t * @param {Iterable<E>} iterable\n\t * @param {function(E, number?)} mapper\n\t * @param {*} context\n\t * @returns {List<E>}\n\t */\n\tstatic from(iterable, mapper, context) {\n\t\treturn new List(Array.from(iterable, mapper, context));\n\t}\n\n\tstatic withSize(size) {\n\t\treturn new List(new Array(size));\n\t}\n\n\t/**\n\t *\n\t * @param {E} element\n\t * @returns {List<E>} The updated list\n\t */\n\tadd(element) {\n\t\tthis.#array.push(element);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t *\n\t * @param {Iterable<E>} iterable\n\t * @returns {List<E>} The updated list\n\t */\n\taddAll(iterable) {\n\t\tthis.#array.push(...iterable);\n\n\t\treturn this;\n\t}\n\n\tclear() {\n\t\tthis.#array.length = 0;\n\t}\n\n\t/**\n\t *\n\t * @param {E} elements\n\t * @returns {List<E>}\n\t */\n\tconcat(elements) {\n\t\treturn this(this.#array.concat(...elements));\n\t}\n\n\t/**\n\t *\n\t * @param {function(E, number?, List<E>?): boolean} predicate\n\t * @returns {boolean}\n\t */\n\tevery(predicate) {\n\t\treturn this.#array.every((element, index) => predicate(element, index, this));\n\t}\n\n\t/**\n\t *\n\t * @param {function(E, number?, List<E>?): boolean} predicate\n\t * @returns {boolean}\n\t */\n\tsome(predicate) {\n\t\treturn this.#array.some((element, index) => predicate(element, index, this));\n\t}\n\n\t/**\n\t *\n\t * @param {function(E, number?, List<E>?): boolean} predicate\n\t * @returns {List<E>}\n\t */\n\tfilter(predicate) {\n\t\treturn new List(this.#array.filter((element, index) => predicate(element, index, this)));\n\t}\n\n\t/**\n\t *\n\t * @param {function(E, number?, List<E>?): boolean} predicate\n\t * @param {*} [context] Optional object to use as this when calling the predicate function\n\t * @returns {E}\n\t */\n\tfind(predicate, context) {\n\t\treturn this.#array.find((element, index) => predicate(element, index, this), context);\n\t}\n\n\t/**\n\t *\n\t * @param {function(E, number?, List<E>?): boolean} predicate\n\t * @param {*} [context] Optional object to use as this when calling the predicate function\n\t * @returns {number}\n\t */\n\tfindIndex(predicate, context) {\n\t\treturn this.#array.findIndex((element, index) => predicate(element, index, this), context);\n\t}\n\n\t/**\n\t *\n\t * @param {function(E, number?, List<E>?)} mapper\n\t * @returns {List<E>}\n\t */\n\tmap(mapper) {\n\t\treturn new List(this.#array.map((element, index) => mapper(element, index, this)));\n\t}\n\n\t/**\n\t * Executes a user-supplied \"reducer\" callback function on each element of the list, in order,\n\t * passing in the return value from the calculation on the preceding element. The final result\n\t * of running the reducer across all elements of the array is a single value.\n\t *\n\t * @param {function(E, E, *)} reducer\n\t * @param {*} [initialValue]\n\t * @returns {*}\n\t */\n\treduce(reducer, initialValue) {\n\t\treturn this.#array.reduce(reducer, initialValue);\n\t}\n\n\t/**\n\t * Sorts the elements of a list in place and returns the reference to the same list, now sorted.\n\t * The default sort order is ascending, built upon converting the elements into strings,\n\t * then comparing their sequences of UTF-16 code units values.\n\t *\n\t * The sort() method preserves empty slots. If the source list is sparse, the empty slots are moved to the end of the list, and always come after all the undefined values.\n\t *\n\t * @example\n\t * // returns\n\t * new List([50, 3, 20, 33, 9, 1]).sort();\n\t * @param {function(E, E): number} [comparator] A function that defines the sort order. If omitted, the list elements are converted to strings, then sorted according to each character's Unicode code point value.\n\t * @returns {List<E>} The reference to the original list, now sorted. Note that the list is sorted in place, and no copy is made.\n\t */\n\tsort(comparator) {\n\t\tthis.#array.sort(comparator);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t *\n\t * @param {function(E, number?, List<E>?): boolean} consumer\n\t */\n\tforEach(consumer) {\n\t\tthis.#array.forEach((element, index) => consumer(element, index, this));\n\t}\n\n\t/**\n\t *\n\t * @param {E} element\n\t * @param {number} [fromIndex]\n\t * @returns {E}\n\t */\n\tget(element, fromIndex = 0) {\n\t\treturn this.#array.at(this.#array.indexOf(element, fromIndex));\n\t}\n\n\t/**\n\t *\n\t * @param {E} element\n\t * @returns {boolean}\n\t */\n\thas(element) {\n\t\treturn this.#array.includes(element);\n\t}\n\n\t/**\n\t * Inserts the entry in a sorted list in the correct position.\n\t * The list must be sorted in ascending order to work.\n\t *\n\t * @param {number} index\n\t * @param {E} element The entry to add to the list.\n\t */\n\tinsert(index, element) {\n\t\tthis.#array.splice(index, 0, element);\n\t}\n\n\t/**\n\t * Removes the element associated to the value and returns a boolean asserting whether an element was successfully removed or not.\n\t *\n\t * @param {E} element The element to remove\n\t * @returns {boolean} true if element was already in {@link List}; otherwise false.\n\t */\n\tdelete(element) {\n\t\treturn this.#array.splice(this.#array.indexOf(element), 1).length == 1;\n\t}\n\n\t/**\n\t * Removes the element at the specified index and returns a boolean asserting whether an element was successfully removed or not.\n\t *\n\t * @param {number} index The element to remove\n\t * @returns {boolean} true if element was already in {@link List}; otherwise false.\n\t */\n\tdeleteAt(index) {\n\t\treturn this.#array.splice(index, 1).length == 1;\n\t}\n\n\tkeys() {\n\t\treturn this.#array.keys();\n\t}\n\n\t/**\n\t *\n\t * @returns {IterableIterator<E>}\n\t */\n\tvalues() {\n\t\treturn this[Symbol.iterator]();\n\t}\n\n\t/**\n\t *\n\t * @returns {IterableIterator<number, E>}\n\t */\n\tentries() {\n\t\treturn this.#array.entries();\n\t}\n\n\t/**\n\t * Checks to see if the list is empty\n\t *\n\t * @returns {boolean} true if the list is empty, false otherwise.\n\t */\n\tisEmpty() {\n\t\treturn this.#array.length === 0;\n\t}\n\n\ttoArray() {\n\t\treturn [...this.#array];\n\t}\n\n\ttoString() {\n\t\treturn this.#array.toString();\n\t}\n\n\tvalueOf() {\n\t\treturn this.#array.valueOf();\n\t}\n\n\tget size() {\n\t\treturn this.#array.length;\n\t}\n\n\t[Symbol.iterator]() {\n\t\treturn this.#array[Symbol.iterator]();\n\t}\n\n\tget [Symbol.toStringTag]() {\n\t\treturn 'List';\n\t}\n}", "import List from './list.js';\n\n/**\n *\n * @template K\n * @template V\n * @typedef {Map<K, V>} MultiMap\n * @extends Map\n */\nexport default class MultiMap extends Map {\n\t/**\n\t * Adds a new element with a specified key and value to the MultiMap.\n\t * If an element with the same key already exists, the value will be added to the underlying {@link List}.\n\t *\n\t * @override\n\t * @template K\n\t * @template V\n\t * @param {K} key The key to set.\n\t * @param {V} value The value to add to the MultiMap\n\t * @returns {MultiMap<K, V>} The MultiMap with the updated key and value.\n\t */\n\tset(key, value) {\n\t\tsuper.set(key, (this.get(key) ?? new List()).add(value));\n\n\t\treturn this;\n\t}\n\n\t[Symbol.toStringTag]() {\n\t\treturn 'MultiMap';\n\t}\n}", "/**\n *\n * @typedef {Map<*, *>} MultiMap\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 MultiMap. 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 MultiMap\n\t * @returns {MultiMap} The MultiMap 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 Node {\n\t#value;\n\t#next;\n\n\tconstructor(value) {\n\t\tthis.#value = value;\n\t\tthis.#next = null;\n\t}\n\n\tget value() {\n\t\treturn this.#value;\n\t}\n\n\tget next() {\n\t\treturn this.#next;\n\t}\n\n\tget [Symbol.toStringTag]() {\n\t\treturn 'Node';\n\t}\n}", "/**\n *\n * @typedef {Map<*, *>} MultiMap\n * @extends Map\n */\nexport default class WeakSetMultiMap extends Map {\n\t/**\n\t * Adds a new element with a specified key and value to the MultiMap. 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 MultiMap\n\t * @returns {MultiMap} The MultiMap with the updated key and value.\n\t */\n\tset(key, value) {\n\t\tsuper.set(key, (super.get(key) ?? new WeakSet()).add(value));\n\n\t\treturn this;\n\t}\n\n\t[Symbol.toStringTag]() {\n\t\treturn 'WeakSetMultiMap';\n\t}\n}", "export default class ContextEventHandler {\n\t#context;\n\t#eventHandler;\n\n\t/**\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\tget context() {\n\t\treturn this.#context;\n\t}\n\n\tget eventHandler() {\n\t\treturn this.#eventHandler;\n\t}\n\n\tget [Symbol.toStringTag]() {\n\t\treturn 'ContextEventHandler';\n\t}\n}", "export default class Subscription {\n\t#eventName;\n\t#contextEventHandler;\n\t#unsubscribe;\n\n\t/**\n\t *\n\t * @param {string} eventName\n\t * @param {import('./context-event-handler.js').default} contextEventHandler\n\t * @param {function(string, function(*): void): boolean} unsubscribe\n\t */\n\tconstructor(eventName, contextEventHandler, unsubscribe) {\n\t\tthis.#eventName = eventName;\n\t\tthis.#contextEventHandler = contextEventHandler;\n\t\tthis.#unsubscribe = unsubscribe;\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 which will be bound to the event handler when it is called.\n\t *\n\t * @returns {*} The event handler context.\n\t */\n\tget context() {\n\t\treturn this.#contextEventHandler.context;\n\t}\n\n\t/**\n\t * Gets the event handler for the subscription.\n\t *\n\t * @returns {function(*): void} The event handler.\n\t */\n\tget eventHandler() {\n\t\treturn this.#contextEventHandler.eventHandler;\n\t}\n\n\t/**\n\t * Unsubscribes from the event.\n\t *\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() {\n\t\treturn this.#unsubscribe();\n\t}\n}", "import { MultiMap } from '@d1g1tal/collections';\nimport ContextEventHandler from './context-event-handler.js';\nimport Subscription from './subscription.js';\n\nexport default class Subscribr {\n\t/** @type {MultiMap<string, ContextEventHandler>} */\n\t#subscribers;\n\n\tconstructor() {\n\t\tthis.#subscribers = new MultiMap();\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(*): 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) {\n\t\tconst contextEventHandler = new ContextEventHandler(context, eventHandler);\n\t\tthis.#subscribers.set(eventName, contextEventHandler);\n\n\t\treturn new Subscription(eventName, contextEventHandler, () => this.#unsubscribe(eventName, contextEventHandler));\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 {*} [data] The value to be passed to the event handler as a parameter.\n\t */\n\tpublish(eventName, data) {\n\t\tthis.#subscribers.get(eventName)?.forEach(({context, eventHandler}) => eventHandler.call(context, 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 {function(*)} subscription.eventHandler 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, eventHandler: handler}) {\n\t\treturn this.#subscribers.get(eventName)?.some(({ eventHandler }) => eventHandler === handler);\n\t}\n\n\tget [Symbol.toStringTag]() {\n\t\treturn 'Subscribr';\n\t}\n\n\t/**\n\t * Unsubscribe from the event\n\t *\n\t * @param {string} eventName The event name to subscribe to.\n\t * @param {ContextEventHandler} 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\t#unsubscribe(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}"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": ["
|
|
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}", "// eslint-disable-next-line jsdoc/valid-types\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[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,cAAe,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,cAAe,CAC1B,MAAO,qBACR,CACD,ECvBA,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,cAAe,CAC1B,MAAO,cACR,CACD,ECjCA,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,SACtC,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,cAAe,CAC1B,MAAO,WACR,CACD",
|
|
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/dist/esm.zip
ADDED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@d1g1tal/subscribr",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "JavaScript Publish/Subscribe Library",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"types": "index.d.js",
|
|
6
7
|
"exports": {
|
|
7
8
|
".": {
|
|
8
9
|
"import": "./src/subscribr.js",
|
|
9
10
|
"default": "./dist/browser/subscribr.min.js"
|
|
10
11
|
}
|
|
11
12
|
},
|
|
12
|
-
"types": "index.d.js",
|
|
13
13
|
"publishConfig": {
|
|
14
14
|
"access": "public"
|
|
15
15
|
},
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
"build:esm": "rimraf dist/esm && esbuild src/subscribr.js --bundle --target=es2022 --format=esm --outfile=dist/esm/subscribr.js && esbuild src/subscribr.js --bundle --sourcemap --minify --target=es2022 --format=esm --outfile=dist/esm/subscribr.min.js",
|
|
25
25
|
"build:browser": "rimraf dist/browser && esbuild src/subscribr.js --bundle --target=es2022 --platform=browser --global-name=Subscribr --footer:js='window.Subscribr = Subscribr.default;' --outfile=dist/browser/subscribr.js && esbuild src/subscribr.js --bundle --sourcemap --minify --target=es2022 --platform=browser --global-name=Subscribr --footer:js='window.Subscribr = Subscribr.default;' --outfile=dist/browser/subscribr.min.js",
|
|
26
26
|
"lint": "eslint --ext .js --fix --ignore-path .gitignore .",
|
|
27
|
+
"d.ts": "tsc --allowJs -declaration --emitDeclarationOnly --skipLibCheck --lib esnext index.js",
|
|
27
28
|
"test": "node --no-warnings --experimental-vm-modules node_modules/jest/bin/jest.js"
|
|
28
29
|
},
|
|
29
30
|
"repository": {
|
|
@@ -44,11 +45,11 @@
|
|
|
44
45
|
"devDependencies": {
|
|
45
46
|
"@d1g1tal/chrysalis": "^1.1.7",
|
|
46
47
|
"@skypack/package-check": "^0.2.2",
|
|
47
|
-
"esbuild": "^0.15.
|
|
48
|
-
"eslint": "^8.
|
|
48
|
+
"esbuild": "^0.15.12",
|
|
49
|
+
"eslint": "^8.26.0",
|
|
49
50
|
"eslint-plugin-compat": "^4.0.2",
|
|
50
|
-
"eslint-plugin-jsdoc": "^39.3.
|
|
51
|
-
"jest": "^29.2.
|
|
51
|
+
"eslint-plugin-jsdoc": "^39.3.21",
|
|
52
|
+
"jest": "^29.2.1",
|
|
52
53
|
"rimraf": "^3.0.2"
|
|
53
54
|
},
|
|
54
55
|
"jest": {
|
|
@@ -67,6 +68,6 @@
|
|
|
67
68
|
"defaults"
|
|
68
69
|
],
|
|
69
70
|
"dependencies": {
|
|
70
|
-
"@d1g1tal/collections": "^0.0.
|
|
71
|
+
"@d1g1tal/collections": "^0.0.4"
|
|
71
72
|
}
|
|
72
73
|
}
|
|
@@ -1,12 +1,22 @@
|
|
|
1
1
|
export default class ContextEventHandler {
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
3
|
* @param {*} context The context to bind to the event handler.
|
|
5
4
|
* @param {function(*): void} eventHandler The event handler to call when the event is published.
|
|
6
5
|
*/
|
|
7
6
|
constructor(context: any, eventHandler: (arg0: any) => void);
|
|
8
|
-
|
|
9
|
-
|
|
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;
|
|
10
20
|
get [Symbol.toStringTag](): string;
|
|
11
21
|
#private;
|
|
12
22
|
}
|
|
@@ -3,7 +3,6 @@ export default class ContextEventHandler {
|
|
|
3
3
|
#eventHandler;
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
6
|
* @param {*} context The context to bind to the event handler.
|
|
8
7
|
* @param {function(*): void} eventHandler The event handler to call when the event is published.
|
|
9
8
|
*/
|
|
@@ -12,12 +11,14 @@ export default class ContextEventHandler {
|
|
|
12
11
|
this.#eventHandler = eventHandler;
|
|
13
12
|
}
|
|
14
13
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Call the event handler for the provided event.
|
|
16
|
+
*
|
|
17
|
+
* @param {Event} event The event to handle
|
|
18
|
+
* @param {*} [data] The value to be passed to the event handler as a parameter.
|
|
19
|
+
*/
|
|
20
|
+
handle(event, data) {
|
|
21
|
+
this.#eventHandler.call(this.#context, event, data);
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
get [Symbol.toStringTag]() {
|
package/src/subscribr.d.ts
CHANGED
|
@@ -3,27 +3,37 @@ export default class Subscribr {
|
|
|
3
3
|
* Subscribe to an event
|
|
4
4
|
*
|
|
5
5
|
* @param {string} eventName The event name to subscribe to.
|
|
6
|
-
* @param {function(*): void} eventHandler The event handler to call when the event is published.
|
|
6
|
+
* @param {function(Event, *): void} eventHandler The event handler to call when the event is published.
|
|
7
7
|
* @param {*} [context] The context to bind to the event handler.
|
|
8
8
|
* @returns {Subscription} An object used to check if the subscription still exists and to unsubscribe from the event.
|
|
9
9
|
*/
|
|
10
|
-
subscribe(eventName: string, eventHandler: (arg0: any) => void, context?: any): Subscription;
|
|
10
|
+
subscribe(eventName: string, eventHandler: (arg0: Event, arg1: any) => void, context?: any): Subscription;
|
|
11
|
+
/**
|
|
12
|
+
* Unsubscribe from the event
|
|
13
|
+
*
|
|
14
|
+
* @param {Subscription} subscription The subscription to unsubscribe.
|
|
15
|
+
* @param {string} subscription.eventName The event name to subscribe to.
|
|
16
|
+
* @param {ContextEventHandler} subscription.contextEventHandler The event handler to call when the event is published.
|
|
17
|
+
* @returns {boolean} true if eventListener has been removed successfully. false if the value is not found or if the value is not an object.
|
|
18
|
+
*/
|
|
19
|
+
unsubscribe({ eventName, contextEventHandler }: Subscription): boolean;
|
|
11
20
|
/**
|
|
12
21
|
* Publish an event
|
|
13
22
|
*
|
|
14
23
|
* @param {string} eventName The name of the event.
|
|
24
|
+
* @param {Event} event The event to be handled.
|
|
15
25
|
* @param {*} [data] The value to be passed to the event handler as a parameter.
|
|
16
26
|
*/
|
|
17
|
-
publish(eventName: string, data?: any): void;
|
|
27
|
+
publish(eventName: string, event?: Event, data?: any): void;
|
|
18
28
|
/**
|
|
19
29
|
* Check if the event and handler are subscribed.
|
|
20
30
|
*
|
|
21
31
|
* @param {Subscription} subscription The subscription object.
|
|
22
32
|
* @param {string} subscription.eventName The name of the event to check.
|
|
23
|
-
* @param {
|
|
33
|
+
* @param {ContextEventHandler} subscription.contextEventHandler The event handler to check.
|
|
24
34
|
* @returns {boolean} true if the event name and handler are subscribed, false otherwise.
|
|
25
35
|
*/
|
|
26
|
-
isSubscribed({ eventName,
|
|
36
|
+
isSubscribed({ eventName, contextEventHandler }: Subscription): boolean;
|
|
27
37
|
get [Symbol.toStringTag](): string;
|
|
28
38
|
#private;
|
|
29
39
|
}
|
package/src/subscribr.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import SetMultiMap from '@d1g1tal/collections/set-multi-map.js';
|
|
2
2
|
import ContextEventHandler from './context-event-handler.js';
|
|
3
3
|
import Subscription from './subscription.js';
|
|
4
4
|
|
|
@@ -14,25 +14,49 @@ export default class Subscribr {
|
|
|
14
14
|
* Subscribe to an event
|
|
15
15
|
*
|
|
16
16
|
* @param {string} eventName The event name to subscribe to.
|
|
17
|
-
* @param {function(*): void} eventHandler The event handler to call when the event is published.
|
|
17
|
+
* @param {function(Event, *): void} eventHandler The event handler to call when the event is published.
|
|
18
18
|
* @param {*} [context] The context to bind to the event handler.
|
|
19
19
|
* @returns {Subscription} An object used to check if the subscription still exists and to unsubscribe from the event.
|
|
20
20
|
*/
|
|
21
|
-
subscribe(eventName, eventHandler, context) {
|
|
21
|
+
subscribe(eventName, eventHandler, context = eventHandler) {
|
|
22
22
|
const contextEventHandler = new ContextEventHandler(context, eventHandler);
|
|
23
23
|
this.#subscribers.set(eventName, contextEventHandler);
|
|
24
24
|
|
|
25
|
-
return new Subscription(eventName, contextEventHandler
|
|
25
|
+
return new Subscription(eventName, contextEventHandler);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Unsubscribe from the event
|
|
30
|
+
*
|
|
31
|
+
* @param {Subscription} subscription The subscription to unsubscribe.
|
|
32
|
+
* @param {string} subscription.eventName The event name to subscribe to.
|
|
33
|
+
* @param {ContextEventHandler} subscription.contextEventHandler The event handler to call when the event is published.
|
|
34
|
+
* @returns {boolean} true if eventListener has been removed successfully. false if the value is not found or if the value is not an object.
|
|
35
|
+
*/
|
|
36
|
+
unsubscribe({ eventName, contextEventHandler }) {
|
|
37
|
+
const contextEventHandlers = this.#subscribers.get(eventName);
|
|
38
|
+
const removed = contextEventHandlers?.delete(contextEventHandler);
|
|
39
|
+
|
|
40
|
+
if (removed && contextEventHandlers.size == 0) {
|
|
41
|
+
this.#subscribers.delete(eventName);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return removed;
|
|
26
45
|
}
|
|
27
46
|
|
|
28
47
|
/**
|
|
29
48
|
* Publish an event
|
|
30
49
|
*
|
|
31
50
|
* @param {string} eventName The name of the event.
|
|
51
|
+
* @param {Event} event The event to be handled.
|
|
32
52
|
* @param {*} [data] The value to be passed to the event handler as a parameter.
|
|
33
53
|
*/
|
|
34
|
-
publish(eventName, data) {
|
|
35
|
-
|
|
54
|
+
publish(eventName, event = new CustomEvent(eventName), data) {
|
|
55
|
+
if (data == null && !(event instanceof Event)) {
|
|
56
|
+
// Swap the event and data parameters becuase only data was passesed without an event object
|
|
57
|
+
[data, event] = [event, new CustomEvent(eventName)];
|
|
58
|
+
}
|
|
59
|
+
this.#subscribers.get(eventName)?.forEach((contextEventHandler) => contextEventHandler.handle(event, data));
|
|
36
60
|
}
|
|
37
61
|
|
|
38
62
|
/**
|
|
@@ -40,32 +64,14 @@ export default class Subscribr {
|
|
|
40
64
|
*
|
|
41
65
|
* @param {Subscription} subscription The subscription object.
|
|
42
66
|
* @param {string} subscription.eventName The name of the event to check.
|
|
43
|
-
* @param {
|
|
67
|
+
* @param {ContextEventHandler} subscription.contextEventHandler The event handler to check.
|
|
44
68
|
* @returns {boolean} true if the event name and handler are subscribed, false otherwise.
|
|
45
69
|
*/
|
|
46
|
-
isSubscribed({ eventName,
|
|
47
|
-
return
|
|
70
|
+
isSubscribed({ eventName, contextEventHandler }) {
|
|
71
|
+
return this.#subscribers.get(eventName)?.has(contextEventHandler);
|
|
48
72
|
}
|
|
49
73
|
|
|
50
74
|
get [Symbol.toStringTag]() {
|
|
51
75
|
return 'Subscribr';
|
|
52
76
|
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Unsubscribe from the event
|
|
56
|
-
*
|
|
57
|
-
* @param {string} eventName The event name to subscribe to.
|
|
58
|
-
* @param {ContextEventHandler} contextEventHandler The event handler to call when the event is published.
|
|
59
|
-
* @returns {boolean} true if eventListener has been removed successfully. false if the value is not found or if the value is not an object.
|
|
60
|
-
*/
|
|
61
|
-
#unsubscribe(eventName, contextEventHandler) {
|
|
62
|
-
const contextEventHandlers = this.#subscribers.get(eventName);
|
|
63
|
-
const removed = contextEventHandlers?.delete(contextEventHandler);
|
|
64
|
-
|
|
65
|
-
if (removed && contextEventHandlers.size == 0) {
|
|
66
|
-
this.#subscribers.delete(eventName);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return removed;
|
|
70
|
-
}
|
|
71
77
|
}
|
package/src/subscription.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
+
/** @typedef {import('./context-event-handler.js').default} ContextEventHandler */
|
|
1
2
|
export default class Subscription {
|
|
2
3
|
/**
|
|
3
|
-
*
|
|
4
|
-
* @param {
|
|
5
|
-
* @param {import('./context-event-handler.js').default} contextEventHandler
|
|
6
|
-
* @param {function(string, function(*): void): boolean} unsubscribe
|
|
4
|
+
* @param {string} eventName The event name.
|
|
5
|
+
* @param {ContextEventHandler} contextEventHandler Then context event handler.
|
|
7
6
|
*/
|
|
8
|
-
constructor(eventName: string, contextEventHandler:
|
|
7
|
+
constructor(eventName: string, contextEventHandler: ContextEventHandler);
|
|
9
8
|
/**
|
|
10
9
|
* Gets the event name for the subscription.
|
|
11
10
|
*
|
|
@@ -13,22 +12,12 @@ export default class Subscription {
|
|
|
13
12
|
*/
|
|
14
13
|
get eventName(): string;
|
|
15
14
|
/**
|
|
16
|
-
* Gets the context
|
|
17
|
-
*
|
|
18
|
-
* @returns {*} The event handler context.
|
|
19
|
-
*/
|
|
20
|
-
get context(): any;
|
|
21
|
-
/**
|
|
22
|
-
* Gets the event handler for the subscription.
|
|
23
|
-
*
|
|
24
|
-
* @returns {function(*): void} The event handler.
|
|
25
|
-
*/
|
|
26
|
-
get eventHandler(): (arg0: any) => void;
|
|
27
|
-
/**
|
|
28
|
-
* Unsubscribes from the event.
|
|
15
|
+
* Gets the context event handler.
|
|
29
16
|
*
|
|
30
|
-
* @returns {
|
|
17
|
+
* @returns {ContextEventHandler} The context event handler
|
|
31
18
|
*/
|
|
32
|
-
|
|
19
|
+
get contextEventHandler(): import("./context-event-handler.js").default;
|
|
20
|
+
get [Symbol.toStringTag](): string;
|
|
33
21
|
#private;
|
|
34
22
|
}
|
|
23
|
+
export type ContextEventHandler = import('./context-event-handler.js').default;
|
package/src/subscription.js
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
+
// eslint-disable-next-line jsdoc/valid-types
|
|
2
|
+
/** @typedef {import('./context-event-handler.js').default} ContextEventHandler */
|
|
3
|
+
|
|
1
4
|
export default class Subscription {
|
|
2
5
|
#eventName;
|
|
3
6
|
#contextEventHandler;
|
|
4
|
-
#unsubscribe;
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
|
-
*
|
|
8
|
-
* @param {
|
|
9
|
-
* @param {import('./context-event-handler.js').default} contextEventHandler
|
|
10
|
-
* @param {function(string, function(*): void): boolean} unsubscribe
|
|
9
|
+
* @param {string} eventName The event name.
|
|
10
|
+
* @param {ContextEventHandler} contextEventHandler Then context event handler.
|
|
11
11
|
*/
|
|
12
|
-
constructor(eventName, contextEventHandler
|
|
12
|
+
constructor(eventName, contextEventHandler) {
|
|
13
13
|
this.#eventName = eventName;
|
|
14
14
|
this.#contextEventHandler = contextEventHandler;
|
|
15
|
-
this.#unsubscribe = unsubscribe;
|
|
16
15
|
}
|
|
17
16
|
|
|
18
17
|
/**
|
|
@@ -25,29 +24,15 @@ export default class Subscription {
|
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
/**
|
|
28
|
-
* Gets the context
|
|
29
|
-
*
|
|
30
|
-
* @returns {*} The event handler context.
|
|
31
|
-
*/
|
|
32
|
-
get context() {
|
|
33
|
-
return this.#contextEventHandler.context;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Gets the event handler for the subscription.
|
|
27
|
+
* Gets the context event handler.
|
|
38
28
|
*
|
|
39
|
-
* @returns {
|
|
29
|
+
* @returns {ContextEventHandler} The context event handler
|
|
40
30
|
*/
|
|
41
|
-
get
|
|
42
|
-
return this.#contextEventHandler
|
|
31
|
+
get contextEventHandler() {
|
|
32
|
+
return this.#contextEventHandler;
|
|
43
33
|
}
|
|
44
34
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
*
|
|
48
|
-
* @returns {boolean} true if eventListener has been removed successfully. false if the value is not found or if the value is not an object.
|
|
49
|
-
*/
|
|
50
|
-
unsubscribe() {
|
|
51
|
-
return this.#unsubscribe();
|
|
35
|
+
get [Symbol.toStringTag]() {
|
|
36
|
+
return 'Subscription';
|
|
52
37
|
}
|
|
53
38
|
}
|