@@ -40,6 +40,11 @@ type MessageFrame = HTMLIFrameElement & {
40
40
metadata : RenderOptions ;
41
41
contentWindow : Window & { messageId : string } ;
42
42
}
43
+ type OccurrenceTrackerState = {
44
+ session : { [ key : string ] : number } ;
45
+ triggers : { [ key : string ] : Array < Timestamp > } ;
46
+ occurrences : { [ key : string ] : Array < Timestamp > } ;
47
+ }
43
48
44
49
class OccurrenceTracker {
45
50
recordOccurrence ( id : MessageId ) : void {
@@ -78,10 +83,13 @@ class OccurrenceTracker {
78
83
load ( ) : void {
79
84
const cache = StorageManager . get ( Constants . DEFAULT_KEYS . MESSAGE_OCCURRENCES )
80
85
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
+ }
85
93
}
86
94
}
87
95
@@ -139,8 +147,8 @@ export default class Messages {
139
147
} )
140
148
events . on ( 'resume' , ( ) => {
141
149
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
144
152
145
153
this . occurrenceTracker . load ( )
146
154
0 commit comments