Skip to content

Commit

Permalink
chore: v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
troch committed Oct 27, 2015
1 parent 3f2bd23 commit c0b137e
Show file tree
Hide file tree
Showing 18 changed files with 430 additions and 176 deletions.
32 changes: 32 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,35 @@
<a name="1.0.0"></a>
# 1.0.0 (2015-10-27)


### Features

* add autoCleanUp option to automatically deregister components ([8a1db8c](https://github.com/router5/router5/commit/8a1db8c))
* add canDeactivate function for boolean values ([13b3015](https://github.com/router5/router5/commit/13b3015))
* add cancel function and router in middleware function context ([eb9266b](https://github.com/router5/router5/commit/eb9266b))
* add logger plugin ([e2c9570](https://github.com/router5/router5/commit/e2c9570))
* add possibility to add data to state objects ([876ea64](https://github.com/router5/router5/commit/876ea64))
* add support for more than one middleware function ([de60d30](https://github.com/router5/router5/commit/de60d30))
* add support for plugins and make listeners a plugin ([02196f6](https://github.com/router5/router5/commit/02196f6))
* output errors as objects with code property rather than string ([885c1de](https://github.com/router5/router5/commit/885c1de))
* remove browser history management from router ([361b500](https://github.com/router5/router5/commit/361b500))
* support custom errors in middleware functions ([8c4e8bd](https://github.com/router5/router5/commit/8c4e8bd))


### BREAKING CHANGES

* errors are now objects and not error codes (strings) to allow more descriptive errors. For instance, CANNOT_ACTIVATE and CANNOT_DEACTIVATE error objects will contain a 'segment' key

* components will now be deregistered automatically by default, to turn it off set autoCleanUp option to false

* onTransition() is now deprecated in favour of useMiddleware()

* history is now managed by a plugin (router5-history). See docs for how to use.

* all listeners registration and deregistration methods are deprecated (addListener, removeListener, onTransitionStart, offTransitionStart, etc...). Custom plugins have to be used instead (see docs for more information)



<a name="1.0.0"></a>
# 1.0.0 (2015-10-22)

Expand Down
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -66,8 +66,9 @@ function per node. Supports asynchronous results.

### Integration

- [router5-react](https://github.com/router5/router5-react)
- [router5-deku](https://github.com/router5/router5-deku)
- [react-router5](https://github.com/router5/react-router5)
- [redux-router5](https://github.com/router5/redux-router5)
- [deku-router5](https://github.com/router5/deku-router5)
- [cyclejs driver gist](https://gist.github.com/axefrog/217e522282a7948737e1)


Expand Down
127 changes: 87 additions & 40 deletions dist/amd/router5.js
Expand Up @@ -26,6 +26,36 @@
define('router5', [], function () {
"use strict";

function transitionPath(toState, fromState) {
function nameToIDs(name) {
return name.split('.').reduce(function (ids, name) {
return ids.concat(ids.length ? ids[ids.length - 1] + '.' + name : name);
}, []);
}

var i = undefined;
var fromStateIds = fromState ? nameToIDs(fromState.name) : [];
var toStateIds = nameToIDs(toState.name);
var maxI = Math.min(fromStateIds.length, toStateIds.length);

if (fromState && fromState.name === toState.name) {
i = Math.max(maxI - 1, 0);
} else {
for (i = 0; i < maxI; i += 1) {
if (fromStateIds[i] !== toStateIds[i]) break;
}
}

var toDeactivate = fromStateIds.slice(i).reverse();
var toActivate = toStateIds.slice(i);
var intersection = fromState && i > 0 ? fromStateIds[i - 1] : '';

return {
intersection: intersection,
toDeactivate: toDeactivate,
toActivate: toActivate
};
}
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
Expand Down Expand Up @@ -638,7 +668,7 @@ define('router5', [], function () {
console.warn('Transition cancelled');
},
onTransitionError: function onTransitionError(toState, fromState, err) {
console.warn('Transition error with code ' + err);
console.warn('Transition error with code ' + err.code);
endGroup();
},
onTransitionSuccess: function onTransitionSuccess(toState, fromState) {
Expand All @@ -657,26 +687,41 @@ define('router5', [], function () {
TRANSITION_ERR: 'TRANSITION_ERR',
TRANSITION_CANCELLED: 'CANCELLED'
};
function asyncProcess(isCancelled, functions, toState, fromState, callback) {
var allowBool = arguments.length <= 5 || arguments[5] === undefined ? true : arguments[5];
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

function asyncProcess(functions, _ref, callback) {
var isCancelled = _ref.isCancelled;
var toState = _ref.toState;
var fromState = _ref.fromState;
var context = _ref.context;
var allowBool = arguments.length <= 3 || arguments[3] === undefined ? true : arguments[3];

var remainingFunctions = Array.isArray(functions) ? functions : Object.keys(functions);

var initialFromState = _extends({}, fromState);
var isState = function isState(obj) {
return typeof obj === 'object' && obj.name !== undefined && obj.params !== undefined && obj.path !== undefined;
};
var hasStateChanged = function hasStateChanged(state) {
return state.name !== toState.name || state.params !== toState.params || state.path !== toState.path;
};

var processFn = function processFn(done) {
if (!remainingFunctions.length) return true;

var isMapped = typeof remainingFunctions[0] === 'string';
var errVal = isMapped ? remainingFunctions[0] : true;
var errVal = isMapped ? remainingFunctions[0] : {};
var stepFn = isMapped ? functions[remainingFunctions[0]] : remainingFunctions[0];
stepFn = context ? stepFn.bind(context) : stepFn;

var len = stepFn.length;
var res = stepFn(toState, fromState, done);

if (allowBool && typeof res === 'boolean') {
done(res ? null : errVal);
} else if (res && typeof res.then === 'function') {
res.then(function () {
return done(null);
res.then(function (resVal) {
return done(null, resVal);
}, function () {
return done(errVal);
});
Expand All @@ -686,8 +731,11 @@ define('router5', [], function () {
return false;
};

var iterate = function iterate(err) {
var iterate = function iterate(err, val) {
if (err) callback(err);else {
if (val && isState(val)) {
if (hasStateChanged(val)) console.error('[router5][transition] State values changed during transition process and ignored.');else toState = val;
}
remainingFunctions = remainingFunctions.slice(1);
next();
}
Expand All @@ -698,13 +746,12 @@ define('router5', [], function () {
callback(null);
} else {
var finished = processFn(iterate);
if (finished) callback(null);
if (finished) callback(null, toState);
}
};

next();
}
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

Expand All @@ -714,25 +761,6 @@ define('router5', [], function () {
}, []);
};

function transitionPath(toState, fromState) {
var i = undefined;
var fromStateIds = fromState ? nameToIDs(fromState.name) : [];
var toStateIds = nameToIDs(toState.name);
var maxI = Math.min(fromStateIds.length, toStateIds.length);

if (fromState && fromState.name === toState.name) i = Math.max(maxI - 1, 0);else {
for (i = 0; i < maxI; i += 1) {
if (fromStateIds[i] !== toStateIds[i]) break;
}
}

var toDeactivate = fromStateIds.slice(i).reverse();
var toActivate = toStateIds.slice(i);
var intersection = fromState && i > 0 ? fromStateIds[i - 1] : '';

return { intersection: intersection, toDeactivate: toDeactivate, toActivate: toActivate };
}

function transition(router, toState, fromState, callback) {
var cancelled = false;
var isCancelled = function isCancelled() {
Expand All @@ -741,7 +769,7 @@ define('router5', [], function () {
var cancel = function cancel() {
return cancelled = true;
};
var done = function done(err) {
var done = function done(err, state) {
if (!err && !isCancelled() && router.options.autoCleanUp) {
(function () {
var activeSegments = nameToIDs(toState.name);
Expand All @@ -750,7 +778,7 @@ define('router5', [], function () {
});
})();
}
callback(isCancelled() ? { code: constants.TRANSITION_CANCELLED } : err);
callback(isCancelled() ? { code: constants.TRANSITION_CANCELLED } : err, state || toState);
};

var _transitionPath = transitionPath(toState, fromState);
Expand All @@ -765,7 +793,7 @@ define('router5', [], function () {
return _extends({}, fnMap, _defineProperty({}, name, router._cmps[name].canDeactivate));
}, {});

asyncProcess(isCancelled, canDeactivateFunctionMap, toState, fromState, function (err) {
asyncProcess(canDeactivateFunctionMap, { isCancelled: isCancelled, toState: toState, fromState: fromState }, function (err) {
return cb(err ? { code: constants.CANNOT_DEACTIVATE, segment: err } : null);
});
};
Expand All @@ -777,7 +805,7 @@ define('router5', [], function () {
return _extends({}, fnMap, _defineProperty({}, name, router._canAct[name]));
}, {});

asyncProcess(isCancelled, canActivateFunctionMap, toState, fromState, function (err) {
asyncProcess(canActivateFunctionMap, { isCancelled: isCancelled, toState: toState, fromState: fromState }, function (err) {
return cb(err ? { code: constants.CANNOT_ACTIVATE, segment: err } : null);
});
};
Expand All @@ -786,21 +814,29 @@ define('router5', [], function () {
var middleware = function middleware(toState, fromState, cb) {
var mwareFunction = Array.isArray(router.mware) ? router.mware : [router.mware];

asyncProcess(isCancelled, mwareFunction, toState, fromState, function (err) {
return cb(err ? { code: constants.TRANSITION_ERR } : null);
asyncProcess(mwareFunction, { isCancelled: isCancelled, toState: toState, fromState: fromState, context: { cancel: cancel, router: router } }, function (err, state) {
var errObj = err ? typeof err === 'object' ? err : { error: err } : null;
cb(err ? _extends({ code: constants.TRANSITION_ERR }, errObj) : null, state || toState);
});
};

var pipeline = (fromState ? [canDeactivate] : []).concat(canActivate).concat(middlewareFn ? middleware : []);

asyncProcess(isCancelled, pipeline, toState, fromState, done);
asyncProcess(pipeline, { isCancelled: isCancelled, toState: toState, fromState: fromState }, done);

return cancel;
}


var makeState = function makeState(name, params, path) {
return { name: name, params: params, path: path };
var state = {};
var setProp = function setProp(key, value) {
return Object.defineProperty(state, key, { value: value, enumerable: true });
};
setProp('name', name);
setProp('params', params);
setProp('path', path);
return state;
};

/**
Expand Down Expand Up @@ -1281,10 +1317,11 @@ define('router5', [], function () {
var _this5 = this;

// Cancel current transition
if (this._tr) this._tr();
this.cancel();
this._invokeListeners('$$start', toState, fromState);

var tr = transition(this, toState, fromState, function (err) {
var tr = transition(this, toState, fromState, function (err, state) {
state = state || toState;
_this5._tr = null;

if (err) {
Expand All @@ -1294,9 +1331,9 @@ define('router5', [], function () {
return;
}

_this5.lastKnownState = toState;
_this5.lastKnownState = state; // toState or modified state?

if (done) done(null, toState);
if (done) done(null, state);
});

this._tr = tr;
Expand All @@ -1305,6 +1342,16 @@ define('router5', [], function () {
};
}

/**
* Undocumented for now
* @private
*/
}, {
key: 'cancel',
value: function cancel() {
if (this._tr) this._tr();
}

/**
* Navigate to a specific route
* @param {String} name The route name
Expand Down
2 changes: 1 addition & 1 deletion dist/amd/router5.min.js

Large diffs are not rendered by default.

0 comments on commit c0b137e

Please sign in to comment.