refactor: Bundle.normalize

This commit is contained in:
cha0s 2022-09-17 06:50:02 -05:00
parent ee566e7c70
commit 12f17641e9
2 changed files with 10 additions and 8 deletions

View File

@ -30,6 +30,11 @@ export default class Bundle {
} }
static maybeNormalize(BundleLike) { static maybeNormalize(BundleLike) {
const normalized = this.normalize(BundleLike);
return normalized.prototype instanceof Bundle ? normalized : undefined;
}
static normalize(BundleLike) {
if (Array.isArray(BundleLike)) { if (Array.isArray(BundleLike)) {
return class AdhocBundle extends Bundle { return class AdhocBundle extends Bundle {
@ -37,10 +42,7 @@ export default class Bundle {
}; };
} }
if (BundleLike.prototype instanceof Bundle) { return BundleLike;
return BundleLike;
}
return undefined;
} }
static select(entity, Components) { static select(entity, Components) {

View File

@ -7,7 +7,7 @@ import Query from '../src/query';
const A = Component.normalize({a: {type: 'int32', defaultValue: 420}}); const A = Component.normalize({a: {type: 'int32', defaultValue: 420}});
const B = Component.normalize({b: {type: 'int32', defaultValue: 69}}); const B = Component.normalize({b: {type: 'int32', defaultValue: 69}});
const C = Component.normalize({c: 'int32'}); const C = Component.normalize({c: 'int32'});
const D = Bundle.maybeNormalize(['B', 'C']); const D = Bundle.normalize(['B', 'C']);
class E extends Bundle { class E extends Bundle {
static Components = ['A', 'B']; static Components = ['A', 'B'];
@ -24,9 +24,9 @@ const ComponentsAndOrBundles = {
B: new B(), B: new B(),
C: new C(), C: new C(),
}; };
ComponentsAndOrBundles.A.createMany([2n, 3n]); ComponentsAndOrBundles.A.createMany([[2n], [3n]]);
ComponentsAndOrBundles.B.createMany([1n, 2n]); ComponentsAndOrBundles.B.createMany([[1n], [2n]]);
ComponentsAndOrBundles.C.createMany([2n, 4n]); ComponentsAndOrBundles.C.createMany([[2n], [4n]]);
ComponentsAndOrBundles.D = new D([ComponentsAndOrBundles.B, ComponentsAndOrBundles.C]); ComponentsAndOrBundles.D = new D([ComponentsAndOrBundles.B, ComponentsAndOrBundles.C]);
ComponentsAndOrBundles.E = new E([ComponentsAndOrBundles.A, ComponentsAndOrBundles.B]); ComponentsAndOrBundles.E = new E([ComponentsAndOrBundles.A, ComponentsAndOrBundles.B]);