@@ -2,12 +2,19 @@ import { expect } from 'aegir/chai'
22import delay from 'delay'
33import pDefer from 'p-defer'
44import { repeatingTask } from '../src/repeating-task.js'
5+ import type { RepeatingTask } from '../src/repeating-task.js'
56
67describe ( 'repeating-task' , ( ) => {
8+ let task : RepeatingTask
9+
10+ afterEach ( ( ) => {
11+ task ?. stop ( )
12+ } )
13+
714 it ( 'should repeat a task' , async ( ) => {
815 let count = 0
916
10- const task = repeatingTask ( ( ) => {
17+ task = repeatingTask ( ( ) => {
1118 count ++
1219 } , 100 )
1320 task . start ( )
@@ -22,7 +29,7 @@ describe('repeating-task', () => {
2229 it ( 'should run a task immediately' , async ( ) => {
2330 let count = 0
2431
25- const task = repeatingTask ( ( ) => {
32+ task = repeatingTask ( ( ) => {
2633 count ++
2734 } , 60000 , {
2835 runImmediately : true
@@ -31,15 +38,13 @@ describe('repeating-task', () => {
3138
3239 await delay ( 10 )
3340
34- task . stop ( )
35-
3641 expect ( count ) . to . equal ( 1 )
3742 } )
3843
3944 it ( 'should time out a task' , async ( ) => {
4045 const deferred = pDefer ( )
4146
42- const task = repeatingTask ( ( opts ) => {
47+ task = repeatingTask ( ( opts ) => {
4348 opts ?. signal ?. addEventListener ( 'abort' , ( ) => {
4449 deferred . resolve ( )
4550 } )
@@ -49,29 +54,26 @@ describe('repeating-task', () => {
4954 task . start ( )
5055
5156 await deferred . promise
52- task . stop ( )
5357 } )
5458
5559 it ( 'should repeat a task that throws' , async ( ) => {
5660 let count = 0
5761
58- const task = repeatingTask ( ( ) => {
62+ task = repeatingTask ( ( ) => {
5963 count ++
6064 throw new Error ( 'Urk!' )
6165 } , 100 )
6266 task . start ( )
6367
6468 await delay ( 1000 )
6569
66- task . stop ( )
67-
6870 expect ( count ) . to . be . greaterThan ( 1 )
6971 } )
7072
7173 it ( 'should update the interval of a task' , async ( ) => {
7274 let count = 0
7375
74- const task = repeatingTask ( ( ) => {
76+ task = repeatingTask ( ( ) => {
7577 count ++
7678
7779 if ( count === 1 ) {
@@ -82,15 +84,13 @@ describe('repeating-task', () => {
8284
8385 await delay ( 1000 )
8486
85- task . stop ( )
86-
8787 expect ( count ) . to . equal ( 1 )
8888 } )
8989
9090 it ( 'should update the timeout of a task' , async ( ) => {
9191 let count = 0
9292
93- const task = repeatingTask ( async ( options ) => {
93+ task = repeatingTask ( async ( options ) => {
9494 // simulate a delay
9595 await delay ( 100 )
9696
@@ -109,15 +109,13 @@ describe('repeating-task', () => {
109109
110110 await delay ( 1000 )
111111
112- task . stop ( )
113-
114112 expect ( count ) . to . equal ( 1 )
115113 } )
116114
117115 it ( 'should not reschedule the task if the interval is updated to the same value' , async ( ) => {
118116 let count = 0
119117
120- const task = repeatingTask ( ( ) => {
118+ task = repeatingTask ( ( ) => {
121119 count ++
122120 } , 1_000 , {
123121 runImmediately : true
@@ -134,7 +132,69 @@ describe('repeating-task', () => {
134132
135133 await delay ( 100 )
136134
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 )
138198
139199 expect ( count ) . to . equal ( 2 )
140200 } )
0 commit comments