@@ -209,6 +209,7 @@ function lsKey(env, user) {
209
209
}
210
210
211
211
function initialize ( env , user , options ) {
212
+ var localStorageKey ;
212
213
options = options || { } ;
213
214
environment = env ;
214
215
flags = typeof ( options . bootstrap ) === 'object' ? options . bootstrap : { } ;
@@ -221,20 +222,26 @@ function initialize(env, user, options) {
221
222
emitter = EventEmitter ( ) ;
222
223
ident = Identity ( user , sendIdentifyEvent ) ;
223
224
requestor = Requestor ( baseUrl , environment ) ;
224
-
225
+ localStorageKey = lsKey ( environment , ident . getUser ( ) ) ;
226
+
225
227
if ( typeof options . bootstrap === 'object' ) {
226
228
// Emitting the event here will happen before the consumer
227
229
// can register a listener, so defer to next tick.
228
230
setTimeout ( function ( ) { emitter . emit ( readyEvent ) ; } , 0 ) ;
229
231
}
230
232
else if ( typeof ( options . bootstrap ) === 'string' && options . bootstrap . toUpperCase ( ) === 'LOCALSTORAGE' && typeof ( Storage ) !== 'undefined' ) {
231
233
useLocalStorage = true ;
232
- flags = JSON . parse ( localStorage . getItem ( lsKey ( environment , ident . getUser ( ) ) ) ) ;
234
+ // check if localstorage data is corrupted, if so clear it
235
+ try {
236
+ flags = JSON . parse ( localStorage . getItem ( localStorageKey ) ) ;
237
+ } catch ( error ) {
238
+ localStorage . setItem ( localStorageKey , null ) ;
239
+ }
233
240
234
241
if ( flags === null ) {
235
242
requestor . fetchFlagSettings ( ident . getUser ( ) , hash , function ( err , settings ) {
236
243
flags = settings ;
237
- localStorage . setItem ( lsKey ( environment , ident . getUser ( ) ) , JSON . stringify ( flags ) ) ;
244
+ localStorage . setItem ( localStorageKey , JSON . stringify ( flags ) ) ;
238
245
emitter . emit ( readyEvent ) ;
239
246
} ) ;
240
247
} else {
@@ -243,7 +250,7 @@ function initialize(env, user, options) {
243
250
// the in-memory flags unless you subscribe for changes
244
251
setTimeout ( function ( ) { emitter . emit ( readyEvent ) ; } , 0 ) ;
245
252
requestor . fetchFlagSettings ( ident . getUser ( ) , hash , function ( err , settings ) {
246
- localStorage . setItem ( lsKey ( environment , ident . getUser ( ) ) , JSON . stringify ( settings ) ) ;
253
+ localStorage . setItem ( localStorageKey , JSON . stringify ( settings ) ) ;
247
254
} ) ;
248
255
}
249
256
}
0 commit comments