import { Tracker } from 'meteor/tracker';
import { autorun } from 'mobx';

export default (trackerMobxAutorun) => {
  let mobxDisposer = null;
  let computation = null;
  let hasBeenStarted;
  let subscriptionHandle;
  return {
    start() {
      let isFirstRun = true;
      computation = Tracker.autorun(() => {
        if (mobxDisposer) {
          mobxDisposer();
          isFirstRun = true;
        }
        mobxDisposer = autorun(() => {
          if (isFirstRun) {
            subscriptionHandle = trackerMobxAutorun();
          } else {
            computation.invalidate();
            subscriptionHandle && subscriptionHandle.stop();
          }
          isFirstRun = false;
        });
      });
      hasBeenStarted = true;
    },
    stop() {
      if (hasBeenStarted) {
        computation.stop();
        mobxDisposer();
        subscriptionHandle && subscriptionHandle.stop();
      }
    }
  };
};

@@ -188,34 +188,8 @@
 ed;%0A
-  let subscriptionHandle;%0A
   re
@@ -195,16 +195,16 @@
 eturn %7B%0A
+
     star
@@ -457,29 +457,8 @@
     
- subscriptionHandle =
  tra
@@ -537,69 +537,8 @@
 ();%0A
-            subscriptionHandle && subscriptionHandle.stop();%0A
     
@@ -698,24 +698,24 @@
 ion.stop();%0A
+
         mobx
@@ -730,65 +730,8 @@
 ();%0A
-        subscriptionHandle && subscriptionHandle.stop();%0A
     

Remove unnecessary subscription stopping