UNPKG

react-routing

Version:

Isomorphic router for React.js applications

322 lines (247 loc) 11.1 kB
/** * React Routing | http://www.kriasoft.com/react-routing * Copyright (c) Konstantin Tarkus <hello@tarkus.me> | The MIT License */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i['return']) _i['return'](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError('Invalid attempt to destructure non-iterable instance'); } }; })(); 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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } var _Route = require('./Route'); var _Route2 = _interopRequireDefault(_Route); var emptyFunction = function emptyFunction() {}; var Router = (function () { /** * Creates a new instance of the `Router` class. */ function Router(initialize) { _classCallCheck(this, Router); this.routes = []; this.events = Object.create(null); if (typeof initialize === 'function') { initialize(this.on.bind(this)); } } /** * Adds a new route to the routing table or registers an event listener. * * @param {String} path A string in the Express format, an array of strings, or a regular expression. * @param {Function|Array} handlers Asynchronous route handler function(s). */ _createClass(Router, [{ key: 'on', value: function on(path) { for (var _len = arguments.length, handlers = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { handlers[_key - 1] = arguments[_key]; } if (path === 'error') { this.events[path] = handlers[0]; } else { this.routes.push(new _Route2['default'](path, handlers)); } } }, { key: 'dispatch', value: function dispatch(state, cb) { var routes, handlers, value, result, done, next; return regeneratorRuntime.async(function dispatch$(context$2$0) { while (1) switch (context$2$0.prev = context$2$0.next) { case 0: next = function next() { var _handlers$next; var _value, _value2, match, handler; return regeneratorRuntime.async(function next$(context$3$0) { while (1) switch (context$3$0.prev = context$3$0.next) { case 0: if (!((_handlers$next = handlers.next(), value = _handlers$next.value, done = _handlers$next.done, _handlers$next) && !done)) { context$3$0.next = 16; break; } _value = value; _value2 = _slicedToArray(_value, 2); match = _value2[0]; handler = _value2[1]; state.params = match.params; if (!(handler.length > 1)) { context$3$0.next = 12; break; } context$3$0.next = 9; return regeneratorRuntime.awrap(handler(state, next)); case 9: context$3$0.t0 = context$3$0.sent; context$3$0.next = 15; break; case 12: context$3$0.next = 14; return regeneratorRuntime.awrap(handler(state)); case 14: context$3$0.t0 = context$3$0.sent; case 15: return context$3$0.abrupt('return', context$3$0.t0); case 16: case 'end': return context$3$0.stop(); } }, null, this); }; if (typeof state === 'string' || state instanceof String) { state = { path: state }; } cb = cb || emptyFunction; routes = this.routes; handlers = regeneratorRuntime.mark(function callee$2$0() { var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, route, match, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, handler; return regeneratorRuntime.wrap(function callee$2$0$(context$3$0) { while (1) switch (context$3$0.prev = context$3$0.next) { case 0: _iteratorNormalCompletion = true; _didIteratorError = false; _iteratorError = undefined; context$3$0.prev = 3; _iterator = routes[Symbol.iterator](); case 5: if (_iteratorNormalCompletion = (_step = _iterator.next()).done) { context$3$0.next = 38; break; } route = _step.value; match = route.match(state.path); if (!match) { context$3$0.next = 35; break; } _iteratorNormalCompletion2 = true; _didIteratorError2 = false; _iteratorError2 = undefined; context$3$0.prev = 12; _iterator2 = match.route.handlers[Symbol.iterator](); case 14: if (_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done) { context$3$0.next = 21; break; } handler = _step2.value; context$3$0.next = 18; return [match, handler]; case 18: _iteratorNormalCompletion2 = true; context$3$0.next = 14; break; case 21: context$3$0.next = 27; break; case 23: context$3$0.prev = 23; context$3$0.t0 = context$3$0['catch'](12); _didIteratorError2 = true; _iteratorError2 = context$3$0.t0; case 27: context$3$0.prev = 27; context$3$0.prev = 28; if (!_iteratorNormalCompletion2 && _iterator2['return']) { _iterator2['return'](); } case 30: context$3$0.prev = 30; if (!_didIteratorError2) { context$3$0.next = 33; break; } throw _iteratorError2; case 33: return context$3$0.finish(30); case 34: return context$3$0.finish(27); case 35: _iteratorNormalCompletion = true; context$3$0.next = 5; break; case 38: context$3$0.next = 44; break; case 40: context$3$0.prev = 40; context$3$0.t1 = context$3$0['catch'](3); _didIteratorError = true; _iteratorError = context$3$0.t1; case 44: context$3$0.prev = 44; context$3$0.prev = 45; if (!_iteratorNormalCompletion && _iterator['return']) { _iterator['return'](); } case 47: context$3$0.prev = 47; if (!_didIteratorError) { context$3$0.next = 50; break; } throw _iteratorError; case 50: return context$3$0.finish(47); case 51: return context$3$0.finish(44); case 52: case 'end': return context$3$0.stop(); } }, callee$2$0, this, [[3, 40, 44, 52], [12, 23, 27, 35], [28,, 30, 34], [45,, 47, 51]]); })(); value = undefined, result = undefined, done = false; case 6: if (done) { context$2$0.next = 16; break; } context$2$0.next = 9; return regeneratorRuntime.awrap(next()); case 9: result = context$2$0.sent; if (!result) { context$2$0.next = 14; break; } state.statusCode = typeof state.statusCode === 'number' ? state.statusCode : 200; cb(state, result); return context$2$0.abrupt('return'); case 14: context$2$0.next = 6; break; case 16: if (!this.events.error) { context$2$0.next = 32; break; } context$2$0.prev = 17; state.statusCode = 404; context$2$0.next = 21; return regeneratorRuntime.awrap(this.events.error(state, new Error('Cannot found a route matching \'' + state.path + '\'.'))); case 21: result = context$2$0.sent; cb(state, result); context$2$0.next = 32; break; case 25: context$2$0.prev = 25; context$2$0.t0 = context$2$0['catch'](17); state.statusCode = 500; context$2$0.next = 30; return regeneratorRuntime.awrap(this.events.error(state, context$2$0.t0)); case 30: result = context$2$0.sent; cb(state, result); case 32: case 'end': return context$2$0.stop(); } }, null, this, [[17, 25]]); } }]); return Router; })(); exports['default'] = Router; module.exports = exports['default'];