Skip to content

Instantly share code, notes, and snippets.

@crapthings
Last active May 19, 2017 08:04
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 crapthings/87e32b2ef11b844efa2e15d4b69361dc to your computer and use it in GitHub Desktop.
Save crapthings/87e32b2ef11b844efa2e15d4b69361dc to your computer and use it in GitHub Desktop.
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