Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3377412
D7658.id26354.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D7658.id26354.diff
View Options
diff --git a/web/redux/create-async-migrate.js b/web/redux/create-async-migrate.js
new file mode 100644
--- /dev/null
+++ b/web/redux/create-async-migrate.js
@@ -0,0 +1,74 @@
+// @flow
+
+import { DEFAULT_VERSION } from 'redux-persist/es/constants.js';
+import type { PersistState } from 'redux-persist/es/types.js';
+
+type MigrationManifest = {
+ +[number | string]: (PersistedState) => Promise<PersistedState>,
+};
+type PersistedState = {
+ +_persist: PersistState,
+ ...
+} | void;
+type ConfigType = {
+ +debug: boolean,
+};
+
+function createAsyncMigrate(
+ migrations: MigrationManifest,
+ config: ConfigType,
+): (state: PersistedState, currentVersion: number) => Promise<PersistedState> {
+ const debug = process.env.NODE_ENV !== 'production' && !!config?.debug;
+ return async function (
+ state: PersistedState,
+ currentVersion: number,
+ ): Promise<PersistedState> {
+ if (!state) {
+ if (debug) {
+ console.log('redux-persist: no inbound state, skipping migration');
+ }
+ return undefined;
+ }
+
+ const inboundVersion: number = state?._persist?.version ?? DEFAULT_VERSION;
+
+ if (inboundVersion === currentVersion) {
+ if (debug) {
+ console.log('redux-persist: versions match, noop migration');
+ }
+ return state;
+ }
+ if (inboundVersion > currentVersion) {
+ if (debug) {
+ console.error('redux-persist: downgrading version is not supported');
+ }
+ return state;
+ }
+
+ const newMigrationKeys = Object.keys(migrations)
+ .map(ver => parseInt(ver))
+ .filter(key => currentVersion >= key && key > inboundVersion);
+ const sortedMigrationKeys = newMigrationKeys.sort((a, b) => a - b);
+
+ if (debug) {
+ console.log('redux-persist: migrationKeys', sortedMigrationKeys);
+ }
+
+ let migratedState: PersistedState = state;
+ for (const versionKey of sortedMigrationKeys) {
+ if (debug) {
+ console.log(
+ 'redux-persist: running migration for versionKey',
+ versionKey,
+ );
+ }
+ if (versionKey) {
+ migratedState = await migrations[versionKey](migratedState);
+ }
+ }
+
+ return migratedState;
+ };
+}
+
+export { createAsyncMigrate };
diff --git a/web/root.js b/web/root.js
--- a/web/root.js
+++ b/web/root.js
@@ -5,7 +5,7 @@
import { Router, Route } from 'react-router';
import { createStore, applyMiddleware, type Store } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension/logOnlyInProduction.js';
-import { createMigrate, persistReducer, persistStore } from 'redux-persist';
+import { persistReducer, persistStore } from 'redux-persist';
import { PersistGate } from 'redux-persist/es/integration/react.js';
import storage from 'redux-persist/es/storage/index.js';
import thunk from 'redux-thunk';
@@ -17,13 +17,14 @@
import { SQLiteDataHandler } from './database/sqlite-data-handler.js';
import ErrorBoundary from './error-boundary.react.js';
import Loading from './loading.react.js';
+import { createAsyncMigrate } from './redux/create-async-migrate.js';
import { reducer } from './redux/redux-setup.js';
import type { AppState, Action } from './redux/redux-setup.js';
import history from './router-history.js';
import Socket from './socket.react.js';
const migrations = {
- [1]: state => {
+ [1]: async state => {
const {
primaryIdentityPublicKey,
...stateWithoutPrimaryIdentityPublicKey
@@ -50,7 +51,7 @@
'notifPermissionAlertInfo',
'commServicesAccessToken',
],
- migrate: (createMigrate(migrations, { debug: isDev }): any),
+ migrate: (createAsyncMigrate(migrations, { debug: isDev }): any),
version: 1,
};
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Nov 28, 5:33 AM (19 h, 54 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2592927
Default Alt Text
D7658.id26354.diff (3 KB)
Attached To
Mode
D7658: [web-db] implement asynchronous `redux-persist` migrations
Attached
Detach File
Event Timeline
Log In to Comment