From 7ad25839c8ef42e5f4eedc8a00f640bfc38b414a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Lyngs=C3=B8?= Date: Tue, 27 Feb 2024 15:02:47 +0100 Subject: [PATCH] implement getContext method --- .../src/libs/class-api/class.mixin.ts | 21 +++++++++++++++++++ .../consume/context-consumer.controller.ts | 2 +- .../src/libs/element-api/element.mixin.ts | 21 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Web.UI.Client/src/libs/class-api/class.mixin.ts b/src/Umbraco.Web.UI.Client/src/libs/class-api/class.mixin.ts index 465d7e3460..a755cebc59 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/class-api/class.mixin.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/class-api/class.mixin.ts @@ -65,6 +65,16 @@ declare class UmbClassMixinDeclaration extends EventTarget implements UmbClassMi callback: UmbContextCallback, ): UmbContextConsumerController; + /** + * @description Retrieve a context. Notice this is a one time retrieving of a context, meaning if you expect this to be up to date with reality you should instead use the consumeContext method. + * @param {string} contextAlias + * @return {Promise} A Promise with the reference to the Context Api Instance + * @memberof UmbClassMixin + */ + getContext( + alias: string | UmbContextToken, + ): Promise; + hasController(controller: UmbController): boolean; getControllers(filterMethod: (ctrl: UmbController) => boolean): UmbController[]; addController(controller: UmbController): void; @@ -129,6 +139,17 @@ export const UmbClassMixin = (superClass: T) => { return new UmbContextConsumerController(this, contextAlias, callback); } + async getContext( + contextAlias: string | UmbContextToken, + ): Promise { + const controller = new UmbContextConsumerController(this, contextAlias); + const promise = controller.asPromise().then((result) => { + controller.destroy(); + return result; + }); + return promise; + } + public destroy(): void { if (this._host) { this._host.removeController(this); diff --git a/src/Umbraco.Web.UI.Client/src/libs/context-api/consume/context-consumer.controller.ts b/src/Umbraco.Web.UI.Client/src/libs/context-api/consume/context-consumer.controller.ts index fcafe8efb8..4faf16708d 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/context-api/consume/context-consumer.controller.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/context-api/consume/context-consumer.controller.ts @@ -17,7 +17,7 @@ export class UmbContextConsumerController, - callback: UmbContextCallback, + callback?: UmbContextCallback, ) { super(host.getHostElement(), contextAlias, callback); this.#host = host; diff --git a/src/Umbraco.Web.UI.Client/src/libs/element-api/element.mixin.ts b/src/Umbraco.Web.UI.Client/src/libs/element-api/element.mixin.ts index dbf72e6a23..feb052e9ab 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/element-api/element.mixin.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/element-api/element.mixin.ts @@ -32,6 +32,9 @@ export declare class UmbElement extends UmbControllerHostElement { alias: string | UmbContextToken, callback: UmbContextCallback, ): UmbContextConsumerController; + getContext( + alias: string | UmbContextToken, + ): Promise; /** * Use the UmbLocalizeController to localize your element. * @see UmbLocalizationController @@ -86,6 +89,24 @@ export const UmbElementMixin = (superClass: T) return new UmbContextConsumerController(this, alias, callback); } + /** + * @description Setup a subscription for a context. The callback is called when the context is resolved. + * @param {string} contextAlias + * @param {method} callback Callback method called when context is resolved. + * @return {UmbContextConsumerController} Reference to a Context Consumer Controller instance + * @memberof UmbElementMixin + */ + async getContext( + contextAlias: string | UmbContextToken, + ): Promise { + const controller = new UmbContextConsumerController(this, contextAlias); + const promise = controller.asPromise().then((result) => { + controller.destroy(); + return result; + }); + return promise; + } + destroy(): void { super.destroy(); (this.localize as any) = undefined;