avocado-old/packages/behavior/item/registry.js
2019-03-17 23:45:48 -05:00

18 lines
477 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) {
behaviorItemRegistry.set(BehaviorItem.type(), BehaviorItem);
}