@oscarpalmer/atoms 0.198.0 → 0.199.0

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/beacon.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { noop } from "./internal/function/misc.mjs";
2
2
  import { isPlainObject } from "./internal/is.mjs";
3
- import { createSubscriptions, isSubscription } from "./internal/subscription.mjs";
3
+ import { SUBSCRIPTION_NAME, isSubscription, subscriptions } from "./internal/subscription.mjs";
4
4
  //#region src/beacon.ts
5
5
  /**
6
6
  * Create a new beacon
@@ -14,8 +14,8 @@ function beacon(value, options) {
14
14
  value,
15
15
  active: true,
16
16
  observable: void 0,
17
- options: getBeaconOptions(options),
18
- subscriptions: createSubscriptions({ property: beaconSubscription })
17
+ options: createBeaconOptions(options),
18
+ subscriptions: subscriptions({ property: beaconSubscription })
19
19
  };
20
20
  const instance = {
21
21
  deactivate: () => finishBeacon(state, false),
@@ -40,28 +40,32 @@ function beacon(value, options) {
40
40
  });
41
41
  return Object.freeze(instance);
42
42
  }
43
- function finishBeacon(state, emit) {
44
- if (!state.active) return;
45
- state.active = false;
46
- for (const [, observer] of state.subscriptions.state.values.to.any) if (emit) observer.complete?.();
47
- state.subscriptions.clear();
48
- state.observable?.deactivate();
49
- state.observable = void 0;
50
- }
51
- function getBeaconOptions(input) {
43
+ function createBeaconOptions(input) {
52
44
  const options = isPlainObject(input) ? input : {};
53
45
  options.equal = typeof options.equal === "function" ? options.equal : Object.is;
54
46
  return options;
55
47
  }
56
- function getObservable(state) {
57
- if (!state.active) throw new Error(BEACON_MESSAGE_RETRIEVE);
58
- state.observable ??= observeBeacon(state);
59
- return state.observable;
60
- }
61
- function getObservableCallback(value) {
62
- return typeof value === "function" ? value : noop;
48
+ function createObservable(beacon) {
49
+ const state = {
50
+ beacon,
51
+ active: beacon.active
52
+ };
53
+ const instance = {
54
+ deactivate: () => {
55
+ state.active = false;
56
+ },
57
+ subscribe: (first, second, third) => subscribeToObservable(state, first, second, third)
58
+ };
59
+ Object.defineProperties(instance, {
60
+ [BEACON_PROPERTY]: { value: BEACON_OBSERVABLE },
61
+ active: {
62
+ enumerable: true,
63
+ get: () => beacon.active && state.active
64
+ }
65
+ });
66
+ return Object.freeze(instance);
63
67
  }
64
- function getObserver(first, second, third) {
68
+ function createObserver(first, second, third) {
65
69
  let observer = { next: noop };
66
70
  if (typeof first === "function") observer = {
67
71
  error: getObservableCallback(second),
@@ -75,6 +79,22 @@ function getObserver(first, second, third) {
75
79
  }
76
80
  return observer;
77
81
  }
82
+ function finishBeacon(state, emit) {
83
+ if (!state.active) return;
84
+ state.active = false;
85
+ for (const [, observer] of state.subscriptions.values.to.any) if (emit) observer.complete?.();
86
+ state.subscriptions.clear();
87
+ state.observable?.deactivate();
88
+ state.observable = void 0;
89
+ }
90
+ function getObservable(state) {
91
+ if (!state.active) throw new Error(BEACON_MESSAGE_RETRIEVE);
92
+ state.observable ??= createObservable(state);
93
+ return state.observable;
94
+ }
95
+ function getObservableCallback(value) {
96
+ return typeof value === "function" ? value : noop;
97
+ }
78
98
  /**
79
99
  * Is the value a beacon?
80
100
  *
@@ -105,29 +125,9 @@ function isBeaconSubscription(value) {
105
125
  function isObservable(value) {
106
126
  return isBeaconInstance(BEACON_OBSERVABLE, value);
107
127
  }
108
- function observeBeacon(beacon) {
109
- const state = {
110
- beacon,
111
- active: beacon.active
112
- };
113
- const instance = {
114
- deactivate: () => {
115
- state.active = false;
116
- },
117
- subscribe: (first, second, third) => subscribeToObservable(state, first, second, third)
118
- };
119
- Object.defineProperties(instance, {
120
- [BEACON_PROPERTY]: { value: BEACON_OBSERVABLE },
121
- active: {
122
- enumerable: true,
123
- get: () => beacon.active && state.active
124
- }
125
- });
126
- return Object.freeze(instance);
127
- }
128
128
  function subscribeToObservable(state, first, second, third) {
129
129
  if (!state.beacon.active || !state.active) throw new Error(BEACON_MESSAGE_SUBSCRIBE);
130
- const observer = getObserver(first, second, third);
130
+ const observer = createObserver(first, second, third);
131
131
  const [subscription] = state.beacon.subscriptions.create({
132
132
  isActive: () => state.beacon.active && state.active,
133
133
  value: observer
@@ -141,7 +141,7 @@ function updateBeacon(type, state, value, finish) {
141
141
  if (state.options.equal(state.value, value)) return;
142
142
  state.value = value;
143
143
  }
144
- for (const [, observer] of state.subscriptions.state.values.to.any) observer[type]?.(value);
144
+ for (const [, observer] of state.subscriptions.values.to.any) observer[type]?.(value);
145
145
  if (finish === true) finishBeacon(state, true);
146
146
  }
147
147
  const BEACON_MESSAGE_RETRIEVE = "Cannot retrieve observable from a closed beacon";
@@ -151,6 +151,9 @@ const BEACON_NAME = "beacon";
151
151
  const BEACON_OBSERVABLE = "observable";
152
152
  const BEACON_TYPE_ERROR = "error";
153
153
  const BEACON_TYPE_NEXT = "next";
154
- const beaconSubscription = { key: BEACON_PROPERTY };
154
+ const beaconSubscription = {
155
+ key: BEACON_PROPERTY,
156
+ value: SUBSCRIPTION_NAME
157
+ };
155
158
  //#endregion
156
159
  export { beacon, isBeacon, isBeaconSubscription, isObservable };
@@ -1,3 +1,4 @@
1
+ import { SUBSCRIPTION_NAME } from "../internal/subscription.mjs";
1
2
  import { herald } from "../internal/herald.mjs";
2
3
  import { COLOR_PROPERTY, COLOR_TYPE } from "./constants.mjs";
3
4
  import { getAlpha } from "./misc/alpha.mjs";
@@ -31,22 +32,22 @@ function color(value) {
31
32
  enumerable: true,
32
33
  value: changes.events
33
34
  },
34
- hex: getColorProperty(COLOR_TYPE.hex, instance, state, changes, false),
35
- hexa: getColorProperty(COLOR_TYPE.hex, instance, state, changes, true),
36
- hsl: getColorProperty(COLOR_TYPE.hsl, instance, state, changes, false),
37
- hsla: getColorProperty(COLOR_TYPE.hsl, instance, state, changes, true),
38
- hwb: getColorProperty(COLOR_TYPE.hwb, instance, state, changes, false),
39
- hwba: getColorProperty(COLOR_TYPE.hwb, instance, state, changes, true),
35
+ hex: createColorProperty(COLOR_TYPE.hex, instance, state, changes, false),
36
+ hexa: createColorProperty(COLOR_TYPE.hex, instance, state, changes, true),
37
+ hsl: createColorProperty(COLOR_TYPE.hsl, instance, state, changes, false),
38
+ hsla: createColorProperty(COLOR_TYPE.hsl, instance, state, changes, true),
39
+ hwb: createColorProperty(COLOR_TYPE.hwb, instance, state, changes, false),
40
+ hwba: createColorProperty(COLOR_TYPE.hwb, instance, state, changes, true),
40
41
  origin: {
41
42
  enumerable: true,
42
43
  get: () => state.origin
43
44
  },
44
- rgb: getColorProperty(COLOR_TYPE.rgb, instance, state, changes, false),
45
- rgba: getColorProperty(COLOR_TYPE.rgb, instance, state, changes, true)
45
+ rgb: createColorProperty(COLOR_TYPE.rgb, instance, state, changes, false),
46
+ rgba: createColorProperty(COLOR_TYPE.rgb, instance, state, changes, true)
46
47
  });
47
48
  return Object.freeze(instance);
48
49
  }
49
- function getColorProperty(space, color, state, changes, alpha) {
50
+ function createColorProperty(space, color, state, changes, alpha) {
50
51
  const setValue = setters[space];
51
52
  return {
52
53
  enumerable: true,
@@ -71,7 +72,10 @@ function onCreateSubscription(instance, event, callback) {
71
72
  function setAlphaValue(state, value) {
72
73
  if (typeof value === "number" && !Number.isNaN(value)) state.alpha = getAlpha(value, false);
73
74
  }
74
- const colorSubscription = { key: COLOR_PROPERTY.name };
75
+ const colorSubscription = {
76
+ key: COLOR_PROPERTY.name,
77
+ value: SUBSCRIPTION_NAME
78
+ };
75
79
  const setters = {
76
80
  hex: setHexColor,
77
81
  hsl: setHSLColor,
@@ -1,4 +1,5 @@
1
1
  import { isPlainObject } from "../internal/is.mjs";
2
+ import { getNumberOrDefault } from "../internal/defaults.mjs";
2
3
  import { TIMER_WAIT, getTimer } from "../internal/function/timer.mjs";
3
4
  //#region src/function/retry.ts
4
5
  /**
@@ -35,7 +36,7 @@ async function asyncRetry(callback, options) {
35
36
  }
36
37
  }
37
38
  }
38
- const { delay, times, when } = getRetryOptions(options);
39
+ const { delay, times, when } = createRetryOptions(options);
39
40
  const timer = getTimer(TIMER_WAIT, handle, delay);
40
41
  let attempts = 0;
41
42
  let rejector;
@@ -46,14 +47,11 @@ async function asyncRetry(callback, options) {
46
47
  handle();
47
48
  });
48
49
  }
49
- function getRetryNumber(value) {
50
- return typeof value === "number" && value > 0 ? value : 0;
51
- }
52
- function getRetryOptions(input) {
50
+ function createRetryOptions(input) {
53
51
  const options = isPlainObject(input) ? input : {};
54
52
  return {
55
- delay: getRetryNumber(options.delay),
56
- times: getRetryNumber(options.times),
53
+ delay: getNumberOrDefault(options.delay, 0),
54
+ times: getNumberOrDefault(options.times, 0),
57
55
  when: typeof options.when === "function" ? options.when : shouldRetry
58
56
  };
59
57
  }
@@ -66,7 +64,7 @@ function getRetryOptions(input) {
66
64
  */
67
65
  function retry(callback, options) {
68
66
  if (typeof callback !== "function") throw new TypeError(RETRY_MESSAGE_EXPECTATION);
69
- const { times, when } = getRetryOptions(options);
67
+ const { times, when } = createRetryOptions(options);
70
68
  let last;
71
69
  for (let index = 0; index <= times; index += 1) try {
72
70
  return callback();
package/dist/index.d.mts CHANGED
@@ -6723,6 +6723,27 @@ type Subscription = {
6723
6723
  */
6724
6724
  unsubscribe(): void;
6725
6725
  };
6726
+ /**
6727
+ * Parameters for creating a subscription
6728
+ */
6729
+ type SubscriptionParameters = {
6730
+ /**
6731
+ * Is the owner of the subscription alive? _(defaults to `true`)_
6732
+ */
6733
+ isActive?: () => boolean;
6734
+ /**
6735
+ * Key used to identify a subscription _(defaults to `undefined`)_
6736
+ */
6737
+ key?: unknown;
6738
+ /**
6739
+ * Signal used to abort the subscription _(defaults to `undefined`)_
6740
+ */
6741
+ signal?: AbortSignal;
6742
+ /**
6743
+ * Value used to identify a subscription
6744
+ */
6745
+ value: unknown;
6746
+ };
6726
6747
  /**
6727
6748
  * Property information for subscription identification
6728
6749
  */
@@ -6732,9 +6753,63 @@ type SubscriptionProperty = {
6732
6753
  */
6733
6754
  key: string;
6734
6755
  /**
6735
- * Value of the property used to identify a subscription _(defaults to `subscription`)_
6756
+ * Value of the property used to identify a subscription
6736
6757
  */
6737
- value?: unknown;
6758
+ value: unknown;
6759
+ };
6760
+ type Subscriptions = {
6761
+ /**
6762
+ * Items of the subscriptions store
6763
+ *
6764
+ * - `any` - Set of subscriptions _(for unkeyed subscriptions)_
6765
+ * - `keyed` - Map of keys to sets of subscriptions _(for keyed subscriptions)_
6766
+ */
6767
+ readonly items: Readonly<SubscriptionsItems>;
6768
+ /**
6769
+ * Values of the subscriptions store
6770
+ *
6771
+ * - `from.any` - Map of values to subscriptions _(for unkeyed subscriptions)_
6772
+ * - `from.keyed` - Map of keys to maps of values to subscriptions _(for keyed subscriptions)_
6773
+ * - `to.any` - Map of subscriptions to values _(for unkeyed subscriptions)_
6774
+ * - `to.keyed` - Map of keys to maps of subscriptions to values _(for keyed subscriptions)_
6775
+ */
6776
+ readonly values: Readonly<SubscriptionsValues>;
6777
+ /**
6778
+ * Clear all subscriptions
6779
+ */
6780
+ clear: () => void;
6781
+ /**
6782
+ * Create _(or retrieve)_ a subscription
6783
+ *
6784
+ * @param parameters Subscription parameters
6785
+ * @returns Tuple holding subsccription and existing boolean
6786
+ */
6787
+ create: (parameters: SubscriptionParameters) => [Subscription, boolean];
6788
+ };
6789
+ /**
6790
+ * Parameters for creating a subscription store
6791
+ */
6792
+ type SubscriptionsParameters = {
6793
+ /**
6794
+ * Allow any or specific keys for subscriptions? _(defaults to `false`, which prevents keyed subscriptions)_
6795
+ */
6796
+ keys?: boolean | Set<Key$1>;
6797
+ /**
6798
+ * Property information for subscription identification
6799
+ */
6800
+ property?: SubscriptionProperty;
6801
+ };
6802
+ type SubscriptionsItems = {
6803
+ any: Set<Subscription>;
6804
+ keyed?: Map<Key$1, Set<Subscription>>;
6805
+ };
6806
+ type SubscriptionsValues = {
6807
+ from: SubscriptionsValuesItem<unknown, Subscription>;
6808
+ to: SubscriptionsValuesItem<Subscription, unknown>;
6809
+ };
6810
+ type SubscriptionsValuesItem<MapKey, MapValue> = {
6811
+ any: Map<MapKey, MapValue>;
6812
+ keyed?: Map<Key$1, Map<MapKey, MapValue>>;
6738
6813
  };
6739
6814
  /**
6740
6815
  * Is the value a subscription?
@@ -6743,6 +6818,20 @@ type SubscriptionProperty = {
6743
6818
  * @returns `true` if the value is a subscription, otherwise `false`
6744
6819
  */
6745
6820
  export declare function isSubscription(value: unknown): value is Subscription;
6821
+ /**
6822
+ * Is the value a subscriptions store?
6823
+ *
6824
+ * @param value Value to check
6825
+ * @returns `true` if the value is a subscriptions store, otherwise `false`
6826
+ */
6827
+ export declare function isSubscriptions(value: unknown): value is Subscriptions;
6828
+ /**
6829
+ * Create a subscription store
6830
+ *
6831
+ * @param parameters Store parameters
6832
+ * @returns Subscription store
6833
+ */
6834
+ export declare function subscriptions(parameters?: SubscriptionsParameters): Subscriptions;
6746
6835
  //#endregion
6747
6836
  //#region src/beacon.d.ts
6748
6837
  /**
@@ -8559,4 +8648,4 @@ export declare class SizedSet<Value = unknown> extends Set<Value> {
8559
8648
  get(value: Value, update?: boolean): Value | undefined;
8560
8649
  }
8561
8650
  //#endregion
8562
- export { type Color, type ColorType, type ColorTypeExtended, type ColorValue, type ColorValueExtended, type HSLAColor, type HSLColor, type HWBAColor, type HWBColor, Key$1 as Key, Logger, type RGBAColor, type RGBColor, type Shaken, type Smushed, type Subscription, type TimedLogger, type Unsmushed, toPromise as fromResult, getRandomFloatingNumber as getRandomFloat, isLogger, isTimedLogger };
8651
+ export { type Color, type ColorType, type ColorTypeExtended, type ColorValue, type ColorValueExtended, type HSLAColor, type HSLColor, type HWBAColor, type HWBColor, Key$1 as Key, Logger, type RGBAColor, type RGBColor, type Shaken, type Smushed, type Subscription, type SubscriptionParameters, type SubscriptionProperty, type Subscriptions, type SubscriptionsParameters, type TimedLogger, type Unsmushed, toPromise as fromResult, getRandomFloatingNumber as getRandomFloat, isLogger, isTimedLogger };