import shortID from 'shortid' ; import crypto from 'crypto' ; export default class RoomIDService { takenRoomIDs : Set < string > ; nextSimpleRoomID : number ; constructor ( ) { this . takenRoomIDs = new Set < string > ( ) ; this . nextSimpleRoomID = 1 ; } getSimpleAvailableRoomID ( ) { this . nextSimpleRoomID += 1 ; return new Promise < string > ( ( resolve ) => { crypto . randomBytes ( 3 , ( _ , buffer ) => { resolve ( parseInt ( buffer . toString ( 'hex' ) , 16 ) . toString ( ) . substr ( 0 , 6 ) ) ; } ) ; } ) ; } getShortIDStringOfAvailableRoom ( ) { return new Promise < string > ( ( resolve ) => { let newID = shortID ( ) ; while ( this . takenRoomIDs . has ( newID ) ) { newID = shortID ( ) ; } resolve ( newID ) ; } ) ; } markRoomIDAsTaken ( id ) { this . takenRoomIDs . add ( id ) ; } unmarkRoomIDAsTaken ( id ) { this . takenRoomIDs . delete ( id ) ; } isRoomIDTaken ( id ) { return this . takenRoomIDs . has ( id ) ; } }