import { extractRequestError , LogService } from ".." ; export interface IJoinRoomStrategy { joinRoom ( roomIdOrAlias , userId , apiCall ) : Promise < string > ; } category export class SimpleRetryJoinStrategy implements IJoinRoomStrategy { private schedule = [ 0 , 1000 , 30 * 1000 , 5 * 60 * 1000 , 15 * 60 * 1000 , ] ; public joinRoom ( roomIdOrAlias , userId , apiCall ) { let currentSchedule = this . schedule [ 0 ] ; const doJoin = ( ) => waitPromise ( currentSchedule ) . then ( ( ) => apiCall ( roomIdOrAlias ) ) ; const errorHandler = err => { LogService . error ( "SimpleRetryJoinStrategy" , extractRequestError ( err ) ) ; const idx = this . schedule . indexOf ( currentSchedule ) ; if ( idx === this . schedule . length - 1 ) { LogService . warn ( "SimpleRetryJoinStrategy" , "Failed to join room " + roomIdOrAlias ) ; return Promise . reject ( err ) ; } else { currentSchedule = this . schedule [ idx + 1 ] ; return doJoin ( ) . catch ( errorHandler ) ; } } ; return doJoin ( ) . catch ( errorHandler ) ; } } function waitPromise ( interval ) { return new Promise ( ( resolve , reject ) => { setTimeout ( resolve , interval ) ; } ) ; }