Created
August 26, 2019 11:03
-
-
Save alonbardavid/52a0c0c74a7ce5a8b978d749dcb44737 to your computer and use it in GitHub Desktop.
Suspending mobx computation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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