@oscarpalmer/timer 0.33.0 → 0.35.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.
@@ -1,50 +1,53 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
1
  function calculate() {
6
- return new Promise((resolve) => {
7
- const values = [];
8
- let last;
9
- function step(now) {
10
- if (last != null) {
11
- values.push(now - last);
12
- }
13
- last = now;
14
- if (values.length >= 10) {
15
- const median = values.sort().slice(2, -2).reduce((first, second) => first + second, 0) / (values.length - 4);
16
- resolve(median);
17
- } else {
18
- requestAnimationFrame(step);
19
- }
20
- }
21
- requestAnimationFrame(step);
22
- });
2
+ return new Promise((resolve) => {
3
+ const values = [];
4
+ let last;
5
+ function step(now) {
6
+ if (last != null) values.push(now - last);
7
+ last = now;
8
+ if (values.length >= 10) {
9
+ const median = values.sort().slice(2, -2).reduce((first, second) => first + second, 0) / (values.length - 4);
10
+ resolve(median);
11
+ } else requestAnimationFrame(step);
12
+ }
13
+ requestAnimationFrame(step);
14
+ });
23
15
  }
16
+ const defaultTimeout = 3e4;
24
17
  const activeTimers = /* @__PURE__ */ new Set();
25
- const beginTypes = /* @__PURE__ */ new Set(["continue", "start"]);
26
18
  const intervalBuffer = 5;
27
19
  const destroyedMessage = "Timer has already been destroyed";
28
- const endTypes = /* @__PURE__ */ new Set(["pause", "stop"]);
29
- const endOrRestartTypes = /* @__PURE__ */ new Set([
30
- "pause",
31
- "restart",
32
- "stop"
33
- ]);
34
20
  const hiddenTimers = /* @__PURE__ */ new Set();
35
- const pauseTypes = /* @__PURE__ */ new Set(["continue", "pause"]);
36
21
  const startedMessage = "Timer has already been started";
37
- exports.milliseconds = 1e3 / 60;
22
+ const TYPE_REPEAT = "repeat";
23
+ const TYPE_WAIT = "wait";
24
+ const TYPE_WHEN = "when";
25
+ const WORK_CONTINUE = "continue";
26
+ const WORK_PAUSE = "pause";
27
+ const WORK_RESTART = "restart";
28
+ const WORK_START = "start";
29
+ const WORK_STOP = "stop";
30
+ let milliseconds = 1e3 / 60;
38
31
  calculate().then((value) => {
39
- exports.milliseconds = value;
32
+ milliseconds = value;
40
33
  });
41
-
34
+ exports.TYPE_REPEAT = TYPE_REPEAT;
35
+ exports.TYPE_WAIT = TYPE_WAIT;
36
+ exports.TYPE_WHEN = TYPE_WHEN;
37
+ exports.WORK_CONTINUE = WORK_CONTINUE;
38
+ exports.WORK_PAUSE = WORK_PAUSE;
39
+ exports.WORK_RESTART = WORK_RESTART;
40
+ exports.WORK_START = WORK_START;
41
+ exports.WORK_STOP = WORK_STOP;
42
42
  exports.activeTimers = activeTimers;
43
- exports.beginTypes = beginTypes;
43
+ exports.defaultTimeout = defaultTimeout;
44
44
  exports.destroyedMessage = destroyedMessage;
45
- exports.endOrRestartTypes = endOrRestartTypes;
46
- exports.endTypes = endTypes;
47
45
  exports.hiddenTimers = hiddenTimers;
48
46
  exports.intervalBuffer = intervalBuffer;
49
- exports.pauseTypes = pauseTypes;
47
+ Object.defineProperty(exports, "milliseconds", {
48
+ enumerable: true,
49
+ get: function() {
50
+ return milliseconds;
51
+ }
52
+ });
50
53
  exports.startedMessage = startedMessage;
package/dist/constants.js CHANGED
@@ -1,38 +1,34 @@
1
1
  function calculate() {
2
- return new Promise((resolve) => {
3
- const values = [];
4
- let last;
5
- function step(now) {
6
- if (last != null) {
7
- values.push(now - last);
8
- }
9
- last = now;
10
- if (values.length >= 10) {
11
- const median = values.sort().slice(2, -2).reduce((first, second) => first + second, 0) / (values.length - 4);
12
- resolve(median);
13
- } else {
14
- requestAnimationFrame(step);
15
- }
16
- }
17
- requestAnimationFrame(step);
18
- });
2
+ return new Promise((resolve) => {
3
+ const values = [];
4
+ let last;
5
+ function step(now) {
6
+ if (last != null) values.push(now - last);
7
+ last = now;
8
+ if (values.length >= 10) {
9
+ const median = values.sort().slice(2, -2).reduce((first, second) => first + second, 0) / (values.length - 4);
10
+ resolve(median);
11
+ } else requestAnimationFrame(step);
12
+ }
13
+ requestAnimationFrame(step);
14
+ });
19
15
  }
