@oscarpalmer/atoms 0.194.0 → 0.195.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/dist/array/index.d.mts +2 -2
- package/dist/array/index.mjs +2 -2
- package/dist/array/move.mjs +1 -1
- package/dist/array/swap.mjs +1 -1
- package/dist/color/instance.mjs +1 -1
- package/dist/color/misc/state.d.mts +1 -1
- package/dist/color/models.d.mts +1 -1
- package/dist/color/space/hex.mjs +1 -1
- package/dist/color/space/rgb.mjs +1 -1
- package/dist/function/index.d.mts +1 -2
- package/dist/function/index.mjs +1 -2
- package/dist/function/limit.d.mts +6 -0
- package/dist/function/limit.mjs +2 -0
- package/dist/function/once.d.mts +3 -0
- package/dist/function/once.mjs +2 -1
- package/dist/function/retry.d.mts +24 -1
- package/dist/function/retry.mjs +1 -0
- package/dist/herald.d.mts +2 -71
- package/dist/herald.mjs +1 -109
- package/dist/index.d.mts +3325 -3282
- package/dist/index.mjs +1508 -1498
- package/dist/{array → internal/array}/get.d.mts +2 -2
- package/dist/{array → internal/array}/get.mjs +2 -2
- package/dist/{array → internal/array}/match.d.mts +2 -2
- package/dist/{array → internal/array}/match.mjs +2 -2
- package/dist/internal/defaults.d.mts +5 -0
- package/dist/internal/defaults.mjs +9 -0
- package/dist/{function → internal/function}/assert.d.mts +2 -2
- package/dist/{function → internal/function}/assert.mjs +2 -2
- package/dist/{function → internal/function}/memoize.d.mts +2 -2
- package/dist/{function → internal/function}/memoize.mjs +3 -3
- package/dist/{function → internal/function}/work.d.mts +2 -2
- package/dist/{function → internal/function}/work.mjs +2 -2
- package/dist/internal/herald.d.mts +71 -0
- package/dist/internal/herald.mjs +110 -0
- package/dist/internal/number.d.mts +1 -2
- package/dist/internal/number.mjs +1 -4
- package/dist/internal/{result.d.mts → result/misc.d.mts} +42 -3
- package/dist/internal/{result.mjs → result/misc.mjs} +29 -3
- package/dist/{result → internal/result}/models.d.mts +2 -2
- package/dist/{sized → internal/sized}/map.d.mts +1 -1
- package/dist/{sized → internal/sized}/map.mjs +2 -2
- package/dist/internal/{sized.d.mts → sized/misc.d.mts} +1 -1
- package/dist/internal/{sized.mjs → sized/misc.mjs} +2 -2
- package/dist/{string → internal/string}/case.d.mts +1 -1
- package/dist/{string → internal/string}/case.mjs +2 -2
- package/dist/internal/{string.d.mts → string/misc.d.mts} +1 -1
- package/dist/internal/{string.mjs → string/misc.mjs} +1 -1
- package/dist/internal/value/compare.mjs +1 -1
- package/dist/internal/value/has.d.mts +1 -1
- package/dist/internal/value/misc.d.mts +1 -1
- package/dist/internal/value/misc.mjs +2 -2
- package/dist/is.mjs +2 -2
- package/dist/logger.mjs +1 -1
- package/dist/promise/helpers.d.mts +1 -1
- package/dist/promise/helpers.mjs +2 -2
- package/dist/promise/index.d.mts +1 -1
- package/dist/promise/misc.d.mts +1 -1
- package/dist/promise/misc.mjs +2 -1
- package/dist/promise/models.d.mts +1 -1
- package/dist/query.mjs +1 -1
- package/dist/queue.mjs +1 -4
- package/dist/random.mjs +1 -1
- package/dist/result/index.d.mts +4 -1
- package/dist/result/index.mjs +2 -1
- package/dist/result/match.d.mts +4 -1
- package/dist/result/match.mjs +2 -1
- package/dist/result/misc.d.mts +3 -42
- package/dist/result/misc.mjs +2 -28
- package/dist/result/work/flow.d.mts +4 -1
- package/dist/result/work/flow.mjs +3 -2
- package/dist/result/work/pipe.d.mts +4 -1
- package/dist/result/work/pipe.mjs +3 -2
- package/dist/sized/set.mjs +1 -1
- package/dist/string/fuzzy.mjs +2 -2
- package/dist/string/index.d.mts +1 -1
- package/dist/string/index.mjs +1 -1
- package/dist/string/match.mjs +1 -1
- package/dist/string/normalize.mjs +2 -2
- package/dist/string/template.mjs +1 -1
- package/dist/subscription.d.mts +2 -0
- package/dist/subscription.mjs +2 -0
- package/dist/value/diff.mjs +1 -1
- package/dist/value/smush.mjs +1 -1
- package/package.json +23 -11
- /package/dist/{result → internal/result}/models.mjs +0 -0
package/dist/array/index.d.mts
CHANGED
|
@@ -3,15 +3,15 @@ import { exists } from "./exists.mjs";
|
|
|
3
3
|
import { find, findLast } from "./find.mjs";
|
|
4
4
|
import { flatten } from "./flatten.mjs";
|
|
5
5
|
import { range, times } from "./from.mjs";
|
|
6
|
-
import { getArray } from "./get.mjs";
|
|
7
6
|
import { chunk } from "../internal/array/chunk.mjs";
|
|
8
7
|
import { compact } from "../internal/array/compact.mjs";
|
|
8
|
+
import { getArray } from "../internal/array/get.mjs";
|
|
9
9
|
import { indexOf, lastIndexOf } from "../internal/array/index-of.mjs";
|
|
10
|
+
import { ArrayComparison, endsWithArray, getArrayComparison, includesArray, indexOfArray, startsWithArray } from "../internal/array/match.mjs";
|
|
10
11
|
import { shuffle } from "../internal/array/shuffle.mjs";
|
|
11
12
|
import { insert } from "./insert.mjs";
|
|
12
13
|
import { intersection } from "./intersection.mjs";
|
|
13
14
|
import { partition } from "./partition.mjs";
|
|
14
|
-
import { ArrayComparison, endsWithArray, getArrayComparison, includesArray, indexOfArray, startsWithArray } from "./match.mjs";
|
|
15
15
|
import { push } from "./push.mjs";
|
|
16
16
|
import { reverse } from "./reverse.mjs";
|
|
17
17
|
import { select } from "./select.mjs";
|
package/dist/array/index.mjs
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import { chunk } from "../internal/array/chunk.mjs";
|
|
2
2
|
import { compact } from "../internal/array/compact.mjs";
|
|
3
|
+
import { getArray } from "../internal/array/get.mjs";
|
|
3
4
|
import { indexOf, lastIndexOf } from "../internal/array/index-of.mjs";
|
|
5
|
+
import { endsWithArray, getArrayComparison, includesArray, indexOfArray, startsWithArray } from "../internal/array/match.mjs";
|
|
4
6
|
import { shuffle } from "../internal/array/shuffle.mjs";
|
|
5
7
|
import { difference } from "./difference.mjs";
|
|
6
8
|
import { exists } from "./exists.mjs";
|
|
7
9
|
import { find, findLast } from "./find.mjs";
|
|
8
10
|
import { flatten } from "./flatten.mjs";
|
|
9
11
|
import { range, times } from "./from.mjs";
|
|
10
|
-
import { getArray } from "./get.mjs";
|
|
11
12
|
import { insert } from "./insert.mjs";
|
|
12
13
|
import { intersection } from "./intersection.mjs";
|
|
13
14
|
import { partition } from "./partition.mjs";
|
|
14
|
-
import { endsWithArray, getArrayComparison, includesArray, indexOfArray, startsWithArray } from "./match.mjs";
|
|
15
15
|
import { push } from "./push.mjs";
|
|
16
16
|
import { reverse } from "./reverse.mjs";
|
|
17
17
|
import { select } from "./select.mjs";
|
package/dist/array/move.mjs
CHANGED
package/dist/array/swap.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { getArrayCallback } from "../internal/array/callbacks.mjs";
|
|
2
2
|
import { indexOf } from "../internal/array/index-of.mjs";
|
|
3
|
-
import { indexOfArray } from "
|
|
3
|
+
import { indexOfArray } from "../internal/array/match.mjs";
|
|
4
4
|
import { arraysOverlap } from "../internal/array/overlap.mjs";
|
|
5
5
|
//#region src/array/swap.ts
|
|
6
6
|
function swap(array, first, second, third) {
|
package/dist/color/instance.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { herald } from "../herald.mjs";
|
|
1
|
+
import { herald } from "../internal/herald.mjs";
|
|
2
2
|
import { COLOR_PROPERTY, COLOR_TYPE } from "./constants.mjs";
|
|
3
3
|
import { getAlpha } from "./misc/alpha.mjs";
|
|
4
4
|
import { getColorFromState, getColorState, setHSLColor, setHWBColor, setHexColor, setRGBColor } from "./misc/state.mjs";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Herald } from "../../herald.mjs";
|
|
1
|
+
import { Herald } from "../../internal/herald.mjs";
|
|
2
2
|
import { Color, ColorChanges, ColorState, ColorType } from "../models.mjs";
|
|
3
3
|
//#region src/color/misc/state.d.ts
|
|
4
4
|
declare function getColorFromState<Type extends ColorType>(state: ColorState, type: Type): NonNullable<ColorState[Type]>;
|
package/dist/color/models.d.mts
CHANGED
package/dist/color/space/hex.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { join } from "../../internal/string.mjs";
|
|
1
|
+
import { join } from "../../internal/string/misc.mjs";
|
|
2
2
|
import { COLOR_ALPHA, COLOR_DEFAULTS, COLOR_EXPRESSION, COLOR_LENGTHS, COLOR_MAX, COLOR_TYPE } from "../constants.mjs";
|
|
3
3
|
import { getHexValue, getPercentage } from "../misc/get.mjs";
|
|
4
4
|
import { isHexColor } from "../misc/is.mjs";
|
package/dist/color/space/rgb.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { join } from "../../internal/string.mjs";
|
|
1
|
+
import { join } from "../../internal/string/misc.mjs";
|
|
2
2
|
import { COLOR_DEFAULTS, COLOR_MAX, COLOR_TYPE } from "../constants.mjs";
|
|
3
3
|
import { getAlpha, getAlphaValue } from "../misc/alpha.mjs";
|
|
4
4
|
import { getHexValue } from "../misc/get.mjs";
|
package/dist/function/index.mjs
CHANGED
|
@@ -36,6 +36,9 @@ declare function asyncThrottle<Callback extends GenericAsyncCallback | GenericCa
|
|
|
36
36
|
* @returns Debounced callback handler with a `cancel` method
|
|
37
37
|
*/
|
|
38
38
|
declare function debounce<Callback extends GenericCallback>(callback: Callback, time?: number): CancelableCallback<Callback>;
|
|
39
|
+
declare namespace debounce {
|
|
40
|
+
var async: typeof asyncDebounce;
|
|
41
|
+
}
|
|
39
42
|
/**
|
|
40
43
|
* Throttle a function, ensuring it is only called once every `time` milliseconds
|
|
41
44
|
*
|
|
@@ -44,5 +47,8 @@ declare function debounce<Callback extends GenericCallback>(callback: Callback,
|
|
|
44
47
|
* @returns Throttled callback handler with a `cancel` method
|
|
45
48
|
*/
|
|
46
49
|
declare function throttle<Callback extends GenericCallback>(callback: Callback, time?: number): CancelableCallback<Callback>;
|
|
50
|
+
declare namespace throttle {
|
|
51
|
+
var async: typeof asyncThrottle;
|
|
52
|
+
}
|
|
47
53
|
//#endregion
|
|
48
54
|
export { asyncDebounce, asyncThrottle, debounce, throttle };
|
package/dist/function/limit.mjs
CHANGED
|
@@ -52,6 +52,8 @@ function debounce(callback, time) {
|
|
|
52
52
|
function throttle(callback, time) {
|
|
53
53
|
return getTimer(TIMER_THROTTLE, callback, time);
|
|
54
54
|
}
|
|
55
|
+
debounce.async = asyncDebounce;
|
|
56
|
+
throttle.async = asyncThrottle;
|
|
55
57
|
Object.defineProperty(debounce, "async", { value: asyncDebounce });
|
|
56
58
|
Object.defineProperty(throttle, "async", { value: asyncThrottle });
|
|
57
59
|
//#endregion
|
package/dist/function/once.d.mts
CHANGED
|
@@ -16,5 +16,8 @@ declare function asyncOnce<Callback extends GenericAsyncCallback>(callback: Call
|
|
|
16
16
|
* @returns _Once_ callback
|
|
17
17
|
*/
|
|
18
18
|
declare function once<Callback extends GenericCallback>(callback: Callback): OnceCallback<Callback>;
|
|
19
|
+
declare namespace once {
|
|
20
|
+
var async: typeof asyncOnce;
|
|
21
|
+
}
|
|
19
22
|
//#endregion
|
|
20
23
|
export { asyncOnce, once };
|
package/dist/function/once.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { assert } from "
|
|
1
|
+
import { assert } from "../internal/function/assert.mjs";
|
|
2
2
|
//#region src/function/once.ts
|
|
3
3
|
/**
|
|
4
4
|
* Create an asynchronous function that can only be called once, rejecting or resolving the same result on subsequent calls
|
|
@@ -113,6 +113,7 @@ function once(callback) {
|
|
|
113
113
|
}
|
|
114
114
|
const ONCE_MESSAGE_CLEARED = "Once has been cleared";
|
|
115
115
|
const ONCE_MESSAGE_EXPECTATION = "Once expected a function";
|
|
116
|
+
once.async = asyncOnce;
|
|
116
117
|
Object.defineProperty(once, "async", { value: asyncOnce });
|
|
117
118
|
//#endregion
|
|
118
119
|
export { asyncOnce, once };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { GenericCallback } from "../models.mjs";
|
|
1
|
+
import { GenericAsyncCallback, GenericCallback } from "../models.mjs";
|
|
2
2
|
//#region src/function/retry.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* An error thrown when a retry fails
|
|
@@ -12,6 +12,26 @@ type RetryOptions = {
|
|
|
12
12
|
times?: number;
|
|
13
13
|
when?: (error: unknown) => boolean;
|
|
14
14
|
};
|
|
15
|
+
/**
|
|
16
|
+
* Retry a callback a specified number of times, with a delay between attempts
|
|
17
|
+
*
|
|
18
|
+
* _Available as `asyncRetry` and `retry.async`_
|
|
19
|
+
*
|
|
20
|
+
* @param callback Callback to retry
|
|
21
|
+
* @param options Retry options
|
|
22
|
+
* @returns Callback result
|
|
23
|
+
*/
|
|
24
|
+
declare function asyncRetry<Callback extends GenericAsyncCallback>(callback: Callback, options?: RetryOptions): Promise<Awaited<ReturnType<Callback>>>;
|
|
25
|
+
/**
|
|
26
|
+
* Retry a callback a specified number of times, with a delay between attempts
|
|
27
|
+
*
|
|
28
|
+
* _Available as `asyncRetry` and `retry.async`_
|
|
29
|
+
*
|
|
30
|
+
* @param callback Callback to retry
|
|
31
|
+
* @param options Retry options
|
|
32
|
+
* @returns Callback result
|
|
33
|
+
*/
|
|
34
|
+
declare function asyncRetry<Callback extends GenericCallback>(callback: Callback, options?: RetryOptions): Promise<ReturnType<Callback>>;
|
|
15
35
|
/**
|
|
16
36
|
* Retry a callback a specified number of times
|
|
17
37
|
*
|
|
@@ -20,5 +40,8 @@ type RetryOptions = {
|
|
|
20
40
|
* @returns Callback result
|
|
21
41
|
*/
|
|
22
42
|
declare function retry<Callback extends GenericCallback>(callback: Callback, options?: Omit<RetryOptions, 'delay'>): ReturnType<Callback>;
|
|
43
|
+
declare namespace retry {
|
|
44
|
+
var async: typeof asyncRetry;
|
|
45
|
+
}
|
|
23
46
|
//#endregion
|
|
24
47
|
export { RetryError, RetryOptions, retry };
|
package/dist/function/retry.mjs
CHANGED
|
@@ -84,6 +84,7 @@ function shouldRetry() {
|
|
|
84
84
|
const RETRY_ERROR_NAME = "RetryError";
|
|
85
85
|
const RETRY_MESSAGE_EXPECTATION = "Retry expected a function";
|
|
86
86
|
const RETRY_MESSAGE_FAILED = "Retry failed";
|
|
87
|
+
retry.async = asyncRetry;
|
|
87
88
|
Object.defineProperty(retry, "async", { value: asyncRetry });
|
|
88
89
|
//#endregion
|
|
89
90
|
export { RetryError, retry };
|
package/dist/herald.d.mts
CHANGED
|
@@ -1,71 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
//#region src/herald.d.ts
|
|
4
|
-
type Herald<Events extends Record<string, GenericCallback>> = {
|
|
5
|
-
readonly events: HeraldEvents<Events>;
|
|
6
|
-
/**
|
|
7
|
-
* Remove all event subscribers
|
|
8
|
-
*/
|
|
9
|
-
clear(): void;
|
|
10
|
-
/**
|
|
11
|
-
* Emit an event with parameters
|
|
12
|
-
*
|
|
13
|
-
* @param event Event name
|
|
14
|
-
* @param parameters Event parameters
|
|
15
|
-
*/
|
|
16
|
-
emit<Event extends keyof Events>(event: Event, ...parameters: Parameters<Events[Event]>): void;
|
|
17
|
-
/**
|
|
18
|
-
* Is the event observed by any subscribers?
|
|
19
|
-
*
|
|
20
|
-
* @param event Event name
|
|
21
|
-
* @returns `true` if the event is observed, otherwise `false`
|
|
22
|
-
*/
|
|
23
|
-
observed<Event extends keyof Events>(event: Event): boolean;
|
|
24
|
-
/**
|
|
25
|
-
* Subscribe to an event with a callback
|
|
26
|
-
*
|
|
27
|
-
* @param event Event name
|
|
28
|
-
* @param callback Callback function
|
|
29
|
-
* @param signal Optional abort signal to cancel the subscription
|
|
30
|
-
* @returns Subscription instance
|
|
31
|
-
*/
|
|
32
|
-
subscribe<Event extends keyof Events>(event: Event, callback: Events[Event], signal?: AbortSignal): Subscription;
|
|
33
|
-
};
|
|
34
|
-
type HeraldEvents<Events extends Record<string, GenericCallback>> = { [Key in keyof Events]: Key extends '*' ? never : (callback: Events[Key], signal?: AbortSignal) => void; };
|
|
35
|
-
type HeraldOnCreate<Events extends Record<string, GenericCallback>> = <Event extends keyof Events>(event: Event, callback: Events[Event], subscription: Subscription) => void;
|
|
36
|
-
type HeraldOptions<Events extends Record<string, GenericCallback>> = {
|
|
37
|
-
names: Array<keyof Events>;
|
|
38
|
-
property?: SubscriptionProperty;
|
|
39
|
-
onCreate?: HeraldOnCreate<Events>;
|
|
40
|
-
};
|
|
41
|
-
/**
|
|
42
|
-
* Create a _Herald_ for announcing named events
|
|
43
|
-
*
|
|
44
|
-
* @param names Event names
|
|
45
|
-
* @param property Optional property for subscription identification _(defaults to `$herald`)_
|
|
46
|
-
* @returns _Herald_ instance
|
|
47
|
-
*/
|
|
48
|
-
declare function herald<Events extends Record<string, GenericCallback>>(options: HeraldOptions<Events>): Herald<Events>;
|
|
49
|
-
/**
|
|
50
|
-
* Is the value events for a herald?
|
|
51
|
-
*
|
|
52
|
-
* @param value Value to check
|
|
53
|
-
* @returns `true` if the value is events for a herald, otherwise `false`
|
|
54
|
-
*/
|
|
55
|
-
declare function isHeraldEvents<Events extends Record<string, GenericCallback> = Record<string, GenericCallback>>(value: unknown): value is HeraldEvents<Events>;
|
|
56
|
-
/**
|
|
57
|
-
* Is the value a herald?
|
|
58
|
-
*
|
|
59
|
-
* @param value Value to check
|
|
60
|
-
* @returns `true` if the value is a herald, otherwise `false`
|
|
61
|
-
*/
|
|
62
|
-
declare function isHerald<Events extends Record<string, GenericCallback> = Record<string, GenericCallback>>(value: unknown): value is Herald<Events>;
|
|
63
|
-
/**
|
|
64
|
-
* Is the value a herald subscription?
|
|
65
|
-
*
|
|
66
|
-
* @param value Value to check
|
|
67
|
-
* @returns `true` if the value is a herald subscription, otherwise `false`
|
|
68
|
-
*/
|
|
69
|
-
declare function isHeraldSubscription(value: unknown): value is Subscription;
|
|
70
|
-
//#endregion
|
|
71
|
-
export { Herald, HeraldEvents, HeraldOptions, herald, isHerald, isHeraldEvents, isHeraldSubscription };
|
|
1
|
+
import { herald, isHerald, isHeraldEvents, isHeraldSubscription } from "./internal/herald.mjs";
|
|
2
|
+
export { herald, isHerald, isHeraldEvents, isHeraldSubscription };
|
package/dist/herald.mjs
CHANGED
|
@@ -1,110 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { createSubscriptions, isSubscription } from "./internal/subscription.mjs";
|
|
3
|
-
//#region src/herald.ts
|
|
4
|
-
function createEvents(herald, state) {
|
|
5
|
-
const events = {};
|
|
6
|
-
Object.defineProperty(events, HERALD_PROPERTY, { value: HERALD_NAME_EVENTS });
|
|
7
|
-
for (const key of state.keys) events[key] = (callback, signal) => herald.subscribe(key, callback, signal);
|
|
8
|
-
return Object.freeze(events);
|
|
9
|
-
}
|
|
10
|
-
function emitForHerald(state, event, ...parameters) {
|
|
11
|
-
const items = state.subscriptions.state.values.from.keyed?.get(event);
|
|
12
|
-
if (items == null || items.size === 0) return;
|
|
13
|
-
for (const [callback] of items) callback(...parameters);
|
|
14
|
-
}
|
|
15
|
-
function getHeraldSubscriptionProperty(input) {
|
|
16
|
-
if (input == null) return heraldSubscription;
|
|
17
|
-
const property = isPlainObject(input) ? input : {};
|
|
18
|
-
if (typeof property.key !== "string" || property.value != null && typeof property.value !== "string") throw new Error(HERALD_MESSAGE_PROPERTY);
|
|
19
|
-
return {
|
|
20
|
-
key: property.key,
|
|
21
|
-
value: property.value
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
function getHeraldState(input) {
|
|
25
|
-
const options = isPlainObject(input) ? input : {};
|
|
26
|
-
if (!Array.isArray(options.names) || options.names.length === 0 || !options.names.every((name) => typeof name === "string")) throw new Error(HERALD_MESSAGE_ARRAY);
|
|
27
|
-
if (options.onCreate != null && typeof options.onCreate !== "function") throw new Error(HERALD_MESSAGE_ONCREATE);
|
|
28
|
-
const keys = new Set(options.names);
|
|
29
|
-
const property = getHeraldSubscriptionProperty(options.property);
|
|
30
|
-
return {
|
|
31
|
-
keys,
|
|
32
|
-
subscriptions: createSubscriptions({
|
|
33
|
-
keys,
|
|
34
|
-
property
|
|
35
|
-
}),
|
|
36
|
-
onCreate: options.onCreate
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Create a _Herald_ for announcing named events
|
|
41
|
-
*
|
|
42
|
-
* @param names Event names
|
|
43
|
-
* @param property Optional property for subscription identification _(defaults to `$herald`)_
|
|
44
|
-
* @returns _Herald_ instance
|
|
45
|
-
*/
|
|
46
|
-
function herald(options) {
|
|
47
|
-
const state = getHeraldState(options);
|
|
48
|
-
const instance = {
|
|
49
|
-
clear: () => state.subscriptions.clear(),
|
|
50
|
-
emit: (event, ...parameters) => emitForHerald(state, event, ...parameters),
|
|
51
|
-
observed: (event) => (state.subscriptions.state.items.keyed?.get(event)?.size ?? 0) > 0,
|
|
52
|
-
subscribe: (key, callback, signal) => subscribeToHerald(state, key, callback, signal)
|
|
53
|
-
};
|
|
54
|
-
Object.defineProperties(instance, {
|
|
55
|
-
[HERALD_PROPERTY]: { value: HERALD_NAME_HERALD },
|
|
56
|
-
events: {
|
|
57
|
-
enumerable: true,
|
|
58
|
-
value: createEvents(instance, state)
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
return Object.freeze(instance);
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Is the value events for a herald?
|
|
65
|
-
*
|
|
66
|
-
* @param value Value to check
|
|
67
|
-
* @returns `true` if the value is events for a herald, otherwise `false`
|
|
68
|
-
*/
|
|
69
|
-
function isHeraldEvents(value) {
|
|
70
|
-
return isHeraldInstance(HERALD_NAME_EVENTS, value);
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Is the value a herald?
|
|
74
|
-
*
|
|
75
|
-
* @param value Value to check
|
|
76
|
-
* @returns `true` if the value is a herald, otherwise `false`
|
|
77
|
-
*/
|
|
78
|
-
function isHerald(value) {
|
|
79
|
-
return isHeraldInstance(HERALD_NAME_HERALD, value);
|
|
80
|
-
}
|
|
81
|
-
function isHeraldInstance(name, value) {
|
|
82
|
-
return isPlainObject(value) && HERALD_PROPERTY in value && value[HERALD_PROPERTY] === name;
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Is the value a herald subscription?
|
|
86
|
-
*
|
|
87
|
-
* @param value Value to check
|
|
88
|
-
* @returns `true` if the value is a herald subscription, otherwise `false`
|
|
89
|
-
*/
|
|
90
|
-
function isHeraldSubscription(value) {
|
|
91
|
-
return isSubscription(value) && value[HERALD_PROPERTY] === "subscription";
|
|
92
|
-
}
|
|
93
|
-
function subscribeToHerald(state, key, callback, signal) {
|
|
94
|
-
const [subscription, existing] = state.subscriptions.create({
|
|
95
|
-
key,
|
|
96
|
-
signal,
|
|
97
|
-
value: callback
|
|
98
|
-
});
|
|
99
|
-
if (!existing) state.onCreate?.(key, callback, subscription);
|
|
100
|
-
return subscription;
|
|
101
|
-
}
|
|
102
|
-
const HERALD_PROPERTY = "$herald";
|
|
103
|
-
const HERALD_MESSAGE_ARRAY = "Herald requires an array of event names.";
|
|
104
|
-
const HERALD_MESSAGE_ONCREATE = `Herald requires a valid onCreate callback for subscription creation`;
|
|
105
|
-
const HERALD_MESSAGE_PROPERTY = `Herald requires valid property information for subscription identification`;
|
|
106
|
-
const HERALD_NAME_EVENTS = "events";
|
|
107
|
-
const HERALD_NAME_HERALD = "herald";
|
|
108
|
-
const heraldSubscription = { key: HERALD_PROPERTY };
|
|
109
|
-
//#endregion
|
|
1
|
+
import { herald, isHerald, isHeraldEvents, isHeraldSubscription } from "./internal/herald.mjs";
|
|
110
2
|
export { herald, isHerald, isHeraldEvents, isHeraldSubscription };
|