Skip to content

Commit

Permalink
feat: do not push to history if states are the same and support reloa…
Browse files Browse the repository at this point in the history
…d option
  • Loading branch information
troch committed Jun 30, 2015
1 parent ee25ff5 commit 9734c8a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
27 changes: 20 additions & 7 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ System.config({
"babel": "npm:babel-core@5.6.15",
"babel-runtime": "npm:babel-runtime@5.6.15",
"core-js": "npm:core-js@0.9.18",
"deku": "npm:deku@0.4.5",
"route-node": "npm:route-node@0.0.3",
"deku": "npm:deku@0.4.6",
"route-node": "npm:route-node@0.0.5",
"github:jspm/nodelibs-buffer@0.1.0": {
"buffer": "npm:buffer@3.2.2"
},
"github:jspm/nodelibs-path@0.1.0": {
"path-browserify": "npm:path-browserify@0.0.0"
},
"github:jspm/nodelibs-process@0.1.1": {
"process": "npm:process@0.10.1"
},
Expand All @@ -39,7 +42,7 @@ System.config({
"process": "github:jspm/nodelibs-process@0.1.1",
"systemjs-json": "github:systemjs/plugin-json@0.1.0"
},
"npm:deku@0.4.5": {
"npm:deku@0.4.6": {
"array-flatten": "npm:array-flatten@1.1.0",
"buffer": "github:jspm/nodelibs-buffer@0.1.0",
"component-emitter": "npm:component-emitter@1.2.0",
Expand All @@ -55,11 +58,21 @@ System.config({
"process": "github:jspm/nodelibs-process@0.1.1",
"sliced": "npm:sliced@0.0.5"
},
"npm:route-node@0.0.2": {
"path-parser": "npm:path-parser@0.0.4"
"npm:path-browserify@0.0.0": {
"process": "github:jspm/nodelibs-process@0.1.1"
},
"npm:route-node@0.0.3": {
"path-parser": "npm:path-parser@0.0.4"
"npm:path-parser@0.0.6": {
"fs": "github:jspm/nodelibs-fs@0.1.2",
"path": "github:jspm/nodelibs-path@0.1.0",
"process": "github:jspm/nodelibs-process@0.1.1",
"systemjs-json": "github:systemjs/plugin-json@0.1.0"
},
"npm:route-node@0.0.5": {
"fs": "github:jspm/nodelibs-fs@0.1.2",
"path": "github:jspm/nodelibs-path@0.1.0",
"path-parser": "npm:path-parser@0.0.6",
"process": "github:jspm/nodelibs-process@0.1.1",
"systemjs-json": "github:systemjs/plugin-json@0.1.0"
}
}
});
Expand Down
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ var nameToIDs = function nameToIDs(name) {
}, []);
};

var areStatesEqual = function areStatesEqual(state1, state2) {
return state1.name === state2.name && state1.params.length === state2.params.length && Object.keys(state1.params).every(function (p) {
return state1.params[p] === state2.params[p];
});
};

var Router5 = (function () {
function Router5(routes) {
var _this = this;
Expand Down Expand Up @@ -83,7 +89,7 @@ var Router5 = (function () {
this.lastStateAttempt = { name: name, path: path, params: params };

if (this.lastKnownState) {
var i = undefined;
console.log('ohohoho', areStatesEqual(this.lastKnownState, this.lastStateAttempt) ? 'true' : 'false');
// Diff segments
var segmentIds = nameToIDs(name);
var activeSegmentIds = nameToIDs(this.lastKnownState.name);
Expand Down
21 changes: 16 additions & 5 deletions modules/Router5.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ let nameToIDs = name => {
}, [])
}

let areStatesEqual = (state1, state2) => {
return state1.name === state2.name &&
Object.keys(state1.params).length === Object.keys(state2.params).length &&
Object.keys(state1.params).every(p => state1.params[p] === state2.params[p])
}

export default class Router5 {
constructor(routes) {
this.callbacks = []
Expand Down Expand Up @@ -45,13 +51,16 @@ export default class Router5 {
// let path = this.rootNode.buildPathFromSegments(segments, params)
let path = this.rootNode.buildPath(name, params)

if (!path) {
throw new Error(`Could not find route "${name}"`)
}
if (!path) throw new Error(`Could not find route "${name}"`)

this.lastStateAttempt = {name, path, params}
let sameStates = this.lastKnownState ? areStatesEqual(this.lastKnownState, this.lastStateAttempt) : false

if (this.lastKnownState) {
// Do not proceed further if states are the same and no reload
// (no desactivation and no callbacks)
if (sameStates && !opts.reload) return

if (this.lastKnownState && !sameStates) {
let i
// Diff segments
let segmentIds = nameToIDs(name)
Expand All @@ -64,7 +73,9 @@ export default class Router5 {
console.log("to deactivate: ", segmentsToDeactivate)
}
// Push to history
window.history[opts.replace ? 'replaceState' : 'pushState'](this.lastStateAttempt, '', path)
if (!sameStates) {
window.history[opts.replace ? 'replaceState' : 'pushState'](this.lastStateAttempt, '', path)
}
// Update lastKnowState
this._invokeCallbacks(this.lastStateAttempt, this.lastKnownState)

Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
},
"homepage": "https://github.com/troch/router5",
"dependencies": {
"route-node": "0.0.3"
"route-node": "0.0.5"
},
"jspm": {
"directories": {},
"directories": {
"lib": "dist/system"
},
"dependencies": {
"deku": "npm:deku@^0.4.5",
"route-node": "npm:route-node@0.0.3"
"route-node": "npm:route-node@0.0.5"
},
"devDependencies": {
"babel": "npm:babel-core@^5.1.13",
Expand Down

0 comments on commit 9734c8a

Please sign in to comment.