implement getContext method

This commit is contained in:
Niels Lyngsø
2024-02-27 15:02:47 +01:00
parent fe7e64baf5
commit 7ad25839c8
3 changed files with 43 additions and 1 deletions

View File

@@ -65,6 +65,16 @@ declare class UmbClassMixinDeclaration extends EventTarget implements UmbClassMi
callback: UmbContextCallback<ResultType>,
): UmbContextConsumerController<BaseType, ResultType>;
/**
* @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<ContextType>} A Promise with the reference to the Context Api Instance
* @memberof UmbClassMixin
*/
getContext<BaseType = unknown, ResultType extends BaseType = BaseType>(
alias: string | UmbContextToken<BaseType, ResultType>,
): Promise<ResultType>;
hasController(controller: UmbController): boolean;
getControllers(filterMethod: (ctrl: UmbController) => boolean): UmbController[];
addController(controller: UmbController): void;
@@ -129,6 +139,17 @@ export const UmbClassMixin = <T extends ClassConstructor>(superClass: T) => {
return new UmbContextConsumerController(this, contextAlias, callback);
}
async getContext<BaseType = unknown, ResultType extends BaseType = BaseType>(
contextAlias: string | UmbContextToken<BaseType, ResultType>,
): Promise<ResultType> {
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);

View File

@@ -17,7 +17,7 @@ export class UmbContextConsumerController<BaseType = unknown, ResultType extends
constructor(
host: UmbControllerHost,
contextAlias: string | UmbContextToken<BaseType, ResultType>,
callback: UmbContextCallback<ResultType>,
callback?: UmbContextCallback<ResultType>,
) {
super(host.getHostElement(), contextAlias, callback);
this.#host = host;

View File

@@ -32,6 +32,9 @@ export declare class UmbElement extends UmbControllerHostElement {
alias: string | UmbContextToken<BaseType, ResultType>,
callback: UmbContextCallback<ResultType>,
): UmbContextConsumerController<BaseType, ResultType>;
getContext<BaseType = unknown, ResultType extends BaseType = BaseType>(
alias: string | UmbContextToken<BaseType, ResultType>,
): Promise<ResultType>;
/**
* Use the UmbLocalizeController to localize your element.
* @see UmbLocalizationController
@@ -86,6 +89,24 @@ export const UmbElementMixin = <T extends HTMLElementConstructor>(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<BaseType = unknown, ResultType extends BaseType = BaseType>(
contextAlias: string | UmbContextToken<BaseType, ResultType>,
): Promise<ResultType> {
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;