2018-03-01 00:27:15 +00:00
|
|
|
import Mixin from '@ember/object/mixin';
|
|
|
|
import { computed } from '@ember/object';
|
|
|
|
import { assert } from '@ember/debug';
|
2018-03-06 22:14:20 +00:00
|
|
|
import WithVisibilityDetection from './with-route-visibility-detection';
|
2018-03-01 00:27:15 +00:00
|
|
|
|
2020-06-09 21:03:28 +00:00
|
|
|
// eslint-disable-next-line ember/no-new-mixins
|
2018-03-06 22:14:20 +00:00
|
|
|
export default Mixin.create(WithVisibilityDetection, {
|
2021-12-28 14:45:20 +00:00
|
|
|
watchers: computed(function () {
|
2020-06-09 21:03:28 +00:00
|
|
|
return [];
|
|
|
|
}),
|
2018-03-01 00:27:15 +00:00
|
|
|
|
2018-03-06 22:14:20 +00:00
|
|
|
cancelAllWatchers() {
|
2021-12-28 14:45:20 +00:00
|
|
|
this.watchers.forEach((watcher) => {
|
2018-03-06 22:14:20 +00:00
|
|
|
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() {
|
2018-03-06 22:27:01 +00:00
|
|
|
if (document.hidden) {
|
2018-03-06 22:14:20 +00:00
|
|
|
this.cancelAllWatchers();
|
|
|
|
} else {
|
|
|
|
this.startWatchers(this.controller, this.controller.get('model'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-03-01 00:27:15 +00:00
|
|
|
actions: {
|
2019-05-20 22:35:59 +00:00
|
|
|
willTransition(transition) {
|
|
|
|
// Don't cancel watchers if transitioning into a sub-route
|
2021-12-28 16:08:12 +00:00
|
|
|
if (
|
|
|
|
!transition.intent.name ||
|
|
|
|
!transition.intent.name.startsWith(this.routeName)
|
|
|
|
) {
|
2019-05-20 22:35:59 +00:00
|
|
|
this.cancelAllWatchers();
|
|
|
|
}
|
2018-11-08 00:08:26 +00:00
|
|
|
|
|
|
|
// Bubble the action up to the application route
|
|
|
|
return true;
|
2018-03-01 00:27:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|