avocado-old/packages/net/s13n/synchronized.js

44 lines
809 B
JavaScript
Raw Normal View History

2019-09-16 21:52:08 -05:00
export function SynchronizedMixin(Superclass) {
return class Synchronized extends Superclass {
2019-09-22 18:45:33 -05:00
constructor(...args) {
super(...args);
2019-09-16 21:52:08 -05:00
this._idempotentPackets = [];
}
cleanPackets() {
this._idempotentPackets = [];
}
2019-09-22 18:45:33 -05:00
destroy() {}
packets(informed) {}
2019-09-16 21:52:08 -05:00
packetsAreIdempotent() {
return true;
}
2019-09-22 18:45:33 -05:00
packetsFor(informed) {
2019-09-16 21:52:08 -05:00
if (this._idempotentPackets.length > 0) {
return this._idempotentPackets;
}
2019-09-22 18:45:33 -05:00
let packets = this.packets(informed);
2019-09-16 21:52:08 -05:00
if (!packets) {
return [];
}
packets = Array.isArray(packets) ? packets : [packets];
if (this.packetsAreIdempotent()) {
this._idempotentPackets = packets;
}
return packets;
}
2019-09-22 18:45:33 -05:00
synchronizationId() {
return 0;
}
2019-09-16 21:52:08 -05:00
}
}