import * as SupData from "./index" ; import * as path from "path" ; const roomRegex = /^[A-Za-z0-9_]{1,20}$/ ; export default class Rooms extends SupData . Base . Dictionary { constructor ( public server ) { super ( ) ; } acquire ( id , owner , callback ) { if ( ! roomRegex . test ( id ) ) { callback ( new Error ( `Invalid room id: ${ id }` ) ) ; return ; } super . acquire ( id , owner , ( err , item ) => { if ( err != null ) { callback ( err ) ; return ; } if ( owner == null ) { callback ( null , item ) ; return ; } item . join ( owner , ( err , roomUser , index ) => { if ( err != null ) { callback ( new Error ( err ) ) ; return ; } this . server . io . in ( `sub:rooms:${ id }` ) . emit ( "edit:rooms" , id , "join" , roomUser , index ) ; callback ( null , item ) ; } ) ; } ) ; } release ( id , owner , options ? ) { super . release ( id , owner , options ) ; if ( owner == null ) return ; this . byId [ id ] . leave ( owner , ( err , roomUserId ) => { if ( err != null ) throw new Error ( err ) ; this . server . io . in ( `sub:rooms:${ id }` ) . emit ( "edit:rooms" , id , "leave" , roomUserId ) ; } ) ; } _load ( id ) { const room = new SupData . Room ( null ) ; room . load ( path . join ( this . server . projectPath , `rooms/${ id }` ) ) ; return room ; } }