Skip to content

Instantly share code, notes, and snippets.

@maxholman
Created March 8, 2018 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxholman/38569b50c123120b85d45c6807c6a816 to your computer and use it in GitHub Desktop.
Save maxholman/38569b50c123120b85d45c6807c6a816 to your computer and use it in GitHub Desktop.
interface WiredActions<S> {
[key: string]: (...args: any[]) => S | WiredActions<S>;
}
type GenericState = any;
type Actions = HyperAppActions<GenericState, WiredActions<GenericState>>;
function rewriteActions(actions: Actions): Actions {
return Object.entries(actions).reduce((accum, [name, action]) => {
// dont rewrite the location actions for @hyperapp/router
if (name === 'location') {
return {
...accum,
[name]: action
};
}
return {
...accum,
[name]: typeof action === 'object' ?
rewriteActions(action) :
(...args1: any[]) => {
return (state: GenericState, actions: BoundActions<GenericState>) => {
const result = action(state, actions);
return result(...args1);
};
}
};
}, {});
}
export function withSnoitac(app: App<GenericState, Actions>): App<GenericState, Actions> {
return (state, actions, view, container) => app(state, rewriteActions(actions), view, container);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment