Introduction

Edit This Page

autorunUntil

autorunUntil(predicate: () => boolean, effect: () => void, scope?)

autorunUntil observes & runs the given predicate until it returns true. Once that happens, the given effect is executed and the autorunner is disposed. The function returns a disposer to cancel the autorunner prematurely.

This function is really useful to dispose or cancel stuff in a reactive way. For example:

  1. class MyResource {
  2. constructor() {
  3. autorunUntil(
  4. // once...
  5. () => !this.isVisible,
  6. // ... then
  7. () => this.dispose()
  8. );
  9. }
  10. @observable get isVisible() {
  11. // indicate whether this item is visible
  12. }
  13. dispose() {
  14. // dispose
  15. }
  16. }