Skip to content

Commit

Permalink
don't nest x-interaction wrappers; rewrap instead
Browse files Browse the repository at this point in the history
  • Loading branch information
apaleslimghost committed Nov 1, 2018
1 parent a9bc8ed commit db7aa9b
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions components/x-interaction/src/Interaction.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,45 @@ import { registerComponent } from './concerns/register-component';
// use the class version for clientside and the static version for server
const Interaction = typeof window !== 'undefined' ? InteractionClass : InteractionSSR;

const invoke = (fnOrObj, ...args) => typeof fnOrObj === 'function'
? fnOrObj(...args)
: fnOrObj;

export const withActions = (getActions) => (Component) => {
const _wraps = { getActions, Component };

// if the component we're wrapping is already wrapped, we don't want
// to wrap it further. so, discard its wrapper and rewrap the original
// component with the new actions on top
if(Component._wraps) {
Component = Component._wraps.Component;
getActions = initialState => Object.assign(
invoke(Component._wraps.getActions, initialState),
invoke(_wraps.getActions, initialState)
);
}

function Enhanced({
id,
actions: extraActions,
actionsRef,
serialiser,
...initialState
}) {
// support passing actions to withActions as an object or a function
// that's called with the initial state
const actions = typeof getActions === 'function'
? getActions(initialState)
: getActions;
const actions = invoke(getActions, initialState);

return <Interaction {...{
id,
Component,
initialState,
actionsRef,
serialiser,
// if extraActions is defined, those are from another level
// of wrapping with withActions, so those should take precedence
actions: Object.assign(actions, extraActions),
actions,
}} />;
}

// store what we're wrapping for later wrappers to replace
Enhanced._wraps = _wraps;

// set the displayName of the Enhanced component for debugging
wrapComponentName(Component, Enhanced);

Expand Down

0 comments on commit db7aa9b

Please sign in to comment.