avocado-old/packages/entity/trait/registrar.js
2020-06-17 04:35:59 -05:00

38 lines
794 B
JavaScript

const {all, idFrom: idFromRaw} = require('./traits.scwp');
let hasMapped = false;
const traitTo = new Map();
const typeTo = new Map();
function mapTypesToTraits() {
const entries = Object.entries(all());
for (let i = 0; i < entries.length; i++) {
const [, M] = entries[i];
const {default: Trait} = M;
typeTo.set(Trait.type(), Trait);
traitTo.set(Trait, M);
}
}
export function idFrom(Trait) {
return idFromRaw(traitTo.get(Trait));
}
export function hasTrait(type) {
if (!hasMapped) {
mapTypesToTraits();
hasMapped = true;
}
return typeTo.has(type);
}
export function lookupTrait(type) {
if (!hasMapped) {
mapTypesToTraits();
hasMapped = true;
}
return typeTo.get(type);
}
module.hot?.accept(['./traits.scwp'], () => hasMapped = false);