avocado-old/packages/net/s13n/synchronized.js
2019-09-30 15:35:57 -05:00

56 lines
962 B
JavaScript

export function SynchronizedMixin(Superclass) {
return class Synchronized extends Superclass {
constructor(...args) {
super(...args);
this._idempotentPackets = [];
}
cleanPackets() {
this._idempotentPackets = [];
}
destroy() {}
fromNetwork(json) {
this.fromJSON(json);
}
packets(informed) {
return [];
}
packetsAreIdempotent() {
return true;
}
packetsFor(informed) {
if (this._idempotentPackets.length > 0) {
return this._idempotentPackets;
}
let packets = this.packets(informed);
if (!packets) {
return [];
}
if (!Array.isArray(packets)) {
packets = [packets];
}
if (this.packetsAreIdempotent()) {
this._idempotentPackets = packets;
}
return packets;
}
synchronizationId() {
return 0;
}
toNetwork(informed) {
return this.toJSON();
}
}
}