Last active
August 31, 2018 16:06
-
-
Save mweststrate/501430974f81d44f84cc9fb359aeab96 to your computer and use it in GitHub Desktop.
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
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