Skip to content

Instantly share code, notes, and snippets.

@mweststrate
Created August 31, 2018 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mweststrate/1c15569ee4ee1f7b423c06d11fe887a7 to your computer and use it in GitHub Desktop.
Save mweststrate/1c15569ee4ee1f7b423c06d11fe887a7 to your computer and use it in GitHub Desktop.
Sending changes to the server
async distributeFirstChange() {
const p = this.pendingActions[0];
const response = await this.server.propose(this, {
base: this.confirmedState.id,
patches: p.patches,
changeId: p.id
});
if (response === "NOPE") {
console.warn(this.name + ": Change rejected; outdated");
// we keep the actions for now, and we will try to replay them after receiving the next change
} else {
// cool, accepted!
this.confirmedState = {
id: p.id,
state: applyPatches(this.confirmedState.state, p.patches)
};
this.pendingActions.shift()!;
// send the next change
if (this.pendingActions.length) this.distributeFirstChange();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment