@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 +7 -7
- package/dist/timer.js +7 -7
- package/package.json +1 -1
- package/src/index.d.ts +6 -4
- package/src/index.js +21 -9
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
|
|
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 ?
|
|
55
|
+
timed.callbacks.default(isRepeated ? index : void 0);
|
|
56
56
|
}
|
|
57
|
-
|
|
58
|
-
if (isRepeated &&
|
|
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 {
|
|
72
|
+
* @param {RepeatedCallback} callback
|
|
73
73
|
* @param {number} time
|
|
74
74
|
* @param {number} count
|
|
75
|
-
* @param {
|
|
75
|
+
* @param {AfterCallback|undefined} afterCallback
|
|
76
76
|
*/
|
|
77
77
|
constructor(callback, time, count, afterCallback) {
|
|
78
78
|
/**
|
|
79
79
|
* @readonly
|
|
80
|
-
* @type {{after
|
|
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
|
|
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 ?
|
|
31
|
+
timed.callbacks.default(isRepeated ? index : void 0);
|
|
32
32
|
}
|
|
33
|
-
|
|
34
|
-
if (isRepeated &&
|
|
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 {
|
|
48
|
+
* @param {RepeatedCallback} callback
|
|
49
49
|
* @param {number} time
|
|
50
50
|
* @param {number} count
|
|
51
|
-
* @param {
|
|
51
|
+
* @param {AfterCallback|undefined} afterCallback
|
|
52
52
|
*/
|
|
53
53
|
constructor(callback, time, count, afterCallback) {
|
|
54
54
|
/**
|
|
55
55
|
* @readonly
|
|
56
|
-
* @type {{after
|
|
56
|
+
* @type {{after: AfterCallback | undefined; default: RepeatedCallback}}
|
|
57
57
|
*/
|
|
58
58
|
__publicField(this, "callbacks");
|
|
59
59
|
/**
|
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
|
-
export function repeat(callback:
|
|
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:
|
|
11
|
+
constructor(callback: RepeatedCallback, time: number, count: number, afterCallback: AfterCallback | undefined);
|
|
10
12
|
readonly callbacks: {
|
|
11
|
-
after
|
|
12
|
-
default:
|
|
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
|
|
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 ?
|
|
48
|
+
timed.callbacks.default(isRepeated ? index : undefined);
|
|
37
49
|
}
|
|
38
50
|
|
|
39
|
-
|
|
51
|
+
index += 1;
|
|
40
52
|
|
|
41
|
-
if (isRepeated &&
|
|
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
|
|
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 {
|
|
100
|
+
* @param {RepeatedCallback} callback
|
|
89
101
|
* @param {number} time
|
|
90
102
|
* @param {number} count
|
|
91
|
-
* @param {
|
|
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 {
|
|
188
|
+
* @param {RepeatedCallback} callback
|
|
177
189
|
* @param {number} time
|
|
178
190
|
* @param {number} count
|
|
179
|
-
* @param {
|
|
191
|
+
* @param {AfterCallback|undefined} afterCallback
|
|
180
192
|
* @return {Repeated} A repeated timer
|
|
181
193
|
*/
|
|
182
194
|
export function repeat(callback, time, count, afterCallback) {
|