-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Showing
8 changed files
with
124 additions
and
49 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,45 @@ | ||
import type { StoreEnhancer } from 'redux' | ||
|
||
export const autoBatch = 'ReduxToolkit_autoBatch' | ||
|
||
export const autoBatchEnhancer = | ||
(batchTimeout: number = -1): StoreEnhancer => | ||
(next) => | ||
(...args) => { | ||
const store = next(...args) | ||
|
||
let notifying = true | ||
let nextNotification: NodeJS.Timeout | undefined = undefined | ||
const listeners = new Set<() => void>() | ||
const notifyListeners = () => { | ||
nextNotification = void listeners.forEach((l) => l()) | ||
} | ||
|
||
const subscribe: typeof store.subscribe = (listener) => { | ||
const wrappedListener: typeof listener = () => notifying && listener() | ||
const unsubscribe = store.subscribe(wrappedListener) | ||
listeners.add(listener) | ||
return () => { | ||
unsubscribe() | ||
listeners.delete(listener) | ||
} | ||
} | ||
|
||
const dispatch: typeof store.dispatch = (action: any) => { | ||
try { | ||
notifying = !action?.meta?.[autoBatch] | ||
if (notifying) { | ||
if (nextNotification) { | ||
nextNotification = void clearTimeout(nextNotification) | ||
} | ||
} else { | ||
nextNotification ||= setTimeout(notifyListeners, batchTimeout) as any | ||
} | ||
return store.dispatch(action) | ||
} finally { | ||
notifying = true | ||
} | ||
} | ||
|
||
return Object.assign({}, store, { dispatch, subscribe }) | ||
} |
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
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
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