open-nomad/ui/app/mixins/with-watchers.js
Buck Doyle 1cca7abcab
Add Ember ESLint plugin (#8134)
This is extracted from #8094, where I have run into some snags. Since
these ESLint fixes aren’t actually connected to the Ember 3.16 update
but involve changes to many files, we might as well address them
separately. Where possible I fixed the problems but in cases where
a fix seemed too involved, I added per-line or -file exceptions.
2020-06-09 16:03:28 -05:00

48 lines
1.2 KiB
JavaScript

import Mixin from '@ember/object/mixin';
import { computed } from '@ember/object';
import { assert } from '@ember/debug';
import WithVisibilityDetection from './with-route-visibility-detection';
// eslint-disable-next-line ember/no-new-mixins
export default Mixin.create(WithVisibilityDetection, {
watchers: computed(function() {
return [];
}),
cancelAllWatchers() {
this.watchers.forEach(watcher => {
assert('Watchers must be Ember Concurrency Tasks.', !!watcher.cancelAll);
watcher.cancelAll();
});
},
startWatchers() {
assert('startWatchers needs to be overridden in the Route', false);
},
setupController() {
this.startWatchers(...arguments);
return this._super(...arguments);
},
visibilityHandler() {
if (document.hidden) {
this.cancelAllWatchers();
} else {
this.startWatchers(this.controller, this.controller.get('model'));
}
},
actions: {
willTransition(transition) {
// Don't cancel watchers if transitioning into a sub-route
if (!transition.intent.name || !transition.intent.name.startsWith(this.routeName)) {
this.cancelAllWatchers();
}
// Bubble the action up to the application route
return true;
},
},
});