import { Injectable } from '@angular/core' ; import { command , FnArg , LocationAdapter , LocationMethods , MessageBus , PrimitiveArg , Serializer , StoreObjectArg } from '../shared' ; import { ServerLocationState } from './server-location-state' ; @ Injectable ( ) export class ServerLocationAdapter extends LocationAdapter { constructor ( private bus , private state , private serializer ) { super ( ) ; } back ( ) { this . callBrowserLocation ( 'back' , ... arguments ) ; } forward ( ) { this . callBrowserLocation ( 'forward' , ... arguments ) ; } getBaseHrefFromDOM ( baseHrefArg ) { const [ baseHref ] = this . deserialize ( ... arguments ) ; this . state . baseHref = baseHref ; } getState ( stateArg ) { const [ state ] = this . deserialize ( ... arguments ) ; this . state . state = state ; } getLocation ( locationArg ) { const [ location ] = this . deserialize ( ... arguments ) ; this . state . location = location ; } onHashChange ( handlerArg ) { this . callBrowserLocation ( 'onHashChange' , ... arguments ) ; } hashChange ( handlerArg , eventArg ) { const [ fn , event ] = this . deserialize ( ... arguments ) ; if ( typeof fn === 'function' ) { fn ( event ) ; } } onPopState ( handlerArg ) { this . callBrowserLocation ( 'onPopState' , ... arguments ) ; } popState ( handlerArg , eventArg ) { const [ fn , event ] = this . deserialize ( ... arguments ) ; if ( typeof fn === 'function' ) { fn ( event ) ; } } pushState ( stateArg , titleArg , urlArg ) { this . callBrowserLocation ( 'pushState' , ... arguments ) ; } replaceState ( stateArg , titleArg , urlArg ) { this . callBrowserLocation ( 'replaceState' , ... arguments ) ; } private callBrowserLocation ( method , ... fnArgs ) { const payload = fnArgs . map ( arg => this . serializer . serialize ( arg . value , arg . type ) ) ; this . bus . sendCommand ( command ( 'location' , method , payload ) ) ; } private deserialize ( ... fnArgs ) { return fnArgs . map ( arg => this . serializer . deserialize ( arg . value , arg . type ) ) ; } }