@oscarpalmer/timer 0.14.0 → 0.15.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 +9 -3
- package/dist/timer.js +9 -3
- package/package.json +12 -8
- package/src/index.js +27 -15
package/dist/timer.cjs
CHANGED
|
@@ -96,13 +96,19 @@ var Timed = class {
|
|
|
96
96
|
throw new TypeError(`A ${type} timer must have a callback function`);
|
|
97
97
|
}
|
|
98
98
|
if (typeof time !== "number" || time < 0) {
|
|
99
|
-
throw new TypeError(
|
|
99
|
+
throw new TypeError(
|
|
100
|
+
`A ${type} timer must have a non-negative number as its time`
|
|
101
|
+
);
|
|
100
102
|
}
|
|
101
103
|
if (isRepeated && (typeof count !== "number" || count < 2)) {
|
|
102
|
-
throw new TypeError(
|
|
104
|
+
throw new TypeError(
|
|
105
|
+
"A repeated timer must have a number above 1 as its repeat count"
|
|
106
|
+
);
|
|
103
107
|
}
|
|
104
108
|
if (isRepeated && afterCallback !== void 0 && typeof afterCallback !== "function") {
|
|
105
|
-
throw new TypeError(
|
|
109
|
+
throw new TypeError(
|
|
110
|
+
"A repeated timer's after-callback must be a function"
|
|
111
|
+
);
|
|
106
112
|
}
|
|
107
113
|
this.configuration = { count, time };
|
|
108
114
|
this.callbacks = {
|
package/dist/timer.js
CHANGED
|
@@ -72,13 +72,19 @@ var Timed = class {
|
|
|
72
72
|
throw new TypeError(`A ${type} timer must have a callback function`);
|
|
73
73
|
}
|
|
74
74
|
if (typeof time !== "number" || time < 0) {
|
|
75
|
-
throw new TypeError(
|
|
75
|
+
throw new TypeError(
|
|
76
|
+
`A ${type} timer must have a non-negative number as its time`
|
|
77
|
+
);
|
|
76
78
|
}
|
|
77
79
|
if (isRepeated && (typeof count !== "number" || count < 2)) {
|
|
78
|
-
throw new TypeError(
|
|
80
|
+
throw new TypeError(
|
|
81
|
+
"A repeated timer must have a number above 1 as its repeat count"
|
|
82
|
+
);
|
|
79
83
|
}
|
|
80
84
|
if (isRepeated && afterCallback !== void 0 && typeof afterCallback !== "function") {
|
|
81
|
-
throw new TypeError(
|
|
85
|
+
throw new TypeError(
|
|
86
|
+
"A repeated timer's after-callback must be a function"
|
|
87
|
+
);
|
|
82
88
|
}
|
|
83
89
|
this.configuration = { count, time };
|
|
84
90
|
this.callbacks = {
|
package/package.json
CHANGED
|
@@ -7,20 +7,23 @@
|
|
|
7
7
|
"description": "A better solution for timeout- and interval-based timers.",
|
|
8
8
|
"devDependencies": {
|
|
9
9
|
"esbuild": "^0.17",
|
|
10
|
-
"typescript": "^5.0",
|
|
11
10
|
"xo": "^0.54"
|
|
12
11
|
},
|
|
13
12
|
"exports": {
|
|
14
13
|
".": {
|
|
15
14
|
"types": "./src/index.d.ts",
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
"browser": "./dist/timer.iife.js",
|
|
16
|
+
"module": "./dist/timer.js",
|
|
17
|
+
"import": "./dist/timer.js",
|
|
18
|
+
"require": "./dist/timer.cjs",
|
|
19
|
+
"default": "./dist/timer.js"
|
|
20
20
|
},
|
|
21
21
|
"./package.json": "./package.json"
|
|
22
22
|
},
|
|
23
|
-
"files": [
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"src"
|
|
26
|
+
],
|
|
24
27
|
"jsdelivr": "dist/timer.iife.js",
|
|
25
28
|
"keywords": [
|
|
26
29
|
"timer",
|
|
@@ -41,10 +44,11 @@
|
|
|
41
44
|
"build:cjs": "esbuild ./src/index.js --bundle --format=cjs --outfile=./dist/timer.cjs --target=es2020",
|
|
42
45
|
"build:esm": "esbuild ./src/index.js --bundle --format=esm --outfile=./dist/timer.js --target=es2020",
|
|
43
46
|
"build:iife": "esbuild ./src/index.js --bundle --minify --format=iife --global-name=Timer --outfile=./dist/timer.iife.js --target=es2020",
|
|
44
|
-
"types": "./node_modules/.bin/tsc ./src/index.js --allowJs --declaration --emitDeclarationOnly --removeComments"
|
|
47
|
+
"types": "./node_modules/.bin/tsc ./src/index.js --allowJs --declaration --emitDeclarationOnly --removeComments",
|
|
48
|
+
"xo": "./node_modules/.bin/xo ./src/*.js --env browser"
|
|
45
49
|
},
|
|
46
50
|
"type": "module",
|
|
47
51
|
"types": "src/index.d.ts",
|
|
48
52
|
"unpkg": "dist/timer.iife.js",
|
|
49
|
-
"version": "0.
|
|
53
|
+
"version": "0.15.0"
|
|
50
54
|
}
|
package/src/index.js
CHANGED
|
@@ -12,11 +12,12 @@
|
|
|
12
12
|
|
|
13
13
|
const milliseconds = Math.round(1000 / 60);
|
|
14
14
|
|
|
15
|
-
const request = globalThis.requestAnimationFrame
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
};
|
|
15
|
+
const request = globalThis.requestAnimationFrame
|
|
16
|
+
?? function (callback) {
|
|
17
|
+
return setTimeout?.(() => {
|
|
18
|
+
callback(Date.now());
|
|
19
|
+
}, milliseconds);
|
|
20
|
+
};
|
|
20
21
|
|
|
21
22
|
/**
|
|
22
23
|
* @param {Timed} timed
|
|
@@ -43,7 +44,10 @@ function run(timed) {
|
|
|
43
44
|
const elapsedMinimum = elapsed - milliseconds;
|
|
44
45
|
const elapsedMaximum = elapsed + milliseconds;
|
|
45
46
|
|
|
46
|
-
if (
|
|
47
|
+
if (
|
|
48
|
+
elapsedMinimum < timed.configuration.time
|
|
49
|
+
&& timed.configuration.time < elapsedMaximum
|
|
50
|
+
) {
|
|
47
51
|
if (timed.state.active) {
|
|
48
52
|
timed.callbacks.default(isRepeated ? index : undefined);
|
|
49
53
|
}
|
|
@@ -105,24 +109,32 @@ class Timed {
|
|
|
105
109
|
constructor(callback, time, count, afterCallback) {
|
|
106
110
|
const isRepeated = this instanceof Repeated;
|
|
107
111
|
|
|
108
|
-
const type = isRepeated
|
|
109
|
-
? 'repeated'
|
|
110
|
-
: 'waited';
|
|
112
|
+
const type = isRepeated ? 'repeated' : 'waited';
|
|
111
113
|
|
|
112
114
|
if (typeof callback !== 'function') {
|
|
113
115
|
throw new TypeError(`A ${type} timer must have a callback function`);
|
|
114
116
|
}
|
|
115
117
|
|
|
116
118
|
if (typeof time !== 'number' || time < 0) {
|
|
117
|
-
throw new TypeError(
|
|
119
|
+
throw new TypeError(
|
|
120
|
+
`A ${type} timer must have a non-negative number as its time`,
|
|
121
|
+
);
|
|
118
122
|
}
|
|
119
123
|
|
|
120
124
|
if (isRepeated && (typeof count !== 'number' || count < 2)) {
|
|
121
|
-
throw new TypeError(
|
|
125
|
+
throw new TypeError(
|
|
126
|
+
'A repeated timer must have a number above 1 as its repeat count',
|
|
127
|
+
);
|
|
122
128
|
}
|
|
123
129
|
|
|
124
|
-
if (
|
|
125
|
-
|
|
130
|
+
if (
|
|
131
|
+
isRepeated
|
|
132
|
+
&& afterCallback !== undefined
|
|
133
|
+
&& typeof afterCallback !== 'function'
|
|
134
|
+
) {
|
|
135
|
+
throw new TypeError(
|
|
136
|
+
'A repeated timer\'s after-callback must be a function',
|
|
137
|
+
);
|
|
126
138
|
}
|
|
127
139
|
|
|
128
140
|
this.configuration = {count, time};
|
|
@@ -192,7 +204,7 @@ export class Waited extends Timed {
|
|
|
192
204
|
* @return {Repeated} A repeated timer
|
|
193
205
|
*/
|
|
194
206
|
export function repeat(callback, time, count, afterCallback) {
|
|
195
|
-
return
|
|
207
|
+
return new Repeated(callback, time, count, afterCallback).start();
|
|
196
208
|
}
|
|
197
209
|
|
|
198
210
|
/**
|
|
@@ -201,5 +213,5 @@ export function repeat(callback, time, count, afterCallback) {
|
|
|
201
213
|
* @return {Waited} A waited timer
|
|
202
214
|
*/
|
|
203
215
|
export function wait(callback, time) {
|
|
204
|
-
return
|
|
216
|
+
return new Waited(callback, time).start();
|
|
205
217
|
}
|