@byloth/core 2.1.0 → 2.1.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 +4 -1
- package/dist/core.cjs +1 -1
- package/dist/core.cjs.map +1 -1
- package/dist/core.esm.js +1 -1
- 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 +7 -7
- package/src/core/types.ts +6 -1
- package/src/index.ts +3 -1
- package/src/models/callbacks/types.ts +107 -0
- package/src/models/collections/types.ts +159 -0
- package/src/models/types.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@byloth/core",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "An unopinionated collection of useful functions and classes that I use widely in all my projects. 🔧",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Core",
|
|
@@ -50,15 +50,15 @@
|
|
|
50
50
|
"types": "src/index.ts",
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@byloth/eslint-config-typescript": "^3.1.0",
|
|
53
|
-
"@eslint/compat": "^1.2.
|
|
54
|
-
"@types/node": "^22.15.
|
|
55
|
-
"@vitest/coverage-v8": "^3.1.
|
|
56
|
-
"eslint": "^9.
|
|
53
|
+
"@eslint/compat": "^1.2.9",
|
|
54
|
+
"@types/node": "^22.15.18",
|
|
55
|
+
"@vitest/coverage-v8": "^3.1.3",
|
|
56
|
+
"eslint": "^9.26.0",
|
|
57
57
|
"husky": "^9.1.7",
|
|
58
58
|
"jsdom": "^26.1.0",
|
|
59
59
|
"typescript": "^5.8.3",
|
|
60
|
-
"vite": "^6.3.
|
|
61
|
-
"vitest": "^3.1.
|
|
60
|
+
"vite": "^6.3.5",
|
|
61
|
+
"vitest": "^3.1.3"
|
|
62
62
|
},
|
|
63
63
|
"scripts": {
|
|
64
64
|
"dev": "vite",
|
package/src/core/types.ts
CHANGED
|
@@ -10,9 +10,14 @@
|
|
|
10
10
|
*
|
|
11
11
|
* const instance: MyObject = factory(MyObject);
|
|
12
12
|
* ```
|
|
13
|
+
*
|
|
14
|
+
* ---
|
|
15
|
+
*
|
|
16
|
+
* @template T The type of the instance to create. Default is `object`.
|
|
17
|
+
* @template P The type of the constructor parameters. Default is `any[]`.
|
|
13
18
|
*/
|
|
14
19
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
15
|
-
export type Constructor<T extends object, P extends unknown[] = any[]> = new (...args: P) => T;
|
|
20
|
+
export type Constructor<T extends object = object, P extends unknown[] = any[]> = new (...args: P) => T;
|
|
16
21
|
|
|
17
22
|
/**
|
|
18
23
|
* A type that represents the return value of {@link setInterval} function,
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export const VERSION = "2.1.
|
|
1
|
+
export const VERSION = "2.1.1";
|
|
2
2
|
|
|
3
3
|
export type { Constructor, Interval, Timeout, ValueOf } from "./core/types.js";
|
|
4
4
|
|
|
@@ -71,11 +71,13 @@ export type {
|
|
|
71
71
|
PromiseExecutor,
|
|
72
72
|
PromiseRejecter,
|
|
73
73
|
PromiseResolver,
|
|
74
|
+
Publishable,
|
|
74
75
|
ReadonlyMapView,
|
|
75
76
|
ReadonlySetView,
|
|
76
77
|
Reducer,
|
|
77
78
|
RejectedHandler,
|
|
78
79
|
SetViewEventsMap,
|
|
80
|
+
Subscribable,
|
|
79
81
|
TypeGuardPredicate
|
|
80
82
|
|
|
81
83
|
} from "./models/types.js";
|
|
@@ -50,3 +50,110 @@ export type Callback<A extends unknown[] = [], R = void> = (...args: A) => R;
|
|
|
50
50
|
*/
|
|
51
51
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
52
52
|
export type CallbackMap<T = Record<string, Callback<unknown[], unknown>>> = { [K in keyof T]: Callback<any[], any> };
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* An utility type that represents a {@link Publisher} object that can be published to.
|
|
56
|
+
* See also {@link Subscribable}.
|
|
57
|
+
*
|
|
58
|
+
* It can be used to prevent the user from modifying the publisher while
|
|
59
|
+
* still allowing them to subscribe to events and publish them.
|
|
60
|
+
*
|
|
61
|
+
* ---
|
|
62
|
+
*
|
|
63
|
+
* @template T
|
|
64
|
+
* A map containing the names of the emittable events and the
|
|
65
|
+
* related callback signatures that can be subscribed to them.
|
|
66
|
+
* Default is `Record<string, (...args: unknown[]) => unknown>`.
|
|
67
|
+
*/
|
|
68
|
+
export interface Publishable<T extends CallbackMap<T> = CallbackMap>
|
|
69
|
+
{
|
|
70
|
+
/**
|
|
71
|
+
* Publishes an event to all the subscribers.
|
|
72
|
+
*
|
|
73
|
+
* ---
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* publisher.subscribe("player:move", (coords) => { [...] });
|
|
78
|
+
* publisher.subscribe("player:move", ({ x, y }) => { [...] });
|
|
79
|
+
* publisher.subscribe("player:move", (evt) => { [...] });
|
|
80
|
+
*
|
|
81
|
+
* publisher.publish("player:move", { x: 10, y: 20 });
|
|
82
|
+
* ```
|
|
83
|
+
*
|
|
84
|
+
* ---
|
|
85
|
+
*
|
|
86
|
+
* @template K The key of the map containing the callback signature to publish.
|
|
87
|
+
*
|
|
88
|
+
* @param event The name of the event to publish.
|
|
89
|
+
* @param args The arguments to pass to the subscribers.
|
|
90
|
+
*
|
|
91
|
+
* @returns An array containing the return values of all the subscribers.
|
|
92
|
+
*/
|
|
93
|
+
publish<K extends keyof T>(event: K & string, ...args: Parameters<T[K]>): ReturnType<T[K]>[];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* An utility type that represents a {@link Publisher} object that can be subscribed to.
|
|
98
|
+
* See also {@link Publishable}.
|
|
99
|
+
*
|
|
100
|
+
* It can be used to prevent the user from modifying the publisher while
|
|
101
|
+
* still allowing them to subscribe to events and publish them.
|
|
102
|
+
*
|
|
103
|
+
* ---
|
|
104
|
+
*
|
|
105
|
+
* @template T
|
|
106
|
+
* A map containing the names of the emittable events and the
|
|
107
|
+
* related callback signatures that can be subscribed to them.
|
|
108
|
+
* Default is `Record<string, (...args: unknown[]) => unknown>`.
|
|
109
|
+
*/
|
|
110
|
+
export interface Subscribable<T extends CallbackMap<T> = CallbackMap>
|
|
111
|
+
{
|
|
112
|
+
/**
|
|
113
|
+
* Subscribes to an event and adds a subscriber to be executed when the event is published.
|
|
114
|
+
*
|
|
115
|
+
* ---
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* let unsubscribe: () => void;
|
|
120
|
+
* publisher.subscribe("player:death", unsubscribe);
|
|
121
|
+
* publisher.subscribe("player:spawn", (evt) =>
|
|
122
|
+
* {
|
|
123
|
+
* unsubscribe = publisher.subscribe("player:move", ({ x, y }) => { [...] });
|
|
124
|
+
* });
|
|
125
|
+
* ```
|
|
126
|
+
*
|
|
127
|
+
* ---
|
|
128
|
+
*
|
|
129
|
+
* @template K The key of the map containing the callback signature to subscribe.
|
|
130
|
+
*
|
|
131
|
+
* @param event The name of the event to subscribe to.
|
|
132
|
+
* @param subscriber The subscriber to execute when the event is published.
|
|
133
|
+
*
|
|
134
|
+
* @returns A function that can be used to unsubscribe the subscriber from the event.
|
|
135
|
+
*/
|
|
136
|
+
subscribe<K extends keyof T>(event: K & string, subscriber: T[K]): () => void;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Unsubscribes from an event and removes a subscriber from being executed when the event is published.
|
|
140
|
+
*
|
|
141
|
+
* ---
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```ts
|
|
145
|
+
* const onPlayerMove = ({ x, y }: Point) => { [...] };
|
|
146
|
+
*
|
|
147
|
+
* publisher.subscribe("player:spawn", (evt) => publisher.subscribe("player:move", onPlayerMove));
|
|
148
|
+
* publisher.subscribe("player:death", () => publisher.unsubscribe("player:move", onPlayerMove));
|
|
149
|
+
* ```
|
|
150
|
+
*
|
|
151
|
+
* ---
|
|
152
|
+
*
|
|
153
|
+
* @template K The key of the map containing the callback signature to unsubscribe.
|
|
154
|
+
*
|
|
155
|
+
* @param event The name of the event to unsubscribe from.
|
|
156
|
+
* @param subscriber The subscriber to remove from the event.
|
|
157
|
+
*/
|
|
158
|
+
unsubscribe<K extends keyof T>(event: K & string, subscriber: T[K]): void;
|
|
159
|
+
}
|
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2
|
+
import type MapView from "./map-view.js";
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
4
|
+
import type SetView from "./set-view.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A type that represents the map of events published by a {@link MapView} object.
|
|
8
|
+
* See also {@link SetViewEventsMap}.
|
|
9
|
+
*
|
|
10
|
+
* The keys of the map are the event names while the values are the callback function signatures.
|
|
11
|
+
*
|
|
12
|
+
* ---
|
|
13
|
+
*
|
|
14
|
+
* @template K The type of the keys in the map.
|
|
15
|
+
* @template V The type of the values in the map.
|
|
16
|
+
*/
|
|
1
17
|
export interface MapViewEventsMap<K, V>
|
|
2
18
|
{
|
|
3
19
|
"entry:add": (key: K, value: V) => void;
|
|
@@ -5,12 +21,89 @@ export interface MapViewEventsMap<K, V>
|
|
|
5
21
|
|
|
6
22
|
"collection:clear": () => void;
|
|
7
23
|
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* An utility type that represents a read-only {@link MapView} object.
|
|
27
|
+
* See also {@link ReadonlySetView}.
|
|
28
|
+
*
|
|
29
|
+
* It can be used to prevent the user from modifying the map while
|
|
30
|
+
* still allowing them to access the entries and subscribe to events.
|
|
31
|
+
*
|
|
32
|
+
* ---
|
|
33
|
+
*
|
|
34
|
+
* @template K The type of keys in the map.
|
|
35
|
+
* @template V The type of values in the map.
|
|
36
|
+
*/
|
|
8
37
|
export interface ReadonlyMapView<K, V> extends ReadonlyMap<K, V>
|
|
9
38
|
{
|
|
39
|
+
/**
|
|
40
|
+
* Subscribes to an event and adds a callback to be executed when the event is published.
|
|
41
|
+
*
|
|
42
|
+
* ---
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* const map = new MapView<string, number>();
|
|
47
|
+
* const unsubscribe = map.subscribe("entry:add", (key: string, value: number) =>
|
|
48
|
+
* {
|
|
49
|
+
* if (key === "answer") { unsubscribe(); }
|
|
50
|
+
* console.log(`Added ${key}: ${value}`);
|
|
51
|
+
* });
|
|
52
|
+
*
|
|
53
|
+
* map.set("key1", 2); // Added key1: 2
|
|
54
|
+
* map.set("answer", 42); // Added answer: 42
|
|
55
|
+
* map.set("key2", 4);
|
|
56
|
+
* map.set("key3", 8);
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* ---
|
|
60
|
+
*
|
|
61
|
+
* @template T The key of the map containing the callback signature to subscribe.
|
|
62
|
+
*
|
|
63
|
+
* @param event The name of the event to subscribe to.
|
|
64
|
+
* @param callback The callback to execute when the event is published.
|
|
65
|
+
*
|
|
66
|
+
* @returns A function that can be used to unsubscribe the callback from the event.
|
|
67
|
+
*/
|
|
10
68
|
subscribe<T extends keyof MapViewEventsMap<K, V>>(event: T, callback: MapViewEventsMap<K, V>[T]): () => void;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Unsubscribes from an event and removes a callback from being executed when the event is published.
|
|
72
|
+
*
|
|
73
|
+
* ---
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* const callback = (key: string, value: number) => console.log(`Added ${key}: ${value}`);
|
|
78
|
+
* const map = new MapView<string, number>();
|
|
79
|
+
*
|
|
80
|
+
* map.subscribe("entry:add", callback);
|
|
81
|
+
* map.set("key1", 2); // Added key1: 2
|
|
82
|
+
*
|
|
83
|
+
* map.unsubscribe("entry:add", callback);
|
|
84
|
+
* map.set("key2", 4);
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* ---
|
|
88
|
+
*
|
|
89
|
+
* @template T The key of the map containing the callback signature to unsubscribe.
|
|
90
|
+
*
|
|
91
|
+
* @param event The name of the event to unsubscribe from.
|
|
92
|
+
* @param callback The callback to remove from the event.
|
|
93
|
+
*/
|
|
11
94
|
unsubscribe<T extends keyof MapViewEventsMap<K, V>>(event: T, callback: MapViewEventsMap<K, V>[T]): void;
|
|
12
95
|
}
|
|
13
96
|
|
|
97
|
+
/**
|
|
98
|
+
* A type that represents the map of events published by a {@link SetView} object.
|
|
99
|
+
* See also {@link MapViewEventsMap}.
|
|
100
|
+
*
|
|
101
|
+
* The keys of the map are the event names while the values are the callback function signatures.
|
|
102
|
+
*
|
|
103
|
+
* ---
|
|
104
|
+
*
|
|
105
|
+
* @template T The type of the values in the set.
|
|
106
|
+
*/
|
|
14
107
|
export interface SetViewEventsMap<T>
|
|
15
108
|
{
|
|
16
109
|
"entry:add": (value: T) => void;
|
|
@@ -18,8 +111,74 @@ export interface SetViewEventsMap<T>
|
|
|
18
111
|
|
|
19
112
|
"collection:clear": () => void;
|
|
20
113
|
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* An utility type that represents a read-only {@link SetView} object.
|
|
117
|
+
* See also {@link ReadonlyMapView}.
|
|
118
|
+
*
|
|
119
|
+
* It can be used to prevent the user from modifying the set while
|
|
120
|
+
* still allowing them to access the entries and subscribe to events.
|
|
121
|
+
*
|
|
122
|
+
* ---
|
|
123
|
+
*
|
|
124
|
+
* @template T The type of values in the set.
|
|
125
|
+
*/
|
|
21
126
|
export interface ReadonlySetView<T> extends ReadonlySet<T>
|
|
22
127
|
{
|
|
128
|
+
/**
|
|
129
|
+
* Subscribes to an event and adds a callback to be executed when the event is published.
|
|
130
|
+
*
|
|
131
|
+
* ---
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```ts
|
|
135
|
+
* const set = new SetView<number>();
|
|
136
|
+
* const unsubscribe = set.subscribe("entry:add", (value: number) =>
|
|
137
|
+
* {
|
|
138
|
+
* if (value === 42) { unsubscribe(); }
|
|
139
|
+
* console.log(`Added ${value}`);
|
|
140
|
+
* });
|
|
141
|
+
*
|
|
142
|
+
* set.add(2); // Added 2
|
|
143
|
+
* set.add(42); // Added 42
|
|
144
|
+
* set.add(4);
|
|
145
|
+
* set.add(8);
|
|
146
|
+
* ```
|
|
147
|
+
*
|
|
148
|
+
* ---
|
|
149
|
+
*
|
|
150
|
+
* @template K The key of the map containing the callback signature to subscribe.
|
|
151
|
+
*
|
|
152
|
+
* @param event The name of the event to subscribe to.
|
|
153
|
+
* @param callback The callback to execute when the event is published.
|
|
154
|
+
*
|
|
155
|
+
* @returns A function that can be used to unsubscribe the callback from the event.
|
|
156
|
+
*/
|
|
23
157
|
subscribe<K extends keyof SetViewEventsMap<T>>(event: K, callback: SetViewEventsMap<T>[K]): () => void;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Unsubscribes from an event and removes a callback from being executed when the event is published.
|
|
161
|
+
*
|
|
162
|
+
* ---
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```ts
|
|
166
|
+
* const callback = (value: number) => console.log(`Added ${value}`);
|
|
167
|
+
* const set = new SetView<number>();
|
|
168
|
+
*
|
|
169
|
+
* set.subscribe("entry:add", callback);
|
|
170
|
+
* set.add(2); // Added 2
|
|
171
|
+
*
|
|
172
|
+
* set.unsubscribe("entry:add", callback);
|
|
173
|
+
* set.add(4);
|
|
174
|
+
* ```
|
|
175
|
+
*
|
|
176
|
+
* ---
|
|
177
|
+
*
|
|
178
|
+
* @template K The key of the map containing the callback signature to unsubscribe.
|
|
179
|
+
*
|
|
180
|
+
* @param event The name of the event to unsubscribe from.
|
|
181
|
+
* @param callback The callback to remove from the event.
|
|
182
|
+
*/
|
|
24
183
|
unsubscribe<K extends keyof SetViewEventsMap<T>>(event: K, callback: SetViewEventsMap<T>[K]): void;
|
|
25
184
|
}
|
package/src/models/types.ts
CHANGED