From fae708aa24272a781a59187151981c10deb14bf1 Mon Sep 17 00:00:00 2001 From: cha0s Date: Wed, 13 Jan 2021 03:19:15 -0600 Subject: [PATCH] refactor: s13n --- packages/s13n/src/client/index.js | 2 - packages/s13n/src/index.js | 13 +-- ...nchronizer.js => receiver-synchronizer.js} | 31 ++++--- packages/s13n/src/sender-synchronizer.js | 91 +++++++++++++++++++ packages/s13n/src/server/server.js | 2 - packages/s13n/src/server/synchronizer.js | 65 ------------- 6 files changed, 110 insertions(+), 94 deletions(-) delete mode 100644 packages/s13n/src/client/index.js rename packages/s13n/src/{client/synchronizer.js => receiver-synchronizer.js} (52%) create mode 100644 packages/s13n/src/sender-synchronizer.js delete mode 100644 packages/s13n/src/server/server.js delete mode 100644 packages/s13n/src/server/synchronizer.js diff --git a/packages/s13n/src/client/index.js b/packages/s13n/src/client/index.js deleted file mode 100644 index 64c3ab0..0000000 --- a/packages/s13n/src/client/index.js +++ /dev/null @@ -1,2 +0,0 @@ -// eslint-disable-next-line import/prefer-default-export -export {default as Synchronizer} from './synchronizer'; diff --git a/packages/s13n/src/index.js b/packages/s13n/src/index.js index 05f6b12..f13045d 100644 --- a/packages/s13n/src/index.js +++ b/packages/s13n/src/index.js @@ -1,21 +1,14 @@ -import {gather, gatherWithLatus} from '@latus/core'; +import {gatherWithLatus} from '@latus/core'; export * from './packets'; export {default as normalize} from './normalize'; +export {default as ReceiverSynchronizer} from './receiver-synchronizer'; +export {default as SenderSynchronizer} from './sender-synchronizer'; export {default as Synchronized, synchronized} from './synchronized'; export default { hooks: { - '@latus/core/starting': (latus) => { - // eslint-disable-next-line no-param-reassign - latus.config['%synchronized'] = gather( - latus, - '@avocado/s13n/synchronized', - 's13nId', - 's13nType', - ); - }, '@latus/socket/packets': gatherWithLatus( require.context('./packets', false, /\.js$/), ), diff --git a/packages/s13n/src/client/synchronizer.js b/packages/s13n/src/receiver-synchronizer.js similarity index 52% rename from packages/s13n/src/client/synchronizer.js rename to packages/s13n/src/receiver-synchronizer.js index aa5c0d1..0a7e437 100644 --- a/packages/s13n/src/client/synchronizer.js +++ b/packages/s13n/src/receiver-synchronizer.js @@ -1,10 +1,11 @@ import {resource} from '@avocado/resource'; -export default class ClientSynchronizer { +export default class ReceiverSynchronizer { + + #synchronized = {}; constructor(latus) { this.latus = latus; - this._synchronized = {}; } acceptPacket(packet) { @@ -13,27 +14,27 @@ export default class ClientSynchronizer { case 'create': { const {id, type} = packet.data.synchronized; const {fromResourceId: {[type]: Resource}} = resource(this.latus); - if (!(type in this._synchronized)) { - this._synchronized[type] = {}; + if (!(type in this.#synchronized)) { + this.#synchronized[type] = {}; } const json = packet.data.spec; - if (this._synchronized[type][id]) { - this._synchronized[type][id].fromNetwork(json); + if (this.#synchronized[type][id]) { + this.#synchronized[type][id].fromNetwork(json); } else { - this._synchronized[type][id] = new Resource(json); + this.#synchronized[type][id] = new Resource(json); } break; } case 'destroy': { const {id, type} = packet.data.synchronized; - this._synchronized[type][id].destroy(); - this._synchronized[type][id] = null; + this.#synchronized[type][id].destroy(); + delete this.#synchronized[type][id]; break; } case 'update': { const {id, type} = packet.data.synchronized; - this._synchronized[type][id].acceptPacket(packet); + this.#synchronized[type][id].acceptPacket(packet); break; } default: @@ -41,16 +42,16 @@ export default class ClientSynchronizer { } addSynchronized(synchronized) { - const {s13nType: type} = synchronized.constructor; - if (!(type in this._synchronized)) { - this._synchronized[type] = {}; + const {resourceId: type} = synchronized.constructor; + if (!(type in this.#synchronized)) { + this.#synchronized[type] = {}; } const id = synchronized.s13Id(); - this._synchronized[type][id] = synchronized; + this.#synchronized[type][id] = synchronized; } synchronized(type, id) { - return this._synchronized[type] ? this._synchronized[type][id] : undefined; + return this.#synchronized[type] ? this.#synchronized[type][id] : undefined; } } diff --git a/packages/s13n/src/sender-synchronizer.js b/packages/s13n/src/sender-synchronizer.js new file mode 100644 index 0000000..4be58dc --- /dev/null +++ b/packages/s13n/src/sender-synchronizer.js @@ -0,0 +1,91 @@ +export default class SenderSynchronizer { + + #added = []; + + #nextSyncPackets = []; + + #queuedPackets = []; + + #removed = []; + + #synchronized = {}; + + #synchronizedFlat = []; + + addSynchronized(synchronized) { + if (this.hasSynchronized(synchronized)) { + return; + } + this.#added.push(synchronized); + const {resourceId: type} = synchronized.constructor; + if (!(type in this.#synchronized)) { + this.#synchronized[type] = {}; + } + this.#synchronizedFlat.push(synchronized); + const id = synchronized.s13Id(); + this.#synchronized[type][id] = synchronized; + } + + destroy() { + this.#queuedPackets = []; + this.#synchronized = {}; + } + + hasSynchronized(synchronized) { + return -1 !== this.#synchronizedFlat.indexOf(synchronized); + } + + packetsFor(informed) { + const payload = []; + for (let i = 0; i < this.#synchronizedFlat.length; i++) { + const synchronized = this.#synchronizedFlat[i]; + if (-1 !== this.#added.indexOf(synchronized)) { + payload.push(synchronized.createPacket(informed)); + } + else if (-1 !== this.#removed.indexOf(synchronized)) { + payload.push(synchronized.destroyPacket(informed)); + } + else { + const packets = synchronized.packetsFor(informed); + for (let j = 0; j < packets.length; j++) { + payload.push(packets[j]); + } + } + } + this.#added = []; + this.#removed = []; + return payload; + } + + removeSynchronized(synchronized) { + if (!this.hasSynchronized(synchronized)) { + return; + } + this.#removed.push(synchronized); + const index = this.#synchronizedFlat.indexOf(synchronized); + this.#synchronizedFlat.splice(index, 1); + const {resourceId: type} = synchronized.constructor; + const id = synchronized.s13Id(); + delete this.#synchronized[type][id]; + } + + send(socket, informed) { + const synchronizerPackets = this.packetsFor(informed); + for (let i = 0; i < synchronizerPackets.length; i++) { + this.#nextSyncPackets.push(synchronizerPackets[i]); + } + for (let i = 0; i < this.#queuedPackets.length; i++) { + this.#nextSyncPackets.push(this.#queuedPackets[i]); + } + this.#queuedPackets = []; + if (socket && this.#nextSyncPackets.length > 0) { + socket.send(['BundlePacket', this.#nextSyncPackets]); + this.#nextSyncPackets = []; + } + } + + queuePacket(packet) { + this.#queuedPackets.push(packet); + } + +} diff --git a/packages/s13n/src/server/server.js b/packages/s13n/src/server/server.js deleted file mode 100644 index 64c3ab0..0000000 --- a/packages/s13n/src/server/server.js +++ /dev/null @@ -1,2 +0,0 @@ -// eslint-disable-next-line import/prefer-default-export -export {default as Synchronizer} from './synchronizer'; diff --git a/packages/s13n/src/server/synchronizer.js b/packages/s13n/src/server/synchronizer.js deleted file mode 100644 index 7996208..0000000 --- a/packages/s13n/src/server/synchronizer.js +++ /dev/null @@ -1,65 +0,0 @@ -export default class ServerSynchronizer { - - constructor() { - this._added = []; - this._removed = []; - this._synchronized = {}; - this._synchronizedFlat = []; - } - - addSynchronized(synchronized) { - if (this.hasSynchronized(synchronized)) { - return; - } - this._added.push(synchronized); - const {s13nType: type} = synchronized.constructor; - if (!(type in this._synchronized)) { - this._synchronized[type] = {}; - } - this._synchronizedFlat.push(synchronized); - const id = synchronized.s13Id(); - this._synchronized[type][id] = synchronized; - } - - // eslint-disable-next-line class-methods-use-this - destroy() {} - - hasSynchronized(synchronized) { - return -1 !== this._synchronizedFlat.indexOf(synchronized); - } - - removeSynchronized(synchronized) { - if (!this.hasSynchronized(synchronized)) { - return; - } - this._removed.push(synchronized); - const index = this._synchronizedFlat.indexOf(synchronized); - this._synchronizedFlat.splice(index, 1); - const {s13nType: type} = synchronized.constructor; - const id = synchronized.s13Id(); - delete this._synchronized[type][id]; - } - - packetsFor(informed) { - const payload = []; - for (let i = 0; i < this._synchronizedFlat.length; i++) { - const synchronized = this._synchronizedFlat[i]; - if (-1 !== this._added.indexOf(synchronized)) { - payload.push(synchronized.createPacket(informed)); - } - else if (-1 !== this._removed.indexOf(synchronized)) { - payload.push(synchronized.destroyPacket(informed)); - } - else { - const packets = synchronized.packetsFor(informed); - for (let j = 0; j < packets.length; j++) { - payload.push(packets[j]); - } - } - } - this._added = []; - this._removed = []; - return payload; - } - -}