@oscarpalmer/timer 0.13.0 → 0.14.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/timer.cjs CHANGED
@@ -40,7 +40,7 @@ function run(timed) {
40
40
  timed.state.active = true;
41
41
  timed.state.finished = false;
42
42
  const isRepeated = timed instanceof Repeated;
43
- let count = 0;
43
+ let index = 0;
44
44
  let start;
45
45
  function step(timestamp) {
46
46
  if (!timed.state.active) {
@@ -52,10 +52,10 @@ function run(timed) {
52
52
  const elapsedMaximum = elapsed + milliseconds;
53
53
  if (elapsedMinimum < timed.configuration.time && timed.configuration.time < elapsedMaximum) {
54
54
  if (timed.state.active) {
55
- timed.callbacks.default(isRepeated ? count : void 0);
55
+ timed.callbacks.default(isRepeated ? index : void 0);
56
56
  }
57
- count += 1;
58
- if (isRepeated && count < timed.configuration.count) {
57
+ index += 1;
58
+ if (isRepeated && index < timed.configuration.count) {
59
59
  start = void 0;
60
60
  } else {
61
61
  timed.state.finished = true;
@@ -69,15 +69,15 @@ function run(timed) {
69
69
  }
70
70
  var Timed = class {
71
71
  /**
72
- * @param {Function} callback
72
+ * @param {RepeatedCallback} callback
73
73
  * @param {number} time
74
74
  * @param {number} count
75
- * @param {Function?} afterCallback
75
+ * @param {AfterCallback|undefined} afterCallback
76
76
  */
77
77
  constructor(callback, time, count, afterCallback) {
78
78
  /**
79
79
  * @readonly
80
- * @type {{after?: Function; default: Function}}
80
+ * @type {{after: AfterCallback | undefined; default: RepeatedCallback}}
81
81
  */
82
82
  __publicField(this, "callbacks");
83
83
  /**
package/dist/timer.js CHANGED
@@ -16,7 +16,7 @@ function run(timed) {
16
16
  timed.state.active = true;
17
17
  timed.state.finished = false;
18
18
  const isRepeated = timed instanceof Repeated;
19
- let count = 0;
19
+ let index = 0;
20
20
  let start;
21
21
  function step(timestamp) {
22
22
  if (!timed.state.active) {
@@ -28,10 +28,10 @@ function run(timed) {
28
28
  const elapsedMaximum = elapsed + milliseconds;
29
29
  if (elapsedMinimum < timed.configuration.time && timed.configuration.time < elapsedMaximum) {
30
30
  if (timed.state.active) {
31
- timed.callbacks.default(isRepeated ? count : void 0);
31
+ timed.callbacks.default(isRepeated ? index : void 0);
32
32
  }
33
- count += 1;
34
- if (isRepeated && count < timed.configuration.count) {
33
+ index += 1;
34
+ if (isRepeated && index < timed.configuration.count) {
35
35
  start = void 0;
36
36
  } else {
37
37
  timed.state.finished = true;
@@ -45,15 +45,15 @@ function run(timed) {
45
45
  }
46
46
  var Timed = class {
47
47
  /**
48
- * @param {Function} callback
48
+ * @param {RepeatedCallback} callback
49
49
  * @param {number} time
50
50
  * @param {number} count
51
- * @param {Function?} afterCallback
51
+ * @param {AfterCallback|undefined} afterCallback
52
52
  */
53
53
  constructor(callback, time, count, afterCallback) {
54
54
  /**
55
55
  * @readonly
56
- * @type {{after?: Function; default: Function}}
56
+ * @type {{after: AfterCallback | undefined; default: RepeatedCallback}}
57
57
  */
58
58
  __publicField(this, "callbacks");
59
59
  /**
package/package.json CHANGED
@@ -46,5 +46,5 @@
46
46
  "type": "module",
47
47
  "types": "src/index.d.ts",
48
48
  "unpkg": "dist/timer.iife.js",
49
- "version": "0.13.0"
49
+ "version": "0.14.0"
50
50
  }
package/src/index.d.ts CHANGED
@@ -1,15 +1,17 @@
1
- export function repeat(callback: Function, time: number, count: number, afterCallback: Function | null): Repeated;
1
+ export function repeat(callback: RepeatedCallback, time: number, count: number, afterCallback: AfterCallback | undefined): Repeated;
2
2
  export function wait(callback: Function, time: number): Waited;
3
3
  export class Repeated extends Timed {
4
4
  }
5
5
  export class Waited extends Timed {
6
6
  constructor(callback: Function, time: number);
7
7
  }
8
+ export type AfterCallback = (finished: boolean) => void;
9
+ export type RepeatedCallback = (index: number) => void;
8
10
  declare class Timed {
9
- constructor(callback: Function, time: number, count: number, afterCallback: Function | null);
11
+ constructor(callback: RepeatedCallback, time: number, count: number, afterCallback: AfterCallback | undefined);
10
12
  readonly callbacks: {
11
- after?: Function;
12
- default: Function;
13
+ after: AfterCallback | undefined;
14
+ default: RepeatedCallback;
13
15
  };
14
16
  readonly configuration: {
15
17
  count: number;
package/src/index.js CHANGED
@@ -1,3 +1,15 @@
1
+ /**
2
+ * @callback AfterCallback
3
+ * @param {boolean} finished
4
+ * @returns {void}
5
+ */
6
+
7
+ /**
8
+ * @callback RepeatedCallback
9
+ * @param {number} index
10
+ * @returns {void}
11
+ */
12
+
1
13
  const milliseconds = Math.round(1000 / 60);
2
14
 
3
15
  const request = globalThis.requestAnimationFrame ?? function (callback) {
@@ -15,7 +27,7 @@ function run(timed) {
15
27
 
16
28
  const isRepeated = timed instanceof Repeated;
17
29
 
18
- let count = 0;
30
+ let index = 0;
19
31
 
20
32
  let start;
21
33
 
@@ -33,12 +45,12 @@ function run(timed) {
33
45
 
34
46
  if (elapsedMinimum < timed.configuration.time && timed.configuration.time < elapsedMaximum) {
35
47
  if (timed.state.active) {
36
- timed.callbacks.default(isRepeated ? count : undefined);
48
+ timed.callbacks.default(isRepeated ? index : undefined);
37
49
  }
38
50
 
39
- count += 1;
51
+ index += 1;
40
52
 
41
- if (isRepeated && count < timed.configuration.count) {
53
+ if (isRepeated && index < timed.configuration.count) {
42
54
  start = undefined;
43
55
  } else {
44
56
  timed.state.finished = true;
@@ -58,7 +70,7 @@ function run(timed) {
58
70
  class Timed {
59
71
  /**
60
72
  * @readonly
61
- * @type {{after?: Function; default: Function}}
73
+ * @type {{after: AfterCallback | undefined; default: RepeatedCallback}}
62
74
  */
63
75
  callbacks;
64
76
 
@@ -85,10 +97,10 @@ class Timed {
85
97
  }
86
98
 
87
99
  /**
88
- * @param {Function} callback
100
+ * @param {RepeatedCallback} callback
89
101
  * @param {number} time
90
102
  * @param {number} count
91
- * @param {Function?} afterCallback
103
+ * @param {AfterCallback|undefined} afterCallback
92
104
  */
93
105
  constructor(callback, time, count, afterCallback) {
94
106
  const isRepeated = this instanceof Repeated;
@@ -173,10 +185,10 @@ export class Waited extends Timed {
173
185
  }
174
186
 
175
187
  /**
176
- * @param {Function} callback
188
+ * @param {RepeatedCallback} callback
177
189
  * @param {number} time
178
190
  * @param {number} count
179
- * @param {Function?} afterCallback
191
+ * @param {AfterCallback|undefined} afterCallback
180
192
  * @return {Repeated} A repeated timer
181
193
  */
182
194
  export function repeat(callback, time, count, afterCallback) {