File tree Expand file tree Collapse file tree 3 files changed +34
-4
lines changed Expand file tree Collapse file tree 3 files changed +34
-4
lines changed Original file line number Diff line number Diff line change 7
7
createEvent ,
8
8
createStore ,
9
9
sample ,
10
+ createWatch ,
10
11
} from 'effector' ;
11
12
import { wait , watch } from '../../test-library' ;
12
13
@@ -203,4 +204,31 @@ describe('edge cases', () => {
203
204
expect ( listener ) . toBeCalledTimes ( 1 ) ;
204
205
expect ( listener ) . toBeCalledWith ( 'two' ) ;
205
206
} ) ;
207
+
208
+ test ( 'no trigger call, but timeout change on the fly' , async ( ) => {
209
+ const trigger = createEvent ( ) ;
210
+
211
+ const changeTimeout = createEvent < number > ( ) ;
212
+ const $timeout = createStore ( 40 ) . on ( changeTimeout , ( _ , x ) => x ) ;
213
+ const db = debounce ( { source : trigger , timeout : $timeout } ) ;
214
+
215
+ const listener = jest . fn ( ) ;
216
+ const triggerListener = jest . fn ( ) ;
217
+ const scope = fork ( )
218
+ createWatch ( {
219
+ unit : db ,
220
+ fn : listener ,
221
+ scope,
222
+ } )
223
+ createWatch ( {
224
+ unit : trigger ,
225
+ fn : triggerListener ,
226
+ scope,
227
+ } )
228
+
229
+ await allSettled ( changeTimeout , { scope, params : 10 } ) ;
230
+
231
+ expect ( listener ) . toBeCalledTimes ( 0 ) ;
232
+ expect ( triggerListener ) . toBeCalledTimes ( 0 ) ;
233
+ } )
206
234
} ) ;
Original file line number Diff line number Diff line change @@ -84,6 +84,7 @@ export function debounce<T>({
84
84
} ) ;
85
85
} ) ;
86
86
const timerFx = attach ( {
87
+ name : `debounce(${ source . shortName || source . kind } ) effect` ,
87
88
source : {
88
89
timeoutId : $timeoutId ,
89
90
rejectPromise : $rejecter ,
@@ -126,7 +127,7 @@ export function debounce<T>({
126
127
const requestTick = merge ( [
127
128
source ,
128
129
// debounce timeout is restarted on timeout change
129
- $timeout ,
130
+ guard ( { clock : $timeout , filter : timerFx . pending } ) ,
130
131
] ) ;
131
132
132
133
guard ( {
Original file line number Diff line number Diff line change @@ -37,9 +37,10 @@ export function throttle<T>({
37
37
38
38
const $timeout = toStoreNumber ( timeout ) ;
39
39
40
- const timerFx = createEffect < number , void > (
41
- ( timeout ) => new Promise ( ( resolve ) => setTimeout ( resolve , timeout ) ) ,
42
- ) ;
40
+ const timerFx = createEffect < number , void > ( {
41
+ name : `throttle(${ ( source as Event < T > ) . shortName || source . kind } ) effect` ,
42
+ handler : ( timeout ) => new Promise ( ( resolve ) => setTimeout ( resolve , timeout ) ) ,
43
+ } ) ;
43
44
44
45
// It's ok - nothing will ever start unless source is triggered
45
46
const $payload = createStore < T > ( null as unknown as T , { serialize : 'ignore' } ) . on (
You can’t perform that action at this time.
0 commit comments