@oscarpalmer/timer 0.5.0 → 0.6.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 +1 -1
- package/dist/timer.js +9 -10
- package/package.json +3 -3
- package/src/index.d.ts +1 -18
- package/src/index.ts +32 -10
- package/test/test.js +13 -13
package/.eslintrc.json
CHANGED
package/README.md
CHANGED
package/dist/timer.js
CHANGED
|
@@ -93,16 +93,15 @@ var Waited = class extends Timed {
|
|
|
93
93
|
super("waited", callback, time, 1);
|
|
94
94
|
}
|
|
95
95
|
};
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
};
|
|
96
|
+
function repeat(callback, time, count) {
|
|
97
|
+
return new Repeated(callback, time, count).start();
|
|
98
|
+
}
|
|
99
|
+
function wait(callback, time) {
|
|
100
|
+
return new Waited(callback, time).start();
|
|
101
|
+
}
|
|
104
102
|
export {
|
|
105
103
|
Repeated,
|
|
106
|
-
|
|
107
|
-
|
|
104
|
+
Waited,
|
|
105
|
+
repeat,
|
|
106
|
+
wait
|
|
108
107
|
};
|
package/package.json
CHANGED
|
@@ -10,9 +10,9 @@
|
|
|
10
10
|
"@typescript-eslint/eslint-plugin": "^5.30.0",
|
|
11
11
|
"@typescript-eslint/parser": "^5.30.0",
|
|
12
12
|
"esbuild": "^0.14.0",
|
|
13
|
-
"eslint": "^8.
|
|
13
|
+
"eslint": "^8.20.0",
|
|
14
14
|
"eslint-config-xo": "^0.41.0",
|
|
15
|
-
"eslint-config-xo-typescript": "^0.
|
|
15
|
+
"eslint-config-xo-typescript": "^0.52.0",
|
|
16
16
|
"typescript": "^4.7.0"
|
|
17
17
|
},
|
|
18
18
|
"exports": {
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"type": "module",
|
|
44
44
|
"types": "src/index.d.ts",
|
|
45
45
|
"unpkg": "dist/timer.js",
|
|
46
|
-
"version": "0.
|
|
46
|
+
"version": "0.6.0"
|
|
47
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
|
@@ -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();
|