Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor migrate util into persistOptions util #1523

Merged
merged 4 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/core/state/favorites/index.ts
Expand Up @@ -43,7 +43,7 @@ import {
} from '~/core/references';
import { AddressOrEth } from '~/core/types/assets';
import { ChainId } from '~/core/types/chains';
import { migrate } from '~/core/utils/migrate';
import { persistOptions } from '~/core/utils/persistOptions';

import { createStore } from '../internal/createStore';

Expand Down Expand Up @@ -149,10 +149,10 @@ export const favoritesStore = createStore<FavoritesState>(
},
}),
{
persist: {
persist: persistOptions({
name: 'favorites',
version: 4,
migrate: migrate(
migrations: [
// version 1 didn't need a migration
(state: FavoritesState) => state,
// version 2 added avalanche
Expand All @@ -164,8 +164,8 @@ export const favoritesStore = createStore<FavoritesState>(
// version 4 added degen
(state) =>
mergeNewOfficiallySupportedChainsState(state, [ChainId.degen]),
),
},
],
}),
},
);

Expand Down
50 changes: 30 additions & 20 deletions src/core/state/rainbowChains/index.ts
Expand Up @@ -7,7 +7,7 @@ import {
chainHardhat,
chainHardhatOptimism,
} from '~/core/types/chains';
import { migrate } from '~/core/utils/migrate';
import { persistOptions } from '~/core/utils/persistOptions';

import { createStore } from '../internal/createStore';

Expand Down Expand Up @@ -218,32 +218,42 @@ export const rainbowChainsStore = createStore<RainbowChainsState>(
},
}),
{
persist: {
persist: persistOptions({
name: 'rainbowChains',
version: 6,
migrate: migrate(
migrations: [
// v1 didn't have need a migration
function v1(s: RainbowChainsState) {
return s;
},

// version 2 added support for Avalanche and Avalanche Fuji
(state) =>
mergeNewOfficiallySupportedChainsState(state, [
ChainId.avalanche,
ChainId.avalancheFuji,
]),
// version 2 added support for Blast
(state: RainbowChainsState) =>
mergeNewOfficiallySupportedChainsState(state, [
function v2(state) {
return mergeNewOfficiallySupportedChainsState(state, [
ChainId.avalanche,
ChainId.avalancheFuji,
]),
(state) =>
removeCustomRPC({
]);
},

// version 3 added support for Blast
function v3(state) {
return mergeNewOfficiallySupportedChainsState(state, [ChainId.blast]);
},

function v4(state) {
return removeCustomRPC({
state,
rpcUrl: 'https://rpc.zora.co',
rainbowChains: state.rainbowChains,
}),
});
},

// version 5 added support for Degen
(state: RainbowChainsState) =>
mergeNewOfficiallySupportedChainsState(state, [ChainId.degen]),
(state: RainbowChainsState) => {
function v5(state: RainbowChainsState) {
return mergeNewOfficiallySupportedChainsState(state, [ChainId.degen]);
},

function v6(state: RainbowChainsState) {
if (
!state.rainbowChains[zora.id] ||
state.rainbowChains[zora.id]?.chains.length === 0
Expand All @@ -252,8 +262,8 @@ export const rainbowChainsStore = createStore<RainbowChainsState>(
}
return state;
},
),
},
],
}),
},
);

Expand Down
39 changes: 0 additions & 39 deletions src/core/utils/migrate.ts

This file was deleted.

125 changes: 125 additions & 0 deletions src/core/utils/persistOptions.ts
@@ -0,0 +1,125 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { PersistOptions } from 'zustand/middleware';

type Opts<T, Migrations> = PersistOptions<T> & {
version: number;
migrations: Migrations;
};

type R<T> = PersistOptions<T> & {
version: number;
migrate: (persistedState: unknown, version: number) => T;
};

interface PersistOptionsWithMigrations {
<Final>(opts: Opts<Final, [(s: any) => Final]>): R<Final>;
<Final, A>(opts: Opts<Final, [(s: any) => A, (s: A) => Final]>): R<Final>;
<Final, A, B>(
opts: Opts<Final, [(s: any) => A, (s: A) => B, (s: B) => Final]>,
): R<Final>;
<Final, A, B, C>(
opts: Opts<
Final,
[(s: any) => A, (s: A) => B, (s: B) => C, (s: C) => Final]
>,
): R<Final>;
<Final, A, B, C, D>(
opts: Opts<
Final,
[(s: any) => A, (s: A) => B, (s: B) => C, (s: C) => D, (s: D) => Final]
>,
): R<Final>;
<Final, A, B, C, D, E>(
opts: Opts<
Final,
[
(s: any) => A,
(s: A) => B,
(s: B) => C,
(s: C) => D,
(s: D) => E,
(s: E) => Final,
]
>,
): R<Final>;
<Final, A, B, C, D, E, F>(
opts: Opts<
Final,
[
(s: any) => A,
(s: A) => B,
(s: B) => C,
(s: C) => D,
(s: D) => E,
(s: E) => F,
(s: F) => Final,
]
>,
): R<Final>;
<Final, A, B, C, D, E, F, G>(
opts: Opts<
Final,
[
(s: any) => A,
(s: A) => B,
(s: B) => C,
(s: C) => D,
(s: D) => E,
(s: E) => F,
(s: F) => G,
(s: G) => Final,
]
>,
): R<Final>;
<Final, A, B, C, D, E, F, G, H>(
opts: Opts<
Final,
[
(s: any) => A,
(s: A) => B,
(s: B) => C,
(s: C) => D,
(s: D) => E,
(s: E) => F,
(s: F) => G,
(s: G) => H,
(s: H) => Final,
]
>,
): R<Final>;

// if you need more migrations, add more overloads here
}

/**
* Migrate a persisted state through a series of migrations, piping the result of each migration to the next.
*
* for example if user has persisted state from **version 1** and we are now at **version 3**, this will migrate the state from version **1** to **2** and then to **3**
* - migrations must be in order
* - zustand persister version must be an integer
*/
export const persistOptions: PersistOptionsWithMigrations = <TState>({
version: storeVersion,
migrations,
...persistOptions
}: PersistOptions<TState> & {
version: number;
migrations: ((s: unknown) => unknown)[];
}) => {
if (migrations && migrations.length !== storeVersion) {
throw new Error(`
!! Review the migrations !!
(store name: ${persistOptions.name})
`);
}
Comment on lines +110 to +115
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the cool part


return {
version: storeVersion,
migrate: (persistedState: unknown, userVersion: number) =>
migrations
.toSpliced(0, userVersion) // remove migration before user version, as they should be already applied
.reduce((acc, fn) => fn(acc), persistedState),
...persistOptions,
};
};