From 436457aecc91f88b7b295fd501c0b94926a3e20a Mon Sep 17 00:00:00 2001 From: cha0s Date: Wed, 8 Jun 2022 12:35:54 -0500 Subject: [PATCH] refactor: tidy --- packages/s13n/src/serializer.js | 6 +-- packages/s13n/src/synchronized/client.js | 47 +++++++++++------------- 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/packages/s13n/src/serializer.js b/packages/s13n/src/serializer.js index 88c0b71..bc3acd2 100644 --- a/packages/s13n/src/serializer.js +++ b/packages/s13n/src/serializer.js @@ -37,11 +37,7 @@ export default class Serializer { if (!promise) { return Promise.resolve(); } - return promise.then((resource) => { - if (resource) { - fn(resource); - } - }); + return promise.then((resource) => (resource ? fn(resource) : undefined)); } } diff --git a/packages/s13n/src/synchronized/client.js b/packages/s13n/src/synchronized/client.js index 5d4c928..b61c89d 100644 --- a/packages/s13n/src/synchronized/client.js +++ b/packages/s13n/src/synchronized/client.js @@ -11,7 +11,6 @@ export default (flecks) => (Resource) => ( async acceptPacket(packet) { const {s13nType} = packet; - // eslint-disable-next-line max-len if (!s13nType) { return; } @@ -24,30 +23,7 @@ export default (flecks) => (Resource) => ( await this.destroySynchronized(type, id); break; case 'update': - if (this.$$synchronized[type]?.[id]) { - 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]); - } - }, - ); - } + await this.updateSynchronized(type, id, packet.data.packets); break; default: } @@ -116,5 +92,26 @@ export default (flecks) => (Resource) => ( 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)), + ), + ); + } + } + } );