From e1c431b06a2bf5016c298da8ccd5abbff0f8e8e1 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Tue, 21 Feb 2023 11:51:15 +0100 Subject: [PATCH] MOBILE-2314 core: Delegate registry functionality --- src/core/singletons/components-registry.ts | 49 +++------------------- 1 file changed, 6 insertions(+), 43 deletions(-) diff --git a/src/core/singletons/components-registry.ts b/src/core/singletons/components-registry.ts index cd123802f..092c66ee6 100644 --- a/src/core/singletons/components-registry.ts +++ b/src/core/singletons/components-registry.ts @@ -14,8 +14,7 @@ import { Component } from '@angular/core'; import { AsyncDirective } from '@classes/async-directive'; -import { CoreUtils } from '@services/utils/utils'; -import { CoreLogger } from './logger'; +import { CoreDirectivesRegistry } from '@singletons/directives-registry'; /** * Registry to keep track of component instances. @@ -24,9 +23,6 @@ import { CoreLogger } from './logger'; */ export class CoreComponentsRegistry { - private static instances: WeakMap = new WeakMap(); - protected static logger = CoreLogger.getInstance('CoreComponentsRegistry'); - /** * Register a component instance. * @@ -34,7 +30,7 @@ export class CoreComponentsRegistry { * @param instance Component instance. */ static register(element: Element, instance: unknown): void { - this.instances.set(element, instance); + CoreDirectivesRegistry.register(element, instance); } /** @@ -45,11 +41,7 @@ export class CoreComponentsRegistry { * @returns Component instance. */ static resolve(element?: Element | null, componentClass?: ComponentConstructor): T | null { - const instance = (element && this.instances.get(element) as T) ?? null; - - return instance && (!componentClass || instance instanceof componentClass) - ? instance - : null; + return CoreDirectivesRegistry.resolve(element, componentClass); } /** @@ -60,13 +52,7 @@ export class CoreComponentsRegistry { * @returns Component instance. */ static require(element: Element, componentClass?: ComponentConstructor): T { - const instance = this.resolve(element, componentClass); - - if (!instance) { - throw new Error('Couldn\'t resolve component instance'); - } - - return instance; + return CoreDirectivesRegistry.require(element, componentClass); } /** @@ -80,14 +66,7 @@ export class CoreComponentsRegistry { element: Element | null, componentClass?: ComponentConstructor, ): Promise { - const instance = this.resolve(element, componentClass); - if (!instance) { - this.logger.error('No instance registered for element ' + componentClass, element); - - return; - } - - await instance.ready(); + return CoreDirectivesRegistry.waitDirectiveReady(element, componentClass); } /** @@ -103,23 +82,7 @@ export class CoreComponentsRegistry { selector: string, componentClass?: ComponentConstructor, ): Promise { - let elements: Element[] = []; - - if (element.matches(selector)) { - // Element to wait is myself. - elements = [element]; - } else { - elements = Array.from(element.querySelectorAll(selector)); - } - - if (!elements.length) { - return; - } - - await Promise.all(elements.map(element => CoreComponentsRegistry.waitComponentReady(element, componentClass))); - - // Wait for next tick to ensure components are completely rendered. - await CoreUtils.nextTick(); + return CoreDirectivesRegistry.waitDirectivesReady(element, selector, componentClass); } }