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