@prodo/route
Version:
Route plugin for [Prodo](https://prodo.dev). See [documentation](https://docs.prodo.dev/plugins/routing) for more info.
50 lines • 1.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@prodo/core");
var actions_1 = require("./actions");
var types_1 = require("./types");
var persistentContext = { isTimeTravelling: false };
var prepareContext = function (ctx, history, universe) {
ctx[types_1.historySymbol] = history;
ctx[types_1.universeSymbol] = universe;
ctx[types_1.persistentSymbol] = persistentContext;
ctx.route = core_1.createUniverseWatcher("route");
};
var plugin = core_1.createPlugin("route");
var parseParams = function (search) {
var searchParams = new URLSearchParams(search);
var params = {};
searchParams.forEach(function (value, key) {
params[key] = value;
});
return params;
};
plugin.init(function (config, universe, store) {
var history = config.route.history;
var currentPath = history.location.pathname;
var params = parseParams(history.location.search);
universe.route = {
path: currentPath,
params: params,
};
config.route.history.listen(function (location) {
return store.dispatch(exports.setRoute)({
path: location.pathname,
params: parseParams(location.search),
});
});
});
plugin.prepareActionCtx(function (_a, config) {
var ctx = _a.ctx, universe = _a.universe;
return prepareContext(ctx, config.route.history, universe);
});
plugin.prepareViewCtx(function (_a, config) {
var ctx = _a.ctx, universe = _a.universe;
return prepareContext(ctx, config.route.history, universe);
});
exports.default = plugin;
exports.push = plugin.action(actions_1.pushAction, "push");
exports.replace = plugin.action(actions_1.replaceAction, "replace");
exports.setRoute = plugin.action(actions_1.setRouteAction, "setRoute");
plugin.exposeUniverseVars(["route"]);
//# sourceMappingURL=plugin.js.map