Last active
May 19, 2017 08:04
-
-
Save crapthings/87e32b2ef11b844efa2e15d4b69361dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { compose } from 'react-komposer' | |
function RV(component, states) { | |
const localStates = {} | |
const localValues = {} | |
for (let state in states) { | |
localStates[state] = new ReactiveVar(states[state]) | |
} | |
const track = (props, onData, env) => { | |
for (let state in localStates) { | |
localValues[state] = localStates[state].get() | |
} | |
onData(null, { localValues, localStates, props, env }) | |
} | |
const Component = compose(composeWithTracker(track))(component) | |
return { | |
localStates, | |
localValues, | |
component: <Component />, | |
} | |
} | |
function composeWithTracker(reactiveMapper) { | |
return (props, onData, env) => { | |
let trackerCleanup = null; | |
const handler = Tracker.nonreactive(() => { | |
return Tracker.autorun(() => { | |
// assign the custom clean-up function. | |
trackerCleanup = reactiveMapper(props, onData, env); | |
}); | |
}); | |
return () => { | |
if (typeof trackerCleanup === 'function') trackerCleanup(); | |
return handler.stop(); | |
}; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment