import { ApplicationRef , Inject , Injectable , NgZone } from '@angular/core' ; import { BehaviorSubject , Subject } from 'rxjs' ; import { CookieService } from 'ngx-cookie-service' ; import { WINDOW } from '@ng-web-apis/common' ; declare global { interface Window { testingMode : { use : ( ) => void ; set : ( ) => void ; clear : ( ) => void ; uniswapSettings : { setSecondsDeadline : ( ) => void ; } ; web3PublicSettings : { setRpcTimeout : ( timeout : number ) => void ; } ; } ; tu : void ; ts : void ; tc : void ; } } @ Injectable ( { providedIn : 'root' } ) export class UseTestingModeService { public isTestingMode = new BehaviorSubject ( false ) ; public uniswapSettings = { secondsDeadline : new BehaviorSubject ( false ) } ; public web3PublicSettings = { rpcTimeout : new Subject < number > ( ) } ; constructor ( @ Inject ( WINDOW ) private window , private zone , private appRef , cookieService ) { this . window . testingMode = { use : ( ) => { this . useTestingMode ( ) ; } , set : ( ) => { cookieService . set ( 'testingMode' , 'true' , null , null , null , null , null ) ; this . useTestingMode ( ) ; } , clear : ( ) => { cookieService . delete ( 'testingMode' ) ; this . window . location . reload ( ) ; } , uniswapSettings : { setSecondsDeadline : ( ) => this . uniswapSettings . secondsDeadline . next ( true ) } , web3PublicSettings : { setRpcTimeout : ( timeout ) => this . web3PublicSettings . rpcTimeout . next ( timeout ) } } ; this . setAliases ( ) ; if ( cookieService . get ( 'testingMode' ) === 'true' ) { this . useTestingMode ( ) ; } } private setAliases ( ) { Object . defineProperty ( this . window , 'tu' , { get ( ) { this . window . testingMode . use ( ) ; } } ) ; Object . defineProperty ( this . window , 'ts' , { get ( ) { this . window . testingMode . set ( ) ; } } ) ; Object . defineProperty ( this . window , 'tc' , { get ( ) { this . window . testingMode . clear ( ) ; } } ) ; } private useTestingMode ( ) { if ( ! this . isTestingMode . getValue ( ) ) { this . isTestingMode . next ( true ) ; } this . zone . run ( ( ) => { setTimeout ( ( ) => this . appRef . tick ( ) , 100 ) ; } ) ; } }