github.com/emate/nomad@v0.8.2-wo-binpacking/ui/app/mixins/with-watchers.js (about)

     1  import Mixin from '@ember/object/mixin';
     2  import { computed } from '@ember/object';
     3  import { assert } from '@ember/debug';
     4  import WithVisibilityDetection from './with-route-visibility-detection';
     5  
     6  export default Mixin.create(WithVisibilityDetection, {
     7    watchers: computed(() => []),
     8  
     9    cancelAllWatchers() {
    10      this.get('watchers').forEach(watcher => {
    11        assert('Watchers must be Ember Concurrency Tasks.', !!watcher.cancelAll);
    12        watcher.cancelAll();
    13      });
    14    },
    15  
    16    startWatchers() {
    17      assert('startWatchers needs to be overridden in the Route', false);
    18    },
    19  
    20    setupController() {
    21      this.startWatchers(...arguments);
    22      return this._super(...arguments);
    23    },
    24  
    25    visibilityHandler() {
    26      if (document.hidden) {
    27        this.cancelAllWatchers();
    28      } else {
    29        this.startWatchers(this.controller, this.controller.get('model'));
    30      }
    31    },
    32  
    33    actions: {
    34      willTransition() {
    35        this.cancelAllWatchers();
    36      },
    37    },
    38  });