Skip to content

Commit 2322d25

Browse files
committed
Migrate Deferred to Promise.withResolvers
In the past few years the JS standard added support for `withResolvers` which obviates the need for a custom implementation of the concept of a Deferred promise. This commit moves to that new standard, adds a polyfill so that we can maintain browser support, and removes our implementation of Deferred. Issue #127 Use packages instead of vendored code
1 parent 714b3c8 commit 2322d25

File tree

19 files changed

+250
-260
lines changed

19 files changed

+250
-260
lines changed

src/app/core/components/all-archives/all-archives.component.ts

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { ActivatedRoute, Router } from '@angular/router';
99
import { Validators } from '@angular/forms';
1010

1111
import { remove, orderBy, partition } from 'lodash';
12-
import { Deferred } from '@root/vendor/deferred';
1312

1413
import { AccountService } from '@shared/services/account/account.service';
1514
import {
@@ -93,7 +92,7 @@ export class AllArchivesComponent implements AfterViewInit, OnDestroy {
9392
}
9493

9594
archiveClick(archive: ArchiveVO) {
96-
const deferred = new Deferred();
95+
const { promise, resolve } = Promise.withResolvers();
9796

9897
const buttons: PromptButton[] = [
9998
{
@@ -119,34 +118,31 @@ export class AllArchivesComponent implements AfterViewInit, OnDestroy {
119118
)} access and switch?`;
120119
}
121120

122-
this.prompt
123-
.promptButtons(buttons, message, deferred.promise)
124-
.then((result) => {
125-
if (result === 'switch') {
126-
let acceptIfNeeded: Promise<ArchiveResponse | any> =
127-
Promise.resolve();
128-
129-
if (archive.isPending()) {
130-
acceptIfNeeded = this.api.archive.accept(archive);
131-
}
132-
133-
acceptIfNeeded
134-
.then(async () => await this.accountService.changeArchive(archive))
135-
.then(() => {
136-
deferred.resolve();
137-
this.router.navigate(['/private']);
138-
})
139-
.catch((response: BaseResponse) => {
140-
deferred.resolve();
141-
this.message.showError({
142-
message: response.getMessage(),
143-
translate: true,
144-
});
145-
});
146-
} else {
147-
deferred.resolve();
121+
this.prompt.promptButtons(buttons, message, promise).then((result) => {
122+
if (result === 'switch') {
123+
let acceptIfNeeded: Promise<ArchiveResponse | any> = Promise.resolve();
124+
125+
if (archive.isPending()) {
126+
acceptIfNeeded = this.api.archive.accept(archive);
148127
}
149-
});
128+
129+
acceptIfNeeded
130+
.then(async () => await this.accountService.changeArchive(archive))
131+
.then(() => {
132+
resolve(undefined);
133+
this.router.navigate(['/private']);
134+
})
135+
.catch((response: BaseResponse) => {
136+
resolve(undefined);
137+
this.message.showError({
138+
message: response.getMessage(),
139+
translate: true,
140+
});
141+
});
142+
} else {
143+
resolve(undefined);
144+
}
145+
});
150146
}
151147

152148
async switchToArchive(archive: ArchiveVO) {
@@ -185,7 +181,7 @@ export class AllArchivesComponent implements AfterViewInit, OnDestroy {
185181
}
186182

187183
onCreateArchiveClick() {
188-
const deferred = new Deferred();
184+
const { promise, resolve, reject } = Promise.withResolvers();
189185

190186
const fields: PromptField[] = [
191187
{
@@ -227,23 +223,23 @@ export class AllArchivesComponent implements AfterViewInit, OnDestroy {
227223
];
228224

229225
this.prompt
230-
.prompt(fields, 'Create new archive', deferred.promise, 'Create archive')
226+
.prompt(fields, 'Create new archive', promise, 'Create archive')
231227
.then(
232228
async (value) => await this.api.archive.create(new ArchiveVO(value)),
233229
)
234230
.then((response: ArchiveResponse) => {
235231
const newArchive = response.getArchiveVO();
236232
this.archives.push(newArchive);
237233
this.archiveClick(newArchive);
238-
deferred.resolve();
234+
resolve(undefined);
239235
})
240236
.catch((response: ArchiveResponse | BaseResponse) => {
241237
if (response) {
242238
this.message.showError({
243239
message: response.getMessage(),
244240
translate: true,
245241
});
246-
deferred.reject();
242+
reject();
247243
}
248244
});
249245
}

src/app/core/components/connections-dialog/connections-dialog.component.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { AccountService } from '@shared/services/account/account.service';
1414
import { RelationshipService } from '@core/services/relationship/relationship.service';
1515
import { RelationVO, ArchiveVO } from '@models';
1616
import { FormInputSelectOption } from '@shared/components/form-input/form-input.component';
17-
import { Deferred } from '@root/vendor/deferred';
1817
import { RelationResponse } from '@shared/services/api/index.repo';
1918
import { remove, find } from 'lodash';
2019
import {
@@ -155,12 +154,12 @@ export class ConnectionsDialogComponent {
155154
}
156155

157156
onRelationRequestClick(relation: RelationVO, skipDecline = false) {
158-
const deferred = new Deferred();
157+
const { promise, resolve } = Promise.withResolvers();
159158
this.promptService
160159
.prompt(
161160
[RELATIONSHIP_FIELD],
162161
`Accept relationship with ${relation.ArchiveVO.fullName}?`,
163-
deferred.promise,
162+
promise,
164163
'Accept',
165164
skipDecline ? 'Cancel' : 'Decline',
166165
)
@@ -191,18 +190,18 @@ export class ConnectionsDialogComponent {
191190
remove(this.connectionRequests, relation);
192191
this.connections.push(relation);
193192

194-
deferred.resolve();
193+
resolve(undefined);
195194
});
196195
})
197196
.catch((response: RelationResponse) => {
198197
if (response) {
199-
deferred.resolve();
198+
resolve(undefined);
200199
this.messageService.showError({
201200
message: response.getMessage(),
202201
translate: true,
203202
});
204203
} else if (!skipDecline) {
205-
deferred.resolve();
204+
resolve(undefined);
206205
this.removeRelation(relation);
207206
}
208207
});
@@ -238,14 +237,14 @@ export class ConnectionsDialogComponent {
238237
async editRelation(relation: RelationVO) {
239238
let updatedRelation: RelationVO;
240239
const isNewRelation = !relation.relationId;
241-
const deferred = new Deferred();
240+
const { promise, resolve, reject } = Promise.withResolvers();
242241
const fields: PromptField[] = [RELATIONSHIP_FIELD_INITIAL(relation.type)];
243242

244243
return await this.promptService
245244
.prompt(
246245
fields,
247246
`Relationship with ${relation.RelationArchiveVO.fullName}`,
248-
deferred.promise,
247+
promise,
249248
'Save',
250249
)
251250
.then(async (value) => {
@@ -270,23 +269,23 @@ export class ConnectionsDialogComponent {
270269
if (isNewRelation) {
271270
relation.relationId = response.getRelationVO().relationId;
272271
}
273-
deferred.resolve();
272+
resolve(undefined);
274273
})
275274
.catch((response: RelationResponse) => {
276275
if (response) {
277276
this.messageService.showError({
278277
message: response.getMessage(),
279278
translate: true,
280279
});
281-
deferred.reject();
280+
reject();
282281
} else if (isNewRelation) {
283282
remove(this.connections, relation);
284283
}
285284
});
286285
}
287286

288287
async removeRelation(relation: RelationVO) {
289-
const deferred = new Deferred();
288+
const { promise, resolve } = Promise.withResolvers();
290289
let confirmTitle = `Remove relationship with ${relation.RelationArchiveVO.fullName}?`;
291290
let confirmText = 'Remove';
292291

@@ -302,7 +301,7 @@ export class ConnectionsDialogComponent {
302301
await this.promptService.confirm(
303302
confirmText,
304303
confirmTitle,
305-
deferred.promise,
304+
promise,
306305
'btn-danger',
307306
);
308307
const response = await this.relationService.remove(relation);
@@ -322,7 +321,7 @@ export class ConnectionsDialogComponent {
322321
});
323322
}
324323
} finally {
325-
deferred.resolve();
324+
resolve(undefined);
326325
}
327326
}
328327
}

src/app/core/components/folder-picker/folder-picker.component.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Component, OnDestroy } from '@angular/core';
22
import { remove } from 'lodash';
3-
import { Deferred } from '@root/vendor/deferred';
43
import { DataService } from '@shared/services/data/data.service';
54
import { FolderVO, ItemVO, RecordVO } from '@root/app/models/index';
65
import { ApiService } from '@shared/services/api/api.service';
@@ -23,7 +22,8 @@ export enum FolderPickerOperations {
2322
})
2423
export class FolderPickerComponent implements OnDestroy {
2524
public currentFolder: FolderVO;
26-
public chooseFolderDeferred: Deferred;
25+
public chooseFolderPromise: Promise<FolderVO | RecordVO>;
26+
public chooseFolderResolve: (value: FolderVO | RecordVO) => void;
2727
public operation: FolderPickerOperations;
2828
public operationName: string;
2929

@@ -85,9 +85,11 @@ export class FolderPickerComponent implements OnDestroy {
8585
this.loadCurrentFolderChildData();
8686
});
8787

88-
this.chooseFolderDeferred = new Deferred();
88+
const { promise, resolve } = Promise.withResolvers<FolderVO | RecordVO>();
89+
this.chooseFolderPromise = promise;
90+
this.chooseFolderResolve = resolve;
8991

90-
return await this.chooseFolderDeferred.promise;
92+
return await this.chooseFolderPromise;
9193
}
9294

9395
onItemClick(item: ItemVO, evt: Event) {
@@ -199,7 +201,8 @@ export class FolderPickerComponent implements OnDestroy {
199201

200202
this.cancelResetTimeout = setTimeout(() => {
201203
this.currentFolder = null;
202-
this.chooseFolderDeferred = null;
204+
this.chooseFolderPromise = null;
205+
this.chooseFolderResolve = null;
203206
this.isRootFolder = true;
204207
this.cancelResetTimeout = null;
205208
}, 500);
@@ -215,9 +218,9 @@ export class FolderPickerComponent implements OnDestroy {
215218

216219
protected setChosenFolder(): void {
217220
if (this.selectedRecord) {
218-
this.chooseFolderDeferred.resolve(this.selectedRecord);
221+
this.chooseFolderResolve(this.selectedRecord);
219222
} else if (this.currentFolder) {
220-
this.chooseFolderDeferred.resolve(this.currentFolder);
223+
this.chooseFolderResolve(this.currentFolder);
221224
}
222225
if (this.savePromise) {
223226
this.saving = true;

src/app/core/components/main/main.component.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import { FolderVO, RecordVO, AccountVO } from '@root/app/models';
2727
import { find } from 'lodash';
2828
import { ApiService } from '@shared/services/api/api.service';
2929
import { ShareResponse } from '@shared/services/api/share.repo';
30-
import { Deferred } from '@root/vendor/deferred';
3130
import { Validators } from '@angular/forms';
3231
import { DataService } from '@shared/services/data/data.service';
3332
import { UploadSessionStatus } from '@core/services/upload/upload.session';
@@ -206,12 +205,13 @@ export class MainComponent
206205
},
207206
];
208207

209-
const folderCreate = new Deferred();
208+
const { promise: folderCreatePromise, resolve: folderCreateResolve } =
209+
Promise.withResolvers();
210210

211211
const promptData: any = await this.prompt.prompt(
212212
secondScreenFields,
213213
'Name your new timeline',
214-
folderCreate.promise,
214+
folderCreatePromise,
215215
'Continue',
216216
null,
217217
secondScreenTemplate,
@@ -231,7 +231,7 @@ export class MainComponent
231231

232232
this.ga.sendEvent(EVENTS.PUBLISH.PublishByUrl.initiated.params);
233233
const newFolder = response.getFolderVO();
234-
folderCreate.resolve();
234+
folderCreateResolve(undefined);
235235
await this.router.navigate([
236236
'/public',
237237
newFolder.archiveNbr,
@@ -305,21 +305,17 @@ export class MainComponent
305305
// no access and no request
306306
const title = `Request access to ${shareItem.displayName} shared by ${shareAccount.fullName}?`;
307307
try {
308-
const deferred = new Deferred();
309-
await this.prompt.confirm(
310-
'Request access',
311-
title,
312-
deferred.promise,
313-
);
308+
const { promise, resolve } = Promise.withResolvers();
309+
await this.prompt.confirm('Request access', title, promise);
314310
try {
315311
await this.api.share.requestShareAccess(shareUrlToken);
316-
deferred.resolve();
312+
resolve(undefined);
317313
this.messageService.showMessage({
318314
message: 'Access requested.',
319315
style: 'success',
320316
});
321317
} catch (err) {
322-
deferred.resolve();
318+
resolve(undefined);
323319
if (err instanceof ShareResponse) {
324320
if (err.messageIncludesPhrase('share.already_exists')) {
325321
this.messageService.showError({

0 commit comments

Comments
 (0)