import type Timer from '../global/timer'; import reactNavigationConfig from './reactNavigationConfig'; export interface ReactNavigationPageInfo { id: string; screenName: string; custom: object; title?: string; path?: string; isback: boolean; rootTag: number; timer?: Timer; } export function buildPage(info: ReactNavigationPageInfo) { const infoConfig = reactNavigationConfig.getScreenConfig(info.screenName); return { page_key: info.screenName, page_title: info.title || info.screenName || '', page_manual_key: infoConfig?.pageID ?? '', page_path: info.path || infoConfig?.pagePath || '', page_custom: info.custom || {}, is_back: info.isback, }; } class PageStack { stack: ReactNavigationPageInfo[] = []; _pageSet: Set = new Set(); constructor() {} getCurrent(): ReactNavigationPageInfo | undefined { if (this.stack.length) { return this.stack[this.stack.length - 1]; } return undefined; } getPage(id: string): ReactNavigationPageInfo | undefined { if (!this._pageSet.has(id)) { return undefined; } return this.stack.filter((page) => page.id === id)[0]; } has(id: string): boolean { return this._pageSet.has(id); } push(pageInfo: ReactNavigationPageInfo) { this.stack.push(pageInfo); this._pageSet.add(pageInfo.id); } backTo(id: string) { let index = this.stack.length - 1; for (let i = index; i >= 0; i--) { const pageInfo = this.stack[i]; if (pageInfo.id === id) { index = i; break; } } this.stack.splice(index + 1, this.stack.length - index); this._pageSet = new Set(this.stack.map((v) => v.id)); } clear() { this.stack = []; this._pageSet = new Set(); } } const pageStackMap = new Map(); export default function getPageStack(rootTag: number): PageStack { if (!pageStackMap.has(rootTag)) { pageStackMap.set(rootTag, new PageStack()); } return pageStackMap.get(rootTag)!; }