@byloth/core 2.1.6 → 2.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core.cjs +1 -1
- package/dist/core.cjs.map +1 -1
- package/dist/core.esm.js +5 -4
- package/dist/core.esm.js.map +1 -1
- package/dist/core.global.js +1 -1
- package/dist/core.global.js.map +1 -1
- package/dist/core.umd.cjs +1 -1
- package/dist/core.umd.cjs.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/models/callbacks/callable-object.ts +3 -3
- package/src/models/callbacks/publisher.ts +17 -67
- package/src/models/callbacks/switchable-callback.ts +1 -1
- package/src/models/callbacks/types.ts +1 -1
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Callback } from "./types.js";
|
|
2
2
|
|
|
3
3
|
const SmartFunction = (Function as unknown) as new<A extends unknown[] = [], R = void>(...args: string[])
|
|
4
|
-
=>
|
|
4
|
+
=> Callback<A, R>;
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* An abstract class that can be used to implement callable objects.
|
|
@@ -30,10 +30,10 @@ const SmartFunction = (Function as unknown) as new<A extends unknown[] = [], R =
|
|
|
30
30
|
*
|
|
31
31
|
* @template T
|
|
32
32
|
* The type signature of the callback function.
|
|
33
|
-
* It must be a function. Default is `(
|
|
33
|
+
* It must be a function. Default is `() => void`.
|
|
34
34
|
*/
|
|
35
35
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
36
|
-
export default abstract class CallableObject<T extends Callback<any[], any> =
|
|
36
|
+
export default abstract class CallableObject<T extends Callback<any[], any> = Callback>
|
|
37
37
|
extends SmartFunction<Parameters<T>, ReturnType<T>>
|
|
38
38
|
{
|
|
39
39
|
/**
|
|
@@ -2,8 +2,8 @@ import { ReferenceException } from "../exceptions/index.js";
|
|
|
2
2
|
|
|
3
3
|
import type { Callback, CallbackMap, InternalsEventsMap, WildcardEventsMap } from "./types.js";
|
|
4
4
|
|
|
5
|
-
type
|
|
6
|
-
type
|
|
5
|
+
type P = InternalsEventsMap;
|
|
6
|
+
type S = WildcardEventsMap & InternalsEventsMap;
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* A class implementing the
|
|
@@ -120,29 +120,29 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
120
120
|
* ---
|
|
121
121
|
*
|
|
122
122
|
* @template U
|
|
123
|
-
* A map containing the names of the emittable events and
|
|
124
|
-
* related callback signatures that can be subscribed to them.
|
|
125
|
-
* Default is `
|
|
123
|
+
* A map containing the additional names of the emittable events and
|
|
124
|
+
* the related callback signatures that can be subscribed to them.
|
|
125
|
+
* Default is `{ }`.
|
|
126
126
|
*
|
|
127
127
|
* @return
|
|
128
128
|
* A new instance of the {@link Publisher} class that can be
|
|
129
129
|
* used to publish and subscribe events within a specific context.
|
|
130
130
|
*/
|
|
131
|
-
|
|
131
|
+
|
|
132
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
|
133
|
+
public createScope<U extends CallbackMap<U> = { }>(): Publisher<U & T>
|
|
132
134
|
{
|
|
133
|
-
const scope = new Publisher
|
|
135
|
+
const scope = new Publisher();
|
|
134
136
|
|
|
135
137
|
this.subscribe("__internals__:clear", () => scope.clear());
|
|
136
|
-
this.subscribe("*", (event, ...args): void =>
|
|
137
|
-
{
|
|
138
|
-
scope.publish(event as keyof U & string, ...args as Parameters<U[keyof U]>);
|
|
139
|
-
});
|
|
138
|
+
this.subscribe("*", (event, ...args): void => { scope.publish(event, ...args); });
|
|
140
139
|
|
|
141
140
|
return scope;
|
|
142
141
|
}
|
|
143
142
|
|
|
144
143
|
/**
|
|
145
|
-
* Publishes an event to all the subscribers.
|
|
144
|
+
* Publishes an event to all the subscribers.
|
|
145
|
+
* This events will trigger the wildcard listeners as well, if any.
|
|
146
146
|
*
|
|
147
147
|
* ---
|
|
148
148
|
*
|
|
@@ -170,7 +170,8 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
170
170
|
* Publishes an internal event to all the subscribers.
|
|
171
171
|
*
|
|
172
172
|
* Internal events follow the pattern `__${string}__:${string}` and are used for internal
|
|
173
|
-
* communication within the publisher system. These events won't trigger wildcard listeners.
|
|
173
|
+
* communication within the publisher system. These events won't trigger wildcard listeners.
|
|
174
|
+
* Please note to use this method only if you know what you are doing.
|
|
174
175
|
*
|
|
175
176
|
* ---
|
|
176
177
|
*
|
|
@@ -189,7 +190,7 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
189
190
|
*
|
|
190
191
|
* @returns An array containing the return values of all the subscribers.
|
|
191
192
|
*/
|
|
192
|
-
public publish<K extends keyof
|
|
193
|
+
public publish<K extends keyof P>(event: K & string, ...args: Parameters<P[K]>): ReturnType<P[K]>[];
|
|
193
194
|
public publish(event: string, ...args: unknown[]): unknown[]
|
|
194
195
|
{
|
|
195
196
|
let results: unknown[];
|
|
@@ -240,32 +241,6 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
240
241
|
*/
|
|
241
242
|
public subscribe<K extends keyof T>(event: K & string, subscriber: T[K]): () => void;
|
|
242
243
|
|
|
243
|
-
/**
|
|
244
|
-
* Subscribes to an internal event and adds a subscriber to be executed when the event is published.
|
|
245
|
-
*
|
|
246
|
-
* Internal events follow the pattern `__${string}__:${string}` and
|
|
247
|
-
* are used for internal communication within the publisher system.
|
|
248
|
-
* Please note to use this method only if you know what you are doing.
|
|
249
|
-
*
|
|
250
|
-
* ---
|
|
251
|
-
*
|
|
252
|
-
* @example
|
|
253
|
-
* ```ts
|
|
254
|
-
* publisher.subscribe("__internals__:clear", () => console.log("All subscribers have been cleared."));
|
|
255
|
-
* publisher.clear(); // "All subscribers have been cleared."
|
|
256
|
-
* ```
|
|
257
|
-
*
|
|
258
|
-
* ---
|
|
259
|
-
*
|
|
260
|
-
* @template K The key of the internal events map containing the callback signature to subscribe.
|
|
261
|
-
*
|
|
262
|
-
* @param event The name of the internal event to subscribe to.
|
|
263
|
-
* @param subscriber The subscriber to execute when the internal event is published.
|
|
264
|
-
*
|
|
265
|
-
* @returns A function that can be used to unsubscribe the subscriber from the event.
|
|
266
|
-
*/
|
|
267
|
-
public subscribe<K extends keyof I>(event: K & string, subscriber: I[K]): () => void;
|
|
268
|
-
|
|
269
244
|
/**
|
|
270
245
|
* Subscribes to the wildcard event to listen to all published events.
|
|
271
246
|
*
|
|
@@ -291,7 +266,7 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
291
266
|
*
|
|
292
267
|
* @returns A function that can be used to unsubscribe the subscriber from the wildcard event.
|
|
293
268
|
*/
|
|
294
|
-
public subscribe<K extends keyof
|
|
269
|
+
public subscribe<K extends keyof S>(event: K & string, subscriber: S[K]): () => void;
|
|
295
270
|
public subscribe(event: string, subscriber: Callback<unknown[], unknown>): () => void
|
|
296
271
|
{
|
|
297
272
|
const subscribers = this._subscribers.get(event) ?? [];
|
|
@@ -334,31 +309,6 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
334
309
|
*/
|
|
335
310
|
public unsubscribe<K extends keyof T>(event: K & string, subscriber: T[K]): void;
|
|
336
311
|
|
|
337
|
-
/**
|
|
338
|
-
* Unsubscribes from an internal event and removes a subscriber from being executed when the event is published.
|
|
339
|
-
*
|
|
340
|
-
* Internal events follow the pattern `__${string}__:${string}` and
|
|
341
|
-
* are used for internal communication within the publisher system.
|
|
342
|
-
*
|
|
343
|
-
* ---
|
|
344
|
-
*
|
|
345
|
-
* @example
|
|
346
|
-
* ```ts
|
|
347
|
-
* const onClear = () => console.log("Publisher cleared");
|
|
348
|
-
*
|
|
349
|
-
* publisher.subscribe("__internals__:clear", onClear);
|
|
350
|
-
* publisher.unsubscribe("__internals__:clear", onClear);
|
|
351
|
-
* ```
|
|
352
|
-
*
|
|
353
|
-
* ---
|
|
354
|
-
*
|
|
355
|
-
* @template K The key of the internal events map containing the callback signature to unsubscribe.
|
|
356
|
-
*
|
|
357
|
-
* @param event The name of the internal event to unsubscribe from.
|
|
358
|
-
* @param subscriber The subscriber to remove from the internal event.
|
|
359
|
-
*/
|
|
360
|
-
public unsubscribe<K extends keyof I>(event: K & string, subscriber: I[K]): void;
|
|
361
|
-
|
|
362
312
|
/**
|
|
363
313
|
* Unsubscribes from the wildcard event and removes a subscriber from being executed for all events.
|
|
364
314
|
*
|
|
@@ -381,7 +331,7 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
381
331
|
* @param event The wildcard event name ("*").
|
|
382
332
|
* @param subscriber The wildcard subscriber to remove.
|
|
383
333
|
*/
|
|
384
|
-
public unsubscribe<K extends keyof
|
|
334
|
+
public unsubscribe<K extends keyof S>(event: K & string, subscriber: S[K]): void;
|
|
385
335
|
public unsubscribe(event: string, subscriber: Callback<unknown[], unknown>): void
|
|
386
336
|
{
|
|
387
337
|
const subscribers = this._subscribers.get(event);
|
|
@@ -27,7 +27,7 @@ const Disabler = () => { /* ... */ };
|
|
|
27
27
|
*
|
|
28
28
|
* ---
|
|
29
29
|
*
|
|
30
|
-
* @template T The type signature of the callback. Default is `(
|
|
30
|
+
* @template T The type signature of the callback. Default is `() => void`.
|
|
31
31
|
*/
|
|
32
32
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
33
33
|
export default class SwitchableCallback<T extends Callback<any[], any> = Callback> extends CallableObject<T>
|
|
@@ -69,7 +69,7 @@ export type CallbackMap<T = Record<string, Callback<unknown[], unknown>>> = { [K
|
|
|
69
69
|
* publisher.clear(); // "Publisher cleared"
|
|
70
70
|
* ```
|
|
71
71
|
*/
|
|
72
|
-
export type InternalsEventsMap = Record<`__${string}__:${string}`,
|
|
72
|
+
export type InternalsEventsMap = Record<`__${string}__:${string}`, Callback<unknown[], unknown>>;
|
|
73
73
|
|
|
74
74
|
/**
|
|
75
75
|
* An utility interface that defines a wildcard event for listening to all events.
|