refactor: easier testing

This commit is contained in:
cha0s 2024-07-07 17:26:17 -05:00
parent 667eaded8e
commit 0372b0ddf4

View File

@ -29,34 +29,41 @@ export default class Component {
}
async create(entityId, values) {
this.createMany([[entityId, values]]);
const [created] = await this.createMany([[entityId, values]]);
return created;
}
async createMany(entries) {
if (entries.length > 0) {
const allocated = this.allocateMany(entries.length);
const {properties} = this.constructor.schema.specification;
const keys = Object.keys(properties);
const promises = [];
for (let i = 0; i < entries.length; ++i) {
const [entityId, values = {}] = entries[i];
this.map[entityId] = allocated[i];
this.data[allocated[i]].entity = entityId;
for (let k = 0; k < keys.length; ++k) {
const j = keys[k];
const {defaultValue} = properties[j];
const instance = this.data[allocated[i]];
if (j in values) {
instance[j] = values[j];
}
else if ('undefined' !== typeof defaultValue) {
instance[j] = defaultValue;
}
}
promises.push(this.load(this.data[allocated[i]]));
}
await Promise.all(promises);
if (0 === entries.length) {
return [];
}
const allocated = this.allocateMany(entries.length);
const {properties} = this.constructor.schema.specification;
const keys = Object.keys(properties);
const promises = [];
for (let i = 0; i < entries.length; ++i) {
const [entityId, values = {}] = entries[i];
this.map[entityId] = allocated[i];
this.data[allocated[i]].entity = entityId;
for (let k = 0; k < keys.length; ++k) {
const j = keys[k];
const {defaultValue} = properties[j];
const instance = this.data[allocated[i]];
if (j in values) {
instance[j] = values[j];
}
else if ('undefined' !== typeof defaultValue) {
instance[j] = defaultValue;
}
}
promises.push(this.load(this.data[allocated[i]]));
}
await Promise.all(promises);
const created = [];
for (let i = 0; i < allocated.length; ++i) {
created.push(this.data[allocated[i]]);
}
return created;
}
deserialize(entityId, view, offset) {