Skip to content

Commit

Permalink
feat: add routedispatcher as wrapper for actiondispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Summe committed Jul 6, 2020
1 parent 78b92c7 commit 942d059
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
19 changes: 15 additions & 4 deletions src/action-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ function constructPath(match) {
return resultParts.join("/");
}

function createActionDispatcher(routesConfig, window) {
function createActionDispatcher(routesConfig, _window = window) {
const { compiledActionMatchers, compiledRouteMatchers } = compileRoutes(
routesConfig
);
Expand All @@ -298,6 +298,7 @@ function createActionDispatcher(routesConfig, window) {

let actionListeners = [];
let currentPath = null;
let currentAction = null;

function ifPathChanged(newPath, cb) {
if (currentPath !== newPath) {
Expand All @@ -307,7 +308,12 @@ function createActionDispatcher(routesConfig, window) {
}

const actionDispatcher = {

get currentPath() {
return currentPath;
},
get currentAction() {
return currentAction;
},
pathForAction,

//hook for everything to get action on route change
Expand All @@ -330,6 +336,8 @@ function createActionDispatcher(routesConfig, window) {

const action = actionForLocation(location);

currentAction = action;

if (action) {
actionListeners.forEach(cb => cb(action));
}
Expand All @@ -344,7 +352,10 @@ function createActionDispatcher(routesConfig, window) {

if (newPath) {
ifPathChanged(newPath, () => {
window.history.pushState({}, "", newPath);
currentAction = action;

_window.history.pushState({}, "", newPath);

if(fireCallbacks) {
actionListeners.forEach(cb => cb(action));
}
Expand All @@ -354,7 +365,7 @@ function createActionDispatcher(routesConfig, window) {

};

window.addEventListener("urlchanged", actionDispatcher);
_window.addEventListener("urlchanged", actionDispatcher);

return actionDispatcher;
}
Expand Down
42 changes: 33 additions & 9 deletions src/provider-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ import * as R from 'ramda';
import React, {useReducer, useEffect} from 'react';
import PropTypes from 'prop-types';

import {createActionDispatcher} from "./action-router";

const RouteContext = React.createContext(null);
const ActionDispatcherContext = React.createContext(null);

function RouteProvider({children, actionDispatcher, _window}) {
function RouteProvider({children, routeDispatcher, _window}) {

const [route, updateRoute] = useReducer((state, action) =>
R.omit(['type'], R.assoc('routeName', action.type, action))
, {});
const [route, updateRoute] = useReducer((state, action) => action, {});

useEffect(() => {
return actionDispatcher.addActionListener(action => updateRoute(action));
return routeDispatcher.addRouteListener(updateRoute);
});

useEffect(() => {
actionDispatcher.receiveLocation(_window.location);
routeDispatcher.receiveLocation(_window.location);
});

return (<ActionDispatcherContext.Provider value={actionDispatcher}>
return (<ActionDispatcherContext.Provider value={routeDispatcher}>
<RouteContext.Provider value={route}>
{children}
</RouteContext.Provider>
Expand All @@ -36,7 +36,7 @@ RouteProvider.propTypes = {
PropTypes.arrayOf(PropTypes.node),
PropTypes.node
]),
actionDispatcher: PropTypes.object
routeDispatcher: PropTypes.object
};

function getDisplayName(WrappedComponent) {
Expand Down Expand Up @@ -89,4 +89,28 @@ RouteLink.propTypes = {
]),
};

export {RouteProvider, withRoute, RouteLink};
function routeToAction(route) {
return R.omit(['routeName'], R.assoc('type', route.routeName, action))
}
function actionToRoute(action) {
return R.omit(['type'], R.assoc('routeName', action.type, action))
}

function createRouteDispatcher(routesConfig, _window = window) {
const actionDispatcher = createActionDispatcher(routesConfig, _window);

actionDispatcher.receiveRoute = (route) => actionDispatcher.receiveAction(routeToAction(route));
actionDispatcher.addRouteListener = (cb) => actionDispatcher.addActionListener((action) => cb(actionToRoute(action)));

Object.defineProperty(actionDispatcher, "currentRoute", {
enumerable: true,
writable: false,
get: function() {
return actionToRoute(this.currentAction)
}
});

return actionDispatcher;
}

export {RouteProvider, withRoute, RouteLink, createRouteDispatcher};

0 comments on commit 942d059

Please sign in to comment.