@oscarpalmer/atoms 0.44.0 → 0.45.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
@@ -840,6 +840,9 @@ function getRandomHex() {
840
840
  return "0123456789ABCDEF"[getRandomInteger(0, 16)];
841
841
  }
842
842
  // src/js/timer.ts
843
+ var getValueOrDefault = function(value, defaultValue) {
844
+ return typeof value === "number" && value > 0 ? value : defaultValue;
845
+ };
843
846
  var is3 = function(value, pattern) {
844
847
  return pattern.test(value?.$timer);
845
848
  };
@@ -856,20 +859,16 @@ function isWhen(value) {
856
859
  return is3(value, /^when$/) && typeof value.then === "function";
857
860
  }
858
861
  function repeat(callback, options) {
859
- return timer("repeat", callback, {
860
- ...options ?? {},
861
- ...{
862
- count: typeof options?.count === "number" ? options.count > 0 ? options.count : 1 : Number.POSITIVE_INFINITY
863
- }
864
- }).start();
865
- }
866
- var timer = function(type, callback, options) {
867
- const extended = {
868
- afterCallback: options.afterCallback,
869
- count: typeof options.count === "number" && options.count > 0 ? options.count : 1,
870
- errorCallback: options.errorCallback,
871
- interval: typeof options.interval === "number" && options.interval >= 0 ? options.interval : 0,
872
- timeout: typeof options.timeout === "number" && options.timeout > 0 ? options.timeout : 30000
862
+ return timer("repeat", callback, options ?? {}).start();
863
+ }
864
+ var timer = function(type, callback, partial) {
865
+ const isRepeated2 = type === "repeat";
866
+ const options = {
867
+ afterCallback: partial.afterCallback,
868
+ count: getValueOrDefault(partial.count, isRepeated2 ? Number.POSITIVE_INFINITY : 1),
869
+ errorCallback: partial.errorCallback,
870
+ interval: getValueOrDefault(partial.interval, 0),
871
+ timeout: getValueOrDefault(partial.timeout, isRepeated2 ? Number.POSITIVE_INFINITY : 30000)
873
872
  };
874
873
  const state = {
875
874
  callback,
@@ -877,13 +876,13 @@ var timer = function(type, callback, options) {
877
876
  };
878
877
  const instance = Object.create({
879
878
  restart() {
880
- return work2("restart", this, state, extended);
879
+ return work2("restart", this, state, options, isRepeated2);
881
880
  },
882
881
  start() {
883
- return work2("start", this, state, extended);
882
+ return work2("start", this, state, options, isRepeated2);
884
883
  },
885
884
  stop() {
886
- return work2("stop", this, state, extended);
885
+ return work2("stop", this, state, options, isRepeated2);
887
886
  }
888
887
  });
889
888
  Object.defineProperties(instance, {
@@ -901,12 +900,9 @@ var timer = function(type, callback, options) {
901
900
  return instance;
902
901
  };
903
902
  function wait(callback, options) {
904
- const optionsIsNumber = typeof options === "number";
905
- return timer("wait", callback, {
906
- count: 1,
907
- errorCallback: optionsIsNumber ? undefined : options?.errorCallback,
908
- interval: optionsIsNumber ? options : options?.interval ?? 0
909
- }).start();
903
+ return timer("wait", callback, options == null || typeof options === "number" ? {
904
+ interval: options
905
+ } : options).start();
910
906
  }
911
907
  function when(condition, options) {
912
908
  let rejecter;
@@ -954,7 +950,7 @@ function when(condition, options) {
954
950
  });
955
951
  return instance;
956
952
  }
957
- var work2 = function(type, timer2, state, options) {
953
+ var work2 = function(type, timer2, state, options, isRepeated2) {
958
954
  if (type === "start" && state.active || type === "stop" && !state.active) {
959
955
  return timer2;
960
956
  }
@@ -969,7 +965,7 @@ var work2 = function(type, timer2, state, options) {
969
965
  return timer2;
970
966
  }
971
967
  state.active = true;
972
- const isRepeated2 = count > 0;
968
+ const canTimeout = timeout > 0 && timeout < Number.POSITIVE_INFINITY;
973
969
  const total = count === Number.POSITIVE_INFINITY ? timeout : count * (interval > 0 ? interval : 17);
974
970
  let current;
975
971
  let start;
@@ -996,7 +992,7 @@ var work2 = function(type, timer2, state, options) {
996
992
  }
997
993
  index += 1;
998
994
  switch (true) {
999
- case (!finished && timestamp - start >= timeout):
995
+ case (canTimeout && !finished && timestamp - start >= timeout):
1000
996
  finish(false, true);
1001
997
  return;
1002
998
  case (!finished && index < count):
package/dist/js/timer.js CHANGED
@@ -1,4 +1,7 @@
1
1
  // src/js/timer.ts
2
+ var getValueOrDefault = function(value, defaultValue) {
3
+ return typeof value === "number" && value > 0 ? value : defaultValue;
4
+ };
2
5
  var is = function(value, pattern) {
3
6
  return pattern.test(value?.$timer);
4
7
  };
@@ -15,20 +18,16 @@ function isWhen(value) {
15
18
  return is(value, /^when$/) && typeof value.then === "function";
16
19
  }
17
20
  function repeat(callback, options) {
18
- return timer("repeat", callback, {
19
- ...options ?? {},
20
- ...{
21
- count: typeof options?.count === "number" ? options.count > 0 ? options.count : 1 : Number.POSITIVE_INFINITY
22
- }
23
- }).start();
21
+ return timer("repeat", callback, options ?? {}).start();
24
22
  }
25
- var timer = function(type, callback, options) {
26
- const extended = {
27
- afterCallback: options.afterCallback,
28
- count: typeof options.count === "number" && options.count > 0 ? options.count : 1,
29
- errorCallback: options.errorCallback,
30
- interval: typeof options.interval === "number" && options.interval >= 0 ? options.interval : 0,
31
- timeout: typeof options.timeout === "number" && options.timeout > 0 ? options.timeout : 30000
23
+ var timer = function(type, callback, partial) {
24
+ const isRepeated2 = type === "repeat";
25
+ const options = {
26
+ afterCallback: partial.afterCallback,
27
+ count: getValueOrDefault(partial.count, isRepeated2 ? Number.POSITIVE_INFINITY : 1),
28
+ errorCallback: partial.errorCallback,
29
+ interval: getValueOrDefault(partial.interval, 0),
30
+ timeout: getValueOrDefault(partial.timeout, isRepeated2 ? Number.POSITIVE_INFINITY : 30000)
32
31
  };
33
32
  const state = {
34
33
  callback,
@@ -36,13 +35,13 @@ var timer = function(type, callback, options) {
36
35
  };
37
36
  const instance = Object.create({
38
37
  restart() {
39
- return work("restart", this, state, extended);
38
+ return work("restart", this, state, options, isRepeated2);
40
39
  },
41
40
  start() {
42
- return work("start", this, state, extended);
41
+ return work("start", this, state, options, isRepeated2);
43
42
  },
44
43
  stop() {
45
- return work("stop", this, state, extended);
44
+ return work("stop", this, state, options, isRepeated2);
46
45
  }
47
46
  });
48
47
  Object.defineProperties(instance, {
@@ -60,12 +59,9 @@ var timer = function(type, callback, options) {
60
59
  return instance;
61
60
  };
62
61
  function wait(callback, options) {
63
- const optionsIsNumber = typeof options === "number";
64
- return timer("wait", callback, {
65
- count: 1,
66
- errorCallback: optionsIsNumber ? undefined : options?.errorCallback,
67
- interval: optionsIsNumber ? options : options?.interval ?? 0
68
- }).start();
62
+ return timer("wait", callback, options == null || typeof options === "number" ? {
63
+ interval: options
64
+ } : options).start();
69
65
  }
70
66
  function when(condition, options) {
71
67
  let rejecter;
@@ -113,7 +109,7 @@ function when(condition, options) {
113
109
  });
114
110
  return instance;
115
111
  }
116
- var work = function(type, timer2, state, options) {
112
+ var work = function(type, timer2, state, options, isRepeated2) {
117
113
  if (type === "start" && state.active || type === "stop" && !state.active) {
118
114
  return timer2;
119
115
  }
@@ -128,7 +124,7 @@ var work = function(type, timer2, state, options) {
128
124
  return timer2;
129
125
  }
130
126
  state.active = true;
131
- const isRepeated2 = count > 0;
127
+ const canTimeout = timeout > 0 && timeout < Number.POSITIVE_INFINITY;
132
128
  const total = count === Number.POSITIVE_INFINITY ? timeout : count * (interval > 0 ? interval : 17);
133
129
  let current;
134
130
  let start;
@@ -155,7 +151,7 @@ var work = function(type, timer2, state, options) {
155
151
  }
156
152
  index += 1;
157
153
  switch (true) {
158
- case (!finished && timestamp - start >= timeout):
154
+ case (canTimeout && !finished && timestamp - start >= timeout):
159
155
  finish(false, true);
160
156
  return;
161
157
  case (!finished && index < count):
package/dist/js/timer.mjs CHANGED
@@ -1,4 +1,7 @@
1
1
  // src/js/timer.ts
2
+ var getValueOrDefault = function(value, defaultValue) {
3
+ return typeof value === "number" && value > 0 ? value : defaultValue;
4
+ };
2
5
  var is = function(value, pattern) {
3
6
  return pattern.test(value?.$timer);
4
7
  };
@@ -15,20 +18,16 @@ function isWhen(value) {
15
18
  return is(value, /^when$/) && typeof value.then === "function";
16
19
  }
17
20
  function repeat(callback, options) {
18
- return timer("repeat", callback, {
19
- ...options ?? {},
20
- ...{
21
- count: typeof options?.count === "number" ? options.count > 0 ? options.count : 1 : Number.POSITIVE_INFINITY
22
- }
23
- }).start();
21
+ return timer("repeat", callback, options ?? {}).start();
24
22
  }
25
- var timer = function(type, callback, options) {
26
- const extended = {
27
- afterCallback: options.afterCallback,
28
- count: typeof options.count === "number" && options.count > 0 ? options.count : 1,
29
- errorCallback: options.errorCallback,
30
- interval: typeof options.interval === "number" && options.interval >= 0 ? options.interval : 0,
31
- timeout: typeof options.timeout === "number" && options.timeout > 0 ? options.timeout : 30000
23
+ var timer = function(type, callback, partial) {
24
+ const isRepeated2 = type === "repeat";
25
+ const options = {
26
+ afterCallback: partial.afterCallback,
27
+ count: getValueOrDefault(partial.count, isRepeated2 ? Number.POSITIVE_INFINITY : 1),
28
+ errorCallback: partial.errorCallback,
29
+ interval: getValueOrDefault(partial.interval, 0),
30
+ timeout: getValueOrDefault(partial.timeout, isRepeated2 ? Number.POSITIVE_INFINITY : 30000)
32
31
  };
33
32
  const state = {
34
33
  callback,
@@ -36,13 +35,13 @@ var timer = function(type, callback, options) {
36
35
  };
37
36
  const instance = Object.create({
38
37
  restart() {
39
- return work("restart", this, state, extended);
38
+ return work("restart", this, state, options, isRepeated2);
40
39
  },
41
40
  start() {
42
- return work("start", this, state, extended);
41
+ return work("start", this, state, options, isRepeated2);
43
42
  },
44
43
  stop() {
45
- return work("stop", this, state, extended);
44
+ return work("stop", this, state, options, isRepeated2);
46
45
  }
47
46
  });
48
47
  Object.defineProperties(instance, {
@@ -60,12 +59,9 @@ var timer = function(type, callback, options) {
60
59
  return instance;
61
60
  };
62
61
  function wait(callback, options) {
63
- const optionsIsNumber = typeof options === "number";
64
- return timer("wait", callback, {
65
- count: 1,
66
- errorCallback: optionsIsNumber ? undefined : options?.errorCallback,
67
- interval: optionsIsNumber ? options : options?.interval ?? 0
68
- }).start();
62
+ return timer("wait", callback, options == null || typeof options === "number" ? {
63
+ interval: options
64
+ } : options).start();
69
65
  }
70
66
  function when(condition, options) {
71
67
  let rejecter;
@@ -113,7 +109,7 @@ function when(condition, options) {
113
109
  });
114
110
  return instance;
115
111
  }
116
- var work = function(type, timer2, state, options) {
112
+ var work = function(type, timer2, state, options, isRepeated2) {
117
113
  if (type === "start" && state.active || type === "stop" && !state.active) {
118
114
  return timer2;
119
115
  }
@@ -128,7 +124,7 @@ var work = function(type, timer2, state, options) {
128
124
  return timer2;
129
125
  }
130
126
  state.active = true;
131
- const isRepeated2 = count > 0;
127
+ const canTimeout = timeout > 0 && timeout < Number.POSITIVE_INFINITY;
132
128
  const total = count === Number.POSITIVE_INFINITY ? timeout : count * (interval > 0 ? interval : 17);
133
129
  let current;
134
130
  let start;
@@ -155,7 +151,7 @@ var work = function(type, timer2, state, options) {
155
151
  }
156
152
  index += 1;
157
153
  switch (true) {
158
- case (!finished && timestamp - start >= timeout):
154
+ case (canTimeout && !finished && timestamp - start >= timeout):
159
155
  finish(false, true);
160
156
  return;
161
157
  case (!finished && index < count):
package/package.json CHANGED
@@ -120,9 +120,10 @@
120
120
  "url": "git+https://github.com/oscarpalmer/atoms.git"
121
121
  },
122
122
  "scripts": {
123
- "build": "bun run build:css && bun run build:js && bun run types",
123
+ "build": "bun run clean && bun run build:css && bun run build:js && bun run types",
124
124
  "build:css": "bunx sass ./src/css:./dist/css --no-source-map",
125
125
  "build:js": "bunx bun ./.bun.ts && bunx bun ./.bun.ts --mjs",
126
+ "clean": "rm -rf ./dist && rm -rf ./types && rm ./tsconfig.tsbuildinfo",
126
127
  "test": "bun test",
127
128
  "types": "bunx tsc -p ./tsconfig.json",
128
129
  "watch:css": "bunx sass ./src/css:./dist/css --no-source-map --watch",
@@ -130,5 +131,5 @@
130
131
  },
131
132
  "type": "module",
132
133
  "types": "./types/index.d.ts",
133
- "version": "0.44.0"
134
+ "version": "0.45.0"
134
135
  }
package/src/js/timer.ts CHANGED
@@ -80,27 +80,31 @@ type TimerOptions = {} & RepeatOptions;
80
80
  type WaitOptions = {} & BaseOptions & OptionsWithError;
81
81
 
82
82
  export type When = {
83
- /**
84
- * Is the timer running?
85
- */
86
- get active(): boolean;
87
- /**
88
- * Stops the timer
89
- */
90
- stop(): void;
91
- /**
92
- * Starts the timer and returns a promise that resolves when the condition is met
93
- */
94
- then(
95
- resolve?: (() => void) | null,
96
- reject?: (() => void) | null,
97
- ): Promise<void>;
98
- };
83
+ /**
84
+ * Is the timer running?
85
+ */
86
+ get active(): boolean;
87
+ /**
88
+ * Stops the timer
89
+ */
90
+ stop(): void;
91
+ /**
92
+ * Starts the timer and returns a promise that resolves when the condition is met
93
+ */
94
+ then(
95
+ resolve?: (() => void) | null,
96
+ reject?: (() => void) | null,
97
+ ): Promise<void>;
98
+ };
99
99
 
100
100
  type WhenOptions = {} & OptionsWithCount;
101
101
 
102
102
  type WorkType = 'restart' | 'start' | 'stop';
103
103
 
104
+ function getValueOrDefault(value: unknown, defaultValue: number): number {
105
+ return typeof value === 'number' && value > 0 ? value : defaultValue;
106
+ }
107
+
104
108
  function is(value: unknown, pattern: RegExp) {
105
109
  return pattern.test((value as PlainObject)?.$timer as string);
106
110
  }
@@ -138,47 +142,36 @@ export function isWhen(value: unknown): value is When {
138
142
  * - calls a callback after a certain amount of time...
139
143
  * - ... and repeats it a certain amount of times
140
144
  * ---
141
- * - `options.count` defaults to `Infinity` _(minimum `1`)_
145
+ * - `options.count` defaults to `Infinity`
142
146
  * - `options.interval` defaults to `0`
143
- * - `options.timeout` defaults to `30_000` _(30 seconds)_
147
+ * - `options.timeout` defaults to `Infinity`
144
148
  */
145
149
  export function repeat(
146
150
  callback: IndexedCallback,
147
151
  options?: Partial<RepeatOptions>,
148
152
  ): Timer {
149
- return timer('repeat', callback, {
150
- ...(options ?? {}),
151
- ...{
152
- count:
153
- typeof options?.count === 'number'
154
- ? options.count > 0
155
- ? options.count
156
- : 1
157
- : Number.POSITIVE_INFINITY,
158
- },
159
- }).start();
153
+ return timer('repeat', callback, options ?? {}).start();
160
154
  }
161
155
 
162
156
  function timer(
163
157
  type: 'repeat' | 'wait',
164
158
  callback: AnyCallback,
165
- options: Partial<TimerOptions>,
159
+ partial: Partial<TimerOptions>,
166
160
  ): Timer {
167
- const extended: TimerOptions = {
168
- afterCallback: options.afterCallback,
169
- count:
170
- typeof options.count === 'number' && options.count > 0
171
- ? options.count
172
- : 1,
173
- errorCallback: options.errorCallback,
174
- interval:
175
- typeof options.interval === 'number' && options.interval >= 0
176
- ? options.interval
177
- : 0,
178
- timeout:
179
- typeof options.timeout === 'number' && options.timeout > 0
180
- ? options.timeout
181
- : 30_000,
161
+ const isRepeated = type === 'repeat';
162
+
163
+ const options: TimerOptions = {
164
+ afterCallback: partial.afterCallback,
165
+ count: getValueOrDefault(
166
+ partial.count,
167
+ isRepeated ? Number.POSITIVE_INFINITY : 1,
168
+ ),
169
+ errorCallback: partial.errorCallback,
170
+ interval: getValueOrDefault(partial.interval, 0),
171
+ timeout: getValueOrDefault(
172
+ partial.timeout,
173
+ isRepeated ? Number.POSITIVE_INFINITY : 30_000,
174
+ ),
182
175
  };
183
176
 
184
177
  const state: State = {
@@ -188,13 +181,13 @@ function timer(
188
181
 
189
182
  const instance = Object.create({
190
183
  restart() {
191
- return work('restart', this as Timer, state, extended);
184
+ return work('restart', this as Timer, state, options, isRepeated);
192
185
  },
193
186
  start() {
194
- return work('start', this as Timer, state, extended);
187
+ return work('start', this as Timer, state, options, isRepeated);
195
188
  },
196
189
  stop() {
197
- return work('stop', this as Timer, state, extended);
190
+ return work('stop', this as Timer, state, options, isRepeated);
198
191
  },
199
192
  });
200
193
 
@@ -238,13 +231,15 @@ export function wait(
238
231
  callback: () => void,
239
232
  options?: number | Partial<WaitOptions>,
240
233
  ): Timer {
241
- const optionsIsNumber = typeof options === 'number';
242
-
243
- return timer('wait', callback, {
244
- count: 1,
245
- errorCallback: optionsIsNumber ? undefined : options?.errorCallback,
246
- interval: optionsIsNumber ? options : options?.interval ?? 0,
247
- }).start();
234
+ return timer(
235
+ 'wait',
236
+ callback,
237
+ options == null || typeof options === 'number'
238
+ ? {
239
+ interval: options,
240
+ }
241
+ : options,
242
+ ).start();
248
243
  }
249
244
 
250
245
  /**
@@ -318,6 +313,7 @@ function work(
318
313
  timer: Timer,
319
314
  state: State,
320
315
  options: RepeatOptions,
316
+ isRepeated: boolean,
321
317
  ): Timer {
322
318
  if (
323
319
  (type === 'start' && state.active) ||
@@ -343,7 +339,7 @@ function work(
343
339
 
344
340
  state.active = true;
345
341
 
346
- const isRepeated = count > 0;
342
+ const canTimeout = timeout > 0 && timeout < Number.POSITIVE_INFINITY;
347
343
 
348
344
  const total =
349
345
  count === Number.POSITIVE_INFINITY
@@ -385,7 +381,7 @@ function work(
385
381
  index += 1;
386
382
 
387
383
  switch (true) {
388
- case !finished && timestamp - start >= timeout:
384
+ case canTimeout && !finished && timestamp - start >= timeout:
389
385
  finish(false, true);
390
386
  return;
391
387
 
package/types/timer.d.ts CHANGED
@@ -94,9 +94,9 @@ export declare function isWhen(value: unknown): value is When;
94
94
  * - calls a callback after a certain amount of time...
95
95
  * - ... and repeats it a certain amount of times
96
96
  * ---
97
- * - `options.count` defaults to `Infinity` _(minimum `1`)_
97
+ * - `options.count` defaults to `Infinity`
98
98
  * - `options.interval` defaults to `0`
99
- * - `options.timeout` defaults to `30_000` _(30 seconds)_
99
+ * - `options.timeout` defaults to `Infinity`
100
100
  */
101
101
  export declare function repeat(callback: IndexedCallback, options?: Partial<RepeatOptions>): Timer;
102
102
  /**