import { LocationChangeListener , PlatformLocation } from '@angular/common' ; import { Injectable , OnDestroy } from '@angular/core' ; import { Subscription } from 'rxjs' ; import { CommandType , fnArg , LocationAdapter , LocationCommand , MessageBus , ObjectStore , SerializerTypes } from '../shared' ; import { ServerLocationState } from './server-location-state' ; @ Injectable ( ) export class ServerPlatformLocation extends PlatformLocation implements OnDestroy { get hash ( ) : string { return this . state . hash ; } get hostname ( ) : string { return this . state . hostname ; } get href ( ) : string { return this . state . href ; } get pathname ( ) : string { return this . state . pathname ; } get port ( ) : string { return this . state . port ; } get protocol ( ) : string { return this . state . protocol ; } get search ( ) : string { return this . state . search ; } private subscription : Subscription ; constructor ( private adapter , private bus , private state , private store ) { super ( ) ; this . subscription = this . bus . getCommands ( ) . subscribe ( ( command ) => { if ( command . target === 'location' ) { this . handleLocationCommand ( command ) ; } } ) ; } back ( ) { this . adapter . back ( ) ; } forward ( ) { this . adapter . forward ( ) ; } getBaseHrefFromDOM ( ) { return this . state . baseHref ; } getState ( ) { return this . state . state ; } onHashChange ( fn ) { this . store . allocateNode ( fn ) ; this . adapter . onHashChange ( fnArg ( fn , SerializerTypes . STORE_OBJECT ) ) ; } onPopState ( fn ) { this . store . allocateNode ( fn ) ; this . adapter . onPopState ( fnArg ( fn , SerializerTypes . STORE_OBJECT ) ) ; } pushState ( state , title , url ) { this . adapter . pushState ( fnArg ( state ) , fnArg ( title ) , fnArg ( url ) ) ; } replaceState ( state , title , url ) { this . adapter . replaceState ( fnArg ( state ) , fnArg ( title ) , fnArg ( url ) ) ; } ngOnDestroy ( ) { if ( this . subscription ) { this . subscription . unsubscribe ( ) ; this . subscription = null ; } } private handleLocationCommand ( command ) { switch ( command . method ) { case 'getBaseHrefFromDOM' : this . adapter . getBaseHrefFromDOM ( fnArg ( command . fnArgs [ 0 ] ) ) ; break ; case 'getState' : this . adapter . getState ( fnArg ( command . fnArgs [ 0 ] ) ) ; break ; case 'getLocation' : this . adapter . getLocation ( fnArg ( command . fnArgs [ 0 ] ) ) ; break ; case 'hashChange' : this . adapter . hashChange ( fnArg ( command . fnArgs [ 0 ] , SerializerTypes . STORE_OBJECT ) , fnArg ( command . fnArgs [ 1 ] ) ) ; break ; case 'popState' : this . adapter . popState ( fnArg ( command . fnArgs [ 0 ] , SerializerTypes . STORE_OBJECT ) , fnArg ( command . fnArgs [ 1 ] ) ) ; break ; } } }