Skip to content

Commit

Permalink
[changed] Public interface for Location objects
Browse files Browse the repository at this point in the history
Also, removed the Flux dispatcher.

Fixes remix-run#363
  • Loading branch information
mjackson committed Oct 8, 2014
1 parent 360a00d commit d57f830
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 110 deletions.
5 changes: 0 additions & 5 deletions modules/actions/LocationActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
*/
var LocationActions = {

/**
* Indicates a location is being setup for the first time.
*/
SETUP: 'setup',

/**
* Indicates a new location is being pushed to the history stack.
*/
Expand Down
18 changes: 0 additions & 18 deletions modules/dispatchers/LocationDispatcher.js

This file was deleted.

31 changes: 8 additions & 23 deletions modules/locations/HashLocation.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
var invariant = require('react/lib/invariant');
var canUseDOM = require('react/lib/ExecutionEnvironment').canUseDOM;
var LocationActions = require('../actions/LocationActions');
var LocationDispatcher = require('../dispatchers/LocationDispatcher');
var getWindowPath = require('../utils/getWindowPath');

function getHashPath() {
Expand All @@ -21,11 +18,13 @@ function ensureSlash() {
return false;
}

var _onChange;

function onHashChange() {
if (ensureSlash()) {
var path = getHashPath();

LocationDispatcher.handleViewAction({
_onChange({
// If we don't have an _actionType then all we know is the hash
// changed. It was probably caused by the user clicking the Back
// button, but may have also been the Forward button or manual
Expand All @@ -38,36 +37,22 @@ function onHashChange() {
}
}

var _isSetup = false;

/**
* A Location that uses `window.location.hash`.
*/
var HashLocation = {

setup: function () {
if (_isSetup)
return;

invariant(
canUseDOM,
'You cannot use HashLocation in an environment with no DOM'
);
setup: function (onChange) {
_onChange = onChange;

// Do this BEFORE listening for hashchange.
ensureSlash();

LocationDispatcher.handleViewAction({
type: LocationActions.SETUP,
path: getHashPath()
});

if (window.addEventListener) {
window.addEventListener('hashchange', onHashChange, false);
} else {
window.attachEvent('onhashchange', onHashChange);
}

_isSetup = true;
},

teardown: function () {
Expand All @@ -76,8 +61,6 @@ var HashLocation = {
} else {
window.detachEvent('onhashchange', onHashChange);
}

_isSetup = false;
},

push: function (path) {
Expand All @@ -95,6 +78,8 @@ var HashLocation = {
window.history.back();
},

getCurrentPath: getHashPath,

toString: function () {
return '<HashLocation>';
}
Expand Down
34 changes: 9 additions & 25 deletions modules/locations/HistoryLocation.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,28 @@
var invariant = require('react/lib/invariant');
var canUseDOM = require('react/lib/ExecutionEnvironment').canUseDOM;
var LocationActions = require('../actions/LocationActions');
var LocationDispatcher = require('../dispatchers/LocationDispatcher');
var getWindowPath = require('../utils/getWindowPath');

var _onChange;

function onPopState() {
LocationDispatcher.handleViewAction({
_onChange({
type: LocationActions.POP,
path: getWindowPath()
});
}

var _isSetup = false;

/**
* A Location that uses HTML5 history.
*/
var HistoryLocation = {

setup: function () {
if (_isSetup)
return;

invariant(
canUseDOM,
'You cannot use HistoryLocation in an environment with no DOM'
);

LocationDispatcher.handleViewAction({
type: LocationActions.SETUP,
path: getWindowPath()
});
setup: function (onChange) {
_onChange = onChange;

if (window.addEventListener) {
window.addEventListener('popstate', onPopState, false);
} else {
window.attachEvent('popstate', onPopState);
}

_isSetup = true;
},

teardown: function () {
Expand All @@ -47,14 +31,12 @@ var HistoryLocation = {
} else {
window.detachEvent('popstate', onPopState);
}

_isSetup = false;
},

push: function (path) {
window.history.pushState({ path: path }, '', path);

LocationDispatcher.handleViewAction({
_onChange({
type: LocationActions.PUSH,
path: getWindowPath()
});
Expand All @@ -63,7 +45,7 @@ var HistoryLocation = {
replace: function (path) {
window.history.replaceState({ path: path }, '', path);

LocationDispatcher.handleViewAction({
_onChange({
type: LocationActions.REPLACE,
path: getWindowPath()
});
Expand All @@ -73,6 +55,8 @@ var HistoryLocation = {
window.history.back();
},

getCurrentPath: getWindowPath,

toString: function () {
return '<HistoryLocation>';
}
Expand Down
18 changes: 2 additions & 16 deletions modules/locations/RefreshLocation.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
var invariant = require('react/lib/invariant');
var canUseDOM = require('react/lib/ExecutionEnvironment').canUseDOM;
var LocationActions = require('../actions/LocationActions');
var LocationDispatcher = require('../dispatchers/LocationDispatcher');
var getWindowPath = require('../utils/getWindowPath');

/**
Expand All @@ -11,18 +7,6 @@ var getWindowPath = require('../utils/getWindowPath');
*/
var RefreshLocation = {

setup: function () {
invariant(
canUseDOM,
'You cannot use RefreshLocation in an environment with no DOM'
);

LocationDispatcher.handleViewAction({
type: LocationActions.SETUP,
path: getWindowPath()
});
},

push: function (path) {
window.location = path;
},
Expand All @@ -35,6 +19,8 @@ var RefreshLocation = {
window.history.back();
},

getCurrentPath: getWindowPath,

toString: function () {
return '<RefreshLocation>';
}
Expand Down
5 changes: 3 additions & 2 deletions modules/mixins/LocationContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var canUseDOM = require('react/lib/ExecutionEnvironment').canUseDOM;
var HashLocation = require('../locations/HashLocation');
var HistoryLocation = require('../locations/HistoryLocation');
var RefreshLocation = require('../locations/RefreshLocation');
var PathStore = require('../stores/PathStore');
var supportsHistory = require('../utils/supportsHistory');

/**
Expand Down Expand Up @@ -60,8 +61,8 @@ var LocationContext = {
'Cannot use location without a DOM'
);

if (location && location.setup)
location.setup();
if (location)
PathStore.useLocation(location);
},

/**
Expand Down
51 changes: 31 additions & 20 deletions modules/stores/PathStore.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var invariant = require('react/lib/invariant');
var EventEmitter = require('events').EventEmitter;
var LocationActions = require('../actions/LocationActions');
var LocationDispatcher = require('../dispatchers/LocationDispatcher');

var CHANGE_EVENT = 'change';
var _events = new EventEmitter;
Expand All @@ -9,7 +9,15 @@ function notifyChange() {
_events.emit(CHANGE_EVENT);
}

var _currentPath, _currentActionType;
var _currentLocation, _currentPath, _currentActionType;

function handleLocationChangeAction(action) {
if (_currentPath !== action.path) {
_currentPath = action.path;
_currentActionType = action.type;
notifyChange();
}
}

/**
* The PathStore keeps track of the current URL path.
Expand All @@ -28,6 +36,26 @@ var PathStore = {
_events.removeAllListeners(CHANGE_EVENT);
},

/**
* Setup the PathStore to use the given location.
*/
useLocation: function (location) {
invariant(
_currentLocation == null || _currentLocation === location,
'You cannot use %s and %s on the same page',
_currentLocation, location
);

if (_currentLocation !== location) {
if (location.setup)
location.setup(handleLocationChangeAction);

_currentPath = location.getCurrentPath();
}

_currentLocation = location;
},

/**
* Returns the current URL path.
*/
Expand All @@ -40,24 +68,7 @@ var PathStore = {
*/
getCurrentActionType: function () {
return _currentActionType;
},

dispatchToken: LocationDispatcher.register(function (payload) {
var action = payload.action;

switch (action.type) {
case LocationActions.SETUP:
case LocationActions.PUSH:
case LocationActions.REPLACE:
case LocationActions.POP:
if (_currentPath !== action.path) {
_currentPath = action.path;
_currentActionType = action.type;
notifyChange();
}
break;
}
})
}

};

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
},
"dependencies": {
"events": "1.0.1",
"flux": "2.0.1",
"qs": "2.2.2",
"when": "3.4.6"
},
Expand Down

0 comments on commit d57f830

Please sign in to comment.