@oscarpalmer/timer 0.4.0 → 0.7.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/.eslintrc.json +1 -1
- package/README.md +40 -2
- package/dist/timer.js +10 -12
- package/package.json +13 -12
- package/src/index.d.ts +1 -18
- package/src/index.ts +33 -11
- package/test/test.js +13 -13
package/.eslintrc.json
CHANGED
package/README.md
CHANGED
|
@@ -1,7 +1,45 @@
|
|
|
1
1
|
# Timer
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://badge.fury.io/js/@oscarpalmer%2Ftimer)
|
|
4
|
+
|
|
5
|
+
A better solution for timeout- and interval-based timers.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Timer is available on _npm_ as [`@oscarpalmer/timer`](https://www.npmjs.com/package/@oscarpalmer/timer).
|
|
10
|
+
|
|
11
|
+
## Getting started
|
|
12
|
+
|
|
13
|
+
This is fairily lightweight package, so hopefully you'll be up and running in seconds :blush:
|
|
14
|
+
|
|
15
|
+
### Examples
|
|
16
|
+
|
|
17
|
+
The timers can be called with nice helper methods, which also auto-start the timers:
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import {repeat, wait} from '@oscarpalmer/timer';
|
|
21
|
+
|
|
22
|
+
let waited = wait(callback, time);
|
|
23
|
+
let repeated = repeat(callback, time, count);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Or they can be created using class syntax, but without being auto-started:
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import {Repeated, Waited} from '@oscarpalmer/timer';
|
|
30
|
+
|
|
31
|
+
waited = new Waited(callback, time);
|
|
32
|
+
repeated = new Repeated(callback, time, count);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Both the nice helper methods and the class syntax create similar objects – `Waited` and `Repeated` – which share methods:
|
|
36
|
+
|
|
37
|
+
|Method|Description|
|
|
38
|
+
|-----:|:----------|
|
|
39
|
+
|`start()`|Starts the timer: necessary when creating a timer using the class syntax _(e.g. `new Waited...`)_, but helpful when the timer needs to be started at other times, as well|
|
|
40
|
+
|`stop()`|Stops the timer|
|
|
41
|
+
|`restart()`|Restarts the timer|
|
|
4
42
|
|
|
5
43
|
## License
|
|
6
44
|
|
|
7
|
-
[
|
|
45
|
+
[MIT licensed](LICENSE), natch :wink:
|
package/dist/timer.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __defProp = Object.defineProperty;
|
|
3
2
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4
3
|
var __publicField = (obj, key, value) => {
|
|
@@ -7,7 +6,7 @@ var __publicField = (obj, key, value) => {
|
|
|
7
6
|
};
|
|
8
7
|
|
|
9
8
|
// src/index.ts
|
|
10
|
-
var milliseconds = 1e3 / 60;
|
|
9
|
+
var milliseconds = Math.round(1e3 / 60);
|
|
11
10
|
var Timed = class {
|
|
12
11
|
constructor(type, callback, time, count) {
|
|
13
12
|
__publicField(this, "callback");
|
|
@@ -93,16 +92,15 @@ var Waited = class extends Timed {
|
|
|
93
92
|
super("waited", callback, time, 1);
|
|
94
93
|
}
|
|
95
94
|
};
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
};
|
|
95
|
+
function repeat(callback, time, count) {
|
|
96
|
+
return new Repeated(callback, time, count).start();
|
|
97
|
+
}
|
|
98
|
+
function wait(callback, time) {
|
|
99
|
+
return new Waited(callback, time).start();
|
|
100
|
+
}
|
|
104
101
|
export {
|
|
105
102
|
Repeated,
|
|
106
|
-
|
|
107
|
-
|
|
103
|
+
Waited,
|
|
104
|
+
repeat,
|
|
105
|
+
wait
|
|
108
106
|
};
|
package/package.json
CHANGED
|
@@ -3,17 +3,17 @@
|
|
|
3
3
|
"name": "Oscar Palmér",
|
|
4
4
|
"url": "https://oscarpalmer.se"
|
|
5
5
|
},
|
|
6
|
-
"description": "A better
|
|
6
|
+
"description": "A better solution for timeout- and interval-based timers.",
|
|
7
7
|
"devDependencies": {
|
|
8
8
|
"@tsconfig/recommended": "^1.0.0",
|
|
9
|
-
"@types/node": "^18.
|
|
10
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
11
|
-
"@typescript-eslint/parser": "^5.
|
|
12
|
-
"esbuild": "^0.
|
|
13
|
-
"eslint": "^8.
|
|
14
|
-
"eslint-config-xo": "^0.
|
|
15
|
-
"eslint-config-xo-typescript": "^0.
|
|
16
|
-
"typescript": "^4.
|
|
9
|
+
"@types/node": "^18.7.0",
|
|
10
|
+
"@typescript-eslint/eslint-plugin": "^5.35.0",
|
|
11
|
+
"@typescript-eslint/parser": "^5.35.0",
|
|
12
|
+
"esbuild": "^0.15.0",
|
|
13
|
+
"eslint": "^8.22.0",
|
|
14
|
+
"eslint-config-xo": "^0.42.0",
|
|
15
|
+
"eslint-config-xo-typescript": "^0.53.0",
|
|
16
|
+
"typescript": "^4.8.0"
|
|
17
17
|
},
|
|
18
18
|
"exports": {
|
|
19
19
|
".": {
|
|
@@ -37,10 +37,11 @@
|
|
|
37
37
|
"url": "git+https://github.com/oscarpalmer/timer.git"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
|
-
|
|
40
|
+
"build": "esbuild ./src/index.ts --bundle --target=es2020 --platform=browser --outfile=./dist/timer.js --format=esm",
|
|
41
41
|
"watch": "esbuild ./src/index.ts --bundle --target=es2020 --platform=browser --outfile=./dist/timer.js --format=esm --watch"
|
|
42
42
|
},
|
|
43
43
|
"type": "module",
|
|
44
|
+
"types": "src/index.d.ts",
|
|
44
45
|
"unpkg": "dist/timer.js",
|
|
45
|
-
"version": "0.
|
|
46
|
-
}
|
|
46
|
+
"version": "0.7.0"
|
|
47
|
+
}
|
package/src/index.d.ts
CHANGED
|
@@ -1,18 +1 @@
|
|
|
1
|
-
|
|
2
|
-
type WaitedCallback = () => void;
|
|
3
|
-
|
|
4
|
-
interface Timed<Callback> {
|
|
5
|
-
get active(): boolean;
|
|
6
|
-
constructor(callback: Callback, time: number, count: number): void;
|
|
7
|
-
restart(): void;
|
|
8
|
-
start(): void;
|
|
9
|
-
stop(): void;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface Repeated extends Timed<RepeatedCallback> {}
|
|
13
|
-
export interface Waited extends Timed<WaitedCallback> {}
|
|
14
|
-
|
|
15
|
-
export interface Timer {
|
|
16
|
-
repeated(callback: RepeatedCallback, time: number, count: number): Repeated;
|
|
17
|
-
waited(callback: WaitedCallback, time: number): Waited;
|
|
18
|
-
}
|
|
1
|
+
export * from './index';
|
package/src/index.ts
CHANGED
|
@@ -2,7 +2,7 @@ type RepeatedCallback = (index: number) => void;
|
|
|
2
2
|
type TimerType = 'repeated' | 'waited';
|
|
3
3
|
type WaitedCallback = () => void;
|
|
4
4
|
|
|
5
|
-
const milliseconds = 1000 / 60;
|
|
5
|
+
const milliseconds = Math.round(1000 / 60);
|
|
6
6
|
|
|
7
7
|
abstract class Timed<Callback> {
|
|
8
8
|
private readonly callback: Callback;
|
|
@@ -12,6 +12,9 @@ abstract class Timed<Callback> {
|
|
|
12
12
|
private readonly time: number;
|
|
13
13
|
private readonly type: TimerType;
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Is the timer active?
|
|
17
|
+
*/
|
|
15
18
|
get active(): boolean {
|
|
16
19
|
return this.running;
|
|
17
20
|
}
|
|
@@ -35,7 +38,10 @@ abstract class Timed<Callback> {
|
|
|
35
38
|
this.count = count;
|
|
36
39
|
}
|
|
37
40
|
|
|
38
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Restart timer
|
|
43
|
+
*/
|
|
44
|
+
restart(): this {
|
|
39
45
|
this.stop();
|
|
40
46
|
|
|
41
47
|
Timed.run(this as never);
|
|
@@ -43,7 +49,10 @@ abstract class Timed<Callback> {
|
|
|
43
49
|
return this;
|
|
44
50
|
}
|
|
45
51
|
|
|
46
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Start timer
|
|
54
|
+
*/
|
|
55
|
+
start(): this {
|
|
47
56
|
if (this.running) {
|
|
48
57
|
return this;
|
|
49
58
|
}
|
|
@@ -53,7 +62,10 @@ abstract class Timed<Callback> {
|
|
|
53
62
|
return this;
|
|
54
63
|
}
|
|
55
64
|
|
|
56
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Stop timer
|
|
67
|
+
*/
|
|
68
|
+
stop(): this {
|
|
57
69
|
this.running = false;
|
|
58
70
|
|
|
59
71
|
if (typeof this.frame === 'undefined') {
|
|
@@ -109,24 +121,34 @@ abstract class Timed<Callback> {
|
|
|
109
121
|
}
|
|
110
122
|
}
|
|
111
123
|
|
|
124
|
+
/**
|
|
125
|
+
* A repeated timer
|
|
126
|
+
*/
|
|
112
127
|
export class Repeated extends Timed<RepeatedCallback> {
|
|
113
128
|
constructor(callback: RepeatedCallback, time: number, count: number) {
|
|
114
129
|
super('repeated', callback, time, count);
|
|
115
130
|
}
|
|
116
131
|
}
|
|
117
132
|
|
|
133
|
+
/**
|
|
134
|
+
* A waited timer
|
|
135
|
+
*/
|
|
118
136
|
export class Waited extends Timed<WaitedCallback> {
|
|
119
137
|
constructor(callback: WaitedCallback, time: number) {
|
|
120
138
|
super('waited', callback, time, 1);
|
|
121
139
|
}
|
|
122
140
|
}
|
|
123
141
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
142
|
+
/**
|
|
143
|
+
* Create and start a new repeated timer
|
|
144
|
+
*/
|
|
145
|
+
export function repeat(callback: RepeatedCallback, time: number, count: number): Repeated {
|
|
146
|
+
return (new Repeated(callback, time, count)).start();
|
|
147
|
+
}
|
|
128
148
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
149
|
+
/**
|
|
150
|
+
* Create and start a new waited timer
|
|
151
|
+
*/
|
|
152
|
+
export function wait(callback: WaitedCallback, time: number): Waited {
|
|
153
|
+
return (new Waited(callback, time)).start();
|
|
132
154
|
}
|
package/test/test.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {repeat, wait, Repeated, Waited} from '../dist/timer.js';
|
|
2
2
|
|
|
3
|
-
describe('
|
|
3
|
+
describe('Functions', function () {
|
|
4
4
|
it('should create and start a repeated timer', function () {
|
|
5
|
-
chai.assert.ok(
|
|
5
|
+
chai.assert.ok(repeat(() => {}, 0, 2) instanceof Repeated);
|
|
6
6
|
});
|
|
7
7
|
|
|
8
8
|
it('should create and start a waited timer', function () {
|
|
9
|
-
chai.assert.ok(
|
|
9
|
+
chai.assert.ok(wait(() => {}, 0) instanceof Waited);
|
|
10
10
|
});
|
|
11
11
|
});
|
|
12
12
|
|
|
@@ -28,11 +28,11 @@ describe('Timer, Repeated', function () {
|
|
|
28
28
|
it('should run as many times as set', function (done) {
|
|
29
29
|
let value = 0;
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
repeat(() => {
|
|
32
32
|
value += 1;
|
|
33
33
|
}, 25, 10);
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
wait(() => {
|
|
36
36
|
chai.assert.equal(value, 10);
|
|
37
37
|
|
|
38
38
|
done();
|
|
@@ -42,15 +42,15 @@ describe('Timer, Repeated', function () {
|
|
|
42
42
|
it('should run until canceled', function (done) {
|
|
43
43
|
let value = 0;
|
|
44
44
|
|
|
45
|
-
const repeated =
|
|
45
|
+
const repeated = repeat(() => {
|
|
46
46
|
value += 1;
|
|
47
47
|
}, 25, 10);
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
wait(() => {
|
|
50
50
|
repeated.stop();
|
|
51
51
|
}, 125);
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
wait(() => {
|
|
54
54
|
chai.assert.ok(value < 10);
|
|
55
55
|
|
|
56
56
|
done();
|
|
@@ -87,11 +87,11 @@ describe('Timer, Repeated & Waited', function () {
|
|
|
87
87
|
value = 1234;
|
|
88
88
|
}, 125);
|
|
89
89
|
|
|
90
|
-
|
|
90
|
+
wait(() => {
|
|
91
91
|
waited.stop();
|
|
92
92
|
}, 250);
|
|
93
93
|
|
|
94
|
-
|
|
94
|
+
wait(() => {
|
|
95
95
|
chai.assert.equal(value, 0);
|
|
96
96
|
|
|
97
97
|
done();
|
|
@@ -107,11 +107,11 @@ describe('Timer, Repeated & Waited', function () {
|
|
|
107
107
|
|
|
108
108
|
waited.start();
|
|
109
109
|
|
|
110
|
-
|
|
110
|
+
wait(() => {
|
|
111
111
|
waited.restart();
|
|
112
112
|
}, 250);
|
|
113
113
|
|
|
114
|
-
|
|
114
|
+
wait(() => {
|
|
115
115
|
chai.assert.equal(value, 2);
|
|
116
116
|
|
|
117
117
|
done();
|