Skip to content

Commit 83aed07

Browse files
authored
fix: only use initial state if there's no stored state (#92)
1 parent 8a0d627 commit 83aed07

File tree

2 files changed

+12
-23
lines changed

2 files changed

+12
-23
lines changed

.changeset/wide-words-smash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@kohaku-eth/privacy-pools": patch
3+
---
4+
5+
Use snapshot state only if there's no previous stored state

packages/privacy-pools/src/state/state-manager.ts

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ import { ragequitThunk } from "./thunks/ragequitThunk";
5555
import { SyncAspThunkParams } from "./thunks/syncAspThunk";
5656
import { syncThunk } from "./thunks/syncThunk";
5757
import { withdrawThunk } from "./thunks/withdrawThunk";
58-
import { deserialize } from "./utils/serialize.utils";
5958

6059
export interface StoreFactoryParams
6160
extends BaseSelectorParams, SyncAspThunkParams {
@@ -179,27 +178,14 @@ const getStoreStorageKey = (
179178

180179
const storeByChainAndEntrypoint = ({
181180
storageToSyncTo,
182-
initialState,
181+
initialState: initialStateByChainAndEntrypoint = {},
183182
...params
184183
}: Omit<StoreFactoryParams, "dataService">) => {
185-
const initialMapState = Object.entries(initialState || {}).map(
186-
([key, state]) =>
187-
[
188-
key,
189-
initializeSelectors({
190-
...params,
191-
store: storeFactory({
192-
entrypointInfo: deserialize(state!.entrypointInfo),
193-
initialState: state,
194-
}),
195-
}),
196-
] as const,
197-
);
198184

199185
const chainStoreMap = new Map<
200186
string,
201187
ReturnType<typeof initializeSelectors<ReturnType<typeof storeFactory>>>
202-
>(initialMapState);
188+
>();
203189

204190
return {
205191
getChainStore: (getChainStoreParams: GetChainStoreParams) => {
@@ -210,14 +196,12 @@ const storeByChainAndEntrypoint = ({
210196
const computedChainKey = getStoreKey(getChainStoreParams);
211197
let storeWithSelectors = chainStoreMap.get(computedChainKey);
212198

213-
const storedState = storageToSyncTo?.get(
214-
getStoreStorageKey(getChainStoreParams),
215-
);
216-
217199
if (!storeWithSelectors) {
218-
const initialState: RootState | undefined = storedState
219-
? JSON.parse(storedState)
220-
: undefined;
200+
const storageKey = getStoreStorageKey(getChainStoreParams);
201+
const rawStoredState = storageToSyncTo?.get(storageKey);
202+
const storedState: RootState | undefined = rawStoredState ? JSON.parse(rawStoredState) : undefined;
203+
const snapshotInitialState = initialStateByChainAndEntrypoint[storageKey];
204+
const initialState: RootState | undefined = storedState || snapshotInitialState;
221205
const store = storeFactory({
222206
entrypointInfo: {
223207
chainId,

0 commit comments

Comments
 (0)