github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/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  // eslint-disable-next-line ember/no-new-mixins
     7  export default Mixin.create(WithVisibilityDetection, {
     8    watchers: computed(function() {
     9      return [];
    10    }),
    11  
    12    cancelAllWatchers() {
    13      this.watchers.forEach(watcher => {
    14        assert('Watchers must be Ember Concurrency Tasks.', !!watcher.cancelAll);
    15        watcher.cancelAll();
    16      });
    17    },
    18  
    19    startWatchers() {
    20      assert('startWatchers needs to be overridden in the Route', false);
    21    },
    22  
    23    setupController() {
    24      this.startWatchers(...arguments);
    25      return this._super(...arguments);
    26    },
    27  
    28    visibilityHandler() {
    29      if (document.hidden) {
    30        this.cancelAllWatchers();
    31      } else {
    32        this.startWatchers(this.controller, this.controller.get('model'));
    33      }
    34    },
    35  
    36    actions: {
    37      willTransition(transition) {
    38        // Don't cancel watchers if transitioning into a sub-route
    39        if (!transition.intent.name || !transition.intent.name.startsWith(this.routeName)) {
    40          this.cancelAllWatchers();
    41        }
    42  
    43        // Bubble the action up to the application route
    44        return true;
    45      },
    46    },
    47  });