@@ -2,12 +2,19 @@ import { expect } from 'aegir/chai'
2
2
import delay from 'delay'
3
3
import pDefer from 'p-defer'
4
4
import { repeatingTask } from '../src/repeating-task.js'
5
+ import type { RepeatingTask } from '../src/repeating-task.js'
5
6
6
7
describe ( 'repeating-task' , ( ) => {
8
+ let task : RepeatingTask
9
+
10
+ afterEach ( ( ) => {
11
+ task ?. stop ( )
12
+ } )
13
+
7
14
it ( 'should repeat a task' , async ( ) => {
8
15
let count = 0
9
16
10
- const task = repeatingTask ( ( ) => {
17
+ task = repeatingTask ( ( ) => {
11
18
count ++
12
19
} , 100 )
13
20
task . start ( )
@@ -22,7 +29,7 @@ describe('repeating-task', () => {
22
29
it ( 'should run a task immediately' , async ( ) => {
23
30
let count = 0
24
31
25
- const task = repeatingTask ( ( ) => {
32
+ task = repeatingTask ( ( ) => {
26
33
count ++
27
34
} , 60000 , {
28
35
runImmediately : true
@@ -31,15 +38,13 @@ describe('repeating-task', () => {
31
38
32
39
await delay ( 10 )
33
40
34
- task . stop ( )
35
-
36
41
expect ( count ) . to . equal ( 1 )
37
42
} )
38
43
39
44
it ( 'should time out a task' , async ( ) => {
40
45
const deferred = pDefer ( )
41
46
42
- const task = repeatingTask ( ( opts ) => {
47
+ task = repeatingTask ( ( opts ) => {
43
48
opts ?. signal ?. addEventListener ( 'abort' , ( ) => {
44
49
deferred . resolve ( )
45
50
} )
@@ -49,29 +54,26 @@ describe('repeating-task', () => {
49
54
task . start ( )
50
55
51
56
await deferred . promise
52
- task . stop ( )
53
57
} )
54
58
55
59
it ( 'should repeat a task that throws' , async ( ) => {
56
60
let count = 0
57
61
58
- const task = repeatingTask ( ( ) => {
62
+ task = repeatingTask ( ( ) => {
59
63
count ++
60
64
throw new Error ( 'Urk!' )
61
65
} , 100 )
62
66
task . start ( )
63
67
64
68
await delay ( 1000 )
65
69
66
- task . stop ( )
67
-
68
70
expect ( count ) . to . be . greaterThan ( 1 )
69
71
} )
70
72
71
73
it ( 'should update the interval of a task' , async ( ) => {
72
74
let count = 0
73
75
74
- const task = repeatingTask ( ( ) => {
76
+ task = repeatingTask ( ( ) => {
75
77
count ++
76
78
77
79
if ( count === 1 ) {
@@ -82,15 +84,13 @@ describe('repeating-task', () => {
82
84
83
85
await delay ( 1000 )
84
86
85
- task . stop ( )
86
-
87
87
expect ( count ) . to . equal ( 1 )
88
88
} )
89
89
90
90
it ( 'should update the timeout of a task' , async ( ) => {
91
91
let count = 0
92
92
93
- const task = repeatingTask ( async ( options ) => {
93
+ task = repeatingTask ( async ( options ) => {
94
94
// simulate a delay
95
95
await delay ( 100 )
96
96
@@ -109,15 +109,13 @@ describe('repeating-task', () => {
109
109
110
110
await delay ( 1000 )
111
111
112
- task . stop ( )
113
-
114
112
expect ( count ) . to . equal ( 1 )
115
113
} )
116
114
117
115
it ( 'should not reschedule the task if the interval is updated to the same value' , async ( ) => {
118
116
let count = 0
119
117
120
- const task = repeatingTask ( ( ) => {
118
+ task = repeatingTask ( ( ) => {
121
119
count ++
122
120
} , 1_000 , {
123
121
runImmediately : true
@@ -134,7 +132,69 @@ describe('repeating-task', () => {
134
132
135
133
await delay ( 100 )
136
134
137
- task . stop ( )
135
+ expect ( count ) . to . equal ( 2 )
136
+ } )
137
+
138
+ it ( 'should allow interrupting the timeout to run the task immediately' , async ( ) => {
139
+ let count = 0
140
+
141
+ task = repeatingTask ( ( ) => {
142
+ count ++
143
+ } , 1_000 )
144
+ task . start ( )
145
+
146
+ // run immediately
147
+ task . run ( )
148
+
149
+ // less than the repeat interval
150
+ await delay ( 200 )
151
+
152
+ expect ( count ) . to . equal ( 1 )
153
+ } )
154
+
155
+ it ( 'should debounce interrupting the timeout to run the task immediately' , async ( ) => {
156
+ let count = 0
157
+
158
+ task = repeatingTask ( ( ) => {
159
+ count ++
160
+ } , 1_000 , {
161
+ debounce : 10
162
+ } )
163
+ task . start ( )
164
+
165
+ // run immediately
166
+ task . run ( )
167
+ task . run ( )
168
+ task . run ( )
169
+ task . run ( )
170
+ task . run ( )
171
+
172
+ // less than the repeat interval
173
+ await delay ( 50 )
174
+
175
+ expect ( count ) . to . equal ( 1 )
176
+ } )
177
+
178
+ it ( 'should schedule re-running the task after interrupting the timeout' , async ( ) => {
179
+ let count = 0
180
+
181
+ task = repeatingTask ( ( ) => {
182
+ count ++
183
+ } , 100 , {
184
+ debounce : 10
185
+ } )
186
+ task . start ( )
187
+
188
+ // run immediately
189
+ task . run ( )
190
+
191
+ // less than the repeat interval
192
+ await delay ( 50 )
193
+
194
+ expect ( count ) . to . equal ( 1 )
195
+
196
+ // wait longer than the repeat interval
197
+ await delay ( 150 )
138
198
139
199
expect ( count ) . to . equal ( 2 )
140
200
} )
0 commit comments