@oscarpalmer/timer 0.37.0 → 0.38.1

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/README.md CHANGED
@@ -36,24 +36,24 @@ const repeated = new Timer(repeatedCallback, 10);
36
36
 
37
37
  When creating a _Timer_, either with the new `new`-keyword or using the functions, you can configure the timer with a few parameters:
38
38
 
39
- |Parameter|Description|
40
- |--------:|:----------|
41
- |`callback`|Callback function to be invoked for each run that are __required__ for all timers.<br>For more information on callbacks, please read [the callbacks section](#callbacks).|
42
- |`count`|How many times the timer should run.<br>If no value is provided, it will default to `1` when using the `new`-keyword and the `wait`-method, but throws an error for the `repeat`-method.|
43
- |`time`|How many milliseconds between each invokations of the provided callback.<br>Defaults to `0`, which is not really _0_ milliseconds, but close enough :wink:|
44
- |`after`|A callback to run after the timer finishes, both when cancelled and completed.<br>If _count_ is greater than `1` and _after_ __is not__ `undefined`, a function is expected.|
39
+ | Parameter | Description |
40
+ | ---------: | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
41
+ | `callback` | Callback function to be invoked for each run that are **required** for all timers.<br>For more information on callbacks, please read [the callbacks section](#callbacks). |
42
+ | `count` | How many times the timer should run.<br>If no value is provided, it will default to `1` when using the `new`-keyword and the `wait`-method, but throws an error for the `repeat`-method. |
43
+ | `time` | How many milliseconds between each invokations of the provided callback.<br>Defaults to `0`, which is not really _0_ milliseconds, but close enough :wink: |
44
+ | `after` | A callback to run after the timer finishes, both when cancelled and completed.<br>If _count_ is greater than `1` and _after_ **is not** `undefined`, a function is expected. |
45
45
 
46
46
  ## Methods and properties
47
47
 
48
48
  An instance of _Timer_ also has a few helpful methods and properties:
49
49
 
50
- |Name|Type|Description|
51
- |---:|----|:----------|
52
- |`active`|_Property_|A `boolean` value to check if the timer is running|
53
- |`finished`|_Property_|A `boolean` value to check if the timer was able to finish|
54
- |`start()`|_Method_|Starts the timer.<br>Necessary when creating a timer using the class syntax _(e.g. `new Waited...`)_, but helpful when the timer needs to be started at other times, as well.|
55
- |`stop()`|_Method_|Stops the timer|
56
- |`restart()`|_Method_|Restarts the timer|
50
+ | Name | Type | Description |
51
+ | ----------: | ---------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
52
+ | `active` | _Property_ | A `boolean` value to check if the timer is running |
53
+ | `finished` | _Property_ | A `boolean` value to check if the timer was able to finish |
54
+ | `start()` | _Method_ | Starts the timer.<br>Necessary when creating a timer using the class syntax _(e.g. `new Waited...`)_, but helpful when the timer needs to be started at other times, as well. |
55
+ | `stop()` | _Method_ | Stops the timer |
56
+ | `restart()` | _Method_ | Restarts the timer |
57
57
 
58
58
  ## Callbacks
59
59
 
package/dist/constants.js CHANGED
@@ -5,20 +5,18 @@ function calculate() {
5
5
  function step(now) {
6
6
  if (last != null) values.push(now - last);
7
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);
8
+ if (values.length >= 10) resolve(values.sort().slice(2, -2).reduce((first, second) => first + second, 0) / (values.length - 4));
9
+ else requestAnimationFrame(step);
12
10
  }
13
11
  requestAnimationFrame(step);
14
12
  });
15
13
  }
16
- const defaultTimeout = 3e4;
17
- const activeTimers = /* @__PURE__ */ new Set();
18
- const intervalBuffer = 5;
19
- const destroyedMessage = "Timer has already been destroyed";
20
- const hiddenTimers = /* @__PURE__ */ new Set();
21
- const startedMessage = "Timer has already been started";
14
+ const BUFFER_INTERVAL = 5;
15
+ const DEFAULT_TIMEOUT = 3e4;
16
+ const MESSAGE_DESTROYED = "Timer has already been destroyed";
17
+ const MESSAGE_STARTED = "Timer has already been started";
18
+ const TIMERS_ACTIVE = /* @__PURE__ */ new Set();
19
+ const TIMERS_HIDDEN = /* @__PURE__ */ new Set();
22
20
  const TYPE_REPEAT = "repeat";
23
21
  const TYPE_WAIT = "wait";
24
22
  const TYPE_WHEN = "when";
@@ -27,8 +25,8 @@ const WORK_PAUSE = "pause";
27
25
  const WORK_RESTART = "restart";
28
26
  const WORK_START = "start";
29
27
  const WORK_STOP = "stop";
30
- let milliseconds = 1e3 / 60;
28
+ let MILLISECONDS = 1e3 / 60;
31
29
  calculate().then((value) => {
32
- milliseconds = value;
30
+ MILLISECONDS = value;
33
31
  });
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 };
32
+ export { BUFFER_INTERVAL, DEFAULT_TIMEOUT, MESSAGE_DESTROYED, MESSAGE_STARTED, MILLISECONDS, TIMERS_ACTIVE, TIMERS_HIDDEN, TYPE_REPEAT, TYPE_WAIT, TYPE_WHEN, WORK_CONTINUE, WORK_PAUSE, WORK_RESTART, WORK_START, WORK_STOP };
package/dist/delay.js CHANGED
@@ -1,12 +1,12 @@
1
- import { intervalBuffer, milliseconds } from "./constants.js";
1
+ import { BUFFER_INTERVAL, MILLISECONDS } from "./constants.js";
2
2
  import { getValidNumber } from "./get.js";
3
3
  function delay(time) {
4
4
  return new Promise((resolve) => {
5
- const interval = getValidNumber(time, milliseconds);
5
+ const interval = getValidNumber(time, MILLISECONDS);
6
6
  let start;
7
7
  function step(now) {
8
8
  start ??= now;
9
- if (interval === milliseconds || now - start >= interval - 5) resolve();
9
+ if (interval === MILLISECONDS || now - start >= interval - 5) resolve();
10
10
  else requestAnimationFrame(step);
11
11
  }
12
12
  requestAnimationFrame(step);
package/dist/get.js CHANGED
@@ -1,10 +1,10 @@
1
- import { defaultTimeout } from "./constants.js";
1
+ import { DEFAULT_TIMEOUT } from "./constants.js";
2
2
  import { noop } from "@oscarpalmer/atoms/function";
3
3
  function getCallback(value) {
4
4
  return typeof value === "function" ? value : noop;
5
5
  }
6
6
  function getValidTimeout(value) {
7
- return typeof value === "number" && value > 0 ? value : defaultTimeout;
7
+ return typeof value === "number" && value > 0 ? value : DEFAULT_TIMEOUT;
8
8
  }
9
9
  function getValidNumber(value, defaultValue) {
10
10
  const actualDefault = defaultValue ?? 0;
package/dist/global.js CHANGED
@@ -1,12 +1,12 @@
1
- import { WORK_CONTINUE, WORK_PAUSE, activeTimers, hiddenTimers } from "./constants.js";
1
+ import { TIMERS_ACTIVE, TIMERS_HIDDEN, WORK_CONTINUE, WORK_PAUSE } from "./constants.js";
2
2
  if (globalThis._oscarpalmer_timers == null) Object.defineProperty(globalThis, "_oscarpalmer_timers", { get() {
3
- return globalThis._oscarpalmer_timer_debug ? [...activeTimers] : [];
3
+ return globalThis._oscarpalmer_timer_debug ? [...TIMERS_ACTIVE] : [];
4
4
  } });
5
5
  /* istanbul ignore next */
6
6
  document.addEventListener("visibilitychange", () => {
7
- const from = document.hidden ? activeTimers : hiddenTimers;
7
+ const from = document.hidden ? TIMERS_ACTIVE : TIMERS_HIDDEN;
8
8
  const method = document.hidden ? WORK_PAUSE : WORK_CONTINUE;
9
- const to = document.hidden ? hiddenTimers : activeTimers;
9
+ const to = document.hidden ? TIMERS_HIDDEN : TIMERS_ACTIVE;
10
10
  for (const timer of from) {
11
11
  timer[method]();
12
12
  to.add(timer);
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
+ import { delay } from "./delay.js";
1
2
  import "./global.js";
2
- import { when } from "./when.js";
3
- import { wait } from "./wait.js";
4
- import { repeat } from "./repeat.js";
5
3
  import { isRepeated, isTimer, isWaited, isWhen } from "./is.js";
6
- import { delay } from "./delay.js";
4
+ import { repeat } from "./repeat.js";
5
+ import { wait } from "./wait.js";
6
+ import { when } from "./when.js";
7
7
  export { delay, isRepeated, isTimer, isWaited, isWhen, repeat, wait, when };
package/dist/repeat.js CHANGED
@@ -1,4 +1,4 @@
1
- import { TYPE_REPEAT, milliseconds } from "./constants.js";
1
+ import { MILLISECONDS, TYPE_REPEAT } from "./constants.js";
2
2
  import { getCallback, getValidNumber } from "./get.js";
3
3
  import "./global.js";
4
4
  import { TimerTrace } from "./models.js";
@@ -11,7 +11,7 @@ function repeat(callback, options) {
11
11
  onAfter: getCallback(options?.onAfter),
12
12
  onError: getCallback(options?.onTimeout),
13
13
  count: getValidNumber(options?.count),
14
- interval: getValidNumber(options?.interval, milliseconds),
14
+ interval: getValidNumber(options?.interval, MILLISECONDS),
15
15
  timeout: getValidNumber(options?.timeout)
16
16
  }, true);
17
17
  }