MOBILE-2314 core: Delegate registry functionality

main
Noel De Martin 2023-02-21 11:51:15 +01:00
parent 3ca1094979
commit e1c431b06a
1 changed files with 6 additions and 43 deletions

View File

@ -14,8 +14,7 @@
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { AsyncDirective } from '@classes/async-directive'; import { AsyncDirective } from '@classes/async-directive';
import { CoreUtils } from '@services/utils/utils'; import { CoreDirectivesRegistry } from '@singletons/directives-registry';
import { CoreLogger } from './logger';
/** /**
* Registry to keep track of component instances. * Registry to keep track of component instances.
@ -24,9 +23,6 @@ import { CoreLogger } from './logger';
*/ */
export class CoreComponentsRegistry { export class CoreComponentsRegistry {
private static instances: WeakMap<Element, unknown> = new WeakMap();
protected static logger = CoreLogger.getInstance('CoreComponentsRegistry');
/** /**
* Register a component instance. * Register a component instance.
* *
@ -34,7 +30,7 @@ export class CoreComponentsRegistry {
* @param instance Component instance. * @param instance Component instance.
*/ */
static register(element: Element, instance: unknown): void { 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. * @returns Component instance.
*/ */
static resolve<T>(element?: Element | null, componentClass?: ComponentConstructor<T>): T | null { static resolve<T>(element?: Element | null, componentClass?: ComponentConstructor<T>): T | null {
const instance = (element && this.instances.get(element) as T) ?? null; return CoreDirectivesRegistry.resolve(element, componentClass);
return instance && (!componentClass || instance instanceof componentClass)
? instance
: null;
} }
/** /**
@ -60,13 +52,7 @@ export class CoreComponentsRegistry {
* @returns Component instance. * @returns Component instance.
*/ */
static require<T>(element: Element, componentClass?: ComponentConstructor<T>): T { static require<T>(element: Element, componentClass?: ComponentConstructor<T>): T {
const instance = this.resolve(element, componentClass); return CoreDirectivesRegistry.require(element, componentClass);
if (!instance) {
throw new Error('Couldn\'t resolve component instance');
}
return instance;
} }
/** /**
@ -80,14 +66,7 @@ export class CoreComponentsRegistry {
element: Element | null, element: Element | null,
componentClass?: ComponentConstructor<T>, componentClass?: ComponentConstructor<T>,
): Promise<void> { ): Promise<void> {
const instance = this.resolve(element, componentClass); return CoreDirectivesRegistry.waitDirectiveReady(element, componentClass);
if (!instance) {
this.logger.error('No instance registered for element ' + componentClass, element);
return;
}
await instance.ready();
} }
/** /**
@ -103,23 +82,7 @@ export class CoreComponentsRegistry {
selector: string, selector: string,
componentClass?: ComponentConstructor<T>, componentClass?: ComponentConstructor<T>,
): Promise<void> { ): Promise<void> {
let elements: Element[] = []; return CoreDirectivesRegistry.waitDirectivesReady(element, selector, componentClass);
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<T>(element, componentClass)));
// Wait for next tick to ensure components are completely rendered.
await CoreUtils.nextTick();
} }
} }