@oscarpalmer/atoms 0.169.0 → 0.170.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/index.mjs CHANGED
@@ -1146,24 +1146,25 @@ function getInterval(value) {
1146
1146
  }
1147
1147
  function getTimer(type, callback, time) {
1148
1148
  const interval = getInterval(time);
1149
- function run(now) {
1149
+ function run() {
1150
+ const now = performance.now();
1150
1151
  start ??= now;
1151
1152
  if (interval === 0 || now - start >= interval - OFFSET) {
1152
1153
  start = throttle ? now : void 0;
1153
1154
  callback(...args);
1154
- } else frame = requestAnimationFrame(run);
1155
+ } else id = startTimer(run);
1155
1156
  }
1156
1157
  const throttle = type === TIMER_THROTTLE;
1157
1158
  let args;
1158
- let frame;
1159
+ let id;
1159
1160
  let start;
1160
1161
  const timer = (...parameters) => {
1161
1162
  timer.cancel();
1162
1163
  args = parameters;
1163
- frame = requestAnimationFrame(run);
1164
+ id = startTimer(run);
1164
1165
  };
1165
1166
  timer.cancel = () => {
1166
- cancelAnimationFrame(frame);
1167
+ clearTimer(id);
1167
1168
  };
1168
1169
  return timer;
1169
1170
  }
@@ -1171,6 +1172,8 @@ const OFFSET = 5;
1171
1172
  const TIMER_DEBOUNCE = "debounce";
1172
1173
  const TIMER_THROTTLE = "throttle";
1173
1174
  const TIMER_WAIT = "wait";
1175
+ const clearTimer = typeof cancelAnimationFrame === "function" ? cancelAnimationFrame : clearTimeout;
1176
+ const startTimer = typeof requestAnimationFrame === "function" ? requestAnimationFrame : setTimeout;
1174
1177
  //#endregion
1175
1178
  //#region src/internal/function/misc.ts
