@@ -17,22 +17,26 @@ export class CacheHandler {
17
17
18
18
if ( ! cache . canBeUsed ) {
19
19
// Reset the cache:
20
- cache . objHashes = { }
21
- cache . objects = { }
20
+ CacheHandler . resetCache ( cache )
22
21
23
22
this . canUseIncomingCache = false
24
23
} else {
25
24
this . canUseIncomingCache = true
26
25
}
27
26
27
+ if ( this . resolvedTimeline . traceResolving ) {
28
+ this . resolvedTimeline . addResolveTrace ( `cache: init` )
29
+ this . resolvedTimeline . addResolveTrace ( `cache: canUseIncomingCache: ${ this . canUseIncomingCache } ` )
30
+ this . resolvedTimeline . addResolveTrace (
31
+ `cache: cached objects: ${ JSON . stringify ( Object . keys ( cache . objects ) ) } `
32
+ )
33
+ }
34
+
28
35
// cache.canBeUsed will be set in this.persistData()
29
36
cache . canBeUsed = false
30
37
31
38
this . cache = cache as ResolverCache
32
39
}
33
- private debug ( ...args : any [ ] ) {
34
- if ( this . resolvedTimeline . options . debug ) console . log ( ...args )
35
- }
36
40
public determineChangedObjects ( ) : void {
37
41
const toc = tic ( ' cache.determineChangedObjects' )
38
42
// Go through all new objects, and determine whether they have changed:
@@ -45,8 +49,15 @@ export class CacheHandler {
45
49
const newHash = hashTimelineObject ( obj )
46
50
allNewObjects [ obj . id ] = true
47
51
48
- if ( ! oldHash ) this . debug ( `Cache: Object "${ obj . id } " is new` )
49
- else if ( oldHash !== newHash ) this . debug ( `Cache: Object "${ obj . id } " has changed` )
52
+ if ( ! oldHash ) {
53
+ if ( this . resolvedTimeline . traceResolving ) {
54
+ this . resolvedTimeline . addResolveTrace ( `cache: object "${ obj . id } " is new` )
55
+ }
56
+ } else if ( oldHash !== newHash ) {
57
+ if ( this . resolvedTimeline . traceResolving ) {
58
+ this . resolvedTimeline . addResolveTrace ( `cache: object "${ obj . id } " has changed` )
59
+ }
60
+ }
50
61
if (
51
62
// Object is new:
52
63
! oldHash ||
@@ -61,16 +72,20 @@ export class CacheHandler {
61
72
} else {
62
73
// No timing-affecting changes detected
63
74
/* istanbul ignore if */
64
- if ( ! oldHash ) this . debug ( `Cache: Object "${ obj . id } " is similar` )
75
+ if ( ! oldHash ) {
76
+ if ( this . resolvedTimeline . traceResolving ) {
77
+ this . resolvedTimeline . addResolveTrace ( `cache: object "${ obj . id } " is similar` )
78
+ }
79
+ }
65
80
66
81
// Even though the timeline-properties hasn't changed,
67
82
// the content (and other properties) might have:
68
83
const oldObj = this . cache . objects [ obj . id ]
69
84
70
85
/* istanbul ignore if */
71
86
if ( ! oldObj ) {
72
- console . error ( ' oldHash' , oldHash )
73
- console . error ( ' ids' , Object . keys ( this . cache . objects ) )
87
+ console . error ( ` oldHash: " ${ oldHash } "` )
88
+ console . error ( ` ids: ${ JSON . stringify ( Object . keys ( this . cache . objects ) ) } ` )
74
89
throw new Error ( `Internal Error: obj "${ obj . id } " not found in cache, even though hashes match!` )
75
90
}
76
91
@@ -149,15 +164,28 @@ export class CacheHandler {
149
164
for ( const reference of changedTracker . listChanged ( ) ) {
150
165
invalidator . invalidateObjectsWithReference ( reference )
151
166
}
167
+ if ( this . resolvedTimeline . traceResolving ) {
168
+ this . resolvedTimeline . addResolveTrace (
169
+ `cache: changed references: ${ JSON . stringify ( Array . from ( changedTracker . listChanged ( ) ) ) } `
170
+ )
171
+ this . resolvedTimeline . addResolveTrace (
172
+ `cache: invalidated objects: ${ JSON . stringify ( Array . from ( invalidator . getInValidObjectIds ( ) ) ) } `
173
+ )
174
+ this . resolvedTimeline . addResolveTrace (
175
+ `cache: unchanged objects: ${ JSON . stringify ( invalidator . getValidObjects ( ) . map ( ( o ) => o . id ) ) } `
176
+ )
177
+ }
152
178
153
179
// At this point, the objects that are left in validObjects are still valid (ie has not changed or is affected by any others).
154
180
// We can reuse the old resolving for those:
155
181
for ( const obj of invalidator . getValidObjects ( ) ) {
156
- if ( ! this . cache . objects [ obj . id ] )
182
+ if ( ! this . cache . objects [ obj . id ] ) {
157
183
/* istanbul ignore next */
158
184
throw new Error (
159
- `Something went wrong: "${ obj . id } " does not exist in cache.resolvedTimeline.objects`
185
+ `Internal Error: Something went wrong: "${ obj . id } " does not exist in cache.resolvedTimeline.objects`
160
186
)
187
+ }
188
+
161
189
this . resolvedTimeline . objectsMap . set ( obj . id , this . cache . objects [ obj . id ] )
162
190
}
163
191
}
@@ -179,6 +207,12 @@ export class CacheHandler {
179
207
180
208
toc ( )
181
209
}
210
+ /** Resets / Clears the cache */
211
+ static resetCache ( cache : Partial < ResolverCache > ) : void {
212
+ delete cache . canBeUsed
213
+ cache . objHashes = { }
214
+ cache . objects = { }
215
+ }
182
216
}
183
217
/** Return a "hash-string" which changes whenever anything that affects timing of a timeline-object has changed. */
184
218
export function hashTimelineObject ( obj : ResolvedTimelineObject ) : string {
@@ -210,21 +244,26 @@ function getAllReferencesThisObjectAffects(newObj: ResolvedTimelineObject): Refe
210
244
}
211
245
return references
212
246
}
247
+ /**
248
+ * Keeps track of which timeline object have been changed
249
+ */
213
250
class ChangedTracker {
214
251
private changedReferences = new Set < Reference > ( )
215
252
253
+ /**
254
+ * Mark an object as "has changed".
255
+ * Will store all references that are affected by this object.
256
+ */
216
257
public addChangedObject ( obj : ResolvedTimelineObject ) {
217
- const references = getAllReferencesThisObjectAffects ( obj )
218
- for ( const ref of references ) {
258
+ for ( const ref of getAllReferencesThisObjectAffects ( obj ) ) {
219
259
this . changedReferences . add ( ref )
220
260
}
221
- if ( objHasLayer ( obj ) ) {
222
- this . changedReferences . add ( `$${ obj . layer } ` )
223
- }
224
261
}
262
+ /** Returns true if a reference has changed */
225
263
public isChanged ( ref : Reference ) : boolean {
226
264
return this . changedReferences . has ( ref )
227
265
}
266
+ /** Returns a list of all changed references */
228
267
public listChanged ( ) : IterableIterator < Reference > {
229
268
return this . changedReferences . keys ( )
230
269
}
@@ -236,6 +275,7 @@ class Invalidator {
236
275
/** All references that depend on another reference (ie objects, class or layers): */
237
276
private affectReferenceMap : { [ ref : Reference ] : Reference [ ] } = { }
238
277
private validObjects : ResolvedTimelineObjects = { }
278
+ private inValidObjectIds : string [ ] = [ ]
239
279
/** Map of which objects can be affected by any other object, per layer */
240
280
private objectLayerMap : { [ layer : string ] : string [ ] } = { }
241
281
@@ -245,6 +285,9 @@ class Invalidator {
245
285
public getValidObjects ( ) : ResolvedTimelineObject [ ] {
246
286
return Object . values < ResolvedTimelineObject > ( this . validObjects )
247
287
}
288
+ public getInValidObjectIds ( ) : string [ ] {
289
+ return this . inValidObjectIds
290
+ }
248
291
public addObjectOnLayer ( layer : string , obj : ResolvedTimelineObject ) {
249
292
if ( ! this . objectLayerMap [ layer ] ) this . objectLayerMap [ layer ] = [ ]
250
293
this . objectLayerMap [ layer ] . push ( obj . id )
@@ -263,6 +306,7 @@ class Invalidator {
263
306
const objId = getRefObjectId ( reference )
264
307
if ( this . validObjects [ objId ] ) {
265
308
delete this . validObjects [ objId ]
309
+ this . inValidObjectIds . push ( objId )
266
310
}
267
311
}
268
312
if ( isLayerReference ( reference ) ) {
0 commit comments