Skip to content

Instantly share code, notes, and snippets.

@mweststrate
Last active August 31, 2018 16:06
Show Gist options
  • Save mweststrate/501430974f81d44f84cc9fb359aeab96 to your computer and use it in GitHub Desktop.
Save mweststrate/501430974f81d44f84cc9fb359aeab96 to your computer and use it in GitHub Desktop.
Server
export interface Change {
base: string;
changeId: string;
patches: Patch[];
}
export class Server {
clients: Client[] = [];
id = v4();
state: any = {};
async onReceiveChange(client: Client, change: Change): Promise<"NOPE" | "ACK"> {
if (change.base !== this.id) {
console.log("[server] rejecting change; " + client.name + " is behind");
return "NOPE";
}
this.state = applyPatches(this.state, change.patches);
this.id = change.changeId;
this.clients.filter(c => c !== client).forEach(c => c.onReceive(change));
return "ACK";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment