|
| 1 | +import { h as hh } from "hyperapp"; |
| 2 | + |
| 3 | +const propRename = name => (name.startsWith("on") ? name.toLowerCase() : name); |
| 4 | +export const h = (type, props, ...children) => |
| 5 | + hh( |
| 6 | + type, |
| 7 | + typeof type === "function" && type.length === 1 |
| 8 | + ? { ...props, children } |
| 9 | + : Object.keys(props).reduce( |
| 10 | + (otherProps, name) => ({ |
| 11 | + ...otherProps, |
| 12 | + [propRename(name)]: props[name] |
| 13 | + }), |
| 14 | + {} |
| 15 | + ), |
| 16 | + ...children |
| 17 | + ); |
| 18 | + |
| 19 | +const secretInternals = (window.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED_SERIOUSLY_IM_NOT_JOKING = {}); |
| 20 | + |
| 21 | +const makeInternalStore = ({ actions, reducer }) => ({ |
| 22 | + getState: () => actions.getState(), |
| 23 | + dispatch: action => actions.setState(reducer(actions.getState(), action)) |
| 24 | +}); |
| 25 | + |
| 26 | +export const withReducer = reducer => nextApp => ( |
| 27 | + initialState, |
| 28 | + actionsTemplate, |
| 29 | + view, |
| 30 | + container |
| 31 | +) => { |
| 32 | + const reducerDefaultState = reducer(undefined, {}); |
| 33 | + const actions = nextApp( |
| 34 | + { ...reducerDefaultState, ...initialState }, |
| 35 | + { |
| 36 | + ...actionsTemplate, |
| 37 | + getState: () => state => state, |
| 38 | + setState: state => state |
| 39 | + }, |
| 40 | + view, |
| 41 | + container |
| 42 | + ); |
| 43 | + secretInternals.store = makeInternalStore({ actions, reducer }); |
| 44 | + return actions; |
| 45 | +}; |
| 46 | + |
| 47 | +export const connect = (mapStateToProps, mapDispatchToProps) => Component => ( |
| 48 | + props, |
| 49 | + children |
| 50 | +) => |
| 51 | + h( |
| 52 | + Component, |
| 53 | + { |
| 54 | + ...props, |
| 55 | + ...mapStateToProps(secretInternals.store.getState(), props), |
| 56 | + ...Object.keys(mapDispatchToProps).reduce( |
| 57 | + (otherActions, name) => ({ |
| 58 | + ...otherActions, |
| 59 | + [name]: data => |
| 60 | + secretInternals.store.dispatch(mapDispatchToProps[name](data)) |
| 61 | + }), |
| 62 | + {} |
| 63 | + ) |
| 64 | + }, |
| 65 | + children |
| 66 | + ); |
0 commit comments