@oscarpalmer/timer 0.16.0 → 0.17.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.iife.js +150 -1
- package/dist/timer.js +2 -2
- package/package.json +5 -17
- package/src/index.js +10 -9
- package/dist/timer.cjs +0 -148
package/dist/timer.iife.js
CHANGED
|
@@ -1 +1,150 @@
|
|
|
1
|
-
var Timer
|
|
1
|
+
var Timer = (() => {
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.js
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
Repeated: () => Repeated,
|
|
24
|
+
Waited: () => Waited,
|
|
25
|
+
repeat: () => repeat,
|
|
26
|
+
wait: () => wait
|
|
27
|
+
});
|
|
28
|
+
var milliseconds = Math.round(1e3 / 60);
|
|
29
|
+
var request = requestAnimationFrame ?? function(callback) {
|
|
30
|
+
return setTimeout?.(() => {
|
|
31
|
+
callback(Date.now());
|
|
32
|
+
}, milliseconds);
|
|
33
|
+
};
|
|
34
|
+
function run(timed) {
|
|
35
|
+
timed.state.active = true;
|
|
36
|
+
timed.state.finished = false;
|
|
37
|
+
const isRepeated = timed instanceof Repeated;
|
|
38
|
+
let index = 0;
|
|
39
|
+
let start;
|
|
40
|
+
function step(timestamp) {
|
|
41
|
+
if (!timed.state.active) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
start ?? (start = timestamp);
|
|
45
|
+
const elapsed = timestamp - start;
|
|
46
|
+
const elapsedMinimum = elapsed - milliseconds;
|
|
47
|
+
const elapsedMaximum = elapsed + milliseconds;
|
|
48
|
+
if (elapsedMinimum < timed.configuration.time && timed.configuration.time < elapsedMaximum) {
|
|
49
|
+
if (timed.state.active) {
|
|
50
|
+
timed.callbacks.default(isRepeated ? index : void 0);
|
|
51
|
+
}
|
|
52
|
+
index += 1;
|
|
53
|
+
if (isRepeated && index < timed.configuration.count) {
|
|
54
|
+
start = void 0;
|
|
55
|
+
} else {
|
|
56
|
+
timed.state.finished = true;
|
|
57
|
+
timed.stop();
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
timed.state.frame = request(step);
|
|
62
|
+
}
|
|
63
|
+
timed.state.frame = request(step);
|
|
64
|
+
}
|
|
65
|
+
var Timed = class {
|
|
66
|
+
get active() {
|
|
67
|
+
return this.state.active;
|
|
68
|
+
}
|
|
69
|
+
get finished() {
|
|
70
|
+
return !this.active && this.state.finished;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* @param {RepeatedCallback} callback
|
|
74
|
+
* @param {number} time
|
|
75
|
+
* @param {number} count
|
|
76
|
+
* @param {AfterCallback|undefined} afterCallback
|
|
77
|
+
*/
|
|
78
|
+
constructor(callback, time, count, afterCallback) {
|
|
79
|
+
const isRepeated = this instanceof Repeated;
|
|
80
|
+
const type = isRepeated ? "repeated" : "waited";
|
|
81
|
+
if (typeof callback !== "function") {
|
|
82
|
+
throw new TypeError(`A ${type} timer must have a callback function`);
|
|
83
|
+
}
|
|
84
|
+
if (typeof time !== "number" || time < 0) {
|
|
85
|
+
throw new TypeError(
|
|
86
|
+
`A ${type} timer must have a non-negative number as its time`
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
if (isRepeated && (typeof count !== "number" || count < 2)) {
|
|
90
|
+
throw new TypeError(
|
|
91
|
+
"A repeated timer must have a number above 1 as its repeat count"
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
if (isRepeated && afterCallback !== void 0 && typeof afterCallback !== "function") {
|
|
95
|
+
throw new TypeError(
|
|
96
|
+
"A repeated timer's after-callback must be a function"
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
this.configuration = { count, time };
|
|
100
|
+
this.callbacks = {
|
|
101
|
+
after: afterCallback,
|
|
102
|
+
default: callback
|
|
103
|
+
};
|
|
104
|
+
this.state = {
|
|
105
|
+
active: false,
|
|
106
|
+
finished: false,
|
|
107
|
+
frame: null
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
restart() {
|
|
111
|
+
this.stop();
|
|
112
|
+
run(this);
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
start() {
|
|
116
|
+
if (!this.state.active) {
|
|
117
|
+
run(this);
|
|
118
|
+
}
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
stop() {
|
|
122
|
+
this.state.active = false;
|
|
123
|
+
if (this.state.frame === void 0) {
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
(cancelAnimationFrame ?? clearTimeout)?.(this.state.frame);
|
|
127
|
+
this.callbacks.after?.(this.finished);
|
|
128
|
+
this.state.frame = void 0;
|
|
129
|
+
return this;
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
var Repeated = class extends Timed {
|
|
133
|
+
};
|
|
134
|
+
var Waited = class extends Timed {
|
|
135
|
+
/**
|
|
136
|
+
* @param {Function} callback
|
|
137
|
+
* @param {number} time
|
|
138
|
+
*/
|
|
139
|
+
constructor(callback, time) {
|
|
140
|
+
super(callback, time, 1, null);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
function repeat(callback, time, count, afterCallback) {
|
|
144
|
+
return new Repeated(callback, time, count, afterCallback).start();
|
|
145
|
+
}
|
|
146
|
+
function wait(callback, time) {
|
|
147
|
+
return new Waited(callback, time).start();
|
|
148
|
+
}
|
|
149
|
+
return __toCommonJS(src_exports);
|
|
150
|
+
})();
|
package/dist/timer.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/index.js
|
|
2
2
|
var milliseconds = Math.round(1e3 / 60);
|
|
3
|
-
var request =
|
|
3
|
+
var request = requestAnimationFrame ?? function(callback) {
|
|
4
4
|
return setTimeout?.(() => {
|
|
5
5
|
callback(Date.now());
|
|
6
6
|
}, milliseconds);
|
|
@@ -97,7 +97,7 @@ var Timed = class {
|
|
|
97
97
|
if (this.state.frame === void 0) {
|
|
98
98
|
return this;
|
|
99
99
|
}
|
|
100
|
-
(
|
|
100
|
+
(cancelAnimationFrame ?? clearTimeout)?.(this.state.frame);
|
|
101
101
|
this.callbacks.after?.(this.finished);
|
|
102
102
|
this.state.frame = void 0;
|
|
103
103
|
return this;
|
package/package.json
CHANGED
|
@@ -6,20 +6,9 @@
|
|
|
6
6
|
"browser": "dist/timer.iife.js",
|
|
7
7
|
"description": "A better solution for timeout- and interval-based timers.",
|
|
8
8
|
"devDependencies": {
|
|
9
|
-
"esbuild": "^0.
|
|
9
|
+
"esbuild": "^0.18",
|
|
10
10
|
"xo": "^0.54"
|
|
11
11
|
},
|
|
12
|
-
"exports": {
|
|
13
|
-
".": {
|
|
14
|
-
"types": "./src/index.d.ts",
|
|
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
|
-
},
|
|
21
|
-
"./package.json": "./package.json"
|
|
22
|
-
},
|
|
23
12
|
"files": [
|
|
24
13
|
"dist",
|
|
25
14
|
"src"
|
|
@@ -32,7 +21,7 @@
|
|
|
32
21
|
"requestAnimationFrame"
|
|
33
22
|
],
|
|
34
23
|
"license": "MIT",
|
|
35
|
-
"main": "dist/timer.
|
|
24
|
+
"main": "dist/timer.iife.js",
|
|
36
25
|
"module": "dist/timer.js",
|
|
37
26
|
"name": "@oscarpalmer/timer",
|
|
38
27
|
"repository": {
|
|
@@ -40,15 +29,14 @@
|
|
|
40
29
|
"url": "git+https://github.com/oscarpalmer/timer.git"
|
|
41
30
|
},
|
|
42
31
|
"scripts": {
|
|
43
|
-
"build": "npm run build:
|
|
44
|
-
"build:cjs": "esbuild ./src/index.js --bundle --format=cjs --outfile=./dist/timer.cjs --target=es2020",
|
|
32
|
+
"build": "npm run build:esm; npm run build:iife; npm run types",
|
|
45
33
|
"build:esm": "esbuild ./src/index.js --bundle --format=esm --outfile=./dist/timer.js --target=es2020",
|
|
46
|
-
"build:iife": "esbuild ./src/index.js --bundle --
|
|
34
|
+
"build:iife": "esbuild ./src/index.js --bundle --format=iife --global-name=Timer --outfile=./dist/timer.iife.js --target=es2020",
|
|
47
35
|
"types": "./node_modules/.bin/tsc ./src/index.js --allowJs --declaration --emitDeclarationOnly --removeComments",
|
|
48
36
|
"xo": "./node_modules/.bin/xo ./src/*.js --env browser"
|
|
49
37
|
},
|
|
50
38
|
"type": "module",
|
|
51
39
|
"types": "src/index.d.ts",
|
|
52
40
|
"unpkg": "dist/timer.iife.js",
|
|
53
|
-
"version": "0.
|
|
41
|
+
"version": "0.17.0"
|
|
54
42
|
}
|
package/src/index.js
CHANGED
|
@@ -12,8 +12,9 @@
|
|
|
12
12
|
|
|
13
13
|
const milliseconds = Math.round(1000 / 60);
|
|
14
14
|
|
|
15
|
-
const request =
|
|
16
|
-
??
|
|
15
|
+
const request =
|
|
16
|
+
requestAnimationFrame ??
|
|
17
|
+
function (callback) {
|
|
17
18
|
return setTimeout?.(() => {
|
|
18
19
|
callback(Date.now());
|
|
19
20
|
}, milliseconds);
|
|
@@ -45,8 +46,8 @@ function run(timed) {
|
|
|
45
46
|
const elapsedMaximum = elapsed + milliseconds;
|
|
46
47
|
|
|
47
48
|
if (
|
|
48
|
-
elapsedMinimum < timed.configuration.time
|
|
49
|
-
|
|
49
|
+
elapsedMinimum < timed.configuration.time &&
|
|
50
|
+
timed.configuration.time < elapsedMaximum
|
|
50
51
|
) {
|
|
51
52
|
if (timed.state.active) {
|
|
52
53
|
timed.callbacks.default(isRepeated ? index : undefined);
|
|
@@ -108,12 +109,12 @@ class Timed {
|
|
|
108
109
|
}
|
|
109
110
|
|
|
110
111
|
if (
|
|
111
|
-
isRepeated
|
|
112
|
-
|
|
113
|
-
|
|
112
|
+
isRepeated &&
|
|
113
|
+
afterCallback !== undefined &&
|
|
114
|
+
typeof afterCallback !== 'function'
|
|
114
115
|
) {
|
|
115
116
|
throw new TypeError(
|
|
116
|
-
|
|
117
|
+
"A repeated timer's after-callback must be a function",
|
|
117
118
|
);
|
|
118
119
|
}
|
|
119
120
|
|
|
@@ -166,7 +167,7 @@ class Timed {
|
|
|
166
167
|
return this;
|
|
167
168
|
}
|
|
168
169
|
|
|
169
|
-
(
|
|
170
|
+
(cancelAnimationFrame ?? clearTimeout)?.(this.state.frame);
|
|
170
171
|
|
|
171
172
|
this.callbacks.after?.(this.finished);
|
|
172
173
|
|
package/dist/timer.cjs
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
|
|
19
|
-
// src/index.js
|
|
20
|
-
var src_exports = {};
|
|
21
|
-
__export(src_exports, {
|
|
22
|
-
Repeated: () => Repeated,
|
|
23
|
-
Waited: () => Waited,
|
|
24
|
-
repeat: () => repeat,
|
|
25
|
-
wait: () => wait
|
|
26
|
-
});
|
|
27
|
-
module.exports = __toCommonJS(src_exports);
|
|
28
|
-
var milliseconds = Math.round(1e3 / 60);
|
|
29
|
-
var request = globalThis.requestAnimationFrame ?? function(callback) {
|
|
30
|
-
return setTimeout?.(() => {
|
|
31
|
-
callback(Date.now());
|
|
32
|
-
}, milliseconds);
|
|
33
|
-
};
|
|
34
|
-
function run(timed) {
|
|
35
|
-
timed.state.active = true;
|
|
36
|
-
timed.state.finished = false;
|
|
37
|
-
const isRepeated = timed instanceof Repeated;
|
|
38
|
-
let index = 0;
|
|
39
|
-
let start;
|
|
40
|
-
function step(timestamp) {
|
|
41
|
-
if (!timed.state.active) {
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
start ?? (start = timestamp);
|
|
45
|
-
const elapsed = timestamp - start;
|
|
46
|
-
const elapsedMinimum = elapsed - milliseconds;
|
|
47
|
-
const elapsedMaximum = elapsed + milliseconds;
|
|
48
|
-
if (elapsedMinimum < timed.configuration.time && timed.configuration.time < elapsedMaximum) {
|
|
49
|
-
if (timed.state.active) {
|
|
50
|
-
timed.callbacks.default(isRepeated ? index : void 0);
|
|
51
|
-
}
|
|
52
|
-
index += 1;
|
|
53
|
-
if (isRepeated && index < timed.configuration.count) {
|
|
54
|
-
start = void 0;
|
|
55
|
-
} else {
|
|
56
|
-
timed.state.finished = true;
|
|
57
|
-
timed.stop();
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
timed.state.frame = request(step);
|
|
62
|
-
}
|
|
63
|
-
timed.state.frame = request(step);
|
|
64
|
-
}
|
|
65
|
-
var Timed = class {
|
|
66
|
-
get active() {
|
|
67
|
-
return this.state.active;
|
|
68
|
-
}
|
|
69
|
-
get finished() {
|
|
70
|
-
return !this.active && this.state.finished;
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* @param {RepeatedCallback} callback
|
|
74
|
-
* @param {number} time
|
|
75
|
-
* @param {number} count
|
|
76
|
-
* @param {AfterCallback|undefined} afterCallback
|
|
77
|
-
*/
|
|
78
|
-
constructor(callback, time, count, afterCallback) {
|
|
79
|
-
const isRepeated = this instanceof Repeated;
|
|
80
|
-
const type = isRepeated ? "repeated" : "waited";
|
|
81
|
-
if (typeof callback !== "function") {
|
|
82
|
-
throw new TypeError(`A ${type} timer must have a callback function`);
|
|
83
|
-
}
|
|
84
|
-
if (typeof time !== "number" || time < 0) {
|
|
85
|
-
throw new TypeError(
|
|
86
|
-
`A ${type} timer must have a non-negative number as its time`
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
if (isRepeated && (typeof count !== "number" || count < 2)) {
|
|
90
|
-
throw new TypeError(
|
|
91
|
-
"A repeated timer must have a number above 1 as its repeat count"
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
if (isRepeated && afterCallback !== void 0 && typeof afterCallback !== "function") {
|
|
95
|
-
throw new TypeError(
|
|
96
|
-
"A repeated timer's after-callback must be a function"
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
this.configuration = { count, time };
|
|
100
|
-
this.callbacks = {
|
|
101
|
-
after: afterCallback,
|
|
102
|
-
default: callback
|
|
103
|
-
};
|
|
104
|
-
this.state = {
|
|
105
|
-
active: false,
|
|
106
|
-
finished: false,
|
|
107
|
-
frame: null
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
restart() {
|
|
111
|
-
this.stop();
|
|
112
|
-
run(this);
|
|
113
|
-
return this;
|
|
114
|
-
}
|
|
115
|
-
start() {
|
|
116
|
-
if (!this.state.active) {
|
|
117
|
-
run(this);
|
|
118
|
-
}
|
|
119
|
-
return this;
|
|
120
|
-
}
|
|
121
|
-
stop() {
|
|
122
|
-
this.state.active = false;
|
|
123
|
-
if (this.state.frame === void 0) {
|
|
124
|
-
return this;
|
|
125
|
-
}
|
|
126
|
-
(globalThis.cancelAnimationFrame ?? clearTimeout)?.(this.state.frame);
|
|
127
|
-
this.callbacks.after?.(this.finished);
|
|
128
|
-
this.state.frame = void 0;
|
|
129
|
-
return this;
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
var Repeated = class extends Timed {
|
|
133
|
-
};
|
|
134
|
-
var Waited = class extends Timed {
|
|
135
|
-
/**
|
|
136
|
-
* @param {Function} callback
|
|
137
|
-
* @param {number} time
|
|
138
|
-
*/
|
|
139
|
-
constructor(callback, time) {
|
|
140
|
-
super(callback, time, 1, null);
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
function repeat(callback, time, count, afterCallback) {
|
|
144
|
-
return new Repeated(callback, time, count, afterCallback).start();
|
|
145
|
-
}
|
|
146
|
-
function wait(callback, time) {
|
|
147
|
-
return new Waited(callback, time).start();
|
|
148
|
-
}
|