@oscarpalmer/atoms 0.47.0 → 0.48.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
@@ -859,9 +859,9 @@ function isWhen(value) {
859
859
  return is3(value, /^when$/) && typeof value.then === "function";
860
860
  }
861
861
  function repeat(callback, options) {
862
- return timer("repeat", callback, options ?? {}).start();
862
+ return timer("repeat", callback, options ?? {}, true);
863
863
  }
864
- var timer = function(type, callback, partial) {
864
+ var timer = function(type, callback, partial, start) {
865
865
  const isRepeated2 = type === "repeat";
866
866
  const options = {
867
867
  afterCallback: partial.afterCallback,
@@ -872,7 +872,8 @@ var timer = function(type, callback, partial) {
872
872
  };
873
873
  const state = {
874
874
  callback,
875
- active: false
875
+ active: false,
876
+ minimum: options.interval - options.interval % milliseconds / 2
876
877
  };
877
878
  const instance = Object.create({
878
879
  continue() {
@@ -903,17 +904,20 @@ var timer = function(type, callback, partial) {
903
904
  }
904
905
  }
905
906
  });
907
+ if (start) {
908
+ instance.start();
909
+ }
906
910
  return instance;
907
911
  };
908
912
  function wait(callback, options) {
909
913
  return timer("wait", callback, options == null || typeof options === "number" ? {
910
914
  interval: options
911
- } : options).start();
915
+ } : options, true);
912
916
  }