1176
1179
  /**
@@ -2313,7 +2316,7 @@ function cloneValue(value, depth, references) {
2313
2316
  case value instanceof Date: return new Date(value.getTime());
2314
2317
  case value instanceof RegExp: return cloneRegularExpression(value, depth, references);
2315
2318
  case value instanceof Map: return cloneMap(value, depth, references);
2316
- case value instanceof Node: return cloneNode(value, depth, references);
2319
+ case typeof Node !== "undefined" && value instanceof Node: return cloneNode(value, depth, references);
2317
2320
  case value instanceof Set: return cloneSet(value, depth, references);
2318
2321
  case isArrayOrPlainObject(value): return clonePlainObject(value, depth, references);
2319
2322
  case isTypedArray(value): return cloneTypedArray(value, depth, references);
@@ -4,24 +4,25 @@ function getInterval(value) {
4
4
  }
5
5
  function getTimer(type, callback, time) {
6
6
  const interval = getInterval(time);
7
- function run(now) {
7
+ function run() {
8
+ const now = performance.now();
8
9
  start ??= now;
9
10
  if (interval === 0 || now - start >= interval - OFFSET) {
10
11
  start = throttle ? now : void 0;
11
12
  callback(...args);
12
- } else frame = requestAnimationFrame(run);
13
+ } else id = startTimer(run);
13
14
  }
14
15
  const throttle = type === TIMER_THROTTLE;
15
16
  let args;
16
- let frame;
17
+ let id;
17
18
  let start;
18
19
  const timer = (...parameters) => {
19
20
  timer.cancel();
20
21
  args = parameters;
21
- frame = requestAnimationFrame(run);
22
+ id = startTimer(run);
22
23
  };
23
24
  timer.cancel = () => {
24
- cancelAnimationFrame(frame);
25
+ clearTimer(id);
25
26
  };
26
27
  return timer;
27
28
  }
@@ -29,5 +30,7 @@ const OFFSET = 5;
29
30
  const TIMER_DEBOUNCE = "debounce";
30
31
  const TIMER_THROTTLE = "throttle";
31
32
  const TIMER_WAIT = "wait";
33
+ const clearTimer = typeof cancelAnimationFrame === "function" ? cancelAnimationFrame : clearTimeout;
34
+ const startTimer = typeof requestAnimationFrame === "function" ? requestAnimationFrame : setTimeout;
32
35
  //#endregion
33
36
  export { TIMER_DEBOUNCE, TIMER_THROTTLE, TIMER_WAIT, getTimer };
@@ -103,7 +103,7 @@ function cloneValue(value, depth, references) {
103
103
  case value instanceof Date: return new Date(value.getTime());
104
104
  case value instanceof RegExp: return cloneRegularExpression(value, depth, references);
105
105
  case value instanceof Map: return cloneMap(value, depth, references);
106
- case value instanceof Node: return cloneNode(value, depth, references);
106
+ case typeof Node !== "undefined" && value instanceof Node: return cloneNode(value, depth, references);
107
107
  case value instanceof Set: return cloneSet(value, depth, references);
108
108
  case isArrayOrPlainObject(value): return clonePlainObject(value, depth, references);
109
109
  case isTypedArray(value): return cloneTypedArray(value, depth, references);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oscarpalmer/atoms",
3
- "version": "0.169.0",
3
+ "version": "0.170.0",
4
4
  "description": "Atomic utilities for making your JavaScript better.",
5
5
  "keywords": [
6
6
  "helper",
@@ -19,7 +19,9 @@ export function getTimer<Callback extends GenericCallback>(
19
19
  ): CancelableCallback<Callback> {
20
20
  const interval = getInterval(time);
21
21
 
22
- function run(now: DOMHighResTimeStamp): void {
22
+ function run(): void {
23
+ const now = performance.now();
24
+
23
25
  start ??= now;
24
26
 
25
27
  if (interval === 0 || now - start >= interval - OFFSET) {
@@ -27,25 +29,26 @@ export function getTimer<Callback extends GenericCallback>(
27
29
 
28
30
  callback(...args);
29
31
  } else {
30
- frame = requestAnimationFrame(run);
32
+ id = startTimer(run);
31
33
  }
32
34
  }
33
35
 
34
36
  const throttle = type === TIMER_THROTTLE;
35
37
 
36
38
  let args: Parameters<Callback>;
37
- let frame: DOMHighResTimeStamp | undefined;
38
- let start: DOMHighResTimeStamp | undefined;
39
+ let id: number;
40
+ let start: number | undefined;
39
41
 
40
42
  const timer = (...parameters: Parameters<Callback>): void => {
41
43
  timer.cancel();
42
44
 
43
45
  args = parameters;
44
- frame = requestAnimationFrame(run);
46
+
47
+ id = startTimer(run);
45
48
  };
46
49
 
47
50
  timer.cancel = (): void => {
48
- cancelAnimationFrame(frame!);
51
+ clearTimer(id);
49
52
  };
50
53
 
51
54
  return timer as CancelableCallback<Callback>;
@@ -63,4 +66,8 @@ export const TIMER_THROTTLE: TimerType = 'throttle';
63
66
 
64
67
  export const TIMER_WAIT: TimerType = 'wait';
65
68
 
69
+ const clearTimer = typeof cancelAnimationFrame === 'function' ? cancelAnimationFrame : clearTimeout;
70
+
71
+ const startTimer = typeof requestAnimationFrame === 'function' ? requestAnimationFrame : setTimeout;
72
+
66
73
  // #endregion
@@ -243,7 +243,7 @@ function cloneValue(value: unknown, depth: number, references: WeakMap<WeakKey,
243
243
  case value instanceof Map:
244
244
  return cloneMap(value, depth, references);
245
245
 
246
- case value instanceof Node:
246
+ case typeof Node !== 'undefined' && value instanceof Node:
247
247
  return cloneNode(value, depth, references);
248
248
 
249
249
  case value instanceof Set: