Skip to content

Instantly share code, notes, and snippets.

@gaspard
Last active January 22, 2018 12:50
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 gaspard/773647c5438bce74b166d4f0708a933c to your computer and use it in GitHub Desktop.
Save gaspard/773647c5438bce74b166d4f0708a933c to your computer and use it in GitHub Desktop.
import { connect } from '@cerebral/react'
import { state, props } from 'cerebral/tags'
interface State {
things: {
[ key: string ]: {
title: string,
description: string
}
}
}
interface Props {
id: string
}
const propsPaths = {
id: 'id'
}
// This could be automatically constructed with a sample state tree and some rules to traverse it.
const statePaths = {
things: {
__path: state`things`,
id: {
__path: state`things.${ props`id` }`,
title: { __path: state`things.${ props`id` }.title` },
description: { __path: state`things.${ props`id` }.description` }
}
}
}
interface Getters {
state: State
props: Props
}
function connect2<T> ( map, comp: ( props: T ) => any ): any {
const newMap =
Object.assign
( {}
, ... Object.keys ( map ).map
( k => ( { [ k ]: map [ k ].__path } ) )
)
return connect ( newMap, comp )
}
// This is done once for the whole app
const state = <State> <any> statePaths
const props = <Props> <any> propsPaths
// A component now looks very much like it used to:
connect2
( { title: State.things [ Props.id ].title }
, function foo ( { title } ) {
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment