refactor: s13n

This commit is contained in:
cha0s 2021-01-13 03:19:15 -06:00
parent c4e798a2a5
commit fae708aa24
6 changed files with 110 additions and 94 deletions

View File

@ -1,2 +0,0 @@
// eslint-disable-next-line import/prefer-default-export
export {default as Synchronizer} from './synchronizer';

View File

@ -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$/),
),

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -1,2 +0,0 @@
// eslint-disable-next-line import/prefer-default-export
export {default as Synchronizer} from './synchronizer';

View File

@ -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;
}
}