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' }); | |||||
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 |