Skip to content
This repository was archived by the owner on Oct 26, 2018. It is now read-only.

Add rackt eslint config #91

Merged
merged 1 commit into from
Dec 10, 2015
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "rackt"
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"license": "MIT",
"scripts": {
"build": "mkdir -p lib && babel ./src/index.js --out-file ./lib/index.js",
"lint": "eslint src test",
"test": "npm run test:node && npm run test:browser",
"test:node": "mocha --compilers js:babel-core/register --recursive ./test/node",
"test:browser": "karma start",
Expand All @@ -34,9 +35,12 @@
"devDependencies": {
"babel-cli": "^6.1.2",
"babel-core": "^6.2.1",
"babel-eslint": "^4.1.6",
"babel-loader": "^6.2.0",
"babel-plugin-transform-object-assign": "^6.0.14",
"babel-preset-es2015": "^6.1.2",
"eslint": "^1.10.3",
"eslint-config-rackt": "^1.1.1",
"expect": "^1.13.0",
"history": "^1.13.1",
"isparta": "^4.0.0",
Expand Down
62 changes: 31 additions & 31 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const deepEqual = require('deep-equal');
const deepEqual = require('deep-equal')

// Constants

const INIT_PATH = "@@router/INIT_PATH";
const UPDATE_PATH = "@@router/UPDATE_PATH";
const SELECT_STATE = state => state.routing;
const INIT_PATH = '@@router/INIT_PATH'
const UPDATE_PATH = '@@router/UPDATE_PATH'
const SELECT_STATE = state => state.routing

// Action creators

Expand All @@ -17,7 +17,7 @@ function initPath(path, state) {
replace: false,
avoidRouterUpdate: true
}
};
}
}

function pushPath(path, state, { avoidRouterUpdate = false } = {}) {
Expand All @@ -29,7 +29,7 @@ function pushPath(path, state, { avoidRouterUpdate = false } = {}) {
replace: false,
avoidRouterUpdate: !!avoidRouterUpdate
}
};
}
}

function replacePath(path, state, { avoidRouterUpdate = false } = {}) {
Expand All @@ -51,7 +51,7 @@ let initialState = {
path: undefined,
state: undefined,
replace: false
};
}

function update(state=initialState, { type, payload }) {
if(type === INIT_PATH || type === UPDATE_PATH) {
Expand All @@ -60,19 +60,19 @@ function update(state=initialState, { type, payload }) {
changeId: state.changeId + (payload.avoidRouterUpdate ? 0 : 1),
state: payload.state,
replace: payload.replace
});
})
}
return state;
return state
}

// Syncing

function locationsAreEqual(a, b) {
return a != null && b != null && a.path === b.path && deepEqual(a.state, b.state);
return a != null && b != null && a.path === b.path && deepEqual(a.state, b.state)
}

function syncReduxAndRouter(history, store, selectRouterState = SELECT_STATE) {
const getRouterState = () => selectRouterState(store.getState());
const getRouterState = () => selectRouterState(store.getState())

// To properly handle store updates we need to track the last route.
// This route contains a `changeId` which is updated on every
Expand All @@ -81,20 +81,20 @@ function syncReduxAndRouter(history, store, selectRouterState = SELECT_STATE) {
// check if the location has changed, and if it is we trigger a
// history update. It's possible for this to happen when something
// reloads the entire app state such as redux devtools.
let lastRoute = undefined;
let lastRoute = undefined

if(!getRouterState()) {
throw new Error(
"Cannot sync router: route state does not exist. Did you " +
"install the routing reducer?"
);
'Cannot sync router: route state does not exist. Did you ' +
'install the routing reducer?'
)
}

const unsubscribeHistory = history.listen(location => {
const route = {
path: history.createPath(location),
state: location.state
};
}

if (!lastRoute) {
// `initialState` *should* represent the current location when
Expand All @@ -112,40 +112,40 @@ function syncReduxAndRouter(history, store, selectRouterState = SELECT_STATE) {
path: route.path,
state: route.state,
replace: false
};
}

// Also set `lastRoute` so that the store subscriber doesn't
// trigger an unnecessary `pushState` on load
lastRoute = initialState;
lastRoute = initialState

store.dispatch(initPath(route.path, route.state));
store.dispatch(initPath(route.path, route.state))
} else if(!locationsAreEqual(getRouterState(), route)) {
// The above check avoids dispatching an action if the store is
// already up-to-date
const method = location.action === 'REPLACE' ? replacePath : pushPath;
store.dispatch(method(route.path, route.state, { avoidRouterUpdate: true }));
const method = location.action === 'REPLACE' ? replacePath : pushPath
store.dispatch(method(route.path, route.state, { avoidRouterUpdate: true }))
}
});
})

const unsubscribeStore = store.subscribe(() => {
let routing = getRouterState();
let routing = getRouterState()

// Only trigger history update if this is a new change or the
// location has changed.
if(lastRoute.changeId !== routing.changeId ||
!locationsAreEqual(lastRoute, routing)) {

lastRoute = routing;
const method = routing.replace ? 'replaceState' : 'pushState';
history[method](routing.state, routing.path);
lastRoute = routing
const method = routing.replace ? 'replaceState' : 'pushState'
history[method](routing.state, routing.path)
}

});
})

return function unsubscribe() {
unsubscribeHistory();
unsubscribeStore();
};
unsubscribeHistory()
unsubscribeStore()
}
}

module.exports = {
Expand All @@ -154,4 +154,4 @@ module.exports = {
replacePath,
syncReduxAndRouter,
routeReducer: update
};
}
8 changes: 4 additions & 4 deletions test/browser/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { createHashHistory, createHistory } = require('history');
const createTests = require('../createTests.js');
const { createHashHistory, createHistory } = require('history')
const createTests = require('../createTests.js')

createTests(createHashHistory, 'Hash History', () => window.location = '#/');
createTests(createHistory, 'Browser History', () => window.history.replaceState(null, null, '/'));
createTests(createHashHistory, 'Hash History', () => window.location = '#/')
createTests(createHistory, 'Browser History', () => window.history.replaceState(null, null, '/'))
Loading