Skip to content

Commit

Permalink
Always call listenBefore hooks on browser navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
stevoland committed Oct 7, 2015
1 parent eb05768 commit 39e080a
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 13 deletions.
3 changes: 3 additions & 0 deletions modules/__tests__/BrowserHistory-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import describeInitialLocation from './describeInitialLocation'
import describeTransitions from './describeTransitions'
import describePushState from './describePushState'
import describeReplaceState from './describeReplaceState'
import describePopState from './describePopState'
import describeHashSupport from './describeHashSupport'
import describeBasename from './describeBasename'
import describeQueries from './describeQueries'
Expand All @@ -20,6 +21,7 @@ describe('browser history', function () {
describeTransitions(createBrowserHistory)
describePushState(createBrowserHistory)
describeReplaceState(createBrowserHistory)
describePopState(createBrowserHistory)
describeHashSupport(createBrowserHistory)
describeBasename(createBrowserHistory)
describeQueries(createBrowserHistory)
Expand All @@ -30,6 +32,7 @@ describe('browser history', function () {
describeTransitions(createBrowserHistory)
describePushState(createBrowserHistory)
describeReplaceState(createBrowserHistory)
describePopState(createBrowserHistory)
describeHashSupport(createBrowserHistory)
describeBasename(createBrowserHistory)
describeQueries(createBrowserHistory)
Expand Down
2 changes: 2 additions & 0 deletions modules/__tests__/HashHistory-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import describeInitialLocation from './describeInitialLocation'
import describeTransitions from './describeTransitions'
import describePushState from './describePushState'
import describeReplaceState from './describeReplaceState'
import describePopState from './describePopState'
import describeQueryKey from './describeQueryKey'
import describeBasename from './describeBasename'
import describeQueries from './describeQueries'
Expand All @@ -21,6 +22,7 @@ describe('hash history', function () {
describeTransitions(createHashHistory)
describePushState(createHashHistory)
describeReplaceState(createHashHistory)
describePopState(createHashHistory)
describeBasename(createHashHistory)
describeQueries(createHashHistory)

Expand Down
50 changes: 50 additions & 0 deletions modules/__tests__/describePopState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*eslint-env mocha */

function describePopState(createHistory) {
describe('when a listenBefore hook is added', function () {
let history, unlisten

beforeEach(function () {
history = createHistory()
history.pushState(null, '/home')
})

afterEach(function () {
if (unlisten)
unlisten()
})

it('is called when browser navigation is used', function (done) {
unlisten = history.listenBefore(function () {
done()
})

window.history.back()
})
})

describe('when a deprecated transition hook is added', function () {
let history, listener

beforeEach(function () {
history = createHistory()
history.pushState(null, '/home')
})

afterEach(function () {
history.unregisterTransitionHook(listener)
})

it('is called when browser navigation is used', function (done) {
listener = function () {
done()
}

history.registerTransitionHook(listener)

window.history.back()
})
})
}

export default describePopState
35 changes: 25 additions & 10 deletions modules/__tests__/describeTransitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import execSteps from './execSteps'

function describeTransitions(createHistory) {
describe('a synchronous transition hook', function () {
let history, unlisten
let history, unlisten, unlistenBefore
beforeEach(function () {
history = createHistory()
})

afterEach(function () {
if (unlistenBefore)
unlistenBefore()

if (unlisten)
unlisten()
})
Expand All @@ -27,7 +30,7 @@ function describeTransitions(createHistory) {
]

let nextLocation
history.listenBefore(function (location) {
unlistenBefore = history.listenBefore(function (location) {
nextLocation = location
})

Expand All @@ -36,12 +39,15 @@ function describeTransitions(createHistory) {
})

describe('an asynchronous transition hook', function () {
let history, unlisten
let history, unlisten, unlistenBefore
beforeEach(function () {
history = createHistory()
})

afterEach(function () {
if (unlistenBefore)
unlistenBefore()

if (unlisten)
unlisten()
})
Expand All @@ -57,7 +63,7 @@ function describeTransitions(createHistory) {
]

let nextLocation
history.listenBefore(function (location, callback) {
unlistenBefore = history.listenBefore(function (location, callback) {
nextLocation = location
setTimeout(callback)
})
Expand All @@ -67,7 +73,7 @@ function describeTransitions(createHistory) {
})

describe('when the user confirms a transition', function () {
let confirmationMessage, location, history, unlisten
let confirmationMessage, location, history, unlisten, unlistenBefore
beforeEach(function () {
location = null
confirmationMessage = 'Are you sure?'
Expand All @@ -79,7 +85,7 @@ function describeTransitions(createHistory) {
}
})

history.listenBefore(function () {
unlistenBefore = history.listenBefore(function () {
return confirmationMessage
})

Expand All @@ -89,6 +95,9 @@ function describeTransitions(createHistory) {
})

afterEach(function () {
if (unlistenBefore)
unlistenBefore()

if (unlisten)
unlisten()
})
Expand All @@ -108,7 +117,7 @@ function describeTransitions(createHistory) {
})

describe('when the user cancels a transition', function () {
let confirmationMessage, location, history, unlisten
let confirmationMessage, location, history, unlisten, unlistenBefore
beforeEach(function () {
location = null
confirmationMessage = 'Are you sure?'
Expand All @@ -120,7 +129,7 @@ function describeTransitions(createHistory) {
}
})

history.listenBefore(function () {
unlistenBefore = history.listenBefore(function () {
return confirmationMessage
})

Expand All @@ -130,6 +139,9 @@ function describeTransitions(createHistory) {
})

afterEach(function () {
if (unlistenBefore)
unlistenBefore()

if (unlisten)
unlisten()
})
Expand All @@ -142,13 +154,13 @@ function describeTransitions(createHistory) {
})

describe('when the transition hook cancels a transition', function () {
let location, history, unlisten
let location, history, unlisten, unlistenBefore
beforeEach(function () {
location = null

history = createHistory()

history.listenBefore(function () {
unlistenBefore = history.listenBefore(function () {
return false
})

Expand All @@ -158,6 +170,9 @@ function describeTransitions(createHistory) {
})

afterEach(function () {
if (unlistenBefore)
unlistenBefore()

if (unlisten)
unlisten()
})
Expand Down
35 changes: 34 additions & 1 deletion modules/createBrowserHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ function createBrowserHistory(options) {

let listenerCount = 0, stopPopStateListener

function listenBefore(listener) {
if (++listenerCount === 1)
stopPopStateListener = startPopStateListener(history)

let unlisten = history.listenBefore(listener)

return function () {
unlisten()

if (--listenerCount === 0)
stopPopStateListener()
}
}

function listen(listener) {
if (++listenerCount === 1)
stopPopStateListener = startPopStateListener(history)
Expand All @@ -112,9 +126,28 @@ function createBrowserHistory(options) {
}
}

// deprecated
function registerTransitionHook(hook) {
if (++listenerCount === 1)
stopPopStateListener = startPopStateListener(history)

history.registerTransitionHook(hook)
}

// deprecated
function unregisterTransitionHook(hook) {
history.unregisterTransitionHook(hook)

if (--listenerCount === 0)
stopPopStateListener()
}

return {
...history,
listen
listenBefore,
listen,
registerTransitionHook,
unregisterTransitionHook
}
}

Expand Down
37 changes: 35 additions & 2 deletions modules/createHashHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function createHashHistory(options={}) {

function getCurrentLocation() {
let path = getHashPath()

let key, state
if (queryKey) {
key = getQueryStringValueFromPath(path, queryKey)
Expand Down Expand Up @@ -129,6 +129,20 @@ function createHashHistory(options={}) {

let listenerCount = 0, stopHashChangeListener

function listenBefore(listener) {
if (++listenerCount === 1)
stopHashChangeListener = startHashChangeListener(history)

let unlisten = history.listenBefore(listener)

return function () {
unlisten()

if (--listenerCount === 0)
stopHashChangeListener()
}
}

function listen(listener) {
if (++listenerCount === 1)
stopHashChangeListener = startHashChangeListener(history)
Expand Down Expand Up @@ -176,13 +190,32 @@ function createHashHistory(options={}) {
return '#' + history.createHref(path)
}

// deprecated
function registerTransitionHook(hook) {
if (++listenerCount === 1)
stopHashChangeListener = startHashChangeListener(history)

history.registerTransitionHook(hook)
}

// deprecated
function unregisterTransitionHook(hook) {
history.unregisterTransitionHook(hook)

if (--listenerCount === 0)
stopHashChangeListener()
}

return {
...history,
listenBefore,
listen,
pushState,
replaceState,
go,
createHref
createHref,
registerTransitionHook,
unregisterTransitionHook
}
}

Expand Down

0 comments on commit 39e080a

Please sign in to comment.