Skip to content

Commit dcb22ae

Browse files
committed
fix migration bug
1 parent 5d4599d commit dcb22ae

File tree

1 file changed

+78
-23
lines changed

1 file changed

+78
-23
lines changed

ui/frontend/local_storage.ts

+78-23
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,47 @@
44

55
import State from './state';
66
import storage from './storage';
7+
import { AssemblyFlavor, DemangleAssembly, Editor, Orientation, PairCharacters, ProcessAssembly } from './types';
78

89
const CURRENT_VERSION = 2;
910

10-
export function serialize(state: State) {
11-
return JSON.stringify({
11+
type V2Configuration = {
12+
version: 2;
13+
configuration: {
14+
editor: Editor;
15+
keybinding: string;
16+
aceTheme: string;
17+
monacoTheme: string;
18+
pairCharacters: PairCharacters;
19+
orientation: Orientation;
20+
assemblyFlavor: AssemblyFlavor;
21+
demangleAssembly: DemangleAssembly;
22+
processAssembly: ProcessAssembly;
23+
};
24+
code: string;
25+
notifications: any;
26+
};
27+
28+
type V1Configuration = {
29+
version: 1;
30+
configuration: {
31+
editor: "simple" | "advanced";
32+
keybinding: string;
33+
theme: string;
34+
pairCharacters: PairCharacters;
35+
orientation: Orientation;
36+
assemblyFlavor: AssemblyFlavor;
37+
demangleAssembly: DemangleAssembly;
38+
processAssembly: ProcessAssembly;
39+
};
40+
code: string;
41+
notifications: any;
42+
};
43+
44+
type CurrentConfiguration = V2Configuration;
45+
46+
export function serialize(state: State): string {
47+
const conf: CurrentConfiguration = {
1248
version: CURRENT_VERSION,
1349
configuration: {
1450
editor: state.configuration.editor,
@@ -23,31 +59,50 @@ export function serialize(state: State) {
2359
},
2460
code: state.code,
2561
notifications: state.notifications,
26-
});
62+
};
63+
return JSON.stringify(conf);
64+
}
65+
66+
/*
67+
Sample configuration for each version, for debug propose
68+
69+
"{\"version\":1,\"configuration\":{\"editor\":\"advanced\",\"keybinding\":\"ace\",\"theme\":\"github\",\"pairCharacters\":\"enabled\",\"orientation\":\"automatic\",\"assemblyFlavor\":\"att\",\"demangleAssembly\":\"demangle\",\"processAssembly\":\"filter\"},\"code\":\"fn main() {\\n println!(\\\"Hello, world!\\\");\\n}\",\"notifications\":{\"seenRustSurvey2018\":true,\"seenRust2018IsDefault\":true,\"seenRustSurvey2020\":false}}"
70+
"{\"version\":2,\"configuration\":{\"editor\":\"ace\",\"keybinding\":\"ace\",\"aceTheme\":\"github\",\"monacoTheme\":\"vscode-dark-plus\",\"pairCharacters\":\"enabled\",\"orientation\":\"automatic\",\"assemblyFlavor\":\"att\",\"demangleAssembly\":\"demangle\",\"processAssembly\":\"filter\"},\"code\":\"fn main() {\\n println!(\\\"Hello, world!\\\");\\n}\",\"notifications\":{\"seenRustSurvey2018\":true,\"seenRust2018IsDefault\":true,\"seenRustSurvey2020\":false}}"
71+
*/
72+
73+
function migrate1(state: V1Configuration): CurrentConfiguration {
74+
const { theme, editor, ...configuration } = state.configuration;
75+
const step: V2Configuration = {
76+
...state,
77+
configuration: {
78+
...configuration,
79+
aceTheme: theme,
80+
monacoTheme: 'vscode-dark-plus',
81+
editor: editor === 'advanced' ? Editor.Ace : Editor.Simple,
82+
},
83+
version: 2,
84+
};
85+
return migrate2(step);
86+
}
87+
88+
function migrate2(state: V2Configuration): CurrentConfiguration {
89+
return state;
2790
}
2891

29-
export function deserialize(savedState) {
92+
export function deserialize(savedState: string) {
3093
if (!savedState) { return undefined; }
31-
const parsedState = JSON.parse(savedState);
94+
const parsedState: V1Configuration | V2Configuration = JSON.parse(savedState);
3295
if (!parsedState) { return undefined; }
33-
let { version } = parsedState;
34-
delete parsedState.version;
35-
36-
// migrations
37-
if (version === 1) {
38-
if (parsedState.editor === 'advanced') {
39-
parsedState.editor = 'ace';
40-
}
41-
parsedState.aceTheme = parsedState.theme;
42-
parsedState.monacoTheme = 'vscode-dark-plus';
43-
delete parsedState.theme;
44-
version = 2;
45-
}
46-
47-
// This assumes that the keys we serialize with match the keys in the
48-
// live state. If that's no longer true, an additional renaming step
49-
// needs to be added.
50-
return parsedState;
96+
let result: CurrentConfiguration;
97+
98+
if (parsedState.version === 1) result = migrate1(parsedState);
99+
if (parsedState.version === 2) result = migrate2(parsedState);
100+
101+
delete result.version;
102+
103+
// There is some keys that we don't store (why?) so type checker needs to be
104+
// suppressed here
105+
return result as any;
51106
}
52107

53108
export default storage({

0 commit comments

Comments
 (0)