913
917
  function when(condition, options) {
914
918
  let rejecter;
915
919
  let resolver;
916
- const repeated = repeat(() => {
920
+ const repeated = timer("repeat", () => {
917
921
  if (condition()) {
918
922
  repeated.stop();
919
923
  resolver?.();
@@ -925,7 +929,7 @@ function when(condition, options) {
925
929
  count: options?.count,
926
930
  interval: options?.interval,
927
931
  timeout: options?.timeout
928
- });
932
+ }, false);
929
933
  const promise = new Promise((resolve, reject) => {
930
934
  resolver = resolve;
931
935
  rejecter = reject;
@@ -967,6 +971,7 @@ var work2 = function(type, timer2, state, options, isRepeated2) {
967
971
  return timer2;
968
972
  }
969
973
  const { count, interval, timeout } = options;
974
+ const { minimum } = state;
970
975
  if (["pause", "stop"].includes(type)) {
971
976
  activeTimers.delete(timer2);
972
977
  cancelAnimationFrame(state.frame);
@@ -985,7 +990,7 @@ var work2 = function(type, timer2, state, options, isRepeated2) {
985
990
  let index = type === "continue" ? +(state.index ?? 0) : 0;
986
991
  state.elapsed = elapsed;
987
992
  state.index = index;
988
- const total = (count === Number.POSITIVE_INFINITY ? timeout : (count - index) * (interval > 0 ? interval : 62.5)) - elapsed;
993
+ const total = (count === Number.POSITIVE_INFINITY ? Number.POSITIVE_INFINITY : (count - index) * (interval > 0 ? interval : milliseconds)) - elapsed;
989
994
  let current;
990
995
  let start;
991
996
  function finish(finished, error) {
@@ -1008,7 +1013,7 @@ var work2 = function(type, timer2, state, options, isRepeated2) {
1008
1013
  const time2 = timestamp - current;
1009
1014
  state.elapsed = elapsed + (current - start);
1010
1015
  const finished = time2 - elapsed >= total;
1011
- if (finished || time2 - 2 < interval && interval < time2 + 2) {
1016
+ if (finished || time2 >= minimum) {
1012
1017
  if (state.active) {
1013
1018
  state.callback(isRepeated2 ? index : undefined);
1014
1019
  }
@@ -1034,6 +1039,7 @@ var work2 = function(type, timer2, state, options, isRepeated2) {
1034
1039
  };
1035
1040
  var activeTimers = new Set;
1036
1041
  var hiddenTimers = new Set;
1042
+ var milliseconds = 16.666666666666668;
1037
1043
  document.addEventListener("visibilitychange", () => {
1038
1044
  if (document.hidden) {
1039
1045
  for (const timer2 of activeTimers) {
package/dist/js/timer.js CHANGED
@@ -18,9 +18,9 @@ function isWhen(value) {
18
18
  return is(value, /^when$/) && typeof value.then === "function";
19
19
  }
20
20
  function repeat(callback, options) {
21
- return timer("repeat", callback, options ?? {}).start();
21
+ return timer("repeat", callback, options ?? {}, true);
22
22
  }
23
- var timer = function(type, callback, partial) {
23
+ var timer = function(type, callback, partial, start) {
24
24
  const isRepeated2 = type === "repeat";
25
25
  const options = {
26
26
  afterCallback: partial.afterCallback,
@@ -31,7 +31,8 @@ var timer = function(type, callback, partial) {
31
31
  };
32
32
  const state = {
33
33
  callback,
34
- active: false
34
+ active: false,
35
+ minimum: options.interval - options.interval % milliseconds / 2
35
36
  };
36
37
  const instance = Object.create({
37
38
  continue() {
@@ -62,17 +63,20 @@ var timer = function(type, callback, partial) {
62
63
  }
63
64
  }
64
65
  });
66
+ if (start) {
67
+ instance.start();
68
+ }
65
69
  return instance;
66
70
  };
67
71
  function wait(callback, options) {
68
72
  return timer("wait", callback, options == null || typeof options === "number" ? {
69
73
  interval: options
70
- } : options).start();
74
+ } : options, true);
71
75
  }
72
76
  function when(condition, options) {
73
77
  let rejecter;
74
78
  let resolver;
75
- const repeated = repeat(() => {
79
+ const repeated = timer("repeat", () => {
76
80
  if (condition()) {
77
81
  repeated.stop();
78
82
  resolver?.();
@@ -84,7 +88,7 @@ function when(condition, options) {
84
88
  count: options?.count,
85
89
  interval: options?.interval,
86
90
  timeout: options?.timeout
87
- });
91
+ }, false);
88
92
  const promise = new Promise((resolve, reject) => {
89
93
  resolver = resolve;
90
94
  rejecter = reject;
@@ -126,6 +130,7 @@ var work = function(type, timer2, state, options, isRepeated2) {
126
130
  return timer2;
127
131
  }
128
132
  const { count, interval, timeout } = options;
133
+ const { minimum } = state;
129
134
  if (["pause", "stop"].includes(type)) {
130
135
  activeTimers.delete(timer2);
131
136
  cancelAnimationFrame(state.frame);
@@ -144,7 +149,7 @@ var work = function(type, timer2, state, options, isRepeated2) {
144
149
  let index = type === "continue" ? +(state.index ?? 0) : 0;
145
150
  state.elapsed = elapsed;
146
151
  state.index = index;
147
- const total = (count === Number.POSITIVE_INFINITY ? timeout : (count - index) * (interval > 0 ? interval : 62.5)) - elapsed;
152
+ const total = (count === Number.POSITIVE_INFINITY ? Number.POSITIVE_INFINITY : (count - index) * (interval > 0 ? interval : milliseconds)) - elapsed;
148
153
  let current;
149
154
  let start;
150
155
  function finish(finished, error) {
@@ -167,7 +172,7 @@ var work = function(type, timer2, state, options, isRepeated2) {
167
172
  const time = timestamp - current;
168
173
  state.elapsed = elapsed + (current - start);
169
174
  const finished = time - elapsed >= total;
170
- if (finished || time - 2 < interval && interval < time + 2) {
175
+ if (finished || time >= minimum) {
171
176
  if (state.active) {
172
177
  state.callback(isRepeated2 ? index : undefined);
173
178
  }
@@ -193,6 +198,7 @@ var work = function(type, timer2, state, options, isRepeated2) {
193
198
  };
194
199
  var activeTimers = new Set;
195
200
  var hiddenTimers = new Set;
201
+ var milliseconds = 16.666666666666668;
196
202
  document.addEventListener("visibilitychange", () => {
197
203
  if (document.hidden) {
198
204
  for (const timer2 of activeTimers) {
package/dist/js/timer.mjs CHANGED
@@ -18,9 +18,9 @@ function isWhen(value) {
18
18
  return is(value, /^when$/) && typeof value.then === "function";
19
19
  }
20
20
  function repeat(callback, options) {
21
- return timer("repeat", callback, options ?? {}).start();
21
+ return timer("repeat", callback, options ?? {}, true);
22
22
  }
23
- var timer = function(type, callback, partial) {
23
+ var timer = function(type, callback, partial, start) {
24
24
  const isRepeated2 = type === "repeat";
25
25
  const options = {
26
26
  afterCallback: partial.afterCallback,
@@ -31,7 +31,8 @@ var timer = function(type, callback, partial) {
31
31
  };
32
32
  const state = {
33
33
  callback,
34
- active: false
34
+ active: false,
35
+ minimum: options.interval - options.interval % milliseconds / 2
35
36
  };
36
37
  const instance = Object.create({
37
38
  continue() {
@@ -62,17 +63,20 @@ var timer = function(type, callback, partial) {
62
63
  }
63
64
  }
64
65
  });
66
+ if (start) {
67
+ instance.start();
68
+ }
65
69
  return instance;
66
70
  };
67
71
  function wait(callback, options) {
68
72
  return timer("wait", callback, options == null || typeof options === "number" ? {
69
73
  interval: options
70
- } : options).start();
74
+ } : options, true);
71
75
  }
72
76
  function when(condition, options) {
73
77
  let rejecter;
74
78
  let resolver;
75
- const repeated = repeat(() => {
79
+ const repeated = timer("repeat", () => {
76
80
  if (condition()) {
77
81
  repeated.stop();
78
82
  resolver?.();
@@ -84,7 +88,7 @@ function when(condition, options) {
84
88
  count: options?.count,
85
89
  interval: options?.interval,
86
90
  timeout: options?.timeout
87
- });
91
+ }, false);
88
92
  const promise = new Promise((resolve, reject) => {
89
93
  resolver = resolve;
90
94
  rejecter = reject;
@@ -126,6 +130,7 @@ var work = function(type, timer2, state, options, isRepeated2) {
126
130
  return timer2;
127
131
  }
128
132
  const { count, interval, timeout } = options;
133
+ const { minimum } = state;
129
134
  if (["pause", "stop"].includes(type)) {
130
135
  activeTimers.delete(timer2);
131
136
  cancelAnimationFrame(state.frame);
@@ -144,7 +149,7 @@ var work = function(type, timer2, state, options, isRepeated2) {
144
149
  let index = type === "continue" ? +(state.index ?? 0) : 0;
145
150
  state.elapsed = elapsed;
146
151
  state.index = index;
147
- const total = (count === Number.POSITIVE_INFINITY ? timeout : (count - index) * (interval > 0 ? interval : 62.5)) - elapsed;
152
+ const total = (count === Number.POSITIVE_INFINITY ? Number.POSITIVE_INFINITY : (count - index) * (interval > 0 ? interval : milliseconds)) - elapsed;
148
153
  let current;
149
154
  let start;
150
155
  function finish(finished, error) {
@@ -167,7 +172,7 @@ var work = function(type, timer2, state, options, isRepeated2) {
167
172
  const time = timestamp - current;
168
173
  state.elapsed = elapsed + (current - start);
169
174
  const finished = time - elapsed >= total;
170
- if (finished || time - 2 < interval && interval < time + 2) {
175
+ if (finished || time >= minimum) {
171
176
  if (state.active) {
172
177
  state.callback(isRepeated2 ? index : undefined);
173
178
  }
@@ -193,6 +198,7 @@ var work = function(type, timer2, state, options, isRepeated2) {
193
198
  };
194
199
  var activeTimers = new Set;
195
200
  var hiddenTimers = new Set;
201
+ var milliseconds = 16.666666666666668;
196
202
  document.addEventListener("visibilitychange", () => {
197
203
  if (document.hidden) {
198
204
  for (const timer2 of activeTimers) {
package/package.json CHANGED
@@ -127,5 +127,5 @@
127
127
  },
128
128
  "type": "module",
129
129
  "types": "./types/index.d.ts",
130
- "version": "0.47.0"
130
+ "version": "0.48.0"
131
131
  }
package/src/js/timer.ts CHANGED
@@ -61,6 +61,7 @@ type State = {
61
61
  elapsed?: number;
62
62
  frame?: number;
63
63
  index?: number;
64
+ minimum: number;
64
65
  };
65
66
 
66
67
  /**
@@ -119,9 +120,21 @@ type WhenOptions = {} & OptionsWithCount;
119
120
 
120
121
  type WorkType = 'continue' | 'pause' | 'restart' | 'start' | 'stop';
121
122
 
123
+ /**
124
+ * A set of all active timers
125
+ */
122
126
  const activeTimers = new Set<Timer>();
127
+
128
+ /**
129
+ * A set of timers that were paused due to the document being hidden
130
+ */
123
131
  const hiddenTimers = new Set<Timer>();
124
132
 
133
+ /**
134
+ * Milliseconds in a frame, probably ;-)
135
+ */
136
+ const milliseconds = 1_000 / 60;
137
+
125
138
  function getValueOrDefault(value: unknown, defaultValue: number): number {
126
139
  return typeof value === 'number' && value > 0 ? value : defaultValue;
127
140
  }
@@ -171,13 +184,14 @@ export function repeat(
171
184
  callback: IndexedCallback,
172
185
  options?: Partial<RepeatOptions>,
173
186
  ): Timer {
174
- return timer('repeat', callback, options ?? {}).start();
187
+ return timer('repeat', callback, options ?? {}, true);
175
188
  }
176
189
 
177
190
  function timer(
178
191
  type: 'repeat' | 'wait',
179
192
  callback: AnyCallback,
180
193
  partial: Partial<TimerOptions>,
194
+ start: boolean,
181
195
  ): Timer {
182
196
  const isRepeated = type === 'repeat';
183
197
 
@@ -198,6 +212,7 @@ function timer(
198
212
  const state: State = {
199
213
  callback,
200
214
  active: false,
215
+ minimum: options.interval - (options.interval % milliseconds) / 2,
201
216
  };
202
217
 
203
218
  const instance = Object.create({
@@ -231,6 +246,10 @@ function timer(
231
246
  },
232
247
  });
233
248
 
249
+ if (start) {
250
+ instance.start();
251
+ }
252
+
234
253
  return instance;
235
254
  }
236
255
 
@@ -266,7 +285,8 @@ export function wait(
266
285
  interval: options,
267
286
  }
268
287
  : options,
269
- ).start();
288
+ true,
289
+ );
270
290
  }
271
291
 
272
292
  /**
@@ -280,7 +300,8 @@ export function when(
280
300
  let rejecter: () => void;
281
301
  let resolver: () => void;
282
302
 
283
- const repeated = repeat(
303
+ const repeated = timer(
304
+ 'repeat',
284
305
  () => {
285
306
  if (condition()) {
286
307
  repeated.stop();
@@ -296,6 +317,7 @@ export function when(
296
317
  interval: options?.interval,
297
318
  timeout: options?.timeout,
298
319
  },
320
+ false,
299
321
  );
300
322
 
301
323
  const promise = new Promise<void>((resolve, reject) => {
@@ -356,6 +378,7 @@ function work(
356
378
  }
357
379
 
358
380
  const {count, interval, timeout} = options;
381
+ const {minimum} = state;
359
382
 
360
383
  if (['pause', 'stop'].includes(type)) {
361
384
  activeTimers.delete(timer);
@@ -387,8 +410,8 @@ function work(
387
410
 
388
411
  const total =
389
412
  (count === Number.POSITIVE_INFINITY
390
- ? timeout
391
- : (count - index) * (interval > 0 ? interval : 1000 / 16)) - elapsed;
413
+ ? Number.POSITIVE_INFINITY
414
+ : (count - index) * (interval > 0 ? interval : milliseconds)) - elapsed;
392
415
 
393
416
  let current: DOMHighResTimeStamp | null;
394
417
  let start: DOMHighResTimeStamp | null;
@@ -422,7 +445,7 @@ function work(
422
445
 
423
446
  const finished = time - elapsed >= total;
424
447
 
425
- if (finished || (time - 2 < interval && interval < time + 2)) {
448
+ if (finished || time >= minimum) {
426
449
  if (state.active) {
427
450
  state.callback((isRepeated ? index : undefined) as never);
428
451
  }