-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHistoryObserver.ts
More file actions
52 lines (41 loc) · 1.15 KB
/
HistoryObserver.ts
File metadata and controls
52 lines (41 loc) · 1.15 KB
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
import { Action } from 'history'
/** Wrapper around History to abstract the API surface & types */
export class HistoryObserver {
constructor(
private history: () => HistorySubset,
private onChange: (update: HistoryUpdate) => void,
) {}
public dispose?(): void
public listen() {
const history = this.history()
this.dispose?.()
this.onChange({ location: history.location, action: Action.Pop })
this.dispose = history.listen(this.onChange)
return this
}
}
export interface HistorySubset {
readonly location: LocationPrimitive
push(to: To, state?: any): void
replace(to: To, state?: any): void
go(delta: number): void
back(): void
forward(): void
listen(listener: (update: HistoryUpdate) => void): () => void
block(blocker: (tx: { retry(): void } & HistoryUpdate) => void): void
}
type To = string | LocationPrimitive
export interface HistoryUpdate {
action: Action
location: LocationPrimitive
}
export enum HistoryAction {
Pop = 'POP',
Push = 'PUSH',
Replace = 'REPLACE',
}
export interface LocationPrimitive {
hash?: undefined | string
pathname?: undefined | string
search?: undefined | string
}