All files / src/Ice DefaultSliceLoader.js

100% Statements 61/61
100% Branches 11/11
100% Functions 7/7
100% Lines 61/61

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 6241x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 2728x 2728x 41x 41x 331x 331x 27x 27x 304x 331x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 2722x 52x 2722x 2722x 2722x 875x 2722x 2722x 2722x 2645x 2722x 2722x 2722x 2722x 2722x 6x 6x 2722x  
// Copyright (c) ZeroC, Inc.
 
/**
 * Implements SliceLoader using generated classes that register themselves by calling defineClass.
 */
class DefaultSliceLoader {
    /**
     * Creates a new DefaultSliceLoader.
     */
    constructor() {
        this.typeIdToTypeMap = new Map(); // Map<String, Type>
    }
 
    /**
     * Adds a new type to the typeIdToTypeMap.
     * @param typeId The type ID or compact type ID.
     * @param type The type.
     */
    add(typeId, type) {
        this.typeIdToTypeMap.set(typeId, type);
    }
 
    newInstance(typeId) {
        const type = this.typeIdToTypeMap.get(typeId);
        if (type === undefined) {
            return null;
        }
        return new type();
    }
}
 
/**
 * The DefaultSliceLoader singleton.
 */
export const defaultSliceLoaderInstance = new DefaultSliceLoader();
 
/**
 * Registers the class for a mapped Slice class or exception. Also adds some methods to the JS class.
 * @param classType The JS class.
 * @param typeId The type ID.
 * @param compactId The compact type ID.
 */
export function defineClass(classType, typeId, compactId = -1) {
    classType.prototype.ice_id = function () {
        return typeId;
    };
 
    classType.prototype._iceMostDerivedType = function () {
        return classType;
    };
 
    classType.ice_staticId = function () {
        return typeId;
    };
 
    defaultSliceLoaderInstance.add(typeId, classType);
 
    if (compactId !== -1) {
        defaultSliceLoaderInstance.add(compactId.toString(), classType);
    }
}