Skip to content

Commit

Permalink
fix push() bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Cap32 committed Feb 9, 2018
1 parent 466f149 commit 4c2ce34
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 30 deletions.
3 changes: 1 addition & 2 deletions src/Router.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import PropTypes from 'prop-types';
import React, { Component, Children } from 'react';
import { withRouter, Router as ReactRouter } from 'react-router-dom';
Expand All @@ -25,7 +24,7 @@ export default function Router({
component: RouterComp,
children,
routerStore,
...other,
...other
}) {
return (
<RouterComp {...other}>
Expand Down
83 changes: 57 additions & 26 deletions src/RouterStore.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { observable, computed, autorun, extendObservable } from 'mobx';
import { parse, stringify } from './queryString';

Expand All @@ -17,48 +16,80 @@ const stripQuery = (loc) => {
export default class RouterStore {
_prevLoc = {};

@observable location = {};
@observable _location = {};

@computed
get location() {
return this._location;
}
set location(location) {
this.push(location);
return true;
}

__initial({ location, history }) {
class Loc {
@computed get query() {
return parse(this.search.slice(1));
@observable _search = '';
@observable _hash = '';
@observable _pathname = '';
@observable state = {};

@computed
get query() {
return parse(this._search.slice(1));
}
set query(query) {
const queryString = stringify(query);
this.search = queryString && `?${queryString}`;
return query;
return true;
}

constructor(loc) {
extendObservable(this, loc);
@computed
get search() {
return this._search;
}
set search(search) {
this._wrapper.push({ search });
return true;
}
}

this.location = new Loc(location);
this._prevLoc = location;
@computed
get hash() {
return this._hash;
}
set hash(hash) {
this._wrapper.push({ hash });
return true;
}

@computed
get pathname() {
return this._pathname;
}
set pathname(pathname) {
this._wrapper.push({ pathname });
return true;
}

constructor(wrapper, loc) {
this._wrapper = wrapper;
this._search = loc.search;
this._path = loc.path;
this._hash = loc.hash;
this._state = loc.state;
}
}

this.history = history;
this._location = new Loc(this, location);
this._prevLoc = location;

history.listen((location) => {
if (typeof this.location === 'string') {
this.location = new Loc({});
}
this.location.pathname = location.pathname;
this.location.search = location.search;
this.location.hash = location.hash;
this._location._pathname = location.pathname;
this._location._search = location.search;
this._location._hash = location.hash;
this._prevLoc = location;
});

autorun(() => {
if (typeof this.location === 'string' ||
this.location.search !== this._prevLoc.search ||
this.location.hash !== this._prevLoc.hash ||
this.location.pathname !== this._prevLoc.pathname
) {
this.push(this.location);
}
});
}

push(loc, state) {
Expand Down
5 changes: 3 additions & 2 deletions src/__tests__/RouterStore.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { RouterStore } from '../index';
import { isObservable, autorun } from 'mobx';
import url from 'url';
Expand All @@ -17,7 +16,9 @@ describe('RouterStore', () => {
routerStore = new RouterStore();
history = {
push: jest.fn((loc) => {
if (typeof loc === 'string') { loc = url.parse(loc); }
if (typeof loc === 'string') {
loc = url.parse(loc);
}
loc.search = loc.search || '';
loc.hash = loc.hash || '';
loc.pathname = loc.pathname || '/';
Expand Down

0 comments on commit 4c2ce34

Please sign in to comment.