Skip to content

Commit aca8b07

Browse files
committed
feat: Throttle state serialization
The storage write debounce has been replaced with the `redux-persist` option `throttle`. This option has the advantage of applying to state serialization as well as writing, so we avoid frequent expensive serialization. Using `throttle` instead of `debounce` also guarantees a write every so often during periods of rapid state updates, ensuring the application doesn't go too long without persisting.
1 parent 90f19f4 commit aca8b07

File tree

1 file changed

+2
-9
lines changed

1 file changed

+2
-9
lines changed

app/store/persistConfig.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,12 @@ import { version, migrations } from './migrations';
77
import Logger from '../util/Logger';
88
import Device from '../util/device';
99
import { UserState } from '../reducers/user';
10-
import { debounce } from 'lodash';
1110
import Engine, { EngineContext } from '../core/Engine';
1211
import { getPersistentState } from '@metamask/base-controller';
1312

1413
const TIMEOUT = 40000;
1514
const STORAGE_DEBOUNCE_DELAY = 200;
1615

17-
const debouncedSetItem = debounce(
18-
async (key: string, value: string) =>
19-
await FilesystemStorage.setItem(key, value, Device.isIos()),
20-
STORAGE_DEBOUNCE_DELAY,
21-
{ leading: false, trailing: true },
22-
);
23-
2416
const MigratedStorage = {
2517
async getItem(key: string) {
2618
try {
@@ -49,7 +41,7 @@ const MigratedStorage = {
4941
},
5042
async setItem(key: string, value: string) {
5143
try {
52-
return await debouncedSetItem(key, value);
44+
return await FilesystemStorage.setItem(key, value, Device.isIos());
5345
} catch (error) {
5446
Logger.error(error as Error, {
5547
message: `Failed to set item for ${key}`,
@@ -139,6 +131,7 @@ const persistConfig = {
139131
stateReconciler: autoMergeLevel2, // see "Merge Process" section for details.
140132
migrate: createMigrate(migrations, { debug: false }),
141133
timeout: TIMEOUT,
134+
throttle: STORAGE_DEBOUNCE_DELAY,
142135
writeFailHandler: (error: Error) =>
143136
Logger.error(error, { message: 'Error persisting data' }), // Log error if saving state fails
144137
};

0 commit comments

Comments
 (0)