feat: hot traits
This commit is contained in:
parent
2a2b9fd8eb
commit
c65612a387
|
@ -1,16 +1,20 @@
|
||||||
const {all, idFrom: idFromRaw} = require('./traits.scwp');
|
const {all, idFrom: idFromRaw} = require('./traits.scwp');
|
||||||
|
const {registerHooks} = require('scwp');
|
||||||
|
|
||||||
let hasMapped = false;
|
let hasMapped = false;
|
||||||
const traitTo = new Map();
|
const traitTo = new Map();
|
||||||
const typeTo = new Map();
|
const typeTo = new Map();
|
||||||
|
|
||||||
function mapTypesToTraits() {
|
function ensureTypeMap() {
|
||||||
const entries = Object.entries(all());
|
if (!hasMapped) {
|
||||||
for (let i = 0; i < entries.length; i++) {
|
const entries = Object.entries(all());
|
||||||
const [, M] = entries[i];
|
for (let i = 0; i < entries.length; i++) {
|
||||||
const {default: Trait} = M;
|
const [, M] = entries[i];
|
||||||
typeTo.set(Trait.type(), Trait);
|
const {default: Trait} = M;
|
||||||
traitTo.set(Trait, M);
|
typeTo.set(Trait.type(), Trait);
|
||||||
|
traitTo.set(Trait, M);
|
||||||
|
}
|
||||||
|
hasMapped = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,19 +23,21 @@ export function idFrom(Trait) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function hasTrait(type) {
|
export function hasTrait(type) {
|
||||||
if (!hasMapped) {
|
ensureTypeMap();
|
||||||
mapTypesToTraits();
|
|
||||||
hasMapped = true;
|
|
||||||
}
|
|
||||||
return typeTo.has(type);
|
return typeTo.has(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function lookupTrait(type) {
|
export function lookupTrait(type) {
|
||||||
if (!hasMapped) {
|
ensureTypeMap();
|
||||||
mapTypesToTraits();
|
|
||||||
hasMapped = true;
|
|
||||||
}
|
|
||||||
return typeTo.get(type);
|
return typeTo.get(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
registerHooks({
|
||||||
|
autoreg$accept: (type, M) => {
|
||||||
|
const {default: Trait} = M;
|
||||||
|
typeTo.set(Trait.type(), Trait);
|
||||||
|
traitTo.set(Trait, M);
|
||||||
|
},
|
||||||
|
}, '@avocado/entity/trait/registrar');
|
||||||
|
|
||||||
module.hot?.accept(['./traits.scwp'], () => hasMapped = false);
|
module.hot?.accept(['./traits.scwp'], () => hasMapped = false);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user