4
4
5
5
import State from './state' ;
6
6
import storage from './storage' ;
7
+ import { AssemblyFlavor , DemangleAssembly , Editor , Orientation , PairCharacters , ProcessAssembly } from './types' ;
7
8
8
9
const CURRENT_VERSION = 2 ;
9
10
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 = {
12
48
version : CURRENT_VERSION ,
13
49
configuration : {
14
50
editor : state . configuration . editor ,
@@ -23,31 +59,50 @@ export function serialize(state: State) {
23
59
} ,
24
60
code : state . code ,
25
61
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 ;
27
90
}
28
91
29
- export function deserialize ( savedState ) {
92
+ export function deserialize ( savedState : string ) {
30
93
if ( ! savedState ) { return undefined ; }
31
- const parsedState = JSON . parse ( savedState ) ;
94
+ const parsedState : V1Configuration | V2Configuration = JSON . parse ( savedState ) ;
32
95
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 ;
51
106
}
52
107
53
108
export default storage ( {
0 commit comments