@@ -40,6 +40,11 @@ type MessageFrame = HTMLIFrameElement & {
4040 metadata : RenderOptions ;
4141 contentWindow : Window & { messageId : string } ;
4242}
43+ type OccurrenceTrackerState = {
44+ session : { [ key : string ] : number } ;
45+ triggers : { [ key : string ] : Array < Timestamp > } ;
46+ occurrences : { [ key : string ] : Array < Timestamp > } ;
47+ }
4348
4449class OccurrenceTracker {
4550 recordOccurrence ( id : MessageId ) : void {
@@ -78,10 +83,13 @@ class OccurrenceTracker {
7883 load ( ) : void {
7984 const cache = StorageManager . get ( Constants . DEFAULT_KEYS . MESSAGE_OCCURRENCES )
8085 if ( cache ) {
81- const json = JSON . parse ( cache )
82- this . session = json . session
83- this . triggers = json . triggers
84- this . occurrences = json . occurrences
86+ const json = maybeJSON ( cache ) as OccurrenceTrackerState
87+
88+ if ( json ) {
89+ this . session = json . session
90+ this . triggers = json . triggers
91+ this . occurrences = json . occurrences
92+ }
8593 }
8694 }
8795
@@ -139,8 +147,8 @@ export default class Messages {
139147 } )
140148 events . on ( 'resume' , ( ) => {
141149 const key = Constants . DEFAULT_KEYS . MESSAGE_CACHE
142- const cache = StorageManager . get ( key )
143- this . _messageCache = cache ? JSON . parse ( cache ) : this . _messageCache
150+ const cache = maybeJSON ( StorageManager . get ( key ) ) as MessageHash
151+ this . _messageCache = cache || this . _messageCache
144152
145153 this . occurrenceTracker . load ( )
146154
0 commit comments