avocado-old/packages/behavior/item/registry.js

23 lines
598 B
JavaScript

const behaviorItemRegistry = new Map();
export function fromJSON({type, ...json}) {
const Class = behaviorItemRegistry.get(type);
if (!Class) {
throw new TypeError(`There is no class for the behavior item "${type}"`);
}
return (new Class()).fromJSON(json);
}
export function deregister(BehaviorItem) {
behaviorItemRegistry.delete(BehaviorItem.type());
}
export function register(BehaviorItem) {
if (!BehaviorItem.type) {
BehaviorItem.type = function() {
return BehaviorItem.name.toLowerCase();
}
}
behaviorItemRegistry.set(BehaviorItem.type(), BehaviorItem);
}