Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix throttle behavior to write all changes at once #1388

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project (after v6.1.0) should be documented in this
The format is (mostly) based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [6.1.1] - 2022-06-03
Change throttle behavior to write all modified keys after one delay instead of
waiting the set amount of time between each key.

## [6.1.0] - 2021-10-17
Thanks to [@smellman](https://github.com/smellman) for the TypeScript updates.
Expand Down
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-persist",
"version": "6.1.0",
"version": "6.1.1",
"description": "persist and rehydrate redux stores",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down
16 changes: 10 additions & 6 deletions src/createPersistoid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function createPersistoid(config: PersistConfig<any>): Persistoid
let lastState: KeyAccessState = {}
const stagedState: KeyAccessState = {}
const keysToProcess: string[] = []
let timeIterator: any = null
let writeTimeout: any = null
let writePromise: Promise<any> | null = null

const update = (state: KeyAccessState) => {
Expand All @@ -54,17 +54,15 @@ export default function createPersistoid(config: PersistConfig<any>): Persistoid
})

// start the time iterator if not running (read: throttle)
if (timeIterator === null) {
timeIterator = setInterval(processNextKey, throttle)
if (writeTimeout === null) {
writeTimeout = setTimeout(flush, throttle)
}

lastState = state
}

function processNextKey() {
if (keysToProcess.length === 0) {
if (timeIterator) clearInterval(timeIterator)
timeIterator = null
return
}

Expand Down Expand Up @@ -123,10 +121,16 @@ export default function createPersistoid(config: PersistConfig<any>): Persistoid
}
}

const flush = () => {
function flush() {
while (keysToProcess.length !== 0) {
processNextKey()
}

if (writeTimeout) {
clearTimeout(writeTimeout)
writeTimeout = null
}

return writePromise || Promise.resolve()
}

Expand Down