Last active
August 31, 2018 17:44
-
-
Save mweststrate/fa556034b14fc0748ff608418d7a5765 to your computer and use it in GitHub Desktop.
Rebase actions after receiving changes by the server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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