Skip to content

Instantly share code, notes, and snippets.

@mweststrate
Last active August 31, 2018 17:44
Show Gist options
  • Save mweststrate/fa556034b14fc0748ff608418d7a5765 to your computer and use it in GitHub Desktop.
Save mweststrate/fa556034b14fc0748ff608418d7a5765 to your computer and use it in GitHub Desktop.
Rebase actions after receiving changes by the server
onReceive(change: Change) {
this.confirmedState = {
id: change.changeId,
state: applyPatches(this.confirmedState.state, change.patches)
};
// reapply all the pending actions, filter out the ones that can't be applied anymore
const { pendingActions } = this;
// reset the pending changes collection. We will fill it again by replaying all actions.
this.pendingActions = [];
this.currentState = this.confirmedState.state;
let p;
while ((p = pendingActions.shift())) {
try {
this.propose(p.fn);
} catch (e) {
console.warn(
`Dropped change ${p.id}, it could not be applied anymore: ${e}`
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment