All files / bin/watch trackers.ts

60.87% Statements 14/23
100% Branches 0/0
52.94% Functions 9/17
60.87% Lines 14/23

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 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                              1x         1x 1x                             3x   3x       2x                       1x               4x                 1x         1x 1x                           3x       1x       3x    
import {
    Cache,
    MimeType,
    PipelineIO
} from '../../lib';
 
/**
 * Called when a tracker detects a dependency on a resource.
 * @param resPath Path to the dependency.
 */
export type TrackerCallback = (resPath: string) => void;
 
/**
 * Wrapper around {@link PipelineIO} that tracks read resources and logs them to a {@link TrackerCallback}.
 */
export class TrackingPipelineIO implements PipelineIO {
    private readonly realIO: PipelineIO;
    private readonly trackerCallback: TrackerCallback;
    
    constructor(realIO: PipelineIO, trackerCallback: TrackerCallback) {
        this.realIO = realIO;
        this.trackerCallback = trackerCallback;
    }
 
    get destinationPath(): string {
        return this.realIO.destinationPath;
    }
    get sourcePath(): string {
        return this.realIO.sourcePath;
    }
 
    async createResource(type: MimeType, contents: string): Promise<string> {
        return await this.realIO.createResource(type, contents);
    }
 
    async getResource(type: MimeType, resPath: string): Promise<string> {
        this.trackerCallback(resPath);
        
        return await this.realIO.getResource(type, resPath);
    }
 
    async writeResource(type: MimeType, resPath: string, contents: string): Promise<void> {
        await this.realIO.writeResource(type, resPath, contents);
    }
 
    createResPath(type: MimeType, contents: string): string {
        return this.realIO.createResPath(type, contents);
    }
    
    getDestinationResPathForAbsolutePath(rawResPath: string): string {
        return this.realIO.getDestinationResPathForAbsolutePath(rawResPath);
    }
 
    getSourceResPathForAbsolutePath(rawResPath: string): string {
        return this.realIO.getSourceResPathForAbsolutePath(rawResPath);
    }
 
    resolveDestinationResource(resPath: string): string {
        return this.realIO.resolveDestinationResource(resPath);
    }
 
    resolveSourceResource(resPath: string): string {
        return this.realIO.resolveSourceResource(resPath);
    }
}
 
/**
 * Wrapper around {@link Cache} that tracks accessed keys and logs them to a {@link TrackerCallback}.
 * This implementation can be used with any Cache that uses string keys,
 * but semantically it should only be used for caches where the key is a resource path.
 */
export class TrackingCache<TKey> implements Cache<string, TKey> {
    private readonly realCache: Cache<string, TKey>;
    private readonly trackerCallback: TrackerCallback;
 
    constructor(realCache: Cache<string, TKey>, trackerCallback: TrackerCallback) {
        this.realCache = realCache;
        this.trackerCallback = trackerCallback;
    }
 
    clear(): void {
        this.realCache.clear();
    }
 
    get(resPath: string): TKey {
        this.trackerCallback(resPath);
 
        return this.realCache.get(resPath);
    }
 
    has(resPath: string): boolean {
        return this.realCache.has(resPath);
    }
 
    remove(resPath: string): void {
        this.realCache.remove(resPath);
    }
 
    store(resPath: string, fragment: TKey): void {
        this.realCache.store(resPath, fragment);
    }
}