16
+ const defaultTimeout = 3e4;
20
17
  const activeTimers = /* @__PURE__ */ new Set();
21
- const beginTypes = /* @__PURE__ */ new Set(["continue", "start"]);
22
18
  const intervalBuffer = 5;
23
19
  const destroyedMessage = "Timer has already been destroyed";
24
- const endTypes = /* @__PURE__ */ new Set(["pause", "stop"]);
25
- const endOrRestartTypes = /* @__PURE__ */ new Set([
26
- "pause",
27
- "restart",
28
- "stop"
29
- ]);
30
20
  const hiddenTimers = /* @__PURE__ */ new Set();
31
- const pauseTypes = /* @__PURE__ */ new Set(["continue", "pause"]);
32
21
  const startedMessage = "Timer has already been started";
22
+ const TYPE_REPEAT = "repeat";
23
+ const TYPE_WAIT = "wait";
24
+ const TYPE_WHEN = "when";
25
+ const WORK_CONTINUE = "continue";
26
+ const WORK_PAUSE = "pause";
27
+ const WORK_RESTART = "restart";
28
+ const WORK_START = "start";
29
+ const WORK_STOP = "stop";
33
30
  let milliseconds = 1e3 / 60;
34
31
  calculate().then((value) => {
35
- milliseconds = value;
32
+ milliseconds = value;
36
33
  });
37
-
38
- export { activeTimers, beginTypes, destroyedMessage, endOrRestartTypes, endTypes, hiddenTimers, intervalBuffer, milliseconds, pauseTypes, startedMessage };
34
+ export { TYPE_REPEAT, TYPE_WAIT, TYPE_WHEN, WORK_CONTINUE, WORK_PAUSE, WORK_RESTART, WORK_START, WORK_STOP, activeTimers, defaultTimeout, destroyedMessage, hiddenTimers, intervalBuffer, milliseconds, startedMessage };
package/dist/delay.cjs CHANGED
@@ -1,24 +1,15 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const constants = require('./constants.cjs');
6
- const get = require('./get.cjs');
7
-
1
+ const require_constants = require("./constants.cjs");
2
+ const require_get = require("./get.cjs");
8
3
  function delay(time) {
9
- return new Promise((resolve) => {
10
- const interval = get.getValidNumber(time, constants.milliseconds);
11
- let start;
12
- function step(now) {
13
- start ??= now;
14
- if (interval === constants.milliseconds || now - start >= interval - constants.intervalBuffer) {
15
- resolve();
16
- } else {
17
- requestAnimationFrame(step);
18
- }
19
- }
20
- requestAnimationFrame(step);
21
- });
4
+ return new Promise((resolve) => {
5
+ const interval = require_get.getValidNumber(time, require_constants.milliseconds);
6
+ let start;
7
+ function step(now) {
8
+ start ??= now;
9
+ if (interval === require_constants.milliseconds || now - start >= interval - require_constants.intervalBuffer) resolve();
10
+ else requestAnimationFrame(step);
11
+ }
12
+ requestAnimationFrame(step);
13
+ });
22
14
  }
23
-
24
15
  exports.delay = delay;
package/dist/delay.js CHANGED
@@ -1,20 +1,15 @@
1
- import { milliseconds, intervalBuffer } from './constants.js';
2
- import { getValidNumber } from './get.js';
3
-
1
+ import { intervalBuffer, milliseconds } from "./constants.js";
2
+ import { getValidNumber } from "./get.js";
4
3
  function delay(time) {
5
- return new Promise((resolve) => {
6
- const interval = getValidNumber(time, milliseconds);
7
- let start;
8
- function step(now) {
9
- start ??= now;
10
- if (interval === milliseconds || now - start >= interval - intervalBuffer) {
11
- resolve();
12
- } else {
13
- requestAnimationFrame(step);
14
- }
15
- }
16
- requestAnimationFrame(step);
17
- });
4
+ return new Promise((resolve) => {
5
+ const interval = getValidNumber(time, milliseconds);
6
+ let start;
7
+ function step(now) {
8
+ start ??= now;
9
+ if (interval === milliseconds || now - start >= interval - intervalBuffer) resolve();
10
+ else requestAnimationFrame(step);
11
+ }
12
+ requestAnimationFrame(step);
13
+ });
18
14
  }
19
-
20
15
  export { delay };
package/dist/get.cjs CHANGED
@@ -1,15 +1,10 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const _function = require('@oscarpalmer/atoms/function');
6
-
1
+ const require_rolldown_runtime = require("./rolldown_runtime.cjs");
2
+ const __oscarpalmer_atoms_function = require_rolldown_runtime.__toESM(require("@oscarpalmer/atoms/function"));
7
3
  function getCallback(value) {
8
- return typeof value === "function" ? value : _function.noop;
4
+ return typeof value === "function" ? value : __oscarpalmer_atoms_function.noop;
9
5
  }
10
6
  function getValidNumber(value, defaultValue) {
11
- return typeof value === "number" && value > (defaultValue ?? 0) ? value : defaultValue ?? 0;
7
+ return typeof value === "number" ? value > 0 ? value : 0 : defaultValue ?? 0;
12
8
  }
13
-
14
9
  exports.getCallback = getCallback;
15
10
  exports.getValidNumber = getValidNumber;
package/dist/get.js CHANGED
@@ -1,10 +1,8 @@
1
- import { noop } from '@oscarpalmer/atoms/function';
2
-
1
+ import { noop } from "@oscarpalmer/atoms/function";
3
2
  function getCallback(value) {
4
- return typeof value === "function" ? value : noop;
3
+ return typeof value === "function" ? value : noop;
5
4
  }
6
5
  function getValidNumber(value, defaultValue) {
7
- return typeof value === "number" && value > (defaultValue ?? 0) ? value : defaultValue ?? 0;
6
+ return typeof value === "number" ? value > 0 ? value : 0 : defaultValue ?? 0;
8
7
  }
9
-
10
8
  export { getCallback, getValidNumber };
package/dist/global.cjs CHANGED
@@ -1,24 +1,14 @@
1
- 'use strict';
2
-
3
- const constants = require('./constants.cjs');
4
-
5
- if (globalThis._oscarpalmer_timers == null) {
6
- Object.defineProperty(globalThis, "_oscarpalmer_timers", {
7
- get() {
8
- return globalThis._oscarpalmer_timer_debug ? [...constants.activeTimers] : [];
9
- }
10
- });
11
- }
1
+ const require_constants = require("./constants.cjs");
2
+ if (globalThis._oscarpalmer_timers == null) Object.defineProperty(globalThis, "_oscarpalmer_timers", { get() {
3
+ return globalThis._oscarpalmer_timer_debug ? [...require_constants.activeTimers] : [];
4
+ } });
12
5
  document.addEventListener("visibilitychange", () => {
13
- if (document.hidden) {
14
- for (const timer of constants.activeTimers) {
15
- constants.hiddenTimers.add(timer);
16
- timer.pause();
17
- }
18
- } else {
19
- for (const timer of constants.hiddenTimers) {
20
- timer.continue();
21
- }
22
- constants.hiddenTimers.clear();
23
- }
6
+ const from = document.hidden ? require_constants.activeTimers : require_constants.hiddenTimers;
7
+ const method = document.hidden ? require_constants.WORK_PAUSE : require_constants.WORK_CONTINUE;
8
+ const to = document.hidden ? require_constants.hiddenTimers : require_constants.activeTimers;
9
+ for (const timer of from) {
10
+ timer[method]();
11
+ to.add(timer);
12
+ }
13
+ from.clear();
24
14
  });
package/dist/global.js CHANGED
@@ -1,22 +1,14 @@
1
- import { activeTimers, hiddenTimers } from './constants.js';
2
-
3
- if (globalThis._oscarpalmer_timers == null) {
4
- Object.defineProperty(globalThis, "_oscarpalmer_timers", {
5
- get() {
6
- return globalThis._oscarpalmer_timer_debug ? [...activeTimers] : [];
7
- }
8
- });
9
- }
1
+ import { WORK_CONTINUE, WORK_PAUSE, activeTimers, hiddenTimers } from "./constants.js";
2
+ if (globalThis._oscarpalmer_timers == null) Object.defineProperty(globalThis, "_oscarpalmer_timers", { get() {
3
+ return globalThis._oscarpalmer_timer_debug ? [...activeTimers] : [];
4
+ } });
10
5
  document.addEventListener("visibilitychange", () => {
11
- if (document.hidden) {
12
- for (const timer of activeTimers) {
13
- hiddenTimers.add(timer);
14
- timer.pause();
15
- }
16
- } else {
17
- for (const timer of hiddenTimers) {
18
- timer.continue();
19
- }
20
- hiddenTimers.clear();
21
- }
6
+ const from = document.hidden ? activeTimers : hiddenTimers;
7
+ const method = document.hidden ? WORK_PAUSE : WORK_CONTINUE;
8
+ const to = document.hidden ? hiddenTimers : activeTimers;
9
+ for (const timer of from) {
10
+ timer[method]();
11
+ to.add(timer);
12
+ }
13
+ from.clear();
22
14
  });
package/dist/index.cjs CHANGED
@@ -1,21 +1,14 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- require('./global.cjs');
6
- const delay = require('./delay.cjs');
7
- const is = require('./is.cjs');
8
- const repeat = require('./repeat.cjs');
9
- const wait = require('./wait.cjs');
10
- const when = require('./when.cjs');
11
-
12
-
13
-
14
- exports.delay = delay.delay;
15
- exports.isRepeated = is.isRepeated;
16
- exports.isTimer = is.isTimer;
17
- exports.isWaited = is.isWaited;
18
- exports.isWhen = is.isWhen;
19
- exports.repeat = repeat.repeat;
20
- exports.wait = wait.wait;
21
- exports.when = when.when;
1
+ require("./global.cjs");
2
+ const require_when = require("./when.cjs");
3
+ const require_wait = require("./wait.cjs");
4
+ const require_repeat = require("./repeat.cjs");
5
+ const require_is = require("./is.cjs");
6
+ const require_delay = require("./delay.cjs");
7
+ exports.delay = require_delay.delay;
8
+ exports.isRepeated = require_is.isRepeated;
9
+ exports.isTimer = require_is.isTimer;
10
+ exports.isWaited = require_is.isWaited;
11
+ exports.isWhen = require_is.isWhen;
12
+ exports.repeat = require_repeat.repeat;
13
+ exports.wait = require_wait.wait;
14
+ exports.when = require_when.when;
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
- import './global.js';
2
- export { delay } from './delay.js';
3
- export { isRepeated, isTimer, isWaited, isWhen } from './is.js';
4
- export { repeat } from './repeat.js';
5
- export { wait } from './wait.js';
6
- export { when } from './when.js';
1
+ import "./global.js";
2
+ import { when } from "./when.js";
3
+ import { wait } from "./wait.js";
4
+ import { repeat } from "./repeat.js";
5
+ import { isRepeated, isTimer, isWaited, isWhen } from "./is.js";
6
+ import { delay } from "./delay.js";
7
+ export { delay, isRepeated, isTimer, isWaited, isWhen, repeat, wait, when };
package/dist/is.cjs CHANGED
@@ -1,23 +1,19 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
1
+ const require_constants = require("./constants.cjs");
5
2
  function is(names, value) {
6
- return names.includes(value?.$timer);
3
+ return names.includes(value?.$timer);
7
4
  }
8
5
  function isRepeated(value) {
9
- return is(["repeat"], value);
6
+ return is([require_constants.TYPE_REPEAT], value);
10
7
  }
11
8
  function isTimer(value) {
12
- return is(["repeat", "wait"], value);
9
+ return is([require_constants.TYPE_REPEAT, require_constants.TYPE_WAIT], value);
13
10
  }
14
11
  function isWaited(value) {
15
- return is(["wait"], value);
12
+ return is([require_constants.TYPE_WAIT], value);
16
13
  }
17
14
  function isWhen(value) {
18
- return is(["when"], value) && typeof value.then === "function";
15
+ return is([require_constants.TYPE_WHEN], value) && typeof value.then === "function";
19
16
  }
20
-
21
17
  exports.isRepeated = isRepeated;
22
18
  exports.isTimer = isTimer;
23
19
  exports.isWaited = isWaited;
package/dist/is.js CHANGED
@@ -1,17 +1,17 @@
1
+ import { TYPE_REPEAT, TYPE_WAIT, TYPE_WHEN } from "./constants.js";
1
2
  function is(names, value) {
2
- return names.includes(value?.$timer);
3
+ return names.includes(value?.$timer);
3
4
  }
4
5
  function isRepeated(value) {
5
- return is(["repeat"], value);
6
+ return is([TYPE_REPEAT], value);
6
7
  }
7
8
  function isTimer(value) {
8
- return is(["repeat", "wait"], value);
9
+ return is([TYPE_REPEAT, TYPE_WAIT], value);
9
10
  }
10
11
  function isWaited(value) {
11
- return is(["wait"], value);
12
+ return is([TYPE_WAIT], value);
12
13
  }
13
14
  function isWhen(value) {
14
- return is(["when"], value) && typeof value.then === "function";
15
+ return is([TYPE_WHEN], value) && typeof value.then === "function";
15
16
  }
16
-
17
17
  export { isRepeated, isTimer, isWaited, isWhen };
package/dist/models.cjs CHANGED
@@ -1,12 +1,7 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- class TimerTrace extends Error {
6
- constructor() {
7
- super();
8
- this.name = "TimerTrace";
9
- }
10
- }
11
-
1
+ var TimerTrace = class extends Error {
2
+ constructor() {
3
+ super();
4
+ this.name = "TimerTrace";
5
+ }
6
+ };
12
7
  exports.TimerTrace = TimerTrace;
package/dist/models.js CHANGED
@@ -1,8 +1,7 @@
1
- class TimerTrace extends Error {
2
- constructor() {
3
- super();
4
- this.name = "TimerTrace";
5
- }
6
- }
7
-
1
+ var TimerTrace = class extends Error {
2
+ constructor() {
3
+ super();
4
+ this.name = "TimerTrace";
5
+ }
6
+ };
8
7
  export { TimerTrace };
package/dist/repeat.cjs CHANGED
@@ -1,29 +1,18 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const constants = require('./constants.cjs');
6
- const get = require('./get.cjs');
7
- require('./global.cjs');
8
- const models = require('./models.cjs');
9
- const timer = require('./timer.cjs');
10
-
1
+ const require_constants = require("./constants.cjs");
2
+ const require_get = require("./get.cjs");
3
+ require("./global.cjs");
4
+ const require_models = require("./models.cjs");
5
+ const require_timer = require("./timer.cjs");
11
6
  function repeat(callback, options) {
12
- return new timer.Timer(
13
- "repeat",
14
- {
15
- callback: get.getCallback(callback),
16
- trace: new models.TimerTrace().stack
17
- },
18
- {
19
- onAfter: get.getCallback(options?.onAfter),
20
- onError: void 0,
21
- count: get.getValidNumber(options?.count),
22
- interval: get.getValidNumber(options?.interval, constants.milliseconds),
23
- timeout: 0
24
- },
25
- true
26
- );
7
+ return new require_timer.Timer(require_constants.TYPE_REPEAT, {
8
+ callback: require_get.getCallback(callback),
9
+ trace: new require_models.TimerTrace().stack
10
+ }, {
11
+ onAfter: require_get.getCallback(options?.onAfter),
12
+ onError: void 0,
13
+ count: require_get.getValidNumber(options?.count),
14
+ interval: require_get.getValidNumber(options?.interval, require_constants.milliseconds),
15
+ timeout: 0
16
+ }, true);
27
17
  }
28
-
29
18
  exports.repeat = repeat;
package/dist/repeat.js CHANGED
@@ -1,25 +1,18 @@
1
- import { milliseconds } from './constants.js';
2
- import { getCallback, getValidNumber } from './get.js';
3
- import './global.js';
4
- import { TimerTrace } from './models.js';
5
- import { Timer } from './timer.js';
6
-
1
+ import { TYPE_REPEAT, milliseconds } from "./constants.js";
2
+ import { getCallback, getValidNumber } from "./get.js";
3
+ import "./global.js";
4
+ import { TimerTrace } from "./models.js";
5
+ import { Timer } from "./timer.js";
7
6
  function repeat(callback, options) {
8
- return new Timer(
9
- "repeat",
10
- {
11
- callback: getCallback(callback),
12
- trace: new TimerTrace().stack
13
- },
14
- {
15
- onAfter: getCallback(options?.onAfter),
16
- onError: void 0,
17
- count: getValidNumber(options?.count),
18
- interval: getValidNumber(options?.interval, milliseconds),
19
- timeout: 0
20
- },
21
- true
22
- );
7
+ return new Timer(TYPE_REPEAT, {
8
+ callback: getCallback(callback),
9
+ trace: new TimerTrace().stack
10
+ }, {
11
+ onAfter: getCallback(options?.onAfter),
12
+ onError: void 0,
13
+ count: getValidNumber(options?.count),
14
+ interval: getValidNumber(options?.interval, milliseconds),
15
+ timeout: 0
16
+ }, true);
23
17
  }
24
-
25
18
  export { repeat };
@@ -0,0 +1,21 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __copyProps = (to, from, except, desc) => {
8
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
9
+ key = keys[i];
10
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
11
+ get: ((k) => from[k]).bind(null, key),
12
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
13
+ });
14
+ }
15
+ return to;
16
+ };
17
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
18
+ value: mod,
19
+ enumerable: true
20
+ }) : target, mod));
21
+ exports.__toESM = __toESM;