/**
 * Copyright (c) 2020 BlockDev AG
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */ import { action , makeObservable , observable , observe } from "mobx" ; import { History , Location , LocationListener , UnregisterCallback } from "history" ; export class RouterStore { location : Location = { pathname : "" , search : "" , state : "" , hash : "" , } ; history : SynchronizedHistory | undefined ; constructor ( ) { makeObservable ( this , { location : observable , updateLocation : action , } ) ; } updateLocation = ( newLocation ) : void => { this . location = newLocation ; } ; push = ( path ) : void => { this . history ?. push ( path ) ; } ; pushRelative = ( relativePath ) : void => { this . history ?. push ( relativePath ) ; } ; } interface SynchronizedHistory extends History { subscribe : ( listener : LocationListener ) => UnregisterCallback ; unsubscribe : UnregisterCallback ; } export const synchronizedHistory = ( history , store ) : History => { store . history = history as SynchronizedHistory ; const handleLocationChange = ( location ) => { store . updateLocation ( location ) ; } ; const unsubscribeFromHistory = history . listen ( handleLocationChange ) ; handleLocationChange ( history . location ) ; const subscribe = ( listener ) : UnregisterCallback => { const unsubscribeFromStore = observe ( store , "location" , ( { newValue } ) => { const rawLocation = { ... ( newValue as Location ) } ; listener ( rawLocation , history . action ) ; } ) ; listener ( store . location , history . action ) ; return unsubscribeFromStore ; } ; store . history . subscribe = subscribe ; store . history . unsubscribe = unsubscribeFromHistory ; return store . history ; } ;