Last active
August 31, 2018 12:50
-
-
Save mweststrate/845ed41e12142161ea4cf55d5dee16f0 to your computer and use it in GitHub Desktop.
Client state
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
interface PendingAction { | |
id: string; | |
fn: (draft: any) => void; | |
patches: Patch[]; | |
} | |
export class Client { | |
pendingActions: PendingAction[] = []; | |
confirmedState!: { | |
id: string; | |
state: any; | |
}; | |
currentState: any; | |
propose(fn: (draft: any) => void) { | |
const id = v4(); | |
let patches!: Patch[]; | |
this.currentState = produce(this.currentState, fn, p => { | |
patches = p; | |
}); | |
this.pendingActions.push({ | |
id, | |
fn, | |
patches | |
}); | |
if (this.pendingActions.length === 1) this.distributeFirstChange(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment