cycle-vtree-switcher
Advanced tools
Comparing version 1.0.0 to 2.0.0
@@ -7,3 +7,3 @@ 'use strict'; | ||
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; }; | ||
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'); } }; })(); | ||
@@ -25,39 +25,61 @@ exports['default'] = vtreeSwitcher; | ||
var routeHandlers = Object.keys(routes).reduce(function (m, route) { | ||
var _ref = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
var _ref$pathDriver = _ref.pathDriver; | ||
var pathDriver = _ref$pathDriver === undefined ? 'path' : _ref$pathDriver; | ||
var _ref$domDriver = _ref.domDriver; | ||
var domDriver = _ref$domDriver === undefined ? 'dom' : _ref$domDriver; | ||
var path$ = responses[pathDriver]; | ||
// { rule: handler } => { rule: name }, { name: handler } | ||
var rules = Object.keys(routes); | ||
var _rules$reduce = rules.reduce(function (_ref2, route) { | ||
var _ref22 = _slicedToArray(_ref2, 2); | ||
var ruleNames = _ref22[0]; | ||
var nameHandlers = _ref22[1]; | ||
var handler = routes[route]; | ||
m[handler.name] = handler; | ||
return m; | ||
}, {}); | ||
var routeNames = Object.keys(routes).reduce(function (m, route) { | ||
var handler = routes[route]; | ||
m[route] = handler.name; | ||
return m; | ||
}, {}); | ||
nameHandlers[handler.name] = handler; | ||
ruleNames[route] = handler.name; | ||
return [ruleNames, nameHandlers]; | ||
}, [{}, {}]); | ||
var names = Object.keys(routeHandlers); | ||
var nameIdx = names.reduce(function (m, name, i) { | ||
m[name] = i; | ||
return m; | ||
}, {}); | ||
var _rules$reduce2 = _slicedToArray(_rules$reduce, 2); | ||
var matchRoute = (0, _cycleRoute.makeRouter)(routeNames); | ||
var Route = responses.Path.map(matchRoute); | ||
var ruleNames = _rules$reduce2[0]; | ||
var nameHandlers = _rules$reduce2[1]; | ||
var responsesWithRoute = _extends({}, responses, { Route: Route }); | ||
var requests = names.reduce(function (m, name) { | ||
var handler = routeHandlers[name]; | ||
m[name] = handler(responsesWithRoute); | ||
return m; | ||
}, {}); | ||
var matchRoute = (0, _cycleRoute.makeRouter)(ruleNames); | ||
var route$ = path$.map(matchRoute); | ||
var names = Object.keys(nameHandlers); | ||
var _names$reduce = names.reduce(function (_ref3, name, i) { | ||
var _ref32 = _slicedToArray(_ref3, 2); | ||
var nameIdx = _ref32[0]; | ||
var requests = _ref32[1]; | ||
var handler = nameHandlers[name]; | ||
nameIdx[name] = i; | ||
requests[name] = handler(route$, responses); | ||
return [nameIdx, requests]; | ||
}, [{}, {}]); | ||
var _names$reduce2 = _slicedToArray(_names$reduce, 2); | ||
var nameIdx = _names$reduce2[0]; | ||
var requests = _names$reduce2[1]; | ||
var vtree$s = names.map(function (name) { | ||
return requests[name].DOM; | ||
return requests[name][domDriver].startWith(null); | ||
}); | ||
var vtree$ = (_Rx$Observable = _rx2['default'].Observable).combineLatest.apply(_Rx$Observable, [Route].concat(_toConsumableArray(vtree$s), [function (route) { | ||
for (var _len = arguments.length, vtrees = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
vtrees[_key - 1] = arguments[_key]; | ||
} | ||
var vtree$ = (_Rx$Observable = _rx2['default'].Observable).combineLatest.apply(_Rx$Observable, _toConsumableArray(vtree$s)).withLatestFrom(route$, function (vtrees, route) { | ||
return vtrees[nameIdx[route.name]]; | ||
}])); | ||
}).filter(function (vtree) { | ||
return vtree; | ||
}); | ||
@@ -64,0 +86,0 @@ return [vtree$, requests]; |
{ | ||
"name": "cycle-vtree-switcher", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "A Cycle.js helper for switching between route handlers", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -31,41 +31,6 @@ # Cycle VTree Switcher | ||
[Cycle.js](http://cycle.js.org/) use case: | ||
Check out [the example](https://github.com/secobarbital/cycle-vtree-switcher/blob/master/examples/foobar/index.js). | ||
```js | ||
import { run, Rx } from '@cycle/core' | ||
import { makeDOMDriver } from '@cycle/dom' | ||
import { makePushStateDriver } from 'cycle-pushstate-driver' | ||
import vtreeSwitcher from 'cycle-vtree-switcher' | ||
## Real World Example | ||
import owers from './owers' | ||
import owees from './owees' | ||
import transactions from './transactions' | ||
import notfound from './notfound' | ||
const routes = { | ||
'/': owers, | ||
'/owers/:ower': owees, | ||
'/transactions/:ower/:owee': transactions, | ||
'*': notfound | ||
} | ||
function main (responses) { | ||
const { DOM, Path } = responses | ||
const [vtree$, requestMap] = vtreeSwitcher(routes, responses) | ||
return { | ||
DOM: vtree$, | ||
Path: navigate$ | ||
} | ||
} | ||
run(main, { | ||
DOM: makeDOMDriver('main'), | ||
Path: makePushStateDriver() | ||
}) | ||
``` | ||
## Full Example | ||
[iouo.me](https://github.com/secobarbital/iouo.me/blob/ba6608179a7bc69e81c378b2639014de7f9c1f26/src/index.js) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5821
60
36