Skip to content

Commit 099257c

Browse files
committed
Created a static 'history' for importing with import {history} from 'react-router/lib/HashHistory'
this is an implementation for what @ryanflorence said on #1339 and @mjackson on #1426.
1 parent 6e23feb commit 099257c

File tree

6 files changed

+17
-5
lines changed

6 files changed

+17
-5
lines changed

modules/BrowserHistory.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import DOMHistory from './DOMHistory';
22
import { getWindowPath, supportsHistory } from './DOMUtils';
33
import NavigationTypes from './NavigationTypes';
44

5+
var instance = null;
6+
57
function updateCurrentState(extraState) {
68
var state = window.history.state;
79

@@ -106,6 +108,10 @@ class BrowserHistory extends DOMHistory {
106108
}
107109
}
108110

111+
static history() {
112+
instance = instance || new BrowserHistory();
113+
return instance;
114+
}
109115
}
110116

111117
export default BrowserHistory;

modules/DOMHistory.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { getWindowScrollPosition } from './DOMUtils';
55
* A history interface that assumes a DOM environment.
66
*/
77
class DOMHistory extends History {
8-
98
constructor(options={}) {
109
super(options);
1110
this.getScrollPosition = options.getScrollPosition || getWindowScrollPosition;

modules/HashHistory.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import NavigationTypes from './NavigationTypes';
44
import { getHashPath, replaceHashPath } from './DOMUtils';
55
import { isAbsolutePath } from './URLUtils';
66

7+
var instance = null;
78
var DefaultQueryKey = '_qk';
89

910
function ensureSlash() {
@@ -34,7 +35,7 @@ function saveState(path, queryKey, state) {
3435
function readState(path, queryKey) {
3536
var sessionKey = getQueryStringValueFromPath(path, queryKey);
3637
var json = sessionKey && window.sessionStorage.getItem(sessionKey);
37-
38+
3839
if (json) {
3940
try {
4041
return JSON.parse(json);
@@ -168,6 +169,10 @@ class HashHistory extends DOMHistory {
168169
return '#' + path;
169170
}
170171

172+
static history() {
173+
instance = instance || new HashHistory();
174+
return instance;
175+
}
171176
}
172177

173178
export default HashHistory;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import describeHistory from './describeHistory';
2-
import BrowserHistory from '../BrowserHistory';
2+
import BrowserHistory, { history } from '../BrowserHistory';
33

44
describe('BrowserHistory', function () {
55
describeHistory(new BrowserHistory);
6+
describeHistory(history());
67
});
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import describeHistory from './describeHistory';
2-
import HashHistory from '../HashHistory';
2+
import HashHistory, { history } from '../HashHistory';
33

44
describe('HashHistory', function () {
55
describeHistory(new HashHistory);
6+
describeHistory(history());
67
});

modules/__tests__/MemoryHistory-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('MemoryHistory', function () {
4040
it('cannot go forward', function () {
4141
expect(history.canGoForward()).toBe(false);
4242
});
43-
43+
4444
describe('and then replacing that path', function () {
4545
beforeEach(function () {
4646
history.replaceState(null, '/replace');

0 commit comments

Comments
 (0)