arunoda / react-komposer

Feed data into React components by composing containers.
MIT License
732 stars 70 forks source link

Tracker integration - child components declare dependencies on parent components #153

Open davidworkman9 opened 7 years ago

davidworkman9 commented 7 years ago

With the 1.x version and the solution the README for 2, a child component can affect an autorun on a parent, which in my case caused a ton of subscriptions to get called, spiking CPU usage to 80%+ on an AWS general purpose medium instance with two people typing on a controlled component. I fixed this by first upgrading to 2.0, then taking the README solution for tracker and wrapping a nonreactive call around the call to onData.

function getTrackerLoader(reactiveMapper) {
    return (props, onDataOrig, env) => {
        const onData = (...args) =>
            Tracker.nonreactive(() =>
                onDataOrig.apply(this, args));

        let trackerCleanup = null;
        const handler = Tracker.nonreactive(() => {

            return Tracker.autorun(() => {
                trackerCleanup = reactiveMapper(props, onData, env);
            });
        });

        return () => {
            if(typeof trackerCleanup === 'function') trackerCleanup();
            return handler.stop();
        };
    };
}