From fbeb0fec1d4bccba49f68e1845da046b0509105d Mon Sep 17 00:00:00 2001 From: Gregory Beaver Date: Mon, 23 Jan 2017 14:46:21 -0600 Subject: [PATCH] fix #3 and prepare for 0.3.5 --- package.json | 2 +- src/RouteManager.js | 6 +++++- src/actions.js | 14 ++++++++++++++ src/index.js | 2 ++ src/types.js | 2 ++ test/RouteManager.test.js | 16 ++++++++++++++++ test/actions.test.js | 12 ++++++++++++ test/index.test.js | 12 ++++++++++++ 8 files changed, 64 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 37c4996..285aeef 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-redux-saga-router", - "version": "0.3.4", + "version": "0.3.5", "description": "elegant powerful routing based on the simplicity of storing url as state", "main": "lib/index.js", "directories": { diff --git a/src/RouteManager.js b/src/RouteManager.js index 750b385..caec902 100644 --- a/src/RouteManager.js +++ b/src/RouteManager.js @@ -2,6 +2,7 @@ import RouteParser from 'route-parser' import { createPath } from 'history' import { put, take, select, call } from 'redux-saga/effects' +import * as types from './types' import * as actions from './actions' import * as selectors from './selectors' @@ -92,7 +93,10 @@ export default class RouteManager { *monitorState() { while (true) { // eslint-disable-line - yield take('*') + const action = yield take('*') + if (action.type === types.PENDING_UPDATES) { + yield take(types.COMMITTED_UPDATES) + } yield call([this, this.locationFromState]) } } diff --git a/src/actions.js b/src/actions.js index 7fd17d9..ae6bcf9 100644 --- a/src/actions.js +++ b/src/actions.js @@ -106,3 +106,17 @@ export function enterRoutes(routes) { payload: routes } } + +export function pending() { + return { + type: types.PENDING_UPDATES, + payload: null + } +} + +export function commit() { + return { + type: types.COMMITTED_UPDATES, + payload: null + } +} diff --git a/src/index.js b/src/index.js index 6de06d9..88ada3d 100644 --- a/src/index.js +++ b/src/index.js @@ -88,6 +88,7 @@ export function *router(connect, routeDefinitions, history, channel) { location = path lastMatches = matchedRoutes + yield put(actions.pending()) yield put(actions.route(locationChange.location)) yield put(actions.matchRoutes(matchedRoutes)) if (exiting.length) { @@ -98,6 +99,7 @@ export function *router(connect, routeDefinitions, history, channel) { } yield keys.map(name => call([routes[name], routes[name].monitorUrl], locationChange.location)) + yield put(actions.commit()) } } } finally { diff --git a/src/types.js b/src/types.js index 436f35d..a1c6e30 100644 --- a/src/types.js +++ b/src/types.js @@ -7,3 +7,5 @@ export const MATCH_ROUTES = '@@react-redux-saga-router/MATCH_ROUTES' export const SET_PARAMS = '@@react-redux-saga-router/SET_PARAMS' export const ENTER_ROUTES = '@@react-redux-saga-router/ENTER_ROUTES' export const EXIT_ROUTES = '@@react-redux-saga-router/EXIT_ROUTES' +export const PENDING_UPDATES = '@@react-redux-saga-router/PENDING_UPDATES' +export const COMMITTED_UPDATES = '@@react-redux-saga-router/COMMITTED_UPDATES' diff --git a/test/RouteManager.test.js b/test/RouteManager.test.js index dc0244e..cfd5dca 100644 --- a/test/RouteManager.test.js +++ b/test/RouteManager.test.js @@ -4,6 +4,7 @@ import { put, take, select, call } from 'redux-saga/effects' import RouteManager, { fake } from '../src/RouteManager' import * as actions from '../src/actions' +import * as types from '../src/types' describe('Route', () => { describe('basics', () => { @@ -311,8 +312,23 @@ describe('Route', () => { let next = saga.next() expect(next.value).eqls(take('*')) + next = saga.next({ type: 'foo'}) + + expect(next.value).eqls(call([route, route.locationFromState])) next = saga.next() + expect(next.value).eqls(take('*')) + }) + it('monitorState during url update', () => { + const saga = route.monitorState() + let next = saga.next() + + expect(next.value).eqls(take('*')) + next = saga.next(actions.pending()) + + expect(next.value).eqls(take(types.COMMITTED_UPDATES)) + next = saga.next(actions.commit()) + expect(next.value).eqls(call([route, route.locationFromState])) next = saga.next() diff --git a/test/actions.test.js b/test/actions.test.js index e8c37a1..b9ee91e 100644 --- a/test/actions.test.js +++ b/test/actions.test.js @@ -130,4 +130,16 @@ describe('react-redux-saga-router actions', () => { payload: ['hi'] }) }) + it('pending', () => { + expect(actions.pending()).eqls({ + type: types.PENDING_UPDATES, + payload: null + }) + }) + it('committed', () => { + expect(actions.commit()).eqls({ + type: types.COMMITTED_UPDATES, + payload: null + }) + }) }) diff --git a/test/index.test.js b/test/index.test.js index f5b5fc9..c82b8eb 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -204,6 +204,9 @@ describe('react-redux-saga-router', () => { } }) + expect(next.value).eqls(put(actions.pending())) + next = saga.next() + expect(next.value).eqls(put(actions.route({ pathname: '/campers/2017', search: '', @@ -238,6 +241,9 @@ describe('react-redux-saga-router', () => { ]) next = saga.next() + expect(next.value).eqls(put(actions.commit())) + next = saga.next() + expect(next.value).eqls(take(channel)) next = saga.next({ location: { @@ -247,6 +253,9 @@ describe('react-redux-saga-router', () => { } }) + expect(next.value).eqls(put(actions.pending())) + next = saga.next() + expect(next.value).eqls(put(actions.route({ pathname: '/campers/2016', search: '', @@ -275,6 +284,9 @@ describe('react-redux-saga-router', () => { ]) next = saga.next() + expect(next.value).eqls(put(actions.commit())) + next = saga.next() + expect(next.value).eqls(take(channel)) next = saga.throw(new Error('all done'))