@tendrock/database
Version:
A database lib under the Tendrock ecosystem for Minecraft Bedrock Edition Script API
33 lines (32 loc) • 1.14 kB
JavaScript
export class ConstructorRegistry {
constructor(_registryImpl) {
this._registryImpl = _registryImpl;
}
register(constructorId, objectConstructor) {
this._registryImpl.register(constructorId, objectConstructor);
}
}
export class ConstructorRegistryImpl {
constructor() {
this._constructorMap = new Map();
this._constructorIdMap = new Map();
this._registry = new ConstructorRegistry(this);
}
register(constructorId, objectConstructor) {
if (objectConstructor.prototype.toJSON === undefined) {
throw new Error("Register constructor failed! The constructor must have 'toJSON' method.");
}
this._constructorMap.set(constructorId, objectConstructor);
this._constructorIdMap.set(objectConstructor, constructorId);
}
get(constructorId) {
return this._constructorMap.get(constructorId);
}
getIdByConstructor(constructor) {
return this._constructorIdMap.get(constructor);
}
getRegistry() {
return this._registry;
}
}
ConstructorRegistryImpl.Instance = new ConstructorRegistryImpl();