@byloth/core 2.0.2 → 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.
@@ -0,0 +1,184 @@
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
+ */
17
+ export interface MapViewEventsMap<K, V>
18
+ {
19
+ "entry:add": (key: K, value: V) => void;
20
+ "entry:remove": (key: K) => void;
21
+
22
+ "collection:clear": () => void;
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
+ */
37
+ export interface ReadonlyMapView<K, V> extends ReadonlyMap<K, V>
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
+ */
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
+ */
94
+ unsubscribe<T extends keyof MapViewEventsMap<K, V>>(event: T, callback: MapViewEventsMap<K, V>[T]): void;
95
+ }
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
+ */
107
+ export interface SetViewEventsMap<T>
108
+ {
109
+ "entry:add": (value: T) => void;
110
+ "entry:remove": (value: T) => void;
111
+
112
+ "collection:clear": () => void;
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
+ */
126
+ export interface ReadonlySetView<T> extends ReadonlySet<T>
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
+ */
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
+ */
183
+ unsubscribe<K extends keyof SetViewEventsMap<T>>(event: K, callback: SetViewEventsMap<T>[K]): void;
184
+ }
@@ -1,5 +1,5 @@
1
1
  /**
2
- * A class representing an exception, subclass of the native `Error` class.
2
+ * A class representing an exception, subclass of the native {@link Error} class.
3
3
  * It's the base class for any other further exception.
4
4
  *
5
5
  * It allows to chain exceptions together, tracking the initial cause of an error and
@@ -6,6 +6,7 @@ export {
6
6
  } from "./aggregators/index.js";
7
7
 
8
8
  export { CallableObject, Publisher, SwitchableCallback } from "./callbacks/index.js";
9
+ export { MapView, SetView } from "./collections/index.js";
9
10
  export {
10
11
  Exception,
11
12
  FatalErrorException,
@@ -4,7 +4,7 @@ import { EnvironmentException } from "../exceptions/index.js";
4
4
  import type { JSONValue } from "./types.js";
5
5
 
6
6
  /**
7
- * A wrapper around the `Storage` API to better store and easily retrieve
7
+ * A wrapper around the {@link Storage} API to better store and easily retrieve
8
8
  * typed JSON values using the classical key-value pair storage system.
9
9
  *
10
10
  * It allows to handle either the volatile {@link sessionStorage} or the persistent
@@ -9,6 +9,7 @@ export type {
9
9
 
10
10
  } from "./aggregators/types.js";
11
11
 
12
+ export type { MapViewEventsMap, ReadonlyMapView, SetViewEventsMap, ReadonlySetView } from "./collections/types.js";
12
13
  export type {
13
14
  GeneratorFunction,
14
15
  AsyncGeneratorFunction,
@@ -26,13 +27,7 @@ export type {
26
27
 
27
28
  } from "./iterators/types.js";
28
29
 
29
- export type {
30
- JSONArray,
31
- JSONObject,
32
- JSONValue
33
-
34
- } from "./json/types.js";
35
-
30
+ export type { JSONArray, JSONObject, JSONValue } from "./json/types.js";
36
31
  export type {
37
32
  MaybePromise,
38
33
  FulfilledHandler,
@@ -43,4 +38,4 @@ export type {
43
38
 
44
39
  } from "./promises/types.js";
45
40
 
46
- export type { Callback, CallbackMap } from "./callbacks/types.js";
41
+ export type { Callback, CallbackMap, Publishable, Subscribable } from "./callbacks/types.js";
@@ -43,7 +43,7 @@ export function chain<T>(...iterables: readonly Iterable<T>[]): SmartIterator<T>
43
43
  * An utility function that counts the number of elements in an iterable.
44
44
  *
45
45
  * Also note that:
46
- * - If the iterable isn't an `Array`, it will be consumed entirely in the process.
46
+ * - If the iterable isn't an {@link Array}, it will be consumed entirely in the process.
47
47
  * - If the iterable is an infinite generator, the function will never return.
48
48
  *
49
49
  * ---
@@ -213,8 +213,8 @@ export function range(start: number, end?: number, step = 1): SmartIterator<numb
213
213
  * algorithm to shuffle the elements.
214
214
  *
215
215
  * Also note that:
216
- * - If the iterable is an `Array`, it won't be modified since the shuffling isn't done in-place.
217
- * - If the iterable isn't an `Array`, it will be consumed entirely in the process.
216
+ * - If the iterable is an {@link Array}, it won't be modified since the shuffling isn't done in-place.
217
+ * - If the iterable isn't an {@link Array}, it will be consumed entirely in the process.
218
218
  * - If the iterable is an infinite generator, the function will never return.
219
219
  *
220
220
  * ---