-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
198 lines (173 loc) · 4.84 KB
/
index.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import {
Action,
Blocker,
BroswerHistory,
BroswerHistoryOptions,
HistoryState,
Listener,
Location,
To,
Transition
} from 'types'
import {
createEvents,
readOnly,
createHref,
promptBeforeUnload,
createKey,
parsePath
} from 'utils'
const { Pop, Push, Replace } = Action
const PopStateEvent = 'popstate'
const BeforeUnloadEventType = 'beforeunload'
export const createBroswerHistory = (
options: BroswerHistoryOptions = {}
): BroswerHistory => {
const { window = document.defaultView } = options
if (!window) throw new Error('OH MY GOD, `window` is missing!!!')
const history = window.history
const getIndexAndLocation = (): [number, Location] => {
const { pathname, search, hash } = window.location
const state = history.state || {}
return [
state.idx,
readOnly<Location>({
pathname,
search,
hash,
state: state.usr || null,
key: state.key || 'default'
})
]
}
let blockedTopTx: Transition | null = null
const listeners = createEvents<Listener>()
const blockers = createEvents<Blocker>()
let [index, location] = getIndexAndLocation()
let action = Pop
// or === undefined
if (index == null) {
index = 0
history.replaceState(
{
...history,
idx: index
},
''
)
}
const getNextLocation = (to: To, state: any = null): Location => {
return readOnly<Location>({
pathname: location.pathname,
search: '',
hash: '',
...(typeof to === 'string' ? parsePath(to) : to),
state,
key: createKey()
})
}
const getHistoryStateAndUrl = (
nextLocation: Location,
index: number
): [HistoryState, string] => [
{ usr: nextLocation.state, key: nextLocation.key, idx: index },
createHref(nextLocation)
]
const allowTx = (action: Action, location: Location, retry: () => void) =>
!blockers.length || (blockers.call({ action, location, retry }), false)
const applyTx = (nextAction: Action) => {
action = nextAction
;[index, location] = getIndexAndLocation()
listeners.call({ action, location })
}
const push = (to: To, state?: any) => {
const nextAction = Push
const nextLocation = getNextLocation(to, state)
const retry = () => push(to, state)
if (allowTx(nextAction, nextLocation, retry)) {
const [historyState, url] = getHistoryStateAndUrl(nextLocation, index + 1)
// TODO: Support forced reloading
// try...catch because iOS limits us to 100 pushState calls :/
try {
history.pushState(historyState, '', url)
} catch (error) {
// They are going to lose state here, but there is no real
// way to warn them about it since the page will refresh...
window.location.assign(url)
}
applyTx(nextAction)
}
}
const replace = (to: To, state?: any) => {
const nextAction = Replace
const nextLocation = getNextLocation(to, state)
const retry = () => push(to, state)
if (allowTx(nextAction, nextLocation, retry)) {
const [historyState, url] = getHistoryStateAndUrl(nextLocation, index + 1)
// TODO: Support forced reloading
history.replaceState(historyState, '', url)
applyTx(nextAction)
}
}
const handlePop = () => {
if (blockedTopTx) {
blockers.call(blockedTopTx)
blockedTopTx = null
} else {
const nextAction = Pop
const [nextIndex, nextLocation] = getIndexAndLocation()
if (blockers.length) {
// here is !=, not !==, {}.idx should be undefined!!!
if (nextIndex != null) {
const delta = index - nextIndex
const retry = () => go(delta * -1)
if (delta) {
blockedTopTx = {
action: nextAction,
location: nextLocation,
retry
}
go(delta)
}
}
} else {
applyTx(nextAction)
}
}
}
window.addEventListener(PopStateEvent, handlePop)
const go = (delta: number) => history.go(delta)
const back = () => history.go(-1)
const forward = () => history.go(1)
const listen = (listener: Listener) => listeners.push(listener)
const block = (blocker: Blocker) => {
const unblock = blockers.push(blocker)
if (blockers.length === 1)
window.addEventListener(BeforeUnloadEventType, promptBeforeUnload)
return () => {
unblock()
// Remove the beforeunload listener so the document may
// still be salvageable in the pagehide event.
// See https://html.spec.whatwg.org/#unloading-documents
if (!blockers.length) {
window.removeEventListener(BeforeUnloadEventType, promptBeforeUnload)
}
}
}
return {
get action() {
return action
},
get location() {
return location
},
push,
replace,
createHref,
go,
back,
forward,
listen,
block
}
}