Skip to content

Instantly share code, notes, and snippets.

@vivainio
Last active December 20, 2017 07:50
Show Gist options
  • Select an option

  • Save vivainio/9d07db5e316d74864ce3935c6710e0a6 to your computer and use it in GitHub Desktop.

Select an option

Save vivainio/9d07db5e316d74864ce3935c6710e0a6 to your computer and use it in GitHub Desktop.
import { autorun } from "mobx";
export function startObserving(component: any, expression: () => void) {
const disposer = autorun(expression);
if (!component.__mobx_unsubs) {
component.__mobx_unsubs = [disposer];
} else {
component.__mobx_unsubs.push(disposer);
}
}
export function stopObserving(component: any) {
if (!component.__mobx_unsubs) {
throw new Error(
"Stopped observing without any observers. Did you screw up?"
);
}
component.__mobx_unsubs.forEach(disposer => disposer());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment