From a42f1b3454ecf791ef01d4dd9d67dff8bde87e09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donk=C3=B3=20Istv=C3=A1n?= Date: Fri, 3 Jun 2022 16:02:22 +0200 Subject: [PATCH] Fix throttle behavior to write all changes at once --- CHANGELOG.md | 3 +++ package-lock.json | 5 +++-- package.json | 2 +- src/createPersistoid.ts | 16 ++++++++++------ 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a220afae..3f5f9b0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/package-lock.json b/package-lock.json index c69c6fd6..9e098f88 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,12 @@ { "name": "redux-persist", - "version": "6.1.0", + "version": "6.1.1", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "6.0.0", + "name": "redux-persist", + "version": "6.1.0", "license": "MIT", "devDependencies": { "@babel/core": "^7.15.0", diff --git a/package.json b/package.json index b59b0556..e9e05e0c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/createPersistoid.ts b/src/createPersistoid.ts index 25e1dabb..dc3c5714 100644 --- a/src/createPersistoid.ts +++ b/src/createPersistoid.ts @@ -28,7 +28,7 @@ export default function createPersistoid(config: PersistConfig): Persistoid let lastState: KeyAccessState = {} const stagedState: KeyAccessState = {} const keysToProcess: string[] = [] - let timeIterator: any = null + let writeTimeout: any = null let writePromise: Promise | null = null const update = (state: KeyAccessState) => { @@ -54,8 +54,8 @@ export default function createPersistoid(config: PersistConfig): 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 @@ -63,8 +63,6 @@ export default function createPersistoid(config: PersistConfig): Persistoid function processNextKey() { if (keysToProcess.length === 0) { - if (timeIterator) clearInterval(timeIterator) - timeIterator = null return } @@ -123,10 +121,16 @@ export default function createPersistoid(config: PersistConfig): Persistoid } } - const flush = () => { + function flush() { while (keysToProcess.length !== 0) { processNextKey() } + + if (writeTimeout) { + clearTimeout(writeTimeout) + writeTimeout = null + } + return writePromise || Promise.resolve() }