@oscarpalmer/atoms 0.41.3 → 0.42.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/js/index.js CHANGED
@@ -681,20 +681,26 @@ function getRandomHex() {
681
681
  return "0123456789ABCDEF"[getRandomInteger(0, 16)];
682
682
  }
683
683
  // src/js/timer.ts
684
+ var is = function(value, pattern) {
685
+ return pattern.test(value?.$timer);
686
+ };
684
687
  function isRepeated(value) {
685
- return /^repeat$/.test(value?.$timer ?? "");
688
+ return is(value, /^repeat$/);
686
689
  }
687
690
  function isTimer(value) {
688
- return /^repeat|wait$/.test(value?.$timer ?? "");
691
+ return is(value, /^repeat|wait$/);
689
692
  }
690
693
  function isWaited(value) {
691
- return /^wait$/.test(value?.$timer ?? "");
694
+ return is(value, /^wait$/);
695
+ }
696
+ function isWhen(value) {
697
+ return is(value, /^when$/) && typeof value.then === "function";
692
698
  }
693
699
  function repeat(callback, options) {
694
700
  return timer("repeat", callback, {
695
701
  ...options ?? {},
696
702
  ...{
697
- count: typeof options?.count === "number" ? options.count : Number.POSITIVE_INFINITY
703
+ count: typeof options?.count === "number" ? options.count > 0 ? options.count : 1 : Number.POSITIVE_INFINITY
698
704
  }
699
705
  }).start();
700
706
  }
@@ -775,16 +781,28 @@ function when(condition, options) {
775
781
  return promise.then(resolve, reject);
776
782
  }
777
783
  });
784
+ Object.defineProperties(instance, {
785
+ $timer: {
786
+ get() {
787
+ return "when";
788
+ }
789
+ },
790
+ active: {
791
+ get() {
792
+ return repeated.active;
793
+ }
794
+ }
795
+ });
778
796
  return instance;
779
797
  }
780
798
  var work = function(type, timer2, state, options) {
781
799
  if (type === "start" && state.active || type === "stop" && !state.active) {
782
800
  return timer2;
783
801
  }
784
- const { afterCallback, count, errorCallback, interval, timeout } = options;
802
+ const { count, interval, timeout } = options;
785
803
  if (typeof state.frame === "number") {
786
804
  cancelAnimationFrame(state.frame);
787
- afterCallback?.(false);
805
+ options.afterCallback?.(false);
788
806
  }
789
807
  if (type === "stop") {
790
808
  state.active = false;
@@ -793,20 +811,17 @@ var work = function(type, timer2, state, options) {
793
811
  }
794
812
  state.active = true;
795
813
  const isRepeated2 = count > 0;
796
- let index = 0;
797
- let total = count * (interval > 0 ? interval : 1);
798
- if (total < 16.66667) {
799
- total = 16.66667;
800
- }
814
+ const total = count === Number.POSITIVE_INFINITY ? timeout : count * (interval > 0 ? interval : 17);
801
815
  let current;
802
816
  let start;
817
+ let index = 0;
803
818
  function finish(finished, error) {
804
819
  state.active = false;
805
820
  state.frame = undefined;
806
821
  if (error) {
807
- errorCallback?.();
822
+ options.errorCallback?.();
808
823
  }
809
- afterCallback?.(finished);
824
+ options.afterCallback?.(finished);
810
825
  }
811
826
  function step(timestamp) {
812
827
  if (!state.active) {
@@ -1060,6 +1075,7 @@ export {
1060
1075
  queue,
1061
1076
  push,
1062
1077
  merge,
1078
+ isWhen,
1063
1079
  isWaited,
1064
1080
  isTimer,
1065
1081
  isTabbableElement,
package/dist/js/timer.js CHANGED
@@ -1,18 +1,24 @@
1
1
  // src/js/timer.ts
2
+ var is = function(value, pattern) {
3
+ return pattern.test(value?.$timer);
4
+ };
2
5
  function isRepeated(value) {
3
- return /^repeat$/.test(value?.$timer ?? "");
6
+ return is(value, /^repeat$/);
4
7
  }
5
8
  function isTimer(value) {
6
- return /^repeat|wait$/.test(value?.$timer ?? "");
9
+ return is(value, /^repeat|wait$/);
7
10
  }
8
11
  function isWaited(value) {
9
- return /^wait$/.test(value?.$timer ?? "");
12
+ return is(value, /^wait$/);
13
+ }
14
+ function isWhen(value) {
15
+ return is(value, /^when$/) && typeof value.then === "function";
10
16
  }
11
17
  function repeat(callback, options) {
12
18
  return timer("repeat", callback, {
13
19
  ...options ?? {},
14
20
  ...{
15
- count: typeof options?.count === "number" ? options.count : Number.POSITIVE_INFINITY
21
+ count: typeof options?.count === "number" ? options.count > 0 ? options.count : 1 : Number.POSITIVE_INFINITY
16
22
  }
17
23
  }).start();
18
24
  }
@@ -93,16 +99,28 @@ function when(condition, options) {
93
99
  return promise.then(resolve, reject);
94
100
  }
95
101
  });
102
+ Object.defineProperties(instance, {
103
+ $timer: {
104
+ get() {
105
+ return "when";
106
+ }
107
+ },
108
+ active: {
109
+ get() {
110
+ return repeated.active;
111
+ }
112
+ }
113
+ });
96
114
  return instance;
97
115
  }
98
116
  var work = function(type, timer2, state, options) {
99
117
  if (type === "start" && state.active || type === "stop" && !state.active) {
100
118
  return timer2;
101
119
  }
102
- const { afterCallback, count, errorCallback, interval, timeout } = options;
120
+ const { count, interval, timeout } = options;
103
121
  if (typeof state.frame === "number") {
104
122
  cancelAnimationFrame(state.frame);
105
- afterCallback?.(false);
123
+ options.afterCallback?.(false);
106
124
  }
107
125
  if (type === "stop") {
108
126
  state.active = false;
@@ -111,20 +129,17 @@ var work = function(type, timer2, state, options) {
111
129
  }
112
130
  state.active = true;
113
131
  const isRepeated2 = count > 0;
114
- let index = 0;
115
- let total = count * (interval > 0 ? interval : 1);
116
- if (total < 16.66667) {
117
- total = 16.66667;
118
- }
132
+ const total = count === Number.POSITIVE_INFINITY ? timeout : count * (interval > 0 ? interval : 17);
119
133
  let current;
120
134
  let start;
135
+ let index = 0;
121
136
  function finish(finished, error) {
122
137
  state.active = false;
123
138
  state.frame = undefined;
124
139
  if (error) {
125
- errorCallback?.();
140
+ options.errorCallback?.();
126
141
  }
127
- afterCallback?.(finished);
142
+ options.afterCallback?.(finished);
128
143
  }
129
144
  function step(timestamp) {
130
145
  if (!state.active) {
@@ -160,6 +175,7 @@ export {
160
175
  when,
161
176
  wait,
162
177
  repeat,
178
+ isWhen,
163
179
  isWaited,
164
180
  isTimer,
165
181
  isRepeated
package/dist/js/timer.mjs CHANGED
@@ -1,18 +1,24 @@
1
1
  // src/js/timer.ts
2
+ var is = function(value, pattern) {
3
+ return pattern.test(value?.$timer);
4
+ };
2
5
  function isRepeated(value) {
3
- return /^repeat$/.test(value?.$timer ?? "");
6
+ return is(value, /^repeat$/);
4
7
  }
5
8
  function isTimer(value) {
6
- return /^repeat|wait$/.test(value?.$timer ?? "");
9
+ return is(value, /^repeat|wait$/);
7
10
  }
8
11
  function isWaited(value) {
9
- return /^wait$/.test(value?.$timer ?? "");
12
+ return is(value, /^wait$/);
13
+ }
14
+ function isWhen(value) {
15
+ return is(value, /^when$/) && typeof value.then === "function";
10
16
  }
11
17
  function repeat(callback, options) {
12
18
  return timer("repeat", callback, {
13
19
  ...options ?? {},
14
20
  ...{
15
- count: typeof options?.count === "number" ? options.count : Number.POSITIVE_INFINITY
21
+ count: typeof options?.count === "number" ? options.count > 0 ? options.count : 1 : Number.POSITIVE_INFINITY
16
22
  }
17
23
  }).start();
18
24
  }
@@ -93,16 +99,28 @@ function when(condition, options) {
93
99
  return promise.then(resolve, reject);
94
100
  }
95
101
  });
102
+ Object.defineProperties(instance, {
103
+ $timer: {
104
+ get() {
105
+ return "when";
106
+ }
107
+ },
108
+ active: {
109
+ get() {
110
+ return repeated.active;
111
+ }
112
+ }
113
+ });
96
114
  return instance;
97
115
  }
98
116
  var work = function(type, timer2, state, options) {
99
117
  if (type === "start" && state.active || type === "stop" && !state.active) {
100
118
  return timer2;
101
119
  }
102
- const { afterCallback, count, errorCallback, interval, timeout } = options;
120
+ const { count, interval, timeout } = options;
103
121
  if (typeof state.frame === "number") {
104
122
  cancelAnimationFrame(state.frame);
105
- afterCallback?.(false);
123
+ options.afterCallback?.(false);
106
124
  }
107
125
  if (type === "stop") {
108
126
  state.active = false;
@@ -111,20 +129,17 @@ var work = function(type, timer2, state, options) {
111
129
  }
112
130
  state.active = true;
113
131
  const isRepeated2 = count > 0;
114
- let index = 0;
115
- let total = count * (interval > 0 ? interval : 1);
116
- if (total < 16.66667) {
117
- total = 16.66667;
118
- }
132
+ const total = count === Number.POSITIVE_INFINITY ? timeout : count * (interval > 0 ? interval : 17);
119
133
  let current;
120
134
  let start;
135
+ let index = 0;
121
136
  function finish(finished, error) {
122
137
  state.active = false;
123
138
  state.frame = undefined;
124
139
  if (error) {
125
- errorCallback?.();
140
+ options.errorCallback?.();
126
141
  }
127
- afterCallback?.(finished);
142
+ options.afterCallback?.(finished);
128
143
  }
129
144
  function step(timestamp) {
130
145
  if (!state.active) {
@@ -160,6 +175,7 @@ export {
160
175
  when,
161
176
  wait,
162
177
  repeat,
178
+ isWhen,
163
179
  isWaited,
164
180
  isTimer,
165
181
  isRepeated
package/package.json CHANGED
@@ -130,5 +130,5 @@
130
130
  },
131
131
  "type": "module",
132
132
  "types": "./types/index.d.ts",
133
- "version": "0.41.3"
133
+ "version": "0.42.0"
134
134
  }
package/src/js/timer.ts CHANGED
@@ -94,25 +94,36 @@ type WhenOptions = {} & OptionsWithCount;
94
94
 
95
95
  type WorkType = 'restart' | 'start' | 'stop';
96
96
 
97
+ function is(value: unknown, pattern: RegExp) {
98
+ return pattern.test((value as PlainObject)?.$timer as string);
99
+ }
100
+
97
101
  /**
98
102
  * Is the value a repeating timer?
99
103
  */
100
104
  export function isRepeated(value: unknown): value is Timer {
101
- return /^repeat$/.test(((value as PlainObject)?.$timer as string) ?? '');
105
+ return is(value, /^repeat$/);
102
106
  }
103
107
 
104
108
  /**
105
109
  * Is the value a timer?
106
110
  */
107
111
  export function isTimer(value: unknown): value is Timer {
108
- return /^repeat|wait$/.test(((value as PlainObject)?.$timer as string) ?? '');
112
+ return is(value, /^repeat|wait$/);
109
113
  }
110
114
 
111
115
  /**
112
116
  * Is the value a waiting timer?
113
117
  */
114
118
  export function isWaited(value: unknown): value is Timer {
115
- return /^wait$/.test(((value as PlainObject)?.$timer as string) ?? '');
119
+ return is(value, /^wait$/);
120
+ }
121
+
122
+ /**
123
+ * Is the value a conditional timer?
124
+ */
125
+ export function isWhen(value: unknown): value is When {
126
+ return is(value, /^when$/) && typeof (value as When).then === 'function';
116
127
  }
117
128
 
118
129
  /**
@@ -120,8 +131,9 @@ export function isWaited(value: unknown): value is Timer {
120
131
  * - calls a callback after a certain amount of time...
121
132
  * - ... and repeats it a certain amount of times
122
133
  * ---
123
- * - `options.count` defaults to `Infinity`
124
- * - `options.time` defaults to `0`
134
+ * - `options.count` defaults to `Infinity` _(minimum `1`)_
135
+ * - `options.interval` defaults to `0`
136
+ * - `options.timeout` defaults to `30_000` _(30 seconds)_
125
137
  */
126
138
  export function repeat(
127
139
  callback: IndexedCallback,
@@ -132,7 +144,9 @@ export function repeat(
132
144
  ...{
133
145
  count:
134
146
  typeof options?.count === 'number'
135
- ? options.count
147
+ ? options.count > 0
148
+ ? options.count
149
+ : 1
136
150
  : Number.POSITIVE_INFINITY,
137
151
  },
138
152
  }).start();
@@ -268,7 +282,7 @@ export function when(
268
282
  rejecter?.();
269
283
  }
270
284
  },
271
- // biome-ignore lint/suspicious/noThenProperty: <explanation>
285
+ // biome-ignore lint/suspicious/noThenProperty: returning a promise-like object, so it's ok ;)
272
286
  then(resolve?: () => void, reject?: () => void) {
273
287
  repeated.start();
274
288
 
@@ -276,6 +290,19 @@ export function when(
276
290
  },
277
291
  });
278
292
 
293
+ Object.defineProperties(instance, {
294
+ $timer: {
295
+ get() {
296
+ return 'when';
297
+ },
298
+ },
299
+ active: {
300
+ get() {
301
+ return repeated.active;
302
+ },
303
+ },
304
+ });
305
+
279
306
  return instance;
280
307
  }
281
308
 
@@ -292,12 +319,12 @@ function work(
292
319
  return timer;
293
320
  }
294
321
 
295
- const {afterCallback, count, errorCallback, interval, timeout} = options;
322
+ const {count, interval, timeout} = options;
296
323
 
297
324
  if (typeof state.frame === 'number') {
298
325
  cancelAnimationFrame(state.frame);
299
326
 
300
- afterCallback?.(false);
327
+ options.afterCallback?.(false);
301
328
  }
302
329
 
303
330
  if (type === 'stop') {
@@ -311,25 +338,25 @@ function work(
311
338
 
312
339
  const isRepeated = count > 0;
313
340
 
314
- let index = 0;
315
- let total = count * (interval > 0 ? interval : 1);
316
-
317
- if (total < 16.66667) {
318
- total = 16.66667;
319
- }
341
+ const total =
342
+ count === Number.POSITIVE_INFINITY
343
+ ? timeout
344
+ : count * (interval > 0 ? interval : 17);
320
345
 
321
346
  let current: DOMHighResTimeStamp | null;
322
347
  let start: DOMHighResTimeStamp | null;
323
348
 
349
+ let index = 0;
350
+
324
351
  function finish(finished: boolean, error: boolean) {
325
352
  state.active = false;
326
353
  state.frame = undefined;
327
354
 
328
355
  if (error) {
329
- errorCallback?.();
356
+ options.errorCallback?.();
330
357
  }
331
358
 
332
- afterCallback?.(finished);
359
+ options.afterCallback?.(finished);
333
360
  }
334
361
 
335
362
  function step(timestamp: DOMHighResTimeStamp): void {
package/types/timer.d.ts CHANGED
@@ -78,13 +78,18 @@ export declare function isTimer(value: unknown): value is Timer;
78
78
  * Is the value a waiting timer?
79
79
  */
80
80
  export declare function isWaited(value: unknown): value is Timer;
81
+ /**
82
+ * Is the value a conditional timer?
83
+ */
84
+ export declare function isWhen(value: unknown): value is When;
81
85
  /**
82
86
  * Creates a timer which:
83
87
  * - calls a callback after a certain amount of time...
84
88
  * - ... and repeats it a certain amount of times
85
89
  * ---
86
- * - `options.count` defaults to `Infinity`
87
- * - `options.time` defaults to `0`
90
+ * - `options.count` defaults to `Infinity` _(minimum `1`)_
91
+ * - `options.interval` defaults to `0`
92
+ * - `options.timeout` defaults to `30_000` _(30 seconds)_
88
93
  */
89
94
  export declare function repeat(callback: IndexedCallback, options?: Partial<RepeatOptions>): Timer;
90
95
  /**