61a41c98-5e16-4333-a56e-9b3d839f91d2.js 2.95 KB
"use strict";
cc._RF.push(module, '61a41yYXhZDM6Vumz2Dn5HS', 'Type');
// script/common/type/Type.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Type = void 0;
const Type = {
    registry: new Map(),
    /**
    *
    * @param typeId  must unique in app
    */
    declare(typeId, ...superTypes) {
        if (this.registry.has(typeId)) {
            throw Error("The type named '" + typeId + "' had been registered already. Check the conflict of the name.");
        }
        let assignableTypes = calTypeId(typeId, ...superTypes);
        let typeObj = {
            id: typeId,
            superTypes: superTypes,
            assignableTypes: assignableTypes,
            /**
            * test if obj is a T instance.
            * @param obj
            */
            isInstanceOf(obj) {
                let objTypeId = obj.__assignableTypes;
                // console.log("isInstanceOf: objTypeId=" + objTypeId)
                // undefined false
                if (objTypeId === undefined)
                    return false;
                // single type
                // console.log("isInstanceOf: __id=" + this.id)
                if (objTypeId === this.id)
                    return true;
                // union types
                let objTypeIds = objTypeId.split("|");
                for (const i in objTypeIds) {
                    let id = objTypeIds[i];
                    // console.log("isInstanceOf: id=" + id)
                    if (id === this.id)
                        return true;
                }
                return false;
            }
        };
        this.registry.set(typeId, typeObj);
        // console.log("type=" + typeId + "Type")
        // console.log("assignableTypes=" + assignableTypes)
        // console.log("typeId=" + typeId)
        return typeObj;
    },
    getType(typeId) {
        if (this.registry.has(typeId))
            return this.registry.get(typeId);
        throw new Error("Type [" + typeId + "] is not found!");
    },
    isInstanceOf(typeId, obj) {
        if (!this.registry.has(typeId))
            return false;
        let typeObj = this.registry.get(typeId);
        return typeObj.isInstanceOf(obj);
    }
};
exports.Type = Type;
/**
 *
 * @param types
 * @returns
 */
function calTypeId(selfTypeId, ...types) {
    let idSet = _calTypeId(selfTypeId, ...types);
    let idStr = "";
    idSet.forEach(id => {
        idStr += id + "|";
    });
    return idStr.substr(0, idStr.length - 1);
}
function _calTypeId(selfTypeId, ...types) {
    let resultTypeIds = new Set();
    if (selfTypeId.length > 0) {
        resultTypeIds.add(selfTypeId);
    }
    for (const type of types) {
        resultTypeIds.add(type.id);
        if (type.superTypes && type.superTypes.length > 0) {
            let superIds = _calTypeId("", ...type.superTypes);
            for (const id of superIds) {
                resultTypeIds.add(id);
            }
        }
    }
    return resultTypeIds;
}

cc._RF.pop();