-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathHistory.js
46 lines (44 loc) · 998 Bytes
/
History.js
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
const {EventEmitter} = require('events');
const {createMemoryHistory} = require('history');
class History extends EventEmitter {
constructor(u) {
super();
this._history = createMemoryHistory({
initialEntries: [u],
});
this._history.listen((location, action) => {
if (action === 'POP') {
const {pathname, search, hash, state} = location;
this.emit('popstate', url.format({
pathname,
search,
hash,
}), state);
}
});
}
back(n) {
this._history.goBack(n);
}
forward(n) {
this._history.goForward(n);
}
go(n) {
this._history.go(n);
}
pushState(state, title, url) {
this._history.push(url, state);
}
replaceState(state, title, url) {
this._history.replace(url, state);
}
get length() {
return this._history.length;
}
set length(length) {}
get state() {
return this._history.location.state;
}
set state(state) {}
}
module.exports.History = History;