diff --git a/src/Umbraco.Web.UI.Client/src/core/controller/controller.class.ts b/src/Umbraco.Web.UI.Client/src/core/controller/controller.class.ts index 95aaca7bc5..7f9603e0bf 100644 --- a/src/Umbraco.Web.UI.Client/src/core/controller/controller.class.ts +++ b/src/Umbraco.Web.UI.Client/src/core/controller/controller.class.ts @@ -1,22 +1,21 @@ -import { UmbControllerHostInterface } from "./controller-host.mixin"; -import { UmbControllerInterface } from "./controller.interface"; +import { UmbControllerHostInterface } from './controller-host.mixin'; +import { UmbControllerInterface } from './controller.interface'; export abstract class UmbController implements UmbControllerInterface { + protected _host?: UmbControllerHostInterface; - protected _host?: UmbControllerHostInterface + constructor(host: UmbControllerHostInterface) { + this._host = host; + this._host.addController(this); + } - constructor(host: UmbControllerHostInterface) { - this._host = host; - this._host.addController(this); - } + abstract hostConnected(): void; + abstract hostDisconnected(): void; - abstract hostConnected(): void; - abstract hostDisconnected(): void; - - public destroy() { - if(this._host) { - this._host.removeController(this); - } - delete this._host; - } -} \ No newline at end of file + public destroy() { + if (this._host) { + this._host.removeController(this); + } + delete this._host; + } +} diff --git a/src/Umbraco.Web.UI.Client/src/core/resources/resource.controller.ts b/src/Umbraco.Web.UI.Client/src/core/resources/resource.controller.ts index 2ea4c63a4f..ecf5f8b31c 100644 --- a/src/Umbraco.Web.UI.Client/src/core/resources/resource.controller.ts +++ b/src/Umbraco.Web.UI.Client/src/core/resources/resource.controller.ts @@ -6,7 +6,6 @@ import { ApiError, CancelablePromise, ProblemDetails } from '@umbraco-cms/backen import { UmbNotificationOptions, UmbNotificationService } from 'src/backoffice/core/services/notification'; import { UmbNotificationDefaultData } from 'src/backoffice/core/services/notification/layouts/default'; - /** * Extract the ProblemDetails object from an ApiError. * @@ -26,45 +25,37 @@ function toProblemDetails(error: unknown): ProblemDetails | undefined { return undefined; } - export class UmbResourceController extends UmbController { - - #promise: Promise; #notificationService?: UmbNotificationService; - constructor(host: UmbControllerHostInterface, promise: Promise) { super(host); this.#promise = promise; - new UmbContextConsumerController(host, 'umbNotificationService', - (_instance: UmbNotificationService) => { - this.#notificationService = _instance; - } - ); + new UmbContextConsumerController(host, 'umbNotificationService', (_instance: UmbNotificationService) => { + this.#notificationService = _instance; + }); } hostConnected() { - // TODO: Make sure we do the right thing here, as connected can be called multiple times without disconnected invoked. - //this.#promises.length = 0; + this.hostConnected(); } hostDisconnected() { this.cancel(); } - /** * Wrap the {execute} function in a try/catch block and return a tuple with the result and the error. */ - async tryExecute(): Promise<{data?: T, error?:ProblemDetails}> { + async tryExecute(): Promise<{ data?: T; error?: ProblemDetails }> { try { - return {data: await this.#promise}; + return { data: await this.#promise }; } catch (e) { - return {error: toProblemDetails(e)}; + return { error: toProblemDetails(e) }; } } @@ -72,8 +63,8 @@ export class UmbResourceController extends UmbController { * Wrap the {execute} function in a try/catch block and return the result. * If the executor function throws an error, then show the details in a notification. */ - async tryExecuteAndNotify(options?: UmbNotificationOptions): Promise<{data?: T, error?:ProblemDetails}> { - const {data, error} = await this.tryExecute(); + async tryExecuteAndNotify(options?: UmbNotificationOptions): Promise<{ data?: T; error?: ProblemDetails }> { + const { data, error } = await this.tryExecute(); if (error) { const data: UmbNotificationDefaultData = { @@ -90,10 +81,9 @@ export class UmbResourceController extends UmbController { } } - return {data, error}; + return { data, error }; } - /** * Cancel all resources that are currently being executed by this controller if they are cancelable. * diff --git a/src/Umbraco.Web.UI.Client/src/core/resources/tryExecuteAndNotify.method.ts b/src/Umbraco.Web.UI.Client/src/core/resources/tryExecuteAndNotify.method.ts index fbf7a26b31..cdd9637d3d 100644 --- a/src/Umbraco.Web.UI.Client/src/core/resources/tryExecuteAndNotify.method.ts +++ b/src/Umbraco.Web.UI.Client/src/core/resources/tryExecuteAndNotify.method.ts @@ -1,8 +1,13 @@ -import { UmbControllerHostInterface } from "../controller/controller-host.mixin"; -import type { ProblemDetails } from "../backend-api/models/ProblemDetails"; -import { UmbResourceController } from "./resource.controller"; -import { UmbNotificationOptions } from "src/backoffice/core/services/notification"; +/* eslint-disable @typescript-eslint/no-explicit-any */ +import { UmbControllerHostInterface } from '../controller/controller-host.mixin'; +import type { ProblemDetails } from '../backend-api/models/ProblemDetails'; +import { UmbResourceController } from './resource.controller'; +import { UmbNotificationOptions } from 'src/backoffice/core/services/notification'; -export async function tryExecuteAndNotify(host:UmbControllerHostInterface, resource:Promise, options?: UmbNotificationOptions): Promise<{data?: T, error?:ProblemDetails}> { - return await new UmbResourceController(host, resource).tryExecuteAndNotify(options); -} \ No newline at end of file +export async function tryExecuteAndNotify( + host: UmbControllerHostInterface, + resource: Promise, + options?: UmbNotificationOptions +): Promise<{ data?: T; error?: ProblemDetails }> { + return await new UmbResourceController(host, resource).tryExecuteAndNotify(options); +}