-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstate.ts
44 lines (38 loc) · 1.1 KB
/
state.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { type App } from 'vue'
import { randomId } from './util'
const currentSessionId = randomId()
/**
* 路由堆栈,存储 app stacks
*/
const routerStack: App[] = []
// 当前的 state
let currentState: { index: number; session: string } | undefined = undefined
const setCurrentState = (state?: typeof currentState) => (currentState = state)
const getCurrentState = () => currentState
let lastBackHookId: string | undefined = undefined
const setLastBackHookId = (id: typeof lastBackHookId) => (lastBackHookId = id)
const getLastBackHookId = () => lastBackHookId
const getLastApp = () => routerStack[routerStack.length - 1]
const backHooks: Record<string, () => void> = {}
const setBackHook = (id: string, resolve: () => void) => {
setLastBackHookId(id)
backHooks[id] = () => {
delete backHooks[id]
lastBackHookId = undefined
resolve()
}
}
const applyBackHook = (id?: string) => {
if (id) backHooks[id]?.apply(null)
}
export {
routerStack,
currentSessionId,
setBackHook,
applyBackHook,
getCurrentState,
setCurrentState,
setLastBackHookId,
getLastBackHookId,
getLastApp,
}