Use willTransition instead of deactivate to cancel requests

deactivate happens _after_ the new route's model hook, which
results in the possibility of canceling new requests right
after they are made rather than existing open connections
This commit is contained in:
Michael Lange 2018-02-28 16:27:15 -08:00
parent 83df67a8ab
commit 2282f3243a
9 changed files with 49 additions and 56 deletions

View File

@ -0,0 +1,16 @@
import Mixin from '@ember/object/mixin';
import { computed } from '@ember/object';
import { assert } from '@ember/debug';
export default Mixin.create({
watchers: computed(() => []),
actions: {
willTransition() {
this.get('watchers').forEach(watcher => {
assert('Watchers must be Ember Concurrency Tasks.', !!watcher.cancelAll);
watcher.cancelAll();
});
},
},
});

View File

@ -1,17 +1,16 @@
import Route from '@ember/routing/route';
import WithModelErrorHandling from 'nomad-ui/mixins/with-model-error-handling';
import { collect } from '@ember/object/computed';
import { watchRecord } from 'nomad-ui/utils/properties/watch';
import WithWatchers from 'nomad-ui/mixins/with-watchers';
export default Route.extend(WithModelErrorHandling, {
export default Route.extend(WithModelErrorHandling, WithWatchers, {
setupController(controller, model) {
controller.set('watcher', this.get('watch').perform(model));
return this._super(...arguments);
},
deactivate() {
this.get('watch').cancelAll();
return this._super(...arguments);
},
watch: watchRecord('allocation'),
watchers: collect('watch'),
});

View File

@ -1,9 +1,11 @@
import { inject as service } from '@ember/service';
import Route from '@ember/routing/route';
import { collect } from '@ember/object/computed';
import notifyError from 'nomad-ui/utils/notify-error';
import { watchRecord, watchRelationship } from 'nomad-ui/utils/properties/watch';
import WithWatchers from 'nomad-ui/mixins/with-watchers';
export default Route.extend({
export default Route.extend(WithWatchers, {
store: service(),
model() {
@ -23,12 +25,8 @@ export default Route.extend({
return this._super(...arguments);
},
deactivate() {
this.get('watch').cancelAll();
this.get('watchAllocations').cancelAll();
return this._super(...arguments);
},
watch: watchRecord('node'),
watchAllocations: watchRelationship('allocations'),
watchers: collect('watch', 'watchAllocations'),
});

View File

@ -1,16 +1,14 @@
import Route from '@ember/routing/route';
import { collect } from '@ember/object/computed';
import { watchAll } from 'nomad-ui/utils/properties/watch';
import WithWatchers from 'nomad-ui/mixins/with-watchers';
export default Route.extend({
export default Route.extend(WithWatchers, {
setupController(controller) {
controller.set('watcher', this.get('watch').perform());
return this._super(...arguments);
},
deactivate() {
this.get('watch').cancelAll();
this._super(...arguments);
},
watch: watchAll('node'),
watchers: collect('watch'),
});

View File

@ -1,18 +1,16 @@
import Route from '@ember/routing/route';
import { collect } from '@ember/object/computed';
import { watchAll } from 'nomad-ui/utils/properties/watch';
import WithWatchers from 'nomad-ui/mixins/with-watchers';
export default Route.extend({
export default Route.extend(WithWatchers, {
setupController(controller) {
controller.set('modelWatch', this.get('watch').perform());
return this._super(...arguments);
},
deactivate() {
this.get('watch').cancelAll();
this._super(...arguments);
},
watch: watchAll('job'),
watchers: collect('watch'),
actions: {
refreshRoute() {

View File

@ -1,8 +1,10 @@
import Route from '@ember/routing/route';
import RSVP from 'rsvp';
import { collect } from '@ember/object/computed';
import { watchRelationship } from 'nomad-ui/utils/properties/watch';
import WithWatchers from 'nomad-ui/mixins/with-watchers';
export default Route.extend({
export default Route.extend(WithWatchers, {
model() {
const job = this.modelFor('jobs.job');
return RSVP.all([job.get('deployments'), job.get('versions')]).then(() => job);
@ -14,12 +16,8 @@ export default Route.extend({
return this._super(...arguments);
},
deactivate() {
this.get('watchDeployments').cancelAll();
this.get('watchVersions').cancelAll();
return this._super(...arguments);
},
watchDeployments: watchRelationship('deployments'),
watchVersions: watchRelationship('versions'),
watchers: collect('watchDeployments', 'watchVersions'),
});

View File

@ -1,8 +1,9 @@
import Route from '@ember/routing/route';
import { collect } from '@ember/object/computed';
import { watchRecord, watchRelationship } from 'nomad-ui/utils/properties/watch';
import WithWatchers from 'nomad-ui/mixins/with-watchers';
export default Route.extend({
export default Route.extend(WithWatchers, {
setupController(controller, model) {
controller.set('watchers', {
model: this.get('watch').perform(model),
@ -14,17 +15,10 @@ export default Route.extend({
return this._super(...arguments);
},
deactivate() {
this.get('allWatchers').forEach(watcher => {
watcher.cancelAll();
});
this._super(...arguments);
},
watch: watchRecord('job'),
watchSummary: watchRelationship('summary'),
watchEvaluations: watchRelationship('evaluations'),
watchDeployments: watchRelationship('deployments'),
allWatchers: collect('watch', 'watchSummary', 'watchEvaluations', 'watchDeployments'),
watchers: collect('watch', 'watchSummary', 'watchEvaluations', 'watchDeployments'),
});

View File

@ -1,8 +1,9 @@
import Route from '@ember/routing/route';
import { collect } from '@ember/object/computed';
import { watchRecord, watchRelationship } from 'nomad-ui/utils/properties/watch';
import WithWatchers from 'nomad-ui/mixins/with-watchers';
export default Route.extend({
export default Route.extend(WithWatchers, {
model({ name }) {
// If the job is a partial (from the list request) it won't have task
// groups. Reload the job to ensure task groups are present.
@ -28,16 +29,9 @@ export default Route.extend({
return this._super(...arguments);
},
deactivate() {
this.get('allWatchers').forEach(watcher => {
watcher.cancelAll();
});
return this._super(...arguments);
},
watchJob: watchRecord('job'),
watchSummary: watchRelationship('summary'),
watchAllocations: watchRelationship('allocations'),
allWatchers: collect('watchJob', 'watchSummary', 'watchAllocations'),
watchers: collect('watchJob', 'watchSummary', 'watchAllocations'),
});

View File

@ -1,7 +1,9 @@
import Route from '@ember/routing/route';
import { collect } from '@ember/object/computed';
import { watchRelationship } from 'nomad-ui/utils/properties/watch';
import WithWatchers from 'nomad-ui/mixins/with-watchers';
export default Route.extend({
export default Route.extend(WithWatchers, {
model() {
const job = this.modelFor('jobs.job');
return job.get('versions').then(() => job);
@ -12,10 +14,6 @@ export default Route.extend({
return this._super(...arguments);
},
deactivate() {
this.get('watchVersions').cancelAll();
return this._super(...arguments);
},
watchVersions: watchRelationship('versions'),
watchers: collect('watchVersions'),
});