All files / lib/pipeline pipelineCache.ts

100% Statements 17/17
100% Branches 2/2
100% Functions 8/8
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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          26x           86x             86x             86x             86x           1x 1x 1x 1x               344x     89x 8x         81x       375x       5x       213x       8x    
import {Cache, EvalFunction, Fragment} from '..';
 
/**
 * Caches any data that can be reused by the pipeline
 */
export class StandardPipelineCache {
    /**
     * Cache that maps resource paths to parsed fragments.
     * Paths are not normalized or converted in any way.
     * If fuzzy / equivalent matching is needed, then the caller is responsible for pre-processing the path key.
     */
    fragmentCache: Cache<string, Fragment> = new MapCache<string, Fragment>();
 
    /**
     * Cache that maps expression strings to parsed functions.
     * Key strings are not normalized or converted in any way.
     * If fuzzy / equivalent matching is needed, then the caller is responsible for pre-processing the key.
     */
    expressionCache: Cache<string, EvalFunction<unknown>> = new MapCache<string, EvalFunction<unknown>>();
 
    /**
     * Cache that maps JS code to parsed functions.
     * Key strings are not normalized or converted in any way.
     * If fuzzy / equivalent matching is needed, then the caller is responsible for pre-processing the key.
     */
    scriptCache: Cache<string, EvalFunction<unknown>> = new MapCache<string, EvalFunction<unknown>>();
 
    /**
     * Cache that maps hashes of created resources to the resource path where that resource is saved.
     * Neither hash keys nor resource paths are normalized or converted in any way.
     * If fuzzy / equivalent matching is needed, then the caller is responsible for pre-processing the hash and value.
     */
    createdResourceCache: Cache<string, string> = new MapCache<string, string>();
 
    /**
     * Removes all stored data from all caches
     */
    clear(): void {
        this.fragmentCache.clear();
        this.expressionCache.clear();
        this.scriptCache.clear();
        this.createdResourceCache.clear();
    }
}
 
/**
 * Implementation of Cache that stores data in a JS Map object.
 */
class MapCache<TKey, TValue> implements Cache<TKey, TValue> {
    private readonly cacheMap = new Map<TKey, TValue>();
 
    get(key: TKey): TValue {
        if (!this.has(key)) {
            throw new Error('Value missing from cache - make sure to call has() before get()');
        }
 
        // Cast is safe because cacheMap is fully controlled and can only contain TValue,
        // unless the type system is bypassed which is out of scope.
        return this.cacheMap.get(key) as TValue;
    }
 
    has(key: TKey): boolean {
        return this.cacheMap.has(key);
    }
 
    remove(key: TKey): void {
        this.cacheMap.delete(key);
    }
 
    store(key: TKey, value: TValue): void {
        this.cacheMap.set(key, value);
    }
 
    clear(): void {
        this.cacheMap.clear();
    }
}