Skip to content

Instantly share code, notes, and snippets.

@alonbardavid
Created August 26, 2019 11:03
Show Gist options
  • Save alonbardavid/52a0c0c74a7ce5a8b978d749dcb44737 to your computer and use it in GitHub Desktop.
Save alonbardavid/52a0c0c74a7ce5a8b978d749dcb44737 to your computer and use it in GitHub Desktop.
Suspending mobx computation
import { computed} from 'mobx'
const suspended = observable.box(false)
export function setSuspended(value) {
suspended.set(value)
}
export function suspendableComputed(
instance: any,
propertyName: PropertyKey,
descriptor: PropertyDescriptor,
...args
) {
let cached
const oldDescriptor = descriptor.get
descriptor.get = function() {
if (!suspended.get()) {
cached = oldDescriptor.apply(this)
}
return cached
}
return computed(instance, propertyName, descriptor, ...args)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment