-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f13c40e
commit 33b32c8
Showing
5 changed files
with
93 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@farfetched/core': patch | ||
--- | ||
|
||
Fix sync batching in `keepFresh` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copied and adopted https://github.com/effector/patronum/blob/main/src/debounce/index.ts | ||
|
||
import { | ||
createEvent, | ||
createStore, | ||
type Event, | ||
type EventAsReturnType, | ||
sample, | ||
attach, | ||
} from 'effector'; | ||
|
||
export function syncBatch<T>(clock: Event<T>): EventAsReturnType<T> { | ||
const saveTimeoutId = createEvent<NodeJS.Timeout>(); | ||
|
||
const $timeoutId = createStore<NodeJS.Timeout | null>(null, { | ||
serialize: 'ignore', | ||
}).on(saveTimeoutId, (_, id) => id); | ||
|
||
const saveReject = createEvent<() => void>(); | ||
|
||
const $rejecter = createStore<(() => void) | null>(null, { | ||
serialize: 'ignore', | ||
}).on(saveReject, (_, rj) => rj); | ||
|
||
const tick = createEvent<T>(); | ||
|
||
const timerFx = attach({ | ||
source: { | ||
timeoutId: $timeoutId, | ||
rejectPromise: $rejecter, | ||
}, | ||
effect: ({ timeoutId, rejectPromise }) => { | ||
if (timeoutId) clearTimeout(timeoutId); | ||
if (rejectPromise) rejectPromise(); | ||
return new Promise((resolve, reject) => { | ||
saveReject(reject); | ||
saveTimeoutId(setTimeout(resolve, 0)); | ||
}); | ||
}, | ||
}); | ||
$rejecter.reset(timerFx.done); | ||
$timeoutId.reset(timerFx.done); | ||
|
||
// It's ok - nothing will ever start unless source is triggered | ||
const $payload = createStore<T[]>([], { serialize: 'ignore' }).on( | ||
clock, | ||
(_, payload) => [payload] | ||
); | ||
|
||
const $canTick = createStore(true, { serialize: 'ignore' }); | ||
|
||
const triggerTick = createEvent(); | ||
|
||
$canTick | ||
.on(triggerTick, () => false) | ||
.on( | ||
[ | ||
tick, | ||
// debounce timeout can be restarted in later ticks | ||
timerFx, | ||
], | ||
() => true | ||
); | ||
|
||
sample({ | ||
clock: clock, | ||
filter: $canTick, | ||
target: triggerTick, | ||
}); | ||
|
||
sample({ | ||
clock: triggerTick, | ||
target: timerFx, | ||
}); | ||
|
||
sample({ | ||
source: $payload, | ||
clock: timerFx.done, | ||
fn: ([payload]) => payload, | ||
target: tick, | ||
}); | ||
|
||
return tick; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters