github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/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.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(transition) { 35 // Don't cancel watchers if transitioning into a sub-route 36 if (!transition.intent.name || !transition.intent.name.startsWith(this.routeName)) { 37 this.cancelAllWatchers(); 38 } 39 40 // Bubble the action up to the application route 41 return true; 42 }, 43 }, 44 });