Changeset View
Changeset View
Standalone View
Standalone View
web/redux/persist.js
Show All 31 Lines | |||||
import commReduxStorageEngine from './comm-redux-storage-engine.js'; | import commReduxStorageEngine from './comm-redux-storage-engine.js'; | ||||
import type { AppState } from './redux-setup.js'; | import type { AppState } from './redux-setup.js'; | ||||
import { getDatabaseModule } from '../database/database-module-provider.js'; | import { getDatabaseModule } from '../database/database-module-provider.js'; | ||||
import { isSQLiteSupported } from '../database/utils/db-utils.js'; | import { isSQLiteSupported } from '../database/utils/db-utils.js'; | ||||
import { workerRequestMessageTypes } from '../types/worker-types.js'; | import { workerRequestMessageTypes } from '../types/worker-types.js'; | ||||
declare var preloadedState: AppState; | declare var preloadedState: AppState; | ||||
const initiallyLoggedInUserID = preloadedState.currentUserInfo?.anonymous | |||||
? undefined | |||||
: preloadedState.currentUserInfo?.id; | |||||
const isDatabaseSupported = isSQLiteSupported(initiallyLoggedInUserID); | |||||
const migrations = { | const migrations = { | ||||
[1]: async state => { | [1]: async state => { | ||||
const { | const { | ||||
primaryIdentityPublicKey, | primaryIdentityPublicKey, | ||||
...stateWithoutPrimaryIdentityPublicKey | ...stateWithoutPrimaryIdentityPublicKey | ||||
} = state; | } = state; | ||||
return { | return { | ||||
...stateWithoutPrimaryIdentityPublicKey, | ...stateWithoutPrimaryIdentityPublicKey, | ||||
cryptoStore: { | cryptoStore: { | ||||
primaryAccount: null, | primaryAccount: null, | ||||
primaryIdentityKeys: null, | primaryIdentityKeys: null, | ||||
notificationAccount: null, | notificationAccount: null, | ||||
notificationIdentityKeys: null, | notificationIdentityKeys: null, | ||||
}, | }, | ||||
}; | }; | ||||
}, | }, | ||||
[2]: async state => { | [2]: async state => { | ||||
const databaseModule = await getDatabaseModule(); | const databaseModule = await getDatabaseModule(); | ||||
const isDatabaseSupported = await databaseModule.isDatabaseSupported(); | |||||
if (!isDatabaseSupported) { | if (!isDatabaseSupported) { | ||||
return state; | return state; | ||||
} | } | ||||
const { drafts } = state.draftStore; | const { drafts } = state.draftStore; | ||||
const draftStoreOperations = []; | const draftStoreOperations = []; | ||||
for (const key in drafts) { | for (const key in drafts) { | ||||
const text = drafts[key]; | const text = drafts[key]; | ||||
Show All 15 Lines | [3]: async (state: AppState) => { | ||||
if (state.draftStore) { | if (state.draftStore) { | ||||
newState = { | newState = { | ||||
...newState, | ...newState, | ||||
draftStore: convertDraftStoreToNewIDSchema(state.draftStore), | draftStore: convertDraftStoreToNewIDSchema(state.draftStore), | ||||
}; | }; | ||||
} | } | ||||
const databaseModule = await getDatabaseModule(); | const databaseModule = await getDatabaseModule(); | ||||
const isDatabaseSupported = await databaseModule.isDatabaseSupported(); | |||||
if (!isDatabaseSupported) { | if (!isDatabaseSupported) { | ||||
return newState; | return newState; | ||||
} | } | ||||
const stores = await databaseModule.schedule({ | const stores = await databaseModule.schedule({ | ||||
type: workerRequestMessageTypes.GET_CLIENT_STORE, | type: workerRequestMessageTypes.GET_CLIENT_STORE, | ||||
}); | }); | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | if (!oldStorage) { | ||||
return undefined; | return undefined; | ||||
} | } | ||||
purgeStoredState({ storage, key: rootKey }); | purgeStoredState({ storage, key: rootKey }); | ||||
if (debug) { | if (debug) { | ||||
console.log('redux-persist: migrating state to SQLite storage'); | console.log('redux-persist: migrating state to SQLite storage'); | ||||
} | } | ||||
// We need to simulate the keyserverStoreTransform for data stored in the | |||||
// old local storage (because redux persist will only run it for the | |||||
// sqlite storage which is empty in this case). | |||||
// We don't just use keyserverStoreTransform.out(oldStorage) because | |||||
// the transform might change in the future, but we need to treat | |||||
// this code like migration code (it shouldn't change). | |||||
if (oldStorage?._persist?.version === 4) { | |||||
const { connection, updatesCurrentAsOf, sessionID } = | |||||
preloadedState.keyserverStore.keyserverInfos[ashoatKeyserverID]; | |||||
return { | |||||
...oldStorage, | |||||
keyserverStore: { | |||||
...oldStorage.keyserverStore, | |||||
keyserverInfos: { | |||||
...oldStorage.keyserverStore.keyserverInfos, | |||||
[ashoatKeyserverID]: { | |||||
...oldStorage.keyserverStore.keyserverInfos[ashoatKeyserverID], | |||||
connection, | |||||
updatesCurrentAsOf, | |||||
sessionID, | |||||
}, | |||||
}, | |||||
}, | |||||
}; | |||||
} | |||||
return oldStorage; | return oldStorage; | ||||
}; | }; | ||||
type PersistedKeyserverInfo = $Diff< | type PersistedKeyserverInfo = $Diff< | ||||
KeyserverInfo, | KeyserverInfo, | ||||
{ | { | ||||
+connection: ConnectionInfo, | +connection: ConnectionInfo, | ||||
+updatesCurrentAsOf: number, | +updatesCurrentAsOf: number, | ||||
Show All 34 Lines | (state: PersistedKeyserverStore): KeyserverStore => { | ||||
}; | }; | ||||
}, | }, | ||||
{ whitelist: ['keyserverStore'] }, | { whitelist: ['keyserverStore'] }, | ||||
); | ); | ||||
const persistConfig: PersistConfig = { | const persistConfig: PersistConfig = { | ||||
key: rootKey, | key: rootKey, | ||||
storage: commReduxStorageEngine, | storage: commReduxStorageEngine, | ||||
whitelist: isDatabaseSupported | whitelist: isSQLiteSupported() | ||||
? persistWhitelist | ? persistWhitelist | ||||
: [...persistWhitelist, 'draftStore'], | : [...persistWhitelist, 'draftStore'], | ||||
migrate: (createAsyncMigrate( | migrate: (createAsyncMigrate( | ||||
migrations, | migrations, | ||||
{ debug: isDev }, | { debug: isDev }, | ||||
migrateStorageToSQLite, | migrateStorageToSQLite, | ||||
): any), | ): any), | ||||
version: 4, | version: 4, | ||||
transforms: [keyserverStoreTransform], | transforms: [keyserverStoreTransform], | ||||
}; | }; | ||||
export { persistConfig }; | export { persistConfig }; |