Skip to content

Commit 9337df3

Browse files
committed
Revert "Change location to be a plain object for effortless record/replay"
This reverts commit aeeaf19.
1 parent aeeaf19 commit 9337df3

File tree

7 files changed

+116
-33
lines changed

7 files changed

+116
-33
lines changed

modules/AbstractHistory.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var invariant = require('invariant');
2+
var Location = require('./Location');
23

34
var _listenSingleton = null;
45

@@ -84,10 +85,7 @@ class AbstractHistory {
8485
}
8586

8687
getLocation() {
87-
return {
88-
path: this.getPath(),
89-
navigationType: this.navigationType
90-
};
88+
return new Location(this.getPath(), this.navigationType);
9189
}
9290

9391
back() {

modules/Location.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var NavigationTypes = require('./NavigationTypes');
2+
var PathUtils = require('./PathUtils');
3+
4+
/**
5+
* A Location answers two important questions:
6+
*
7+
* 1. Where am I?
8+
* 2. How did I get here?
9+
*/
10+
class Location {
11+
12+
/**
13+
* Revives the location from its serialized form.
14+
* Use `Location.revive` as a second argument to `JSON.parse`.
15+
*
16+
* var serialized = JSON.stringify(location);
17+
* var deserialized = JSON.parse(serialized, Location.revive);
18+
*
19+
*/
20+
static revive(key, value) {
21+
if (key === '') {
22+
return new Location(value.path, value.navigationType);
23+
} else {
24+
return value;
25+
}
26+
}
27+
28+
constructor(path, navigationType) {
29+
this.path = path;
30+
this.navigationType = navigationType || NavigationTypes.POP;
31+
}
32+
33+
getPathname() {
34+
return PathUtils.getPathname(this.path);
35+
}
36+
37+
getQueryString() {
38+
return PathUtils.getQueryString(this.path);
39+
}
40+
41+
getQuery(options) {
42+
return PathUtils.getQuery(this.path, options);
43+
}
44+
45+
toJSON() {
46+
return {
47+
path: this.path,
48+
navigationType: this.navigationType
49+
};
50+
}
51+
52+
}
53+
54+
module.exports = Location;

modules/PropTypes.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
var { func, object, string, arrayOf, instanceOf, oneOfType, shape } = require('react').PropTypes;
1+
var { func, object, arrayOf, instanceOf, oneOfType } = require('react').PropTypes;
22
var AbstractHistory = require('./AbstractHistory');
3+
var Location = require('./Location');
34

45
function falsy(props, propName, componentName) {
56
if (props[propName])
@@ -9,17 +10,14 @@ function falsy(props, propName, componentName) {
910
var component = func;
1011
var components = oneOfType([ component, arrayOf(component) ]);
1112
var history = instanceOf(AbstractHistory);
13+
var location = instanceOf(Location);
1214
var route = object;
13-
var location = shape({
14-
path: string.isRequired,
15-
navigationType: string
16-
});
1715

1816
module.exports = {
1917
falsy,
2018
component,
2119
components,
2220
history,
23-
route,
24-
location
21+
location,
22+
route
2523
};

modules/StateMixin.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var PathUtils = require('./PathUtils');
1+
var { isAbsolutePath } = require('./PathUtils');
22

33
function routeIsActive(activeRoutes, route) {
44
if (typeof route === 'object')
@@ -39,15 +39,15 @@ var StateMixin = {
3939
},
4040

4141
getPathname() {
42-
return PathUtils.getPathname(this.getLocation());
42+
return this.getLocation().getPathname();
4343
},
4444

4545
getQueryString() {
46-
return PathUtils.getQueryString(this.getLocation());
46+
return this.getLocation().getQueryString();
4747
},
4848

4949
getQuery() {
50-
return PathUtils.getQuery(this.getLocation());
50+
return this.getLocation().getQuery();
5151
},
5252

5353
getBranch() {
@@ -66,7 +66,7 @@ var StateMixin = {
6666
if (!this.getLocation())
6767
return false;
6868

69-
if (PathUtils.isAbsolutePath(to))
69+
if (isAbsolutePath(to))
7070
return to === this.getPath();
7171

7272
return routeIsActive(this.getBranch(), to) &&

modules/__tests__/Location-test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
var expect = require('expect');
2+
var Location = require('../Location');
3+
var NavigationTypes = require('../NavigationTypes');
4+
5+
describe('Location', function () {
6+
var location;
7+
8+
it('can be revived', function () {
9+
location = new Location('/the/path', NavigationTypes.POP);
10+
11+
var serialized = JSON.stringify(location);
12+
var revived = JSON.parse(serialized, Location.revive);
13+
14+
expect(revived instanceof Location).toEqual(true);
15+
expect(revived.path).toEqual(location.path);
16+
expect(revived.navigationType).toEqual(location.navigationType);
17+
});
18+
19+
describe('with a query string', function () {
20+
beforeEach(function () {
21+
location = new Location('/the/path?the=query');
22+
});
23+
24+
it('knows its pathname', function() {
25+
expect(location.getPathname()).toEqual('/the/path');
26+
});
27+
28+
it('knows its query string', function() {
29+
expect(location.getQueryString()).toEqual('the=query');
30+
});
31+
});
32+
33+
describe('without a query string', function () {
34+
beforeEach(function () {
35+
location = new Location('/the/path');
36+
});
37+
38+
it('knows its pathname', function() {
39+
expect(location.getPathname()).toEqual('/the/path');
40+
});
41+
42+
it('has an empty query string', function() {
43+
expect(location.getQueryString()).toEqual('');
44+
});
45+
});
46+
});

modules/__tests__/PathUtils-test.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,25 +100,11 @@ describe('PathUtils.injectParams', function () {
100100
});
101101

102102
describe('PathUtils.getPathname', function () {
103-
it('returns the path when there is no query', function () {
104-
expect(PathUtils.getPathname('/a/b/c')).toEqual('/a/b/c');
105-
});
106-
107103
it('returns the pathname portion of a path', function () {
108104
expect(PathUtils.getPathname('/a/b/c?id=def')).toEqual('/a/b/c');
109105
});
110106
});
111107

112-
describe('PathUtils.getQueryString', function () {
113-
it('returns an empty string when there is no query', function () {
114-
expect(PathUtils.getQueryString('/a/b/c')).toEqual('');
115-
});
116-
117-
it('returns the querystring portion of a path', function () {
118-
expect(PathUtils.getQueryString('/a/b/c?id=def')).toEqual('id=def');
119-
});
120-
});
121-
122108
describe('PathUtils.withQuery', function () {
123109
it('appends the query string', function () {
124110
expect(PathUtils.withQuery('/a/b/c', { id: 'def' })).toEqual('/a/b/c?id=def');

modules/createRouter.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var NavigationMixin = require('./NavigationMixin');
1010
var TransitionMixin = require('./TransitionMixin');
1111
var StateMixin = require('./StateMixin');
1212
var findMatch = require('./findMatch');
13+
var Location = require('./Location');
1314
var AbstractHistory = require('./AbstractHistory');
1415

1516
function createElement(component, props) {
@@ -53,7 +54,7 @@ function checkProps(props) {
5354
* determine what to render to the page.
5455
*
5556
* In a client-side environment you simply pass a History object to the Router
56-
* as a prop. A History acts like a store for location objects and emits new
57+
* as a prop. A History acts like a store for Location objects and emits new
5758
* ones as the location changes over time (i.e. a user navigates around your
5859
* site). History objects are included for all the most common scenarios in
5960
* which the router may be used.
@@ -97,10 +98,10 @@ function createRouter(routes) {
9798
*/
9899
static run(location, callback) {
99100
if (typeof location === 'string')
100-
location = { path: location };
101+
location = new Location(location);
101102

102103
invariant(
103-
typeof location.path === 'string',
104+
location instanceof Location,
104105
'Router.run needs a Location'
105106
);
106107

0 commit comments

Comments
 (0)