UNPKG

boa-handler-history

Version:
48 lines (47 loc) 1.94 kB
// History API Action (pushState / onpopstate event) -> RouteAction // GoToAction -> History API Action "use strict"; var boa_router_1 = require('boa-router'); var hash_history_1 = require('./hash-history'); var history_1 = require('./history'); var do_1 = require('rxjs/operator/do'); var filter_1 = require('rxjs/operator/filter'); var share_1 = require('rxjs/operator/share'); var makeHistory = function (type, callback) { if (type === 'hash') { return new hash_history_1.HashHistory(callback); } else { // TODO: fallback return new history_1.History(callback); } }; var init = function (historyOptions) { var routes = historyOptions.routes, goToActionType = historyOptions.goToActionType, routeActionType = historyOptions.routeActionType, historyType = historyOptions.historyType; var goToType = goToActionType ? goToActionType : 'go-to'; var historyImpl = historyType ? historyType : 'auto'; var routeType = routeActionType ? routeActionType : 'route'; var router = boa_router_1.init(routes); return { handler: function (action$, options) { var re = options.re; var history = makeHistory(historyImpl, (function (path) { var data = router(path); re({ type: routeType, data: data }); })); var isStarted = false; return share_1.share.call(filter_1.filter.call(do_1._do.call(filter_1.filter.call(do_1._do.call(action$, function () { if (!isStarted) { isStarted = true; history.start(); } }), function (action) { return action.type === goToType; }), function (_a) { var path = _a.data; return history.go(path); }), function () { return false; } // remove all // remove all )); } }; }; exports.init = init;