@oscarpalmer/timer 0.30.0 → 0.32.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.
@@ -2,25 +2,28 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- function stepper(now) {
6
- if (values.length === 7) {
7
- exports.milliseconds = Math.floor(
8
- values.slice(2).reduce((first, second) => first + second) / 5 * 2
9
- ) / 2;
10
- last = void 0;
11
- values.length = 0;
12
- } else {
13
- last ??= now;
14
- const difference = now - last;
15
- if (difference > 0) {
16
- values.push(difference);
5
+ 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
+ }
17
20
  }
18
- last = now;
19
- requestAnimationFrame(stepper);
20
- }
21
+ requestAnimationFrame(step);
22
+ });
21
23
  }
22
24
  const activeTimers = /* @__PURE__ */ new Set();
23
25
  const beginTypes = /* @__PURE__ */ new Set(["continue", "start"]);
26
+ const intervalBuffer = 5;
24
27
  const destroyedMessage = "Timer has already been destroyed";
25
28
  const endTypes = /* @__PURE__ */ new Set(["pause", "stop"]);
26
29
  const endOrRestartTypes = /* @__PURE__ */ new Set([
@@ -31,10 +34,10 @@ const endOrRestartTypes = /* @__PURE__ */ new Set([
31
34
  const hiddenTimers = /* @__PURE__ */ new Set();
32
35
  const pauseTypes = /* @__PURE__ */ new Set(["continue", "pause"]);
33
36
  const startedMessage = "Timer has already been started";
34
- const values = [];
35
- let last;
36
- exports.milliseconds = Math.floor(1e3 / 60 * 2) / 2;
37
- requestAnimationFrame(stepper);
37
+ exports.milliseconds = 1e3 / 60;
38
+ calculate().then((value) => {
39
+ exports.milliseconds = value;
40
+ });
38
41
 
39
42
  exports.activeTimers = activeTimers;
40
43
  exports.beginTypes = beginTypes;
@@ -42,5 +45,6 @@ exports.destroyedMessage = destroyedMessage;
42
45
  exports.endOrRestartTypes = endOrRestartTypes;
43
46
  exports.endTypes = endTypes;
44
47
  exports.hiddenTimers = hiddenTimers;
48
+ exports.intervalBuffer = intervalBuffer;
45
49
  exports.pauseTypes = pauseTypes;
46
50
  exports.startedMessage = startedMessage;
package/dist/constants.js CHANGED
@@ -1,22 +1,25 @@
1
- function stepper(now) {
2
- if (values.length === 7) {
3
- milliseconds = Math.floor(
4
- values.slice(2).reduce((first, second) => first + second) / 5 * 2
5
- ) / 2;
6
- last = void 0;
7
- values.length = 0;
8
- } else {
9
- last ??= now;
10
- const difference = now - last;
11
- if (difference > 0) {
12
- values.push(difference);
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
+ }
13
16
  }
14
- last = now;
15
- requestAnimationFrame(stepper);
16
- }
17
+ requestAnimationFrame(step);
18
+ });
17
19
  }
18
20
  const activeTimers = /* @__PURE__ */ new Set();
19
21
  const beginTypes = /* @__PURE__ */ new Set(["continue", "start"]);
22
+ const intervalBuffer = 5;
20
23
  const destroyedMessage = "Timer has already been destroyed";
21
24
  const endTypes = /* @__PURE__ */ new Set(["pause", "stop"]);
22
25
  const endOrRestartTypes = /* @__PURE__ */ new Set([
@@ -27,9 +30,9 @@ const endOrRestartTypes = /* @__PURE__ */ new Set([
27
30
  const hiddenTimers = /* @__PURE__ */ new Set();
28
31
  const pauseTypes = /* @__PURE__ */ new Set(["continue", "pause"]);
29
32
  const startedMessage = "Timer has already been started";
30
- const values = [];
31
- let last;
32
- let milliseconds = Math.floor(1e3 / 60 * 2) / 2;
33
- requestAnimationFrame(stepper);
33
+ let milliseconds = 1e3 / 60;
34
+ calculate().then((value) => {
35
+ milliseconds = value;
36
+ });
34
37
 
35
- export { activeTimers, beginTypes, destroyedMessage, endOrRestartTypes, endTypes, hiddenTimers, milliseconds, pauseTypes, startedMessage };
38
+ export { activeTimers, beginTypes, destroyedMessage, endOrRestartTypes, endTypes, hiddenTimers, intervalBuffer, milliseconds, pauseTypes, startedMessage };
package/dist/delay.cjs CHANGED
@@ -11,7 +11,7 @@ function delay(time) {
11
11
  let start;
12
12
  function step(now) {
13
13
  start ??= now;
14
- if (interval === constants.milliseconds || now - start >= interval - 5) {
14
+ if (interval === constants.milliseconds || now - start >= interval - constants.intervalBuffer) {
15
15
  resolve();
16
16
  } else {
17
17
  requestAnimationFrame(step);
package/dist/delay.js CHANGED
@@ -1,4 +1,4 @@
1
- import { milliseconds } from './constants.js';
1
+ import { milliseconds, intervalBuffer } from './constants.js';
2
2
  import { getValidNumber } from './get.js';
3
3
 
4
4
  function delay(time) {
@@ -7,7 +7,7 @@ function delay(time) {
7
7
  let start;
8
8
  function step(now) {
9
9
  start ??= now;
10
- if (interval === milliseconds || now - start >= interval - 5) {
10
+ if (interval === milliseconds || now - start >= interval - intervalBuffer) {
11
11
  resolve();
12
12
  } else {
13
13
  requestAnimationFrame(step);
package/dist/get.cjs CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- const _function = require('./node_modules/@oscarpalmer/atoms/dist/function.cjs');
5
+ const _function = require('@oscarpalmer/atoms/function');
6
6
 
7
7
  function getCallback(value) {
8
8
  return typeof value === "function" ? value : _function.noop;
package/dist/get.js CHANGED
@@ -1,4 +1,4 @@
1
- import { noop } from './node_modules/@oscarpalmer/atoms/dist/function.js';
1
+ import { noop } from '@oscarpalmer/atoms/function';
2
2
 
3
3
  function getCallback(value) {
4
4
  return typeof value === "function" ? value : noop;
package/dist/repeat.cjs CHANGED
@@ -4,14 +4,13 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
5
  const constants = require('./constants.cjs');
6
6
  const get = require('./get.cjs');
7
+ require('./global.cjs');
7
8
  const models = require('./models.cjs');
8
9
  const timer = require('./timer.cjs');
9
- const work = require('./work.cjs');
10
10
 
11
11
  function repeat(callback, options) {
12
12
  return new timer.Timer(
13
13
  "repeat",
14
- work.work,
15
14
  {
16
15
  callback: get.getCallback(callback),
17
16
  trace: new models.TimerTrace().stack
package/dist/repeat.js CHANGED
@@ -1,13 +1,12 @@
1
1
  import { milliseconds } from './constants.js';
2
2
  import { getCallback, getValidNumber } from './get.js';
3
+ import './global.js';
3
4
  import { TimerTrace } from './models.js';
4
5
  import { Timer } from './timer.js';
5
- import { work } from './work.js';
6
6
 
7
7
  function repeat(callback, options) {
8
8
  return new Timer(
9
9
  "repeat",
10
- work,
11
10
  {
12
11
  callback: getCallback(callback),
13
12
  trace: new TimerTrace().stack
package/dist/timer.cjs CHANGED
@@ -2,9 +2,11 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
+ const _function = require('@oscarpalmer/atoms/function');
6
+ const work = require('./work.cjs');
7
+
5
8
  class Timer {
6
- constructor(type, worker, state, options, start) {
7
- this.worker = worker;
9
+ constructor(type, state, options, start) {
8
10
  this.options = options;
9
11
  this.$timer = type;
10
12
  this.state = {
@@ -57,7 +59,20 @@ class Timer {
57
59
  */
58
60
  destroy() {
59
61
  this.state.destroyed = true;
60
- this.#work("stop");
62
+ this.options.onAfter = _function.noop;
63
+ this.options.onError = _function.noop;
64
+ this.state.callback = _function.noop;
65
+ if (!globalThis._oscarpalmer_timer_debug) {
66
+ this.state.trace = void 0;
67
+ }
68
+ work.stop(
69
+ {
70
+ instance: this,
71
+ type: this.$timer
72
+ },
73
+ this.state,
74
+ this.options
75
+ );
61
76
  }
62
77
  /**
63
78
  * Pause the timer _(if it's running)_
@@ -84,7 +99,7 @@ class Timer {
84
99
  return this.#work("stop");
85
100
  }
86
101
  #work(type) {
87
- return this.worker(
102
+ return work.work(
88
103
  type,
89
104
  {
90
105
  instance: this,