-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathcacheInvalidation.ts
More file actions
370 lines (323 loc) · 11.1 KB
/
cacheInvalidation.ts
File metadata and controls
370 lines (323 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/**
* Cache Invalidation Module
*
* Manages cache invalidation logic for header/footer and body content caches.
* Ensures caches are properly invalidated when content, constraints, or metadata changes.
*
* Invalidation triggers:
* 1. Header/footer content changes (block hash differs)
* 2. Section metadata or numbering format/restart changes
* 3. Constraints (width/height/margins) change
* 4. Body measure cache for affected block IDs after token resolution
*/
import type { FlowBlock, SectionMetadata } from '@superdoc/contracts';
import type { HeaderFooterConstraints } from '@superdoc/layout-engine';
import type { MeasureCache } from './cache';
import type { HeaderFooterLayoutCache } from './layoutHeaderFooter';
import { HeaderFooterCacheLogger } from './instrumentation';
/**
* Computes a hash for header/footer block content.
* Used to detect when header/footer content has changed.
*
* @param blocks - Header/footer blocks
* @returns Content hash string
*/
export function computeHeaderFooterContentHash(blocks: FlowBlock[]): string {
if (!blocks || blocks.length === 0) {
return '';
}
// Simple hash based on block IDs and paragraph run content
const parts: string[] = [];
for (const block of blocks) {
parts.push(block.id);
if (block.kind === 'paragraph') {
for (const run of block.runs) {
// Only TextRun and TabRun have text property; ImageRun, LineBreakRun, BreakRun, and FieldAnnotationRun do not
if (run.kind === 'math') {
parts.push(`math:${run.textContent}`);
} else if (
!('src' in run) &&
run.kind !== 'lineBreak' &&
run.kind !== 'break' &&
run.kind !== 'fieldAnnotation'
) {
parts.push(run.text ?? '');
}
if ('bold' in run && run.bold) parts.push('b');
if ('italic' in run && run.italic) parts.push('i');
if ('token' in run && run.token) parts.push(`token:${run.token}`);
}
}
}
return parts.join('|');
}
/**
* Computes a hash for section metadata.
* Used to detect when section numbering or properties have changed.
*
* @param sections - Section metadata array
* @returns Metadata hash string
*/
export function computeSectionMetadataHash(sections: SectionMetadata[]): string {
if (!sections || sections.length === 0) {
return '';
}
const parts: string[] = [];
for (const section of sections) {
parts.push(`section:${section.sectionIndex}`);
// Include numbering properties that affect display
if (section.numbering) {
const num = section.numbering;
parts.push(`num:${num.format ?? 'decimal'}:${num.start ?? 1}`);
}
// Include header/footer refs that affect which variants are used
if (section.headerRefs) {
const refs = section.headerRefs;
if (refs.default) parts.push(`hdr-def:${refs.default}`);
if (refs.first) parts.push(`hdr-first:${refs.first}`);
if (refs.even) parts.push(`hdr-even:${refs.even}`);
if (refs.odd) parts.push(`hdr-odd:${refs.odd}`);
}
if (section.footerRefs) {
const refs = section.footerRefs;
if (refs.default) parts.push(`ftr-def:${refs.default}`);
if (refs.first) parts.push(`ftr-first:${refs.first}`);
if (refs.even) parts.push(`ftr-even:${refs.even}`);
if (refs.odd) parts.push(`ftr-odd:${refs.odd}`);
}
}
return parts.join('|');
}
/**
* Computes a hash for header/footer constraints.
* Used to detect when layout constraints have changed.
*
* @param constraints - Header/footer constraints
* @returns Constraints hash string
*/
export function computeConstraintsHash(constraints: HeaderFooterConstraints): string {
const { width, height, pageWidth, pageHeight, margins, overflowBaseHeight } = constraints;
const parts = [`w:${width}`, `h:${height}`];
if (pageWidth !== undefined) {
parts.push(`pw:${pageWidth}`);
}
if (pageHeight !== undefined) {
parts.push(`ph:${pageHeight}`);
}
if (overflowBaseHeight !== undefined) {
parts.push(`obh:${overflowBaseHeight}`);
}
if (margins) {
parts.push(`ml:${margins.left}`, `mr:${margins.right}`);
if (margins.top !== undefined) {
parts.push(`mt:${margins.top}`);
}
if (margins.bottom !== undefined) {
parts.push(`mb:${margins.bottom}`);
}
if (margins.header !== undefined) {
parts.push(`mh:${margins.header}`);
}
if (margins.footer !== undefined) {
parts.push(`mf:${margins.footer}`);
}
}
return parts.join('|');
}
/**
* Cache state tracker for header/footer layouts.
* Stores hashes of content, constraints, and metadata to detect changes.
*/
export class HeaderFooterCacheState {
private contentHashes = new Map<string, string>(); // variantKey -> contentHash
private constraintsHash: string = '';
private sectionMetadataHash: string = '';
/**
* Checks if header/footer content has changed for a variant.
*
* @param variantKey - Unique key for the variant (e.g., 'header-default')
* @param blocks - Current blocks for the variant
* @returns True if content has changed
*/
hasContentChanged(variantKey: string, blocks: FlowBlock[]): boolean {
const currentHash = computeHeaderFooterContentHash(blocks);
const previousHash = this.contentHashes.get(variantKey);
if (previousHash === undefined) {
// First time seeing this variant
this.contentHashes.set(variantKey, currentHash);
return false;
}
const changed = currentHash !== previousHash;
if (changed) {
this.contentHashes.set(variantKey, currentHash);
}
return changed;
}
/**
* Checks if constraints have changed.
*
* @param constraints - Current constraints
* @returns True if constraints have changed
*/
hasConstraintsChanged(constraints: HeaderFooterConstraints): boolean {
const currentHash = computeConstraintsHash(constraints);
if (this.constraintsHash === '') {
// First time
this.constraintsHash = currentHash;
return false;
}
const changed = currentHash !== this.constraintsHash;
if (changed) {
this.constraintsHash = currentHash;
}
return changed;
}
/**
* Checks if section metadata has changed.
*
* @param sections - Current section metadata
* @returns True if metadata has changed
*/
hasSectionMetadataChanged(sections: SectionMetadata[]): boolean {
const currentHash = computeSectionMetadataHash(sections);
if (this.sectionMetadataHash === '') {
// First time
this.sectionMetadataHash = currentHash;
return false;
}
const changed = currentHash !== this.sectionMetadataHash;
if (changed) {
this.sectionMetadataHash = currentHash;
}
return changed;
}
/**
* Resets all cached state.
* Called when performing a full cache clear.
*/
reset(): void {
this.contentHashes.clear();
this.constraintsHash = '';
this.sectionMetadataHash = '';
}
}
/**
* Invalidates header/footer cache based on change detection.
*
* This function checks what has changed (content, constraints, metadata) and
* invalidates the appropriate cache entries. It uses the cache state tracker
* to detect changes between layout runs.
*
* @param cache - Header/footer layout cache
* @param cacheState - Cache state tracker
* @param headerBlocks - Current header blocks (optional)
* @param footerBlocks - Current footer blocks (optional)
* @param constraints - Current constraints
* @param sections - Current section metadata
*
* @example
* ```typescript
* invalidateHeaderFooterCache(
* cache,
* cacheState,
* headerBlocks,
* footerBlocks,
* constraints,
* sections
* );
* ```
*/
export function invalidateHeaderFooterCache(
cache: HeaderFooterLayoutCache,
cacheState: HeaderFooterCacheState,
headerBlocks?: { default?: FlowBlock[]; first?: FlowBlock[]; even?: FlowBlock[]; odd?: FlowBlock[] },
footerBlocks?: { default?: FlowBlock[]; first?: FlowBlock[]; even?: FlowBlock[]; odd?: FlowBlock[] },
constraints?: HeaderFooterConstraints,
sections?: SectionMetadata[],
): void {
const invalidationReasons: string[] = [];
const affectedBlockIds: string[] = [];
// Check if constraints changed
if (constraints && cacheState.hasConstraintsChanged(constraints)) {
invalidationReasons.push('constraints changed');
// Invalidate entire cache when constraints change (affects all layouts)
if (headerBlocks) {
Object.values(headerBlocks).forEach((blocks) => {
if (blocks) affectedBlockIds.push(...blocks.map((b) => b.id));
});
}
if (footerBlocks) {
Object.values(footerBlocks).forEach((blocks) => {
if (blocks) affectedBlockIds.push(...blocks.map((b) => b.id));
});
}
}
// Check if section metadata changed
if (sections && cacheState.hasSectionMetadataChanged(sections)) {
invalidationReasons.push('section metadata changed');
// Invalidate entire cache when metadata changes (affects numbering)
if (headerBlocks) {
Object.values(headerBlocks).forEach((blocks) => {
if (blocks) affectedBlockIds.push(...blocks.map((b) => b.id));
});
}
if (footerBlocks) {
Object.values(footerBlocks).forEach((blocks) => {
if (blocks) affectedBlockIds.push(...blocks.map((b) => b.id));
});
}
}
// Check if header/footer content changed for each variant
if (headerBlocks) {
for (const [variant, blocks] of Object.entries(headerBlocks)) {
if (!blocks) continue;
const variantKey = `header-${variant}`;
if (cacheState.hasContentChanged(variantKey, blocks)) {
invalidationReasons.push(`header ${variant} content changed`);
affectedBlockIds.push(...blocks.map((b) => b.id));
}
}
}
if (footerBlocks) {
for (const [variant, blocks] of Object.entries(footerBlocks)) {
if (!blocks) continue;
const variantKey = `footer-${variant}`;
if (cacheState.hasContentChanged(variantKey, blocks)) {
invalidationReasons.push(`footer ${variant} content changed`);
affectedBlockIds.push(...blocks.map((b) => b.id));
}
}
}
// Perform invalidation if any changes detected
if (affectedBlockIds.length > 0) {
// Remove duplicates
const uniqueBlockIds = Array.from(new Set(affectedBlockIds));
// Invalidate cache
cache.invalidate(uniqueBlockIds);
// Log invalidation
HeaderFooterCacheLogger.logInvalidation(invalidationReasons.join(', '), uniqueBlockIds);
}
}
/**
* Invalidates body measure cache for blocks affected by token resolution.
*
* This should be called after page token resolution to ensure re-measurement
* of blocks with resolved tokens.
*
* @param cache - Body measure cache
* @param affectedBlockIds - Set of block IDs affected by token resolution
*
* @example
* ```typescript
* const result = resolvePageNumberTokens(layout, blocks, measures, numberingCtx);
* invalidateBodyMeasureCache(measureCache, result.affectedBlockIds);
* ```
*/
export function invalidateBodyMeasureCache<T>(cache: MeasureCache<T>, affectedBlockIds: Set<string>): void {
if (affectedBlockIds.size === 0) {
return;
}
const blockIdsArray = Array.from(affectedBlockIds);
cache.invalidate(blockIdsArray);
// Note: Logging is handled in incrementalLayout.ts where this is called
}