Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions modules/actions/LocationActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var RefreshLocation = require('../locations/RefreshLocation');
var LocationDispatcher = require('../dispatchers/LocationDispatcher');
var ActionTypes = require('../constants/ActionTypes');
var warning = require('react/lib/warning');
var invariant = require('react/lib/invariant');
var isAbsoluteURL = require('../utils/isAbsoluteURL');
var makePath = require('../utils/makePath');

Expand All @@ -12,15 +13,47 @@ function loadURL(url) {
}

var _location = null;
var _pendingActions = [];
var _isDispatching = false;
var _previousPath = null;

/**
* Safely enqueues action for dispatch in current tick.
* If a different action is being dispatched, this action
* will be dispatched right after it.
*/
function dispatchAction(actionType, operation) {
if (_isDispatching)
throw new Error('Cannot handle ' + actionType + ' in the middle of another action.');
_pendingActions.push({
actionType: actionType,
operation: operation
});

if (!_isDispatching) {
dispatchPendingActions();
}
}

/**
* Dispatches all pending actions one by one.
* If more pending actions are enqueued during the loop, they will be processed in the same tick.
*/
function dispatchPendingActions() {
invariant(!_isDispatching, 'Cannot dispatch in the middle of dispatch');
_isDispatching = true;

while (_pendingActions.length > 0) {
dispatchNextPendingAction();
}

_isDispatching = false;
}

function dispatchNextPendingAction() {
var pendingAction = _pendingActions.shift();

var operation = pendingAction.operation;
var actionType = pendingAction.actionType;

var scrollPosition = {
x: window.scrollX,
y: window.scrollY
Expand All @@ -30,23 +63,25 @@ function dispatchAction(actionType, operation) {
operation(_location);

var path = _location.getCurrentPath();

LocationDispatcher.handleViewAction({
type: actionType,
path: path,
scrollPosition: scrollPosition
});

_isDispatching = false;
_previousPath = path;
}

function handleChange() {
var path = _location.getCurrentPath();

// Ignore changes inside or caused by dispatchAction
if (!_isDispatching && path !== _previousPath) {
dispatchAction(ActionTypes.POP);
// Ignore location change inside or caused by dispatchAction
if (_isDispatching || path === _previousPath) {
return;
}

dispatchAction(ActionTypes.POP);
}

/**
Expand Down