@@ -43,35 +43,117 @@ function initInfo(infoKey, description) {
43
43
}
44
44
45
45
if ( object . session ) {
46
- const sessionDefaults = object . session ;
46
+ const sessionSpecs = object . session ;
47
47
48
48
object . session = { } ;
49
49
50
- for ( const [ key , defaultValue ] of Object . entries ( sessionDefaults ) ) {
50
+ for ( const [ key , spec ] of Object . entries ( sessionSpecs ) ) {
51
+ const hasSpec =
52
+ typeof spec === 'object' && spec !== null ;
53
+
54
+ const defaultValue =
55
+ ( hasSpec
56
+ ? spec . default ?? null
57
+ : spec ) ;
58
+
59
+ let formatRead = value => value ;
60
+ let formatWrite = value => value ;
61
+ if ( hasSpec && spec . type ) {
62
+ switch ( spec . type ) {
63
+ case 'number' :
64
+ formatRead = parseFloat ;
65
+ formatWrite = String ;
66
+ break ;
67
+
68
+ case 'boolean' :
69
+ formatRead = Boolean ;
70
+ formatWrite = String ;
71
+ break ;
72
+
73
+ case 'string' :
74
+ formatRead = String ;
75
+ formatWrite = String ;
76
+ break ;
77
+
78
+ case 'json' :
79
+ formatRead = JSON . parse ;
80
+ formatWrite = JSON . stringify ;
81
+ break ;
82
+
83
+ default :
84
+ throw new Error ( `Unknown type for session storage spec "${ spec . type } "` ) ;
85
+ }
86
+ }
87
+
88
+ let getMaxLength =
89
+ ( ! hasSpec
90
+ ? ( ) => Infinity
91
+ : typeof spec . maxLength === 'function'
92
+ ? ( object . settings
93
+ ? ( ) => spec . maxLength ( object . settings )
94
+ : ( ) => spec . maxLength ( ) )
95
+ : ( ) => spec . maxLength ) ;
96
+
51
97
const storageKey = `hsmusic.${ infoKey } .${ key } ` ;
52
98
53
99
let fallbackValue = defaultValue ;
54
100
55
101
Object . defineProperty ( object . session , key , {
56
102
get : ( ) => {
103
+ let value ;
57
104
try {
58
- return sessionStorage . getItem ( storageKey ) ?? defaultValue ;
105
+ value = sessionStorage . getItem ( storageKey ) ?? defaultValue ;
59
106
} catch ( error ) {
60
107
if ( error instanceof DOMException ) {
61
- return fallbackValue ;
108
+ value = fallbackValue ;
62
109
} else {
63
110
throw error ;
64
111
}
65
112
}
113
+
114
+ if ( value === null ) {
115
+ return null ;
116
+ }
117
+
118
+ return formatRead ( value ) ;
66
119
} ,
67
120
68
121
set : ( value ) => {
122
+ if ( value !== null && value !== '' ) {
123
+ value = formatWrite ( value ) ;
124
+ }
125
+
126
+ if ( value === null ) {
127
+ value = '' ;
128
+ }
129
+
130
+ const maxLength = getMaxLength ( ) ;
131
+ if ( value . length > maxLength ) {
132
+ console . warn (
133
+ `Requested to set session storage ${ storageKey } ` +
134
+ `beyond maximum length ${ maxLength } , ` +
135
+ `ignoring this value.` ) ;
136
+ console . trace ( ) ;
137
+ return ;
138
+ }
139
+
140
+ let operation ;
141
+ if ( value === '' ) {
142
+ fallbackValue = null ;
143
+ operation = ( ) => {
144
+ sessionStorage . removeItem ( storageKey ) ;
145
+ } ;
146
+ } else {
147
+ fallbackValue = value ;
148
+ operation = ( ) => {
149
+ sessionStorage . setItem ( storageKey , value ) ;
150
+ } ;
151
+ }
152
+
69
153
try {
70
- sessionStorage . setItem ( storageKey , value ) ;
154
+ operation ( ) ;
71
155
} catch ( error ) {
72
- if ( error instanceof DOMException ) {
73
- fallbackValue = value ;
74
- } else {
156
+ if ( ! ( error instanceof DOMException ) ) {
75
157
throw error ;
76
158
}
77
159
}
@@ -3723,10 +3805,19 @@ const sidebarSearchInfo = initInfo('sidebarSearchInfo', {
3723
3805
} ,
3724
3806
3725
3807
session : {
3726
- activeQuery : null ,
3727
- activeQueryResults : null ,
3808
+ activeQuery : {
3809
+ type : 'string' ,
3810
+ } ,
3728
3811
3729
- repeatQueryOnReload : false ,
3812
+ activeQueryResults : {
3813
+ type : 'json' ,
3814
+ maxLength : settings => settings . maxActiveResultsStorage ,
3815
+ } ,
3816
+
3817
+ repeatQueryOnReload : {
3818
+ type : 'boolean' ,
3819
+ default : false ,
3820
+ } ,
3730
3821
} ,
3731
3822
3732
3823
settings : {
@@ -3978,7 +4069,7 @@ function initializeSidebarSearchState() {
3978
4069
if ( session . repeatQueryOnReload ) {
3979
4070
activateSidebarSearch ( session . activeQuery ) ;
3980
4071
} else if ( session . activeQueryResults ) {
3981
- showSidebarSearchResults ( JSON . parse ( session . activeQueryResults ) ) ;
4072
+ showSidebarSearchResults ( session . activeQueryResults ) ;
3982
4073
}
3983
4074
}
3984
4075
}
@@ -4063,11 +4154,7 @@ async function activateSidebarSearch(query) {
4063
4154
updateSidebarSearchStatus ( ) ;
4064
4155
4065
4156
session . activeQuery = query ;
4066
-
4067
- const stringifiedResults = JSON . stringify ( results ) ;
4068
- if ( stringifiedResults . length < settings . maxActiveResultsStorage ) {
4069
- session . activeQueryResults = JSON . stringify ( results ) ;
4070
- }
4157
+ session . activeQueryResults = results ;
4071
4158
4072
4159
showSidebarSearchResults ( results ) ;
4073
4160
}
@@ -4088,8 +4175,8 @@ function clearSidebarSearch() {
4088
4175
4089
4176
state . searchStage = null ;
4090
4177
4091
- session . activeQuery = '' ;
4092
- session . activeQueryResults = '' ;
4178
+ session . activeQuery = null ;
4179
+ session . activeQueryResults = null ;
4093
4180
4094
4181
hideSidebarSearchResults ( ) ;
4095
4182
}
0 commit comments