@oscarpalmer/timer 0.25.0 → 0.27.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.
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const activeTimers = /* @__PURE__ */ new Set();
4
+ const beginTypes = /* @__PURE__ */ new Set(["continue", "start"]);
5
+ const endTypes = /* @__PURE__ */ new Set(["pause", "stop"]);
6
+ const endOrRestartTypes = /* @__PURE__ */ new Set([
7
+ "pause",
8
+ "restart",
9
+ "stop"
10
+ ]);
11
+ const hiddenTimers = /* @__PURE__ */ new Set();
12
+ const milliseconds = 1e3 / 60;
13
+ exports.activeTimers = activeTimers;
14
+ exports.beginTypes = beginTypes;
15
+ exports.endOrRestartTypes = endOrRestartTypes;
16
+ exports.endTypes = endTypes;
17
+ exports.hiddenTimers = hiddenTimers;
18
+ exports.milliseconds = milliseconds;
package/dist/constants.js CHANGED
@@ -1,19 +1,18 @@
1
- // src/constants.ts
2
- var activeTimers = new Set;
3
- var beginTypes = new Set(["continue", "start"]);
4
- var endTypes = new Set(["pause", "stop"]);
5
- var endOrRestartTypes = new Set([
1
+ const activeTimers = /* @__PURE__ */ new Set();
2
+ const beginTypes = /* @__PURE__ */ new Set(["continue", "start"]);
3
+ const endTypes = /* @__PURE__ */ new Set(["pause", "stop"]);
4
+ const endOrRestartTypes = /* @__PURE__ */ new Set([
6
5
  "pause",
7
6
  "restart",
8
7
  "stop"
9
8
  ]);
10
- var hiddenTimers = new Set;
11
- var milliseconds = 1000 / 60;
9
+ const hiddenTimers = /* @__PURE__ */ new Set();
10
+ const milliseconds = 1e3 / 60;
12
11
  export {
13
- milliseconds,
14
- hiddenTimers,
15
- endTypes,
16
- endOrRestartTypes,
12
+ activeTimers,
17
13
  beginTypes,
18
- activeTimers
14
+ endOrRestartTypes,
15
+ endTypes,
16
+ hiddenTimers,
17
+ milliseconds
19
18
  };
@@ -1,42 +1,44 @@
1
- // src/functions.ts
2
- import {
3
- activeTimers,
4
- beginTypes,
5
- endOrRestartTypes,
6
- endTypes,
7
- milliseconds
8
- } from "./constants";
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const constants = require("./constants.cjs");
9
4
  function getOptions(options, isRepeated) {
10
5
  return {
11
6
  afterCallback: options.afterCallback,
12
- count: getValueOrDefault(options.count, isRepeated ? Number.POSITIVE_INFINITY : 1),
7
+ count: getValueOrDefault(
8
+ options.count,
9
+ isRepeated ? Number.POSITIVE_INFINITY : 1
10
+ ),
13
11
  errorCallback: options.errorCallback,
14
- interval: getValueOrDefault(options.interval, milliseconds, milliseconds),
15
- timeout: getValueOrDefault(options.timeout, isRepeated ? Number.POSITIVE_INFINITY : 30000)
12
+ interval: getValueOrDefault(options.interval, constants.milliseconds, constants.milliseconds),
13
+ timeout: getValueOrDefault(
14
+ options.timeout,
15
+ isRepeated ? Number.POSITIVE_INFINITY : 3e4
16
+ )
16
17
  };
17
18
  }
18
19
  function getValueOrDefault(value, defaultValue, minimum) {
19
20
  return typeof value === "number" && value > (minimum ?? 0) ? value : defaultValue;
20
21
  }
21
22
  function work(type, timer, state, options) {
22
- if (state.destroyed && type !== "stop" || beginTypes.has(type) && state.active || endTypes.has(type) && !state.active) {
23
+ var _a;
24
+ if (state.destroyed && type !== "stop" || constants.beginTypes.has(type) && state.active || constants.endTypes.has(type) && !state.active) {
23
25
  return timer;
24
26
  }
25
27
  const { count, interval, timeout } = options;
26
28
  const { isRepeated, minimum } = state;
27
- if (endOrRestartTypes.has(type)) {
29
+ if (constants.endOrRestartTypes.has(type)) {
28
30
  const isStop = type === "stop";
29
- activeTimers.delete(timer);
31
+ constants.activeTimers.delete(timer);
30
32
  cancelAnimationFrame(state.frame);
31
33
  if (isStop) {
32
- options.afterCallback?.(false);
34
+ (_a = options.afterCallback) == null ? void 0 : _a.call(options, false);
33
35
  }
34
36
  state.active = false;
35
- state.frame = undefined;
37
+ state.frame = void 0;
36
38
  state.paused = !isStop;
37
39
  if (isStop) {
38
- state.elapsed = undefined;
39
- state.index = undefined;
40
+ state.elapsed = void 0;
41
+ state.index = void 0;
40
42
  }
41
43
  return type === "restart" ? work("start", timer, state, options) : timer;
42
44
  }
@@ -46,26 +48,27 @@ function work(type, timer, state, options) {
46
48
  let index = type === "continue" ? +(state.index ?? 0) : 0;
47
49
  state.elapsed = elapsed;
48
50
  state.index = index;
49
- const total = (count === Number.POSITIVE_INFINITY ? Number.POSITIVE_INFINITY : (count - index) * (interval > 0 ? interval : milliseconds)) - elapsed;
51
+ const total = (count === Number.POSITIVE_INFINITY ? Number.POSITIVE_INFINITY : (count - index) * (interval > 0 ? interval : constants.milliseconds)) - elapsed;
50
52
  let current;
51
53
  let start;
52
54
  function finish(finished, error) {
53
- activeTimers.delete(timer);
55
+ var _a2, _b;
56
+ constants.activeTimers.delete(timer);
54
57
  state.active = false;
55
- state.elapsed = undefined;
56
- state.frame = undefined;
57
- state.index = undefined;
58
+ state.elapsed = void 0;
59
+ state.frame = void 0;
60
+ state.index = void 0;
58
61
  if (error) {
59
- options.errorCallback?.();
62
+ (_a2 = options.errorCallback) == null ? void 0 : _a2.call(options);
60
63
  }
61
- options.afterCallback?.(finished);
64
+ (_b = options.afterCallback) == null ? void 0 : _b.call(options, finished);
62
65
  }
63
66
  function step(timestamp) {
64
67
  if (!state.active) {
65
68
  return;
66
69
  }
67
- current ??= timestamp;
68
- start ??= timestamp;
70
+ current ?? (current = timestamp);
71
+ start ?? (start = timestamp);
69
72
  const time = timestamp - current;
70
73
  state.elapsed = elapsed + (current - start);
71
74
  const finished = time - elapsed >= total;
@@ -75,7 +78,7 @@ function work(type, timer, state, options) {
75
78
  }
76
79
  if (finished || time >= minimum) {
77
80
  if (state.active) {
78
- state.callback(isRepeated ? index : undefined);
81
+ state.callback(isRepeated ? index : void 0);
79
82
  }
80
83
  index += 1;
81
84
  state.index = index;
@@ -88,12 +91,10 @@ function work(type, timer, state, options) {
88
91
  }
89
92
  state.frame = requestAnimationFrame(step);
90
93
  }
91
- activeTimers.add(timer);
94
+ constants.activeTimers.add(timer);
92
95
  state.frame = requestAnimationFrame(step);
93
96
  return timer;
94
97
  }
95
- export {
96
- work,
97
- getValueOrDefault,
98
- getOptions
99
- };
98
+ exports.getOptions = getOptions;
99
+ exports.getValueOrDefault = getValueOrDefault;
100
+ exports.work = work;
package/dist/functions.js CHANGED
@@ -1,29 +1,24 @@
1
- // src/constants.ts
2
- var activeTimers = new Set;
3
- var beginTypes = new Set(["continue", "start"]);
4
- var endTypes = new Set(["pause", "stop"]);
5
- var endOrRestartTypes = new Set([
6
- "pause",
7
- "restart",
8
- "stop"
9
- ]);
10
- var hiddenTimers = new Set;
11
- var milliseconds = 1000 / 60;
12
-
13
- // src/functions.ts
1
+ import { beginTypes, endTypes, endOrRestartTypes, activeTimers, milliseconds } from "./constants.js";
14
2
  function getOptions(options, isRepeated) {
15
3
  return {
16
4
  afterCallback: options.afterCallback,
17
- count: getValueOrDefault(options.count, isRepeated ? Number.POSITIVE_INFINITY : 1),
5
+ count: getValueOrDefault(
6
+ options.count,
7
+ isRepeated ? Number.POSITIVE_INFINITY : 1
8
+ ),
18
9
  errorCallback: options.errorCallback,
19
10
  interval: getValueOrDefault(options.interval, milliseconds, milliseconds),
20
- timeout: getValueOrDefault(options.timeout, isRepeated ? Number.POSITIVE_INFINITY : 30000)
11
+ timeout: getValueOrDefault(
12
+ options.timeout,
13
+ isRepeated ? Number.POSITIVE_INFINITY : 3e4
14
+ )
21
15
  };
22
16
  }
23
17
  function getValueOrDefault(value, defaultValue, minimum) {
24
18
  return typeof value === "number" && value > (minimum ?? 0) ? value : defaultValue;
25
19
  }
26
20
  function work(type, timer, state, options) {
21
+ var _a;
27
22
  if (state.destroyed && type !== "stop" || beginTypes.has(type) && state.active || endTypes.has(type) && !state.active) {
28
23
  return timer;
29
24
  }
@@ -34,14 +29,14 @@ function work(type, timer, state, options) {
34
29
  activeTimers.delete(timer);
35
30
  cancelAnimationFrame(state.frame);
36
31
  if (isStop) {
37
- options.afterCallback?.(false);
32
+ (_a = options.afterCallback) == null ? void 0 : _a.call(options, false);
38
33
  }
39
34
  state.active = false;
40
- state.frame = undefined;
35
+ state.frame = void 0;
41
36
  state.paused = !isStop;
42
37
  if (isStop) {
43
- state.elapsed = undefined;
44
- state.index = undefined;
38
+ state.elapsed = void 0;
39
+ state.index = void 0;
45
40
  }
46
41
  return type === "restart" ? work("start", timer, state, options) : timer;
47
42
  }
@@ -55,22 +50,23 @@ function work(type, timer, state, options) {
55
50
  let current;
56
51
  let start;
57
52
  function finish(finished, error) {
53
+ var _a2, _b;
58
54
  activeTimers.delete(timer);
59
55
  state.active = false;
60
- state.elapsed = undefined;
61
- state.frame = undefined;
62
- state.index = undefined;
56
+ state.elapsed = void 0;
57
+ state.frame = void 0;
58
+ state.index = void 0;
63
59
  if (error) {
64
- options.errorCallback?.();
60
+ (_a2 = options.errorCallback) == null ? void 0 : _a2.call(options);
65
61
  }
66
- options.afterCallback?.(finished);
62
+ (_b = options.afterCallback) == null ? void 0 : _b.call(options, finished);
67
63
  }
68
64
  function step(timestamp) {
69
65
  if (!state.active) {
70
66
  return;
71
67
  }
72
- current ??= timestamp;
73
- start ??= timestamp;
68
+ current ?? (current = timestamp);
69
+ start ?? (start = timestamp);
74
70
  const time = timestamp - current;
75
71
  state.elapsed = elapsed + (current - start);
76
72
  const finished = time - elapsed >= total;
@@ -80,7 +76,7 @@ function work(type, timer, state, options) {
80
76
  }
81
77
  if (finished || time >= minimum) {
82
78
  if (state.active) {
83
- state.callback(isRepeated ? index : undefined);
79
+ state.callback(isRepeated ? index : void 0);
84
80
  }
85
81
  index += 1;
86
82
  state.index = index;
@@ -98,7 +94,7 @@ function work(type, timer, state, options) {
98
94
  return timer;
99
95
  }
100
96
  export {
101
- work,
97
+ getOptions,
102
98
  getValueOrDefault,
103
- getOptions
99
+ work
104
100
  };
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ const constants = require("./constants.cjs");
3
+ if (globalThis._oscarpalmer_timers == null) {
4
+ Object.defineProperty(globalThis, "_oscarpalmer_timers", {
5
+ get() {
6
+ return globalThis._oscarpalmer_timer_debug ? [...constants.activeTimers] : [];
7
+ }
8
+ });
9
+ }
package/dist/global.js CHANGED
@@ -1,16 +1,4 @@
1
- // src/constants.ts
2
- var activeTimers = new Set;
3
- var beginTypes = new Set(["continue", "start"]);
4
- var endTypes = new Set(["pause", "stop"]);
5
- var endOrRestartTypes = new Set([
6
- "pause",
7
- "restart",
8
- "stop"
9
- ]);
10
- var hiddenTimers = new Set;
11
- var milliseconds = 1000 / 60;
12
-
13
- // src/global.ts
1
+ import { activeTimers } from "./constants.js";
14
2
  if (globalThis._oscarpalmer_timers == null) {
15
3
  Object.defineProperty(globalThis, "_oscarpalmer_timers", {
16
4
  get() {
package/dist/index.cjs ADDED
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const _function = require("@oscarpalmer/atoms/function");
4
+ const constants = require("./constants.cjs");
5
+ require("./global.cjs");
6
+ const timer = require("./timer.cjs");
7
+ const is = require("./is.cjs");
8
+ const when = require("./when.cjs");
9
+ function delay(time, timeout) {
10
+ return new Promise((resolve, reject) => {
11
+ const delayed = timer.wait(
12
+ () => {
13
+ delayed.destroy();
14
+ (resolve ?? _function.noop)();
15
+ },
16
+ {
17
+ timeout,
18
+ errorCallback: () => {
19
+ delayed.destroy();
20
+ (reject ?? _function.noop)();
21
+ },
22
+ interval: time
23
+ }
24
+ );
25
+ });
26
+ }
27
+ document.addEventListener("visibilitychange", () => {
28
+ if (document.hidden) {
29
+ for (const timer2 of constants.activeTimers) {
30
+ constants.hiddenTimers.add(timer2);
31
+ timer2.pause();
32
+ }
33
+ } else {
34
+ for (const timer2 of constants.hiddenTimers) {
35
+ timer2.continue();
36
+ }
37
+ constants.hiddenTimers.clear();
38
+ }
39
+ });
40
+ exports.repeat = timer.repeat;
41
+ exports.wait = timer.wait;
42
+ exports.isRepeated = is.isRepeated;
43
+ exports.isTimer = is.isTimer;
44
+ exports.isWaited = is.isWaited;
45
+ exports.isWhen = is.isWhen;
46
+ exports.when = when.when;
47
+ exports.delay = delay;