Skip to content

Instantly share code, notes, and snippets.

@mweststrate
Last active August 31, 2018 12:50
Show Gist options
  • Save mweststrate/845ed41e12142161ea4cf55d5dee16f0 to your computer and use it in GitHub Desktop.
Save mweststrate/845ed41e12142161ea4cf55d5dee16f0 to your computer and use it in GitHub Desktop.
Client state
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