avocado-old/packages/state/synchronizer.js
2019-05-21 03:09:06 -05:00

33 lines
865 B
JavaScript

export class Synchronizer {
constructor(children) {
this.children = children;
this.childrenPacketsForUpdate = this.children.map((child) => {
return child.packetsForUpdate.bind(child);
});
}
acceptPacket(packet) {
for (let i = 0; i < this.children.length; i++) {
this.children[i].acceptPacket(packet);
}
}
addChild(child) {
this.children.push(child);
this.childrenPacketsForUpdate.push(child.packetsForUpdate.bind(child));
}
packetsForUpdate(force = false) {
const packetsForUpdate = [];
for (let i = 0; i < this.childrenPacketsForUpdate.length; i++) {
const childPacketsForUpdate = this.childrenPacketsForUpdate[i](force);
for (let j = 0; j < childPacketsForUpdate.length; j++) {
packetsForUpdate.push(childPacketsForUpdate[j]);
}
}
return packetsForUpdate;
}
}