@oscarpalmer/atoms 0.197.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();