refactor: tidy

This commit is contained in:
cha0s 2022-06-08 12:35:54 -05:00
parent 5f5f998543
commit 436457aecc
2 changed files with 23 additions and 30 deletions

View File

@ -37,11 +37,7 @@ export default class Serializer {
if (!promise) { if (!promise) {
return Promise.resolve(); return Promise.resolve();
} }
return promise.then((resource) => { return promise.then((resource) => (resource ? fn(resource) : undefined));
if (resource) {
fn(resource);
}
});
} }
} }

View File

@ -11,7 +11,6 @@ export default (flecks) => (Resource) => (
async acceptPacket(packet) { async acceptPacket(packet) {
const {s13nType} = packet; const {s13nType} = packet;
// eslint-disable-next-line max-len
if (!s13nType) { if (!s13nType) {
return; return;
} }
@ -24,30 +23,7 @@ export default (flecks) => (Resource) => (
await this.destroySynchronized(type, id); await this.destroySynchronized(type, id);
break; break;
case 'update': case 'update':
if (this.$$synchronized[type]?.[id]) { await this.updateSynchronized(type, id, packet.data.packets);
const promises = [];
for (let i = 0; i < packet.data.packets.length; i++) {
const update = packet.data.packets[i];
promises.push(
Promise
.resolve(this.$$synchronized[type][id])
.then((resource) => {
resource.acceptPacket(update);
}),
);
}
await Promise.all(promises);
}
else {
await this.$$serializer.later(
`${type}:${id}`,
(resource) => {
for (let i = 0; i < packet.data.packets.length; i++) {
resource.acceptPacket(packet.data.packets[i]);
}
},
);
}
break; break;
default: default:
} }
@ -116,5 +92,26 @@ export default (flecks) => (Resource) => (
return this.$$synchronized[type]?.[id]; return this.$$synchronized[type]?.[id];
} }
async updateSynchronized(type, id, packets) {
if (this.$$synchronized[type]?.[id]) {
await Promise.all(
packets.map((update) => (
Promise
.resolve(this.$$synchronized[type][id])
.then((resource) => resource.acceptPacket(update))
)),
);
}
else {
await this.$$serializer.later(
`${type}:${id}`,
(resource) => Promise.all(
packets
.map((packet) => resource.acceptPacket(packet)),
),
);
}
}
} }
); );