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
|
|
|
|
2018-03-06 22:14:20 +00:00
|
|
|
export default Mixin.create(WithVisibilityDetection, {
|
2018-03-01 00:27:15 +00:00
|
|
|
watchers: computed(() => []),
|
|
|
|
|
2018-03-06 22:14:20 +00:00
|
|
|
cancelAllWatchers() {
|
|
|
|
this.get('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() {
|
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: {
|
|
|
|
willTransition() {
|
2018-03-06 22:14:20 +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
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|