Skip to content

Instantly share code, notes, and snippets.

@LukeWood
Created December 4, 2020 20:21
Show Gist options
  • Save LukeWood/31b3059e9193b8e71c53a467a967a310 to your computer and use it in GitHub Desktop.
Save LukeWood/31b3059e9193b8e71c53a467a967a310 to your computer and use it in GitHub Desktop.
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);
}
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);
}
}
@LukeWood
Copy link
Author

LukeWood commented Dec 4, 2020

The caveat you need to avoid here is that IDs must be globally unique.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment