Changeset View
Changeset View
Standalone View
Standalone View
web/redux/create-async-migrate.js
// @flow | // @flow | ||||
import { getStoredState, purgeStoredState } from 'redux-persist'; | |||||
import { DEFAULT_VERSION } from 'redux-persist/es/constants.js'; | import { DEFAULT_VERSION } from 'redux-persist/es/constants.js'; | ||||
import storage from 'redux-persist/es/storage/index.js'; | |||||
import type { PersistState } from 'redux-persist/es/types.js'; | import type { PersistState } from 'redux-persist/es/types.js'; | ||||
import { databaseModule } from '../database/database-module-provider.js'; | |||||
type MigrationManifest = { | type MigrationManifest = { | ||||
+[number | string]: (PersistedState) => Promise<PersistedState>, | +[number | string]: (PersistedState) => Promise<PersistedState>, | ||||
}; | }; | ||||
type PersistedState = { | type PersistedState = { | ||||
+_persist: PersistState, | +_persist: PersistState, | ||||
... | ... | ||||
} | void; | } | void; | ||||
type ConfigType = { | type ConfigType = { | ||||
+debug: boolean, | +debug: boolean, | ||||
}; | }; | ||||
function createAsyncMigrate( | function createAsyncMigrate( | ||||
migrations: MigrationManifest, | migrations: MigrationManifest, | ||||
config: ConfigType, | config: ConfigType, | ||||
): (state: PersistedState, currentVersion: number) => Promise<PersistedState> { | ): (state: PersistedState, currentVersion: number) => Promise<PersistedState> { | ||||
const debug = process.env.NODE_ENV !== 'production' && !!config?.debug; | const debug = process.env.NODE_ENV !== 'production' && !!config?.debug; | ||||
return async function ( | return async function ( | ||||
state: PersistedState, | state: PersistedState, | ||||
currentVersion: number, | currentVersion: number, | ||||
): Promise<PersistedState> { | ): Promise<PersistedState> { | ||||
if (!state) { | if (!state) { | ||||
const isSupported = await databaseModule.isDatabaseSupported(); | |||||
if (!isSupported) { | |||||
if (debug) { | if (debug) { | ||||
console.log('redux-persist: no inbound state, skipping migration'); | console.log('redux-persist: no inbound state, skipping migration'); | ||||
} | } | ||||
return undefined; | return undefined; | ||||
} | } | ||||
const oldStorage = await getStoredState({ storage, key: 'root' }); | |||||
if (!oldStorage) { | |||||
return undefined; | |||||
} | |||||
state = oldStorage; | |||||
purgeStoredState({ storage, key: 'root' }); | |||||
tomek: Is it possible that something goes wrong (e.g. an exception is thrown) and we remove old state… | |||||
kamilAuthorUnsubmitted Done Inline ActionsIn this case, I don't consider this as an issue. This code is responsible only for reading persisted data into memory (and changing it if there are some migrations) and then with the next modifications data will be persisted in the new engine (that being said I don't think we should leave this data here, and we can purge this). Problems with saving data into new storage are separate things which mean something is wrong with the database and we should fetch new data for the user - in this case, it'll be reloaded. kamil: In this case, I don't consider this as an issue. This code is responsible only for reading… | |||||
if (debug) { | |||||
console.log('redux-persist: migrating state to SQLite storage'); | |||||
} | |||||
} | |||||
const inboundVersion: number = state?._persist?.version ?? DEFAULT_VERSION; | const inboundVersion: number = state?._persist?.version ?? DEFAULT_VERSION; | ||||
if (inboundVersion === currentVersion) { | if (inboundVersion === currentVersion) { | ||||
if (debug) { | if (debug) { | ||||
console.log('redux-persist: versions match, noop migration'); | console.log('redux-persist: versions match, noop migration'); | ||||
} | } | ||||
return state; | return state; | ||||
} | } | ||||
Show All 34 Lines |
Is it possible that something goes wrong (e.g. an exception is thrown) and we remove old state before saving it to a new place? Can we purge only after the successful migration to SQLite?