@oscarpalmer/timer 0.14.0 → 0.16.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 +15 -30
- package/dist/timer.iife.js +1 -1
- package/dist/timer.js +15 -32
- package/package.json +12 -8
- package/src/index.d.ts +6 -6
- package/src/index.js +39 -35
package/dist/timer.cjs
CHANGED
|
@@ -2,7 +2,6 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
3
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
4
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
5
|
var __export = (target, all) => {
|
|
7
6
|
for (var name in all)
|
|
8
7
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -16,10 +15,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
15
|
return to;
|
|
17
16
|
};
|
|
18
17
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var __publicField = (obj, key, value) => {
|
|
20
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
21
|
-
return value;
|
|
22
|
-
};
|
|
23
18
|
|
|
24
19
|
// src/index.js
|
|
25
20
|
var src_exports = {};
|
|
@@ -68,6 +63,12 @@ function run(timed) {
|
|
|
68
63
|
timed.state.frame = request(step);
|
|
69
64
|
}
|
|
70
65
|
var Timed = class {
|
|
66
|
+
get active() {
|
|
67
|
+
return this.state.active;
|
|
68
|
+
}
|
|
69
|
+
get finished() {
|
|
70
|
+
return !this.active && this.state.finished;
|
|
71
|
+
}
|
|
71
72
|
/**
|
|
72
73
|
* @param {RepeatedCallback} callback
|
|
73
74
|
* @param {number} time
|
|
@@ -75,34 +76,25 @@ var Timed = class {
|
|
|
75
76
|
* @param {AfterCallback|undefined} afterCallback
|
|
76
77
|
*/
|
|
77
78
|
constructor(callback, time, count, afterCallback) {
|
|
78
|
-
/**
|
|
79
|
-
* @readonly
|
|
80
|
-
* @type {{after: AfterCallback | undefined; default: RepeatedCallback}}
|
|
81
|
-
*/
|
|
82
|
-
__publicField(this, "callbacks");
|
|
83
|
-
/**
|
|
84
|
-
* @readonly
|
|
85
|
-
* @type {{count: number; time: number}}
|
|
86
|
-
*/
|
|
87
|
-
__publicField(this, "configuration");
|
|
88
|
-
/**
|
|
89
|
-
* @readonly
|
|
90
|
-
* @type {{active: boolean; finished: boolean; frame?: DOMHighResTimeStamp}}
|
|
91
|
-
*/
|
|
92
|
-
__publicField(this, "state");
|
|
93
79
|
const isRepeated = this instanceof Repeated;
|
|
94
80
|
const type = isRepeated ? "repeated" : "waited";
|
|
95
81
|
if (typeof callback !== "function") {
|
|
96
82
|
throw new TypeError(`A ${type} timer must have a callback function`);
|
|
97
83
|
}
|
|
98
84
|
if (typeof time !== "number" || time < 0) {
|
|
99
|
-
throw new TypeError(
|
|
85
|
+
throw new TypeError(
|
|
86
|
+
`A ${type} timer must have a non-negative number as its time`
|
|
87
|
+
);
|
|
100
88
|
}
|
|
101
89
|
if (isRepeated && (typeof count !== "number" || count < 2)) {
|
|
102
|
-
throw new TypeError(
|
|
90
|
+
throw new TypeError(
|
|
91
|
+
"A repeated timer must have a number above 1 as its repeat count"
|
|
92
|
+
);
|
|
103
93
|
}
|
|
104
94
|
if (isRepeated && afterCallback !== void 0 && typeof afterCallback !== "function") {
|
|
105
|
-
throw new TypeError(
|
|
95
|
+
throw new TypeError(
|
|
96
|
+
"A repeated timer's after-callback must be a function"
|
|
97
|
+
);
|
|
106
98
|
}
|
|
107
99
|
this.configuration = { count, time };
|
|
108
100
|
this.callbacks = {
|
|
@@ -115,13 +107,6 @@ var Timed = class {
|
|
|
115
107
|
frame: null
|
|
116
108
|
};
|
|
117
109
|
}
|
|
118
|
-
/** */
|
|
119
|
-
get active() {
|
|
120
|
-
return this.state.active;
|
|
121
|
-
}
|
|
122
|
-
get finished() {
|
|
123
|
-
return !this.active && this.state.finished;
|
|
124
|
-
}
|
|
125
110
|
restart() {
|
|
126
111
|
this.stop();
|
|
127
112
|
run(this);
|
package/dist/timer.iife.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var Timer=(()=>{var
|
|
1
|
+
var Timer=(()=>{var c=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var b=Object.prototype.hasOwnProperty;var g=(t,e)=>{for(var s in e)c(t,s,{get:e[s],enumerable:!0})},y=(t,e,s,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of w(e))!b.call(t,i)&&i!==s&&c(t,i,{get:()=>e[i],enumerable:!(n=v(e,i))||n.enumerable});return t};var x=t=>y(c({},"__esModule",{value:!0}),t);var E={};g(E,{Repeated:()=>r,Waited:()=>f,repeat:()=>T,wait:()=>A});var h=Math.round(16.666666666666668),l=globalThis.requestAnimationFrame??function(t){return setTimeout?.(()=>{t(Date.now())},h)};function p(t){t.state.active=!0,t.state.finished=!1;let e=t instanceof r,s=0,n;function i(a){if(!t.state.active)return;n??(n=a);let o=a-n,d=o-h,m=o+h;if(d<t.configuration.time&&t.configuration.time<m)if(t.state.active&&t.callbacks.default(e?s:void 0),s+=1,e&&s<t.configuration.count)n=void 0;else{t.state.finished=!0,t.stop();return}t.state.frame=l(i)}t.state.frame=l(i)}var u=class{get active(){return this.state.active}get finished(){return!this.active&&this.state.finished}constructor(e,s,n,i){let a=this instanceof r,o=a?"repeated":"waited";if(typeof e!="function")throw new TypeError(`A ${o} timer must have a callback function`);if(typeof s!="number"||s<0)throw new TypeError(`A ${o} timer must have a non-negative number as its time`);if(a&&(typeof n!="number"||n<2))throw new TypeError("A repeated timer must have a number above 1 as its repeat count");if(a&&i!==void 0&&typeof i!="function")throw new TypeError("A repeated timer's after-callback must be a function");this.configuration={count:n,time:s},this.callbacks={after:i,default:e},this.state={active:!1,finished:!1,frame:null}}restart(){return this.stop(),p(this),this}start(){return this.state.active||p(this),this}stop(){return this.state.active=!1,this.state.frame===void 0?this:((globalThis.cancelAnimationFrame??clearTimeout)?.(this.state.frame),this.callbacks.after?.(this.finished),this.state.frame=void 0,this)}},r=class extends u{},f=class extends u{constructor(e,s){super(e,s,1,null)}};function T(t,e,s,n){return new r(t,e,s,n).start()}function A(t,e){return new f(t,e).start()}return x(E);})();
|
package/dist/timer.js
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
-
var __publicField = (obj, key, value) => {
|
|
4
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
-
return value;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
1
|
// src/index.js
|
|
9
2
|
var milliseconds = Math.round(1e3 / 60);
|
|
10
3
|
var request = globalThis.requestAnimationFrame ?? function(callback) {
|
|
@@ -44,6 +37,12 @@ function run(timed) {
|
|
|
44
37
|
timed.state.frame = request(step);
|
|
45
38
|
}
|
|
46
39
|
var Timed = class {
|
|
40
|
+
get active() {
|
|
41
|
+
return this.state.active;
|
|
42
|
+
}
|
|
43
|
+
get finished() {
|
|
44
|
+
return !this.active && this.state.finished;
|
|
45
|
+
}
|
|
47
46
|
/**
|
|
48
47
|
* @param {RepeatedCallback} callback
|
|
49
48
|
* @param {number} time
|
|
@@ -51,34 +50,25 @@ var Timed = class {
|
|
|
51
50
|
* @param {AfterCallback|undefined} afterCallback
|
|
52
51
|
*/
|
|
53
52
|
constructor(callback, time, count, afterCallback) {
|
|
54
|
-
/**
|
|
55
|
-
* @readonly
|
|
56
|
-
* @type {{after: AfterCallback | undefined; default: RepeatedCallback}}
|
|
57
|
-
*/
|
|
58
|
-
__publicField(this, "callbacks");
|
|
59
|
-
/**
|
|
60
|
-
* @readonly
|
|
61
|
-
* @type {{count: number; time: number}}
|
|
62
|
-
*/
|
|
63
|
-
__publicField(this, "configuration");
|
|
64
|
-
/**
|
|
65
|
-
* @readonly
|
|
66
|
-
* @type {{active: boolean; finished: boolean; frame?: DOMHighResTimeStamp}}
|
|
67
|
-
*/
|
|
68
|
-
__publicField(this, "state");
|
|
69
53
|
const isRepeated = this instanceof Repeated;
|
|
70
54
|
const type = isRepeated ? "repeated" : "waited";
|
|
71
55
|
if (typeof callback !== "function") {
|
|
72
56
|
throw new TypeError(`A ${type} timer must have a callback function`);
|
|
73
57
|
}
|
|
74
58
|
if (typeof time !== "number" || time < 0) {
|
|
75
|
-
throw new TypeError(
|
|
59
|
+
throw new TypeError(
|
|
60
|
+
`A ${type} timer must have a non-negative number as its time`
|
|
61
|
+
);
|
|
76
62
|
}
|
|
77
63
|
if (isRepeated && (typeof count !== "number" || count < 2)) {
|
|
78
|
-
throw new TypeError(
|
|
64
|
+
throw new TypeError(
|
|
65
|
+
"A repeated timer must have a number above 1 as its repeat count"
|
|
66
|
+
);
|
|
79
67
|
}
|
|
80
68
|
if (isRepeated && afterCallback !== void 0 && typeof afterCallback !== "function") {
|
|
81
|
-
throw new TypeError(
|
|
69
|
+
throw new TypeError(
|
|
70
|
+
"A repeated timer's after-callback must be a function"
|
|
71
|
+
);
|
|
82
72
|
}
|
|
83
73
|
this.configuration = { count, time };
|
|
84
74
|
this.callbacks = {
|
|
@@ -91,13 +81,6 @@ var Timed = class {
|
|
|
91
81
|
frame: null
|
|
92
82
|
};
|
|
93
83
|
}
|
|
94
|
-
/** */
|
|
95
|
-
get active() {
|
|
96
|
-
return this.state.active;
|
|
97
|
-
}
|
|
98
|
-
get finished() {
|
|
99
|
-
return !this.active && this.state.finished;
|
|
100
|
-
}
|
|
101
84
|
restart() {
|
|
102
85
|
this.stop();
|
|
103
86
|
run(this);
|
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.16.0"
|
|
50
54
|
}
|
package/src/index.d.ts
CHANGED
|
@@ -9,21 +9,21 @@ export type AfterCallback = (finished: boolean) => void;
|
|
|
9
9
|
export type RepeatedCallback = (index: number) => void;
|
|
10
10
|
declare class Timed {
|
|
11
11
|
constructor(callback: RepeatedCallback, time: number, count: number, afterCallback: AfterCallback | undefined);
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
default: RepeatedCallback;
|
|
15
|
-
};
|
|
12
|
+
get active(): boolean;
|
|
13
|
+
get finished(): boolean;
|
|
16
14
|
readonly configuration: {
|
|
17
15
|
count: number;
|
|
18
16
|
time: number;
|
|
19
17
|
};
|
|
18
|
+
readonly callbacks: {
|
|
19
|
+
after: AfterCallback | undefined;
|
|
20
|
+
default: RepeatedCallback;
|
|
21
|
+
};
|
|
20
22
|
readonly state: {
|
|
21
23
|
active: boolean;
|
|
22
24
|
finished: boolean;
|
|
23
25
|
frame?: DOMHighResTimeStamp;
|
|
24
26
|
};
|
|
25
|
-
get active(): boolean;
|
|
26
|
-
get finished(): boolean;
|
|
27
27
|
restart(): Timed;
|
|
28
28
|
start(): Timed;
|
|
29
29
|
stop(): Timed;
|
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
|
}
|
|
@@ -68,26 +72,6 @@ function run(timed) {
|
|
|
68
72
|
}
|
|
69
73
|
|
|
70
74
|
class Timed {
|
|
71
|
-
/**
|
|
72
|
-
* @readonly
|
|
73
|
-
* @type {{after: AfterCallback | undefined; default: RepeatedCallback}}
|
|
74
|
-
*/
|
|
75
|
-
callbacks;
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* @readonly
|
|
79
|
-
* @type {{count: number; time: number}}
|
|
80
|
-
*/
|
|
81
|
-
configuration;
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* @readonly
|
|
85
|
-
* @type {{active: boolean; finished: boolean; frame?: DOMHighResTimeStamp}}
|
|
86
|
-
*/
|
|
87
|
-
state;
|
|
88
|
-
|
|
89
|
-
/** */
|
|
90
|
-
|
|
91
75
|
get active() {
|
|
92
76
|
return this.state.active;
|
|
93
77
|
}
|
|
@@ -105,33 +89,53 @@ class Timed {
|
|
|
105
89
|
constructor(callback, time, count, afterCallback) {
|
|
106
90
|
const isRepeated = this instanceof Repeated;
|
|
107
91
|
|
|
108
|
-
const type = isRepeated
|
|
109
|
-
? 'repeated'
|
|
110
|
-
: 'waited';
|
|
92
|
+
const type = isRepeated ? 'repeated' : 'waited';
|
|
111
93
|
|
|
112
94
|
if (typeof callback !== 'function') {
|
|
113
95
|
throw new TypeError(`A ${type} timer must have a callback function`);
|
|
114
96
|
}
|
|
115
97
|
|
|
116
98
|
if (typeof time !== 'number' || time < 0) {
|
|
117
|
-
throw new TypeError(
|
|
99
|
+
throw new TypeError(
|
|
100
|
+
`A ${type} timer must have a non-negative number as its time`,
|
|
101
|
+
);
|
|
118
102
|
}
|
|
119
103
|
|
|
120
104
|
if (isRepeated && (typeof count !== 'number' || count < 2)) {
|
|
121
|
-
throw new TypeError(
|
|
105
|
+
throw new TypeError(
|
|
106
|
+
'A repeated timer must have a number above 1 as its repeat count',
|
|
107
|
+
);
|
|
122
108
|
}
|
|
123
109
|
|
|
124
|
-
if (
|
|
125
|
-
|
|
110
|
+
if (
|
|
111
|
+
isRepeated
|
|
112
|
+
&& afterCallback !== undefined
|
|
113
|
+
&& typeof afterCallback !== 'function'
|
|
114
|
+
) {
|
|
115
|
+
throw new TypeError(
|
|
116
|
+
'A repeated timer\'s after-callback must be a function',
|
|
117
|
+
);
|
|
126
118
|
}
|
|
127
119
|
|
|
120
|
+
/**
|
|
121
|
+
* @readonly
|
|
122
|
+
* @type {{count: number; time: number}}
|
|
123
|
+
*/
|
|
128
124
|
this.configuration = {count, time};
|
|
129
125
|
|
|
126
|
+
/**
|
|
127
|
+
* @readonly
|
|
128
|
+
* @type {{after: AfterCallback | undefined; default: RepeatedCallback}}
|
|
129
|
+
*/
|
|
130
130
|
this.callbacks = {
|
|
131
131
|
after: afterCallback,
|
|
132
132
|
default: callback,
|
|
133
133
|
};
|
|
134
134
|
|
|
135
|
+
/**
|
|
136
|
+
* @readonly
|
|
137
|
+
* @type {{active: boolean; finished: boolean; frame?: DOMHighResTimeStamp}}
|
|
138
|
+
*/
|
|
135
139
|
this.state = {
|
|
136
140
|
active: false,
|
|
137
141
|
finished: false,
|
|
@@ -192,7 +196,7 @@ export class Waited extends Timed {
|
|
|
192
196
|
* @return {Repeated} A repeated timer
|
|
193
197
|
*/
|
|
194
198
|
export function repeat(callback, time, count, afterCallback) {
|
|
195
|
-
return
|
|
199
|
+
return new Repeated(callback, time, count, afterCallback).start();
|
|
196
200
|
}
|
|
197
201
|
|
|
198
202
|
/**
|
|
@@ -201,5 +205,5 @@ export function repeat(callback, time, count, afterCallback) {
|
|
|
201
205
|
* @return {Waited} A waited timer
|
|
202
206
|
*/
|
|
203
207
|
export function wait(callback, time) {
|
|
204
|
-
return
|
|
208
|
+
return new Waited(callback, time).start();
|
|
205
209
|
}
|