@byloth/core 2.1.1 → 2.1.3
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 +2 -2
- package/dist/core.cjs.map +1 -1
- package/dist/core.esm.js +60 -61
- package/dist/core.esm.js.map +1 -1
- package/dist/core.global.js +2 -2
- package/dist/core.global.js.map +1 -1
- package/dist/core.umd.cjs +2 -2
- package/dist/core.umd.cjs.map +1 -1
- package/package.json +5 -5
- package/src/index.ts +1 -1
- package/src/models/callbacks/callable-object.ts +1 -2
- package/src/models/callbacks/publisher.ts +9 -4
- package/src/models/collections/map-view.ts +7 -3
- package/src/models/collections/types.ts +2 -1
- package/src/utils/date.ts +1 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@byloth/core",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.3",
|
|
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",
|
|
@@ -51,14 +51,14 @@
|
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@byloth/eslint-config-typescript": "^3.1.0",
|
|
53
53
|
"@eslint/compat": "^1.2.9",
|
|
54
|
-
"@types/node": "^22.15.
|
|
55
|
-
"@vitest/coverage-v8": "^3.
|
|
56
|
-
"eslint": "^9.
|
|
54
|
+
"@types/node": "^22.15.30",
|
|
55
|
+
"@vitest/coverage-v8": "^3.2.2",
|
|
56
|
+
"eslint": "^9.28.0",
|
|
57
57
|
"husky": "^9.1.7",
|
|
58
58
|
"jsdom": "^26.1.0",
|
|
59
59
|
"typescript": "^5.8.3",
|
|
60
60
|
"vite": "^6.3.5",
|
|
61
|
-
"vitest": "^3.
|
|
61
|
+
"vitest": "^3.2.2"
|
|
62
62
|
},
|
|
63
63
|
"scripts": {
|
|
64
64
|
"dev": "vite",
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
|
|
3
1
|
import type { Callback } from "./types.js";
|
|
4
2
|
|
|
5
3
|
const SmartFunction = (Function as unknown) as new<A extends unknown[] = [], R = void>(...args: string[])
|
|
@@ -34,6 +32,7 @@ const SmartFunction = (Function as unknown) as new<A extends unknown[] = [], R =
|
|
|
34
32
|
* The type signature of the callback function.
|
|
35
33
|
* It must be a function. Default is `(...args: any[]) => any`.
|
|
36
34
|
*/
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
37
36
|
export default abstract class CallableObject<T extends Callback<any[], any> = () => void>
|
|
38
37
|
extends SmartFunction<Parameters<T>, ReturnType<T>>
|
|
39
38
|
{
|
|
@@ -148,11 +148,11 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
148
148
|
*/
|
|
149
149
|
public subscribe<K extends keyof T>(event: K & string, subscriber: T[K]): () => void
|
|
150
150
|
{
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const subscribers = this._subscribers.get(event)!;
|
|
151
|
+
const subscribers = this._subscribers.get(event) ?? [];
|
|
154
152
|
subscribers.push(subscriber);
|
|
155
153
|
|
|
154
|
+
this._subscribers.set(event, subscribers);
|
|
155
|
+
|
|
156
156
|
return () =>
|
|
157
157
|
{
|
|
158
158
|
const index = subscribers.indexOf(subscriber);
|
|
@@ -189,7 +189,11 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
189
189
|
public unsubscribe<K extends keyof T>(event: K & string, subscriber: T[K]): void
|
|
190
190
|
{
|
|
191
191
|
const subscribers = this._subscribers.get(event);
|
|
192
|
-
if (!(subscribers))
|
|
192
|
+
if (!(subscribers))
|
|
193
|
+
{
|
|
194
|
+
throw new ReferenceException("Unable to unsubscribe the required subscriber. " +
|
|
195
|
+
"The subscription was already unsubscribed or was never subscribed.");
|
|
196
|
+
}
|
|
193
197
|
|
|
194
198
|
const index = subscribers.indexOf(subscriber);
|
|
195
199
|
if (index < 0)
|
|
@@ -199,6 +203,7 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
199
203
|
}
|
|
200
204
|
|
|
201
205
|
subscribers.splice(index, 1);
|
|
206
|
+
if (subscribers.length === 0) { this._subscribers.delete(event); }
|
|
202
207
|
}
|
|
203
208
|
|
|
204
209
|
public readonly [Symbol.toStringTag]: string = "Publisher";
|
|
@@ -112,10 +112,14 @@ export default class MapView<K, V> extends Map<K, V>
|
|
|
112
112
|
*/
|
|
113
113
|
public override delete(key: K): boolean
|
|
114
114
|
{
|
|
115
|
-
const
|
|
116
|
-
if (
|
|
115
|
+
const value = this.get(key);
|
|
116
|
+
if (value === undefined) { return false; }
|
|
117
117
|
|
|
118
|
-
|
|
118
|
+
super.delete(key);
|
|
119
|
+
|
|
120
|
+
this._publisher.publish("entry:remove", key, value);
|
|
121
|
+
|
|
122
|
+
return true;
|
|
119
123
|
}
|
|
120
124
|
|
|
121
125
|
/**
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2
2
|
import type MapView from "./map-view.js";
|
|
3
|
+
|
|
3
4
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
4
5
|
import type SetView from "./set-view.js";
|
|
5
6
|
|
|
@@ -17,7 +18,7 @@ import type SetView from "./set-view.js";
|
|
|
17
18
|
export interface MapViewEventsMap<K, V>
|
|
18
19
|
{
|
|
19
20
|
"entry:add": (key: K, value: V) => void;
|
|
20
|
-
"entry:remove": (key: K) => void;
|
|
21
|
+
"entry:remove": (key: K, value: V) => void;
|
|
21
22
|
|
|
22
23
|
"collection:clear": () => void;
|
|
23
24
|
}
|
package/src/utils/date.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/prefer-literal-enum-member */
|
|
1
2
|
|
|
2
3
|
import { RangeException, SmartIterator } from "../models/index.js";
|
|
3
4
|
|
|
@@ -15,8 +16,6 @@ import { RangeException, SmartIterator } from "../models/index.js";
|
|
|
15
16
|
*/
|
|
16
17
|
export enum TimeUnit
|
|
17
18
|
{
|
|
18
|
-
/* eslint-disable @typescript-eslint/prefer-literal-enum-member */
|
|
19
|
-
|
|
20
19
|
/**
|
|
21
20
|
* A millisecond: the base time unit.
|
|
22
21
|
*/
|