All files resourcemanager.mjs

23.33% Statements 35/150
100% Branches 2/2
9.09% Functions 1/11
23.33% Lines 35/150

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 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 1511x 1x 1x 1x                               1x 1x                                                 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x                                                   1x 1x                       1x 1x 1x                           1x 1x             1x 1x               1x 1x                 1x 1x       1x 1x       1x 1x 1x  
import Logger from './logger.mjs';
 
class Resource {
    constructor(res, callback) {
        this.name = res.name || 'unnamed';
        this.description = res.description || '';
        this.type = res.type;
        this.data = res.data || null;
        this.image = res.image || null;
        this.imageURL = res.imageURL || null;
        this.dataURL = res.dataURL || null;

        this.loaded = false;
        this.callback = callback;

        if(!this.validate()){
            throw new Error('Invalid resource');
        };
    }
 
    validate() {
        const validTypes = ['image', 'audio', 'video', 'text', 'json', 'object', 'binary', 'arraybuffer', 'blob'];
        if(!validTypes.includes(this.type)) {
            return false;
        }

        if(this.type === 'image' && this.imageURL) {
            this.image = new Image();
            this.image.src = this.imageURL;
            this.image.onload = () => { 
                this.loaded = true;
                this.callback(this);
            };
        
            return true;
        }

        if (this.type === 'text') { this.loaded = true; return true;}

        if (this.type === 'json') { this.loaded = true; return true;}

        if (this.type === 'object') { this.loaded = true; return true;}
        
        return false;
    }
}
 
class ResourceManager {
    constructor() {
        this.resources = new Map();
        this.logger = new Logger('ResourceManager');
        
    }
 
 
    loadResourceMap (resourceMapPath) {
        this.logger.log(`Loading resource map from: ${resourceMapPath}`);
        const request = new Request(resourceMapPath);
        fetch(resourceMapPath)
        .then(async (response) => {
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }
            let resourceMap = await response.json();

            this.logger.log('Resource map loaded successfully.');
            this.logger.log(resourceMap);
            
            resourceMap.forEach(res => {
                try {
                    const resource = new Resource(res, (loadedResource) => {
                        this.logger.log(`Resource "${loadedResource.name}" loaded successfully.`);
                    });
                    this.addResource(resource.name, resource);
                }
                catch (error) {
                    this.logger.log(`Failed to load resource "${res.name}": ${error.message}`);
                }
            });
        })
    }
 
    allLoaded () {
        if(this.resources.length === 0 || this.resources.size === 0) {
            return false;
        }

        for (let resource of this.resources.values()) {
            if (!resource.loaded) {
                return false;
            }
        }
        return true;
    }
 
    // return a value between 0 and 1 representing the load progress
    getLoadProgress () {

        if(this.resources.length === 0 || this.resources.size === 0) {
            return 0;
        }

        let loadedCount = 0;
        for (let resource of this.resources.values()) {
            if (resource.loaded) {
                loadedCount++;
            }
        }
        return loadedCount / this.resources.size;
    }
 
    addResource (name, resource) {
        if (this.resources.has(name)) {
            this.logger.log(`Resource with name "${name}" already exists. It will be overwritten.`);
        }
        this.resources.set(name, resource);
        this.logger.log(`Resource "${name}" added.`);
    }
 
    getResource (name) {
        if (!this.resources.has(name)) {
            this.logger.log(`Resource with name "${name}" not found.`);
            return null;
        }
        this.logger.log(`Resource "${name}" retrieved.`);
        return this.resources.get(name);
    }
 
    removeResource (name) {
        if (!this.resources.has(name)) {
            this.logger.log(`Resource with name "${name}" not found. Cannot remove.`);
            return false;
        }
        this.resources.delete(name);
        this.logger.log(`Resource "${name}" removed.`);
        return true;
    }
 
    listResources () {
        this.logger.log('Listing all resources.');
        return Array.from(this.resources.keys());
    }
 
    clearResources () {
        this.resources.clear();
        this.logger.log('All resources cleared.');
    }
}
 
export {ResourceManager, Resource};