Skip to content

Commit

Permalink
refactor(workbench/dialog): consolidate API for closing a dialog
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The method `closeWithError` has been removed from the `WorkbenchDialog` handle. Instead pass an `Error` object to the `close` method.

### The following snippets illustrate how a migration could look like:

#### Before migration

```ts
import {WorkbenchDialog} from '@scion/workbench';

inject(WorkbenchDialog).closeWithError('some error');
```

#### After migration

```ts
import {WorkbenchDialog} from '@scion/workbench';

inject(WorkbenchDialog).close(new Error('some error'));
```
  • Loading branch information
k-genov authored and danielwiehl committed Mar 29, 2024
1 parent 1f8cedf commit 40414c4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,8 @@ export class DialogPageComponent {
}

public onClose(): void {
if (this.form.controls.closeWithError.value) {
this.dialog.closeWithError(this.form.controls.result.value);
}
else {
this.dialog.close(this.form.controls.result.value);
}
const result = this.form.controls.closeWithError.value ? new Error(this.form.controls.result.value) : this.form.controls.result.value;
this.dialog.close(result);
}

private installPropertyUpdater(): void {
Expand Down
9 changes: 2 additions & 7 deletions projects/scion/workbench/src/lib/dialog/workbench-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,9 @@ export abstract class WorkbenchDialog<R = unknown> {
public abstract cssClass: string | string[];

/**
* Closes the dialog. Optionally, pass a result to the dialog opener.
* Closes the dialog. Optionally, pass a result or an error to the dialog opener.
*/
public abstract close(result?: R): void;

/**
* Closes the dialog returning the given error to the dialog opener.
*/
public abstract closeWithError(error: Error | string): void;
public abstract close(result?: R | Error): void;
}

/**
Expand Down
28 changes: 4 additions & 24 deletions projects/scion/workbench/src/lib/dialog/ɵworkbench-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export class ɵWorkbenchDialog<R = unknown> implements WorkbenchDialog<R>, Block
private _blink$ = new Subject<void>();

/**
* Contains the result to be passed to the dialog opener.
* Result (or error) to be passed to the dialog opener.
*/
private _result: R | ɵDialogErrorResult | undefined;
private _result: R | Error | undefined;
private _componentRef: ComponentRef<WorkbenchDialogComponent> | undefined;
private _cssClass: string;

Expand Down Expand Up @@ -108,28 +108,17 @@ export class ɵWorkbenchDialog<R = unknown> implements WorkbenchDialog<R>, Block
// Wait for the dialog to close, resolving to its result or rejecting if closed with an error.
return new Promise<R | undefined>((resolve, reject) => {
this._destroyRef.onDestroy(() => {
if (this._result instanceof ɵDialogErrorResult) {
reject(this._result.error);
}
else {
resolve(this._result);
}
this._result instanceof Error ? reject(this._result) : resolve(this._result);
});
});
}

/** @inheritDoc */
public close(result?: R): void {
public close(result?: R | Error): void {
this._result = result;
this.destroy();
}

/** @inheritDoc */
public closeWithError(error: Error | string): void {
this._result = new ɵDialogErrorResult(error);
this.destroy();
}

/**
* Inputs passed to the dialog.
*/
Expand Down Expand Up @@ -358,12 +347,3 @@ export class ɵWorkbenchDialog<R = unknown> implements WorkbenchDialog<R>, Block
}
}
}

/**
* Wrapper to identify an erroneous result.
*/
class ɵDialogErrorResult {

constructor(public error: string | Error) {
}
}

0 comments on commit 40414c4

Please sign in to comment.