From 1f8ddac128afe929279ab841c3e17209e51f9ed7 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 30 Jul 2024 09:56:26 +0200 Subject: [PATCH] MOBILE-4612 compile: Expose effect to plugins --- .../components/compile-html/compile-html.ts | 3 +++ src/core/features/compile/services/compile.ts | 25 +++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/core/features/compile/components/compile-html/compile-html.ts b/src/core/features/compile/components/compile-html/compile-html.ts index 0fa63f9c1..dd6b6edbc 100644 --- a/src/core/features/compile/components/compile-html/compile-html.ts +++ b/src/core/features/compile/components/compile-html/compile-html.ts @@ -33,6 +33,7 @@ import { AfterViewInit, Type, KeyValueDiffer, + Injector, } from '@angular/core'; import { CorePromisedValue } from '@classes/promised-value'; @@ -89,6 +90,7 @@ export class CoreCompileHtmlComponent implements OnChanges, OnDestroy, DoCheck { constructor( protected changeDetector: ChangeDetectorRef, + protected injector: Injector, element: ElementRef, differs: KeyValueDiffers, ) { @@ -223,6 +225,7 @@ export class CoreCompileHtmlComponent implements OnChanges, OnDestroy, DoCheck { CoreCompile.injectLibraries( this, compileInstance.extraProviders, + compileInstance.injector, ); // Always add these elements, they could be needed on component init (componentObservable). diff --git a/src/core/features/compile/services/compile.ts b/src/core/features/compile/services/compile.ts index 5bf93d338..476c60c0d 100644 --- a/src/core/features/compile/services/compile.ts +++ b/src/core/features/compile/services/compile.ts @@ -23,6 +23,10 @@ import { ViewContainerRef, signal, computed, + effect, + EffectCleanupRegisterFn, + CreateEffectOptions, + EffectRef, } from '@angular/core'; import { ActionSheetController, @@ -260,9 +264,10 @@ export class CoreCompileProvider { * * @param instance The instance where to inject the libraries. * @param extraLibraries Extra imported providers if needed and not imported by this class. + * @param injector Injector of the injection context. E.g. for a component, use the component's injector. */ // eslint-disable-next-line @typescript-eslint/no-explicit-any - injectLibraries(instance: any, extraLibraries: Type[] = []): void { + injectLibraries(instance: any, extraLibraries: Type[] = [], injector?: Injector): void { if (!this.libraries || !this.exportedObjects) { throw new CoreError('Libraries not loaded. You need to call loadLibraries before calling injectLibraries.'); } @@ -271,6 +276,7 @@ export class CoreCompileProvider { ...this.libraries, ...extraLibraries, ]; + injector = injector ?? this.injector; // We cannot inject anything to this constructor. Use the Injector to inject all the providers into the instance. for (const i in libraries) { @@ -278,7 +284,7 @@ export class CoreCompileProvider { if (typeof libraryDef === 'function' && libraryDef.name) { try { // Inject the provider to the instance. We use the class name as the property name. - instance[libraryDef.name.replace(/DelegateService$/, 'Delegate')] = this.injector.get(libraryDef); + instance[libraryDef.name.replace(/DelegateService$/, 'Delegate')] = injector.get(libraryDef); } catch (ex) { this.logger.error('Error injecting provider', libraryDef.name, ex); } @@ -289,17 +295,26 @@ export class CoreCompileProvider { instance['CoreCompileProvider'] = this; // Add some final classes. - instance['injector'] = this.injector; + instance['injector'] = injector; instance['Validators'] = Validators; instance['CoreConstants'] = CoreConstants; instance['DownloadStatus'] = DownloadStatus; instance['CoreConfigConstants'] = CoreConstants.CONFIG; instance['CoreEventsProvider'] = CoreEvents; instance['CoreLoggerProvider'] = CoreLogger; - instance['signal'] = signal; - instance['computed'] = computed; instance['moment'] = moment; instance['Md5'] = Md5; + instance['signal'] = signal; + instance['computed'] = computed; + // Create a wrapper to call effect with the proper injection context. + instance['effect'] = ( + effectFn: (onCleanup: EffectCleanupRegisterFn) => void, + options?: Omit, + ): EffectRef => + effect(effectFn, { + ...options, + injector, + }); /** * @deprecated since 4.1, plugins should use CoreNetwork instead. * Keeping this a bit more to avoid plugins breaking.