summaryrefslogtreecommitdiff
path: root/apps/web/src/stores/keybindings/migrations/index.ts
blob: 64d86ffa4e5389b39cefef0e9ae3bc283d62ef36 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { v2ToV3 } from "./v2-to-v3";
import { v3ToV4 } from "./v3-to-v4";

type MigrationFn = ({ state }: { state: unknown }) => unknown;

/**
 * key = version we're migrating from
 * value = migration function
 */
const migrations: Record<number, MigrationFn> = {
	2: v2ToV3,
	3: v3ToV4,
};

export const CURRENT_VERSION = 4;

export function runMigrations({
	state,
	fromVersion,
}: {
	state: unknown;
	fromVersion: number;
}): unknown {
	let current = state;
	for (let version = fromVersion; version < CURRENT_VERSION; version++) {
		const migrate = migrations[version];
		if (migrate) current = migrate({ state: current });
	}
	return current;
}