From 887efc7e767051e1179bb684d40f8303887f32bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Lyngs=C3=B8?= Date: Thu, 12 Jan 2023 09:57:26 +0100 Subject: [PATCH] add and use unique to replace controllers if one of the same type comes along. --- .../consume/context-consumer.controller.ts | 4 ++++ .../context-api/consume/context-consumer.ts | 2 +- .../provide/context-provider.controller.ts | 4 ++++ .../context-api/provide/context-provider.ts | 8 ++++---- .../core/controller/controller-host.mixin.ts | 11 +++++++++++ .../src/core/controller/controller.class.ts | 9 ++++++++- .../core/controller/controller.interface.ts | 1 + .../src/core/element/element.mixin.ts | 6 +++--- .../observable-api/observer.controller.ts | 19 +++++++++++++++++-- .../src/core/observable-api/observer.ts | 12 ++---------- .../src/core/resources/resource.controller.ts | 5 +++-- 11 files changed, 58 insertions(+), 23 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/core/context-api/consume/context-consumer.controller.ts b/src/Umbraco.Web.UI.Client/src/core/context-api/consume/context-consumer.controller.ts index fd89ccf2ea..ecf921af17 100644 --- a/src/Umbraco.Web.UI.Client/src/core/context-api/consume/context-consumer.controller.ts +++ b/src/Umbraco.Web.UI.Client/src/core/context-api/consume/context-consumer.controller.ts @@ -6,6 +6,10 @@ import { UmbControllerHostInterface } from 'src/core/controller/controller-host. export class UmbContextConsumerController extends UmbContextConsumer implements UmbControllerInterface { + public get unique() { + return this._contextAlias; + } + constructor(host:UmbControllerHostInterface, contextAlias: string, callback: UmbContextCallback) { super(host, contextAlias, callback); host.addController(this); diff --git a/src/Umbraco.Web.UI.Client/src/core/context-api/consume/context-consumer.ts b/src/Umbraco.Web.UI.Client/src/core/context-api/consume/context-consumer.ts index 84c8ba1af4..0d6261875a 100644 --- a/src/Umbraco.Web.UI.Client/src/core/context-api/consume/context-consumer.ts +++ b/src/Umbraco.Web.UI.Client/src/core/context-api/consume/context-consumer.ts @@ -23,7 +23,7 @@ export class UmbContextConsumer { * @param {UmbContextCallback} _callback * @memberof UmbContextConsumer */ - constructor(protected host: HostType, private _contextAlias: string, private _callback: UmbContextCallback) {} + constructor(protected host: HostType, protected _contextAlias: string, private _callback: UmbContextCallback) {} private _onResponse = (instance: unknown) => { diff --git a/src/Umbraco.Web.UI.Client/src/core/context-api/provide/context-provider.controller.ts b/src/Umbraco.Web.UI.Client/src/core/context-api/provide/context-provider.controller.ts index efd9d239a8..301f260c27 100644 --- a/src/Umbraco.Web.UI.Client/src/core/context-api/provide/context-provider.controller.ts +++ b/src/Umbraco.Web.UI.Client/src/core/context-api/provide/context-provider.controller.ts @@ -5,6 +5,10 @@ import { UmbControllerHostInterface } from 'src/core/controller/controller-host. export class UmbContextProviderController extends UmbContextProvider implements UmbControllerInterface { + public get unique() { + return this._contextAlias; + } + constructor(host:UmbControllerHostInterface, contextAlias: string, instance: unknown) { super(host, contextAlias, instance); diff --git a/src/Umbraco.Web.UI.Client/src/core/context-api/provide/context-provider.ts b/src/Umbraco.Web.UI.Client/src/core/context-api/provide/context-provider.ts index 3bb525cd56..02d183216f 100644 --- a/src/Umbraco.Web.UI.Client/src/core/context-api/provide/context-provider.ts +++ b/src/Umbraco.Web.UI.Client/src/core/context-api/provide/context-provider.ts @@ -9,8 +9,8 @@ export class UmbContextProvider { protected host: HostType; - private _contextAlias: string; - private _instance: unknown; + protected _contextAlias: string; + #instance: unknown; /** * Creates an instance of UmbContextProvider. @@ -22,7 +22,7 @@ export class UmbContextProvider { constructor(host: HostType, contextAlias: string, instance: unknown) { this.host = host; this._contextAlias = contextAlias; - this._instance = instance; + this.#instance = instance; } /** @@ -51,6 +51,6 @@ export class UmbContextProvider { if (event.contextAlias !== this._contextAlias) return; event.stopPropagation(); - event.callback(this._instance); + event.callback(this.#instance); }; } diff --git a/src/Umbraco.Web.UI.Client/src/core/controller/controller-host.mixin.ts b/src/Umbraco.Web.UI.Client/src/core/controller/controller-host.mixin.ts index ede2d1a138..a71bc71ab7 100644 --- a/src/Umbraco.Web.UI.Client/src/core/controller/controller-host.mixin.ts +++ b/src/Umbraco.Web.UI.Client/src/core/controller/controller-host.mixin.ts @@ -45,6 +45,17 @@ export const UmbControllerHostMixin = (superCl * @param {UmbControllerInterface} ctrl */ addController(ctrl: UmbControllerInterface): void { + + // Check if there is one already with same unique + if(ctrl.unique) { + this.#controllers.forEach(x => { + if(x.unique === ctrl.unique) { + debugger; + this.removeController(x); + } + }); + } + this.#controllers.push(ctrl); if(this.#attached) { ctrl.hostConnected(); 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 355bee9e47..02235df414 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 @@ -4,8 +4,15 @@ import { UmbControllerInterface } from './controller.interface'; export abstract class UmbController implements UmbControllerInterface { protected host?: UmbControllerHostInterface; - constructor(host: UmbControllerHostInterface) { + + private _alias?: string; + public get unique() { + return this._alias; + } + + constructor(host: UmbControllerHostInterface, alias?: string) { this.host = host; + this._alias = alias; this.host.addController(this); } diff --git a/src/Umbraco.Web.UI.Client/src/core/controller/controller.interface.ts b/src/Umbraco.Web.UI.Client/src/core/controller/controller.interface.ts index a70411fca4..f8feb3ff9c 100644 --- a/src/Umbraco.Web.UI.Client/src/core/controller/controller.interface.ts +++ b/src/Umbraco.Web.UI.Client/src/core/controller/controller.interface.ts @@ -1,4 +1,5 @@ export interface UmbControllerInterface { + get unique(): string | undefined; hostConnected(): void; hostDisconnected(): void; destroy(): void; diff --git a/src/Umbraco.Web.UI.Client/src/core/element/element.mixin.ts b/src/Umbraco.Web.UI.Client/src/core/element/element.mixin.ts index 87255a95df..d4bcaf7d3b 100644 --- a/src/Umbraco.Web.UI.Client/src/core/element/element.mixin.ts +++ b/src/Umbraco.Web.UI.Client/src/core/element/element.mixin.ts @@ -12,7 +12,7 @@ interface ResolvedContexts { } export declare class UmbElementMixinInterface extends UmbControllerHostInterface { - observe(source: Observable, callback: (_value: T) => void): UmbObserverController; + observe(source: Observable, callback: (_value: T) => void, unique?: string): UmbObserverController; provideContext(alias: string, instance: unknown): UmbContextProviderController; consumeContext(alias: string, callback: UmbContextCallback): UmbContextConsumerController; consumeAllContexts(contextAliases: string[], callback: (_instances: ResolvedContexts) => void): void; @@ -28,8 +28,8 @@ export const UmbElementMixin = (superClass: T) * @return {UmbObserverController} Reference to a Observer Controller instance * @memberof UmbElementMixin */ - observe(source: Observable, callback: (_value: T) => void): UmbObserverController { - return new UmbObserverController(this, source, callback); + observe(source: Observable, callback: (_value: T) => void, unique?: string): UmbObserverController { + return new UmbObserverController(this, source, callback, unique); } /** diff --git a/src/Umbraco.Web.UI.Client/src/core/observable-api/observer.controller.ts b/src/Umbraco.Web.UI.Client/src/core/observable-api/observer.controller.ts index afc6225df7..6ccf637ff9 100644 --- a/src/Umbraco.Web.UI.Client/src/core/observable-api/observer.controller.ts +++ b/src/Umbraco.Web.UI.Client/src/core/observable-api/observer.controller.ts @@ -6,9 +6,23 @@ import { UmbControllerHostInterface } from 'src/core/controller/controller-host. export class UmbObserverController extends UmbObserver implements UmbControllerInterface { - constructor(host:UmbControllerHostInterface, source: Observable, callback: (_value: T) => void) { + _alias?: string; + public get unique() { + return this._alias; + } + + constructor(host:UmbControllerHostInterface, source: Observable, callback: (_value: T) => void, alias?: string) { super(source, callback); - // TODO: What should happen if source or some? identifier is already present? + this._alias = alias; + + // Lets check if controller is already here: + /* + if (this._subscriptions.has(source)) { + const subscription = this._subscriptions.get(source); + subscription?.unsubscribe(); + } + */ + host.addController(this); } @@ -16,4 +30,5 @@ export class UmbObserverController extends UmbObserver implements UmbContr return; } + } diff --git a/src/Umbraco.Web.UI.Client/src/core/observable-api/observer.ts b/src/Umbraco.Web.UI.Client/src/core/observable-api/observer.ts index d21a9359a0..9fa9968e3f 100644 --- a/src/Umbraco.Web.UI.Client/src/core/observable-api/observer.ts +++ b/src/Umbraco.Web.UI.Client/src/core/observable-api/observer.ts @@ -3,16 +3,8 @@ import { Observable, Subscription } from 'rxjs'; export class UmbObserver { #subscription!: Subscription; - - constructor(source: Observable, callback: (_value: T) => void) { - // TODO: can be transferred to something using alias? - /* - if (this._subscriptions.has(source)) { - const subscription = this._subscriptions.get(source); - subscription?.unsubscribe(); - } - */ + constructor(source: Observable, callback: (_value: T) => void) { this.#subscription = source.subscribe((value) => callback(value)); } @@ -27,4 +19,4 @@ export class UmbObserver { this.#subscription.unsubscribe(); } -}; \ No newline at end of file +}; 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 5668c41180..ae3776f47d 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 @@ -7,12 +7,13 @@ import { UmbNotificationOptions, UmbNotificationService } from 'src/core/notific import { UmbNotificationDefaultData } from 'src/core/notification/layouts/default'; export class UmbResourceController extends UmbController { + #promise: Promise; #notificationService?: UmbNotificationService; - constructor(host: UmbControllerHostInterface, promise: Promise) { - super(host); + constructor(host: UmbControllerHostInterface, promise: Promise, alias?: string) { + super(host, alias); this.#promise = promise;