Created
December 4, 2020 20:21
-
-
Save LukeWood/31b3059e9193b8e71c53a467a967a310 to your computer and use it in GitHub Desktop.
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
const ids = new Set(); | |
export function isIdFree(id: string): boolean { | |
return ids.has(id); | |
} | |
export function takeId(id: string) { | |
ids.add(id); | |
} | |
export function freeId(id: string) { | |
ids.delete(id); | |
} |
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
import {isIdFree, freeId, takeId} from '../idManagement'; | |
class State extends Schema{} | |
export class SomeRoom extends Room<SomeState> { | |
onCreate (options: Partial<Types.RoomOptions>){ | |
this.setState(new State()) | |
if (options.roomId && idFree(options.roomId)) { | |
this.roomId = options.roomId; | |
} | |
takeId(this.roomId); | |
} | |
onDispose() { | |
freeId(this.roomId); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The caveat you need to avoid here is that IDs must be globally unique.