fix consumeAllContexts

This commit is contained in:
Niels Lyngsø
2023-01-03 11:51:09 +01:00
parent ff82bc8884
commit 1e1cd27052
4 changed files with 25 additions and 5 deletions

View File

@@ -26,6 +26,7 @@ export class UmbCurrentUserHeaderApp extends UmbLitElement {
constructor() {
super();
this.consumeAllContexts(['umbCurrentUserStore', 'umbModalService'], (instances) => {
debugger;
this._currentUserStore = instances['umbCurrentUserStore'];
this._modalService = instances['umbModalService'];
this._observeCurrentUser();
@@ -36,6 +37,7 @@ export class UmbCurrentUserHeaderApp extends UmbLitElement {
if (!this._currentUserStore) return;
this.observe<UserDetails>(this._currentUserStore.currentUser, (currentUser) => {
debugger;
this._currentUser = currentUser;
});
}

View File

@@ -7,6 +7,12 @@ import { UmbContextRequestEventImplementation, UmbContextCallback } from './cont
*/
export class UmbContextConsumer {
private _instance?: unknown;
get instance(): unknown | undefined {
return this._instance;
}
get consumerAlias() {
return this._contextAlias;
}
@@ -20,11 +26,17 @@ export class UmbContextConsumer {
*/
constructor(protected target: EventTarget, private _contextAlias: string, private _callback: UmbContextCallback) {}
private _onResponse = (instance: unknown) => {
this._instance = instance;
this._callback(instance);
}
/**
* @memberof UmbContextConsumer
*/
public request() {
const event = new UmbContextRequestEventImplementation(this._contextAlias, this._callback);
const event = new UmbContextRequestEventImplementation(this._contextAlias, this._onResponse);
this.target.dispatchEvent(event);
}

View File

@@ -1,6 +1,6 @@
export const umbContextRequestEventType = 'umb:context-request';
export type UmbContextCallback = (instance: any) => void;
export type UmbContextCallback = (instance: unknown) => void;
/**
* @export

View File

@@ -66,10 +66,16 @@ export const UmbElementMixin = <T extends HTMLElementConstructor>(superClass: T)
new UmbContextConsumerController(this, alias, () => {
resolvedAmount++;
//const allResolved = this.getControllers((ctrl: UmbControllerInterface):boolean => isContextConsumerType(ctrl) && _contextAliases.indexOf(ctrl.consumerAlias) !== -1 );
if (resolvedAmount === _contextAliases.length) {
callback(controllers);
const result: ResolvedContexts = {};
controllers.forEach((contextCtrl: UmbContextConsumerController) => {
result[contextCtrl.consumerAlias] = contextCtrl.instance;
});
callback(result);
}
})
);