All files / ts/controller/assets/sources imageSources.ts

100% Statements 18/18
100% Branches 4/4
100% Functions 7/7
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 411x             1x         6x 6x       4x       2x 2x 2x 1x   1x 1x         1x 1x 1x 1x 1x 1x      
import { IImageSource, ImageSource } from "./imageSource.js";
 
export interface IImageSources extends Map<string, IImageSource> {
    add(...images: Array<string>): void;
    remove(...images: Array<string>): void;
    loadAsync(): Promise<void>;
}
export class ImageSources extends Map<string, IImageSource> implements IImageSources {
 
    private readonly _imagePaths: Array<string>;
 
    public constructor() {
        super();
        this._imagePaths = new Array<string>();
    }
 
    public add(...images: Array<string>): void {
        this._imagePaths.push(...images);
    }
 
    public remove(...images: Array<string>): void {
        images.forEach(image => {
            const index = this._imagePaths.indexOf(image);
            if (index < 0 || index >= this._imagePaths.length)
                return;
 
            this._imagePaths.splice(index, 1);
            this.delete(image);
        });
    }
 
    public async loadAsync(): Promise<void> {
        await Promise.all(this._imagePaths.map(async (path): Promise<void> => {
            const image = new Image();
            image.src = path;
            await image.decode();
            const imageName = path.split('/').pop()!;
            this.set(imageName, new ImageSource(imageName, image));
        }));
    }
}