feat: hot traits
This commit is contained in:
parent
2a2b9fd8eb
commit
c65612a387
|
@ -1,10 +1,12 @@
|
||||||
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() {
|
||||||
|
if (!hasMapped) {
|
||||||
const entries = Object.entries(all());
|
const entries = Object.entries(all());
|
||||||
for (let i = 0; i < entries.length; i++) {
|
for (let i = 0; i < entries.length; i++) {
|
||||||
const [, M] = entries[i];
|
const [, M] = entries[i];
|
||||||
|
@ -12,6 +14,8 @@ function mapTypesToTraits() {
|
||||||
typeTo.set(Trait.type(), Trait);
|
typeTo.set(Trait.type(), Trait);
|
||||||
traitTo.set(Trait, M);
|
traitTo.set(Trait, M);
|
||||||
}
|
}
|
||||||
|
hasMapped = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function idFrom(Trait) {
|
export function idFrom(Trait) {
|
||||||
|
@ -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