avocado-old/packages/state/synchronizer.js

33 lines
865 B
JavaScript
Raw Normal View History

2019-04-07 20:04:40 -05:00
export class Synchronizer {
2019-03-20 18:33:13 -05:00
2019-05-13 21:07:51 -05:00
constructor(children) {
this.children = children;
2019-05-14 03:42:26 -05:00
this.childrenPacketsForUpdate = this.children.map((child) => {
return child.packetsForUpdate.bind(child);
});
2019-03-20 18:33:13 -05:00
}
2019-05-13 21:07:51 -05:00
acceptPacket(packet) {
for (let i = 0; i < this.children.length; i++) {
this.children[i].acceptPacket(packet);
2019-04-05 15:16:55 -05:00
}
}
2019-05-21 03:09:06 -05:00
addChild(child) {
this.children.push(child);
this.childrenPacketsForUpdate.push(child.packetsForUpdate.bind(child));
}
2019-05-13 21:07:51 -05:00
packetsForUpdate(force = false) {
const packetsForUpdate = [];
2019-05-14 03:42:26 -05:00
for (let i = 0; i < this.childrenPacketsForUpdate.length; i++) {
const childPacketsForUpdate = this.childrenPacketsForUpdate[i](force);
2019-05-13 21:07:51 -05:00
for (let j = 0; j < childPacketsForUpdate.length; j++) {
packetsForUpdate.push(childPacketsForUpdate[j]);
}
2019-04-23 15:25:03 -05:00
}
2019-05-13 21:07:51 -05:00
return packetsForUpdate;
2019-03-20 18:33:13 -05:00
}
}