import { route } from '@goldfishjs/route' ; import { observable , state } from '@goldfishjs/reactive-connect' ; import Plugin , { GetPlugin } from './Plugin' ; import BridgePlugin from './BridgePlugin' ; interface IPage { path : string ; query ? : Record < string , any > ; referrerInfo ? : any ; } @ observable export default class Route extends Plugin { public static type = 'route' ; private bridge : BridgePlugin = new BridgePlugin ( ) ; @ state public pages : IPage [ ] = [ ] ; public async init ( getPlugin ) { this . bridge = getPlugin ( BridgePlugin ) ; } public destroy ( ) { } private setPages ( ) { const currentPages = getCurrentPages ( ) ; const newPageStack = currentPages . map ( ( page ) => ( { path : page . route , } ) ) ; const newPages = newPageStack . map ( ( newPage ) => this . pages . find ( ( page ) => page . path === newPage . path ) || newPage ) ; this . pages = [ ... newPages ] ; } public updatePages ( options ? ) { this . setPages ( ) ; if ( ! options || ! options . path || ! ( options . query || options . referrerInfo ) ) return ; const index = this . pages . findIndex ( ( page ) => page . path === options . path ) ; if ( index >= 0 ) { this . pages [ index ] . query = options . query ; this . pages [ index ] . referrerInfo = options . referrerInfo ; } } public async reLaunch ( url ) { try { await this . bridge . call ( 'reLaunch' , { url , } ) ; } catch ( e ) { route . pushWindow ( url ) ; } } public popWindow ( delta = 1 ) { this . bridge . call ( 'navigateBack' , { delta } ) ; } public popToHome ( ) { this . popWindow ( getCurrentPages ( ) . length - 1 ) ; } public async switchTab ( options ) { await this . bridge . call ( 'switchTab' , { ... options , } ) ; } public backToPage ( page ) { const pages = getCurrentPages ( ) ; let targetIndex = - 1 ; pages . some ( ( p , index ) => { if ( p . route === page ) { targetIndex = index ; return true ; } return false ; } ) ; this . popWindow ( pages . length - targetIndex - 1 ) ; } public pushWindow ( ... args ) { return route . pushWindow ( ... args ) ; } }