Chore/Tech debt: Remove (most) instances of $q angular service use (#20668)

* Chore/Tech debt: Remove (most) instances of $q angular service use
Removes instances where the angular $q service is used and replaces
it with native Promises.
This commit is contained in:
kay delaney
2019-12-05 09:04:03 +00:00
committed by GitHub
parent 62f0aca3e6
commit 880fbcb09a
58 changed files with 320 additions and 467 deletions

View File

@@ -1,5 +1,5 @@
import _ from 'lodash';
import angular, { IQService } from 'angular';
import angular from 'angular';
import coreModule from 'app/core/core_module';
import { DashboardModel } from 'app/features/dashboard/state';
import DatasourceSrv from 'app/features/plugins/datasource_srv';
@@ -16,7 +16,6 @@ export class AdHocFiltersCtrl {
constructor(
private uiSegmentSrv: any,
private datasourceSrv: DatasourceSrv,
private $q: IQService,
private variableSrv: VariableSrv,
$scope: any
) {
@@ -51,11 +50,11 @@ export class AdHocFiltersCtrl {
getOptions(segment: { type: string }, index: number) {
if (segment.type === 'operator') {
return this.$q.when(this.uiSegmentSrv.newOperators(['=', '!=', '<', '>', '=~', '!~']));
return Promise.resolve(this.uiSegmentSrv.newOperators(['=', '!=', '<', '>', '=~', '!~']));
}
if (segment.type === 'condition') {
return this.$q.when([this.uiSegmentSrv.newSegment('AND')]);
return Promise.resolve([this.uiSegmentSrv.newSegment('AND')]);
}
return this.datasourceSrv.get(this.variable.datasource).then(ds => {

View File

@@ -1,4 +1,4 @@
import angular, { IQService } from 'angular';
import angular from 'angular';
import _ from 'lodash';
import { iconMap } from './DashLinksEditorCtrl';
import { LinkSrv } from 'app/features/panel/panellinks/link_srv';
@@ -97,7 +97,6 @@ export class DashLinksContainerCtrl {
constructor(
$scope: any,
$rootScope: GrafanaRootScope,
$q: IQService,
backendSrv: BackendSrv,
dashboardSrv: DashboardSrv,
linkSrv: LinkSrv
@@ -108,11 +107,11 @@ export class DashLinksContainerCtrl {
if (linkDef.type === 'dashboards') {
if (!linkDef.tags) {
console.log('Dashboard link missing tag');
return $q.when([]);
return Promise.resolve([]);
}
if (linkDef.asDropdown) {
return $q.when([
return Promise.resolve([
{
title: linkDef.title,
tags: linkDef.tags,
@@ -129,7 +128,7 @@ export class DashLinksContainerCtrl {
}
if (linkDef.type === 'link') {
return $q.when([
return Promise.resolve([
{
url: linkDef.url,
title: linkDef.title,
@@ -143,13 +142,13 @@ export class DashLinksContainerCtrl {
]);
}
return $q.when([]);
return Promise.resolve([]);
}
function updateDashLinks() {
const promises = _.map($scope.links, buildLinks);
$q.all(promises).then(results => {
Promise.all(promises).then(results => {
$scope.generatedLinks = _.flatten(results);
});
}

View File

@@ -1,8 +1,6 @@
import _ from 'lodash';
import { HistoryListCtrl } from './HistoryListCtrl';
import { versions, compare, restore } from './__mocks__/history';
// @ts-ignore
import $q from 'q';
import { CoreEvents } from 'app/types';
describe('HistoryListCtrl', () => {
@@ -18,7 +16,7 @@ describe('HistoryListCtrl', () => {
beforeEach(() => {
historySrv = {
calculateDiff: jest.fn(),
restoreDashboard: jest.fn(() => $q.when({})),
restoreDashboard: jest.fn(() => Promise.resolve({})),
};
$rootScope = {
appEvent: jest.fn(),
@@ -27,13 +25,10 @@ describe('HistoryListCtrl', () => {
});
describe('when the history list component is loaded', () => {
let deferred: any;
beforeEach(() => {
deferred = $q.defer({});
historySrv.getHistoryList = jest.fn(() => deferred.promise);
historySrv.getHistoryList = jest.fn(() => Promise.resolve({}));
historyListCtrl = new HistoryListCtrl({}, $rootScope, {} as any, $q, historySrv, {});
historyListCtrl = new HistoryListCtrl({}, $rootScope, {} as any, historySrv, {});
historyListCtrl.dashboard = {
id: 2,
@@ -48,7 +43,7 @@ describe('HistoryListCtrl', () => {
describe('and the history list is successfully fetched', () => {
beforeEach(async () => {
deferred.resolve(versionsResponse);
historySrv.getHistoryList = jest.fn(() => Promise.resolve(versionsResponse));
await historyListCtrl.getLog();
});
@@ -87,13 +82,9 @@ describe('HistoryListCtrl', () => {
describe('and fetching the history list fails', () => {
beforeEach(async () => {
deferred = $q.defer();
historySrv.getHistoryList = jest.fn(() => Promise.reject(new Error('HistoryListError')));
historySrv.getHistoryList = jest.fn(() => deferred.promise);
historyListCtrl = new HistoryListCtrl({}, $rootScope, {} as any, $q, historySrv, {});
deferred.reject(new Error('HistoryListError'));
historyListCtrl = new HistoryListCtrl({}, $rootScope, {} as any, historySrv, {});
await historyListCtrl.getLog();
});
@@ -132,14 +123,11 @@ describe('HistoryListCtrl', () => {
});
describe('when the user wants to compare two revisions', () => {
let deferred: any;
beforeEach(async () => {
deferred = $q.defer({});
historySrv.getHistoryList = jest.fn(() => $q.when(versionsResponse));
historySrv.calculateDiff = jest.fn(() => deferred.promise);
historySrv.getHistoryList = jest.fn(() => Promise.resolve(versionsResponse));
historySrv.calculateDiff = jest.fn(() => Promise.resolve({}));
historyListCtrl = new HistoryListCtrl({}, $rootScope, {} as any, $q, historySrv, {});
historyListCtrl = new HistoryListCtrl({}, $rootScope, {} as any, historySrv, {});
historyListCtrl.dashboard = {
id: 2,
@@ -147,7 +135,7 @@ describe('HistoryListCtrl', () => {
formatDate: jest.fn(() => 'date'),
};
deferred.resolve(versionsResponse);
historySrv.calculateDiff = jest.fn(() => Promise.resolve(versionsResponse));
await historyListCtrl.getLog();
});
@@ -173,9 +161,7 @@ describe('HistoryListCtrl', () => {
describe('and the basic diff is successfully fetched', () => {
beforeEach(async () => {
deferred = $q.defer({});
historySrv.calculateDiff = jest.fn(() => deferred.promise);
deferred.resolve(compare('basic'));
historySrv.calculateDiff = jest.fn(() => Promise.resolve(compare('basic')));
historyListCtrl.revisions[1].checked = true;
historyListCtrl.revisions[3].checked = true;
await historyListCtrl.getDiff('basic');
@@ -199,9 +185,7 @@ describe('HistoryListCtrl', () => {
describe('and the json diff is successfully fetched', () => {
beforeEach(async () => {
deferred = $q.defer({});
historySrv.calculateDiff = jest.fn(() => deferred.promise);
deferred.resolve(compare('json'));
historySrv.calculateDiff = jest.fn(() => Promise.resolve(compare('json')));
historyListCtrl.revisions[1].checked = true;
historyListCtrl.revisions[3].checked = true;
await historyListCtrl.getDiff('json');
@@ -225,7 +209,7 @@ describe('HistoryListCtrl', () => {
describe('and diffs have already been fetched', () => {
beforeEach(async () => {
deferred.resolve(compare('basic'));
historySrv.calculateDiff = jest.fn(() => Promise.resolve(compare('basic')));
historyListCtrl.revisions[3].checked = true;
historyListCtrl.revisions[1].checked = true;
@@ -246,12 +230,10 @@ describe('HistoryListCtrl', () => {
describe('and fetching the diff fails', () => {
beforeEach(async () => {
deferred = $q.defer({});
historySrv.calculateDiff = jest.fn(() => deferred.promise);
historySrv.calculateDiff = jest.fn(() => Promise.reject());
historyListCtrl.revisions[3].checked = true;
historyListCtrl.revisions[1].checked = true;
deferred.reject();
await historyListCtrl.getDiff('basic');
});
@@ -274,20 +256,17 @@ describe('HistoryListCtrl', () => {
});
describe('when the user wants to restore a revision', () => {
let deferred: any;
beforeEach(async () => {
deferred = $q.defer();
historySrv.getHistoryList = jest.fn(() => $q.when(versionsResponse));
historySrv.restoreDashboard = jest.fn(() => deferred.promise);
historySrv.getHistoryList = jest.fn(() => Promise.resolve(versionsResponse));
historySrv.restoreDashboard = jest.fn(() => Promise.resolve());
historyListCtrl = new HistoryListCtrl({}, $rootScope, {} as any, $q, historySrv, {});
historyListCtrl = new HistoryListCtrl({}, $rootScope, {} as any, historySrv, {});
historyListCtrl.dashboard = {
id: 1,
};
historyListCtrl.restore();
deferred.resolve(versionsResponse);
historySrv.restoreDashboard = jest.fn(() => Promise.resolve(versionsResponse));
await historyListCtrl.getLog();
});
@@ -298,11 +277,10 @@ describe('HistoryListCtrl', () => {
describe('and restore fails to fetch', () => {
beforeEach(async () => {
deferred = $q.defer();
historySrv.getHistoryList = jest.fn(() => $q.when(versionsResponse));
historySrv.restoreDashboard = jest.fn(() => deferred.promise);
historyListCtrl = new HistoryListCtrl({}, $rootScope, {} as any, $q, historySrv, {});
deferred.reject(new Error('RestoreError'));
historySrv.getHistoryList = jest.fn(() => Promise.resolve(versionsResponse));
historySrv.restoreDashboard = jest.fn(() => Promise.resolve());
historyListCtrl = new HistoryListCtrl({}, $rootScope, {} as any, historySrv, {});
historySrv.restoreDashboard = jest.fn(() => Promise.reject(new Error('RestoreError')));
historyListCtrl.restoreConfirm(RESTORE_ID);
await historyListCtrl.getLog();
});

View File

@@ -1,5 +1,5 @@
import _ from 'lodash';
import angular, { ILocationService, IQService } from 'angular';
import angular, { ILocationService } from 'angular';
import locationUtil from 'app/core/utils/location_util';
import { DashboardModel } from '../../state/DashboardModel';
@@ -29,7 +29,6 @@ export class HistoryListCtrl {
private $route: any,
private $rootScope: GrafanaRootScope,
private $location: ILocationService,
private $q: IQService,
private historySrv: HistorySrv,
public $scope: any
) {
@@ -81,15 +80,13 @@ export class HistoryListCtrl {
return then.from(now);
}
getDiff(diff: string) {
getDiff(diff: 'basic' | 'json') {
this.diff = diff;
this.mode = 'compare';
// have it already been fetched?
// @ts-ignore
if (this.delta[this.diff]) {
// @ts-ignore
return this.$q.when(this.delta[this.diff]);
// has it already been fetched?
if (this.delta[diff]) {
return Promise.resolve(this.delta[diff]);
}
const selected = _.filter(this.revisions, { checked: true });