MOBILE-4612 compile: Expose effect to plugins

main
Dani Palou 2024-07-30 09:56:26 +02:00
parent e33627f1c1
commit 1f8ddac128
2 changed files with 23 additions and 5 deletions

View File

@ -33,6 +33,7 @@ import {
AfterViewInit, AfterViewInit,
Type, Type,
KeyValueDiffer, KeyValueDiffer,
Injector,
} from '@angular/core'; } from '@angular/core';
import { CorePromisedValue } from '@classes/promised-value'; import { CorePromisedValue } from '@classes/promised-value';
@ -89,6 +90,7 @@ export class CoreCompileHtmlComponent implements OnChanges, OnDestroy, DoCheck {
constructor( constructor(
protected changeDetector: ChangeDetectorRef, protected changeDetector: ChangeDetectorRef,
protected injector: Injector,
element: ElementRef, element: ElementRef,
differs: KeyValueDiffers, differs: KeyValueDiffers,
) { ) {
@ -223,6 +225,7 @@ export class CoreCompileHtmlComponent implements OnChanges, OnDestroy, DoCheck {
CoreCompile.injectLibraries( CoreCompile.injectLibraries(
this, this,
compileInstance.extraProviders, compileInstance.extraProviders,
compileInstance.injector,
); );
// Always add these elements, they could be needed on component init (componentObservable). // Always add these elements, they could be needed on component init (componentObservable).

View File

@ -23,6 +23,10 @@ import {
ViewContainerRef, ViewContainerRef,
signal, signal,
computed, computed,
effect,
EffectCleanupRegisterFn,
CreateEffectOptions,
EffectRef,
} from '@angular/core'; } from '@angular/core';
import { import {
ActionSheetController, ActionSheetController,
@ -260,9 +264,10 @@ export class CoreCompileProvider {
* *
* @param instance The instance where to inject the libraries. * @param instance The instance where to inject the libraries.
* @param extraLibraries Extra imported providers if needed and not imported by this class. * @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 // eslint-disable-next-line @typescript-eslint/no-explicit-any
injectLibraries(instance: any, extraLibraries: Type<unknown>[] = []): void { injectLibraries(instance: any, extraLibraries: Type<unknown>[] = [], injector?: Injector): void {
if (!this.libraries || !this.exportedObjects) { if (!this.libraries || !this.exportedObjects) {
throw new CoreError('Libraries not loaded. You need to call loadLibraries before calling injectLibraries.'); throw new CoreError('Libraries not loaded. You need to call loadLibraries before calling injectLibraries.');
} }
@ -271,6 +276,7 @@ export class CoreCompileProvider {
...this.libraries, ...this.libraries,
...extraLibraries, ...extraLibraries,
]; ];
injector = injector ?? this.injector;
// We cannot inject anything to this constructor. Use the Injector to inject all the providers into the instance. // We cannot inject anything to this constructor. Use the Injector to inject all the providers into the instance.
for (const i in libraries) { for (const i in libraries) {
@ -278,7 +284,7 @@ export class CoreCompileProvider {
if (typeof libraryDef === 'function' && libraryDef.name) { if (typeof libraryDef === 'function' && libraryDef.name) {
try { try {
// Inject the provider to the instance. We use the class name as the property name. // Inject the provider to the instance. We use the class name as the property name.
instance[libraryDef.name.replace(/DelegateService$/, 'Delegate')] = this.injector.get<Provider>(libraryDef); instance[libraryDef.name.replace(/DelegateService$/, 'Delegate')] = injector.get<Provider>(libraryDef);
} catch (ex) { } catch (ex) {
this.logger.error('Error injecting provider', libraryDef.name, ex); this.logger.error('Error injecting provider', libraryDef.name, ex);
} }
@ -289,17 +295,26 @@ export class CoreCompileProvider {
instance['CoreCompileProvider'] = this; instance['CoreCompileProvider'] = this;
// Add some final classes. // Add some final classes.
instance['injector'] = this.injector; instance['injector'] = injector;
instance['Validators'] = Validators; instance['Validators'] = Validators;
instance['CoreConstants'] = CoreConstants; instance['CoreConstants'] = CoreConstants;
instance['DownloadStatus'] = DownloadStatus; instance['DownloadStatus'] = DownloadStatus;
instance['CoreConfigConstants'] = CoreConstants.CONFIG; instance['CoreConfigConstants'] = CoreConstants.CONFIG;
instance['CoreEventsProvider'] = CoreEvents; instance['CoreEventsProvider'] = CoreEvents;
instance['CoreLoggerProvider'] = CoreLogger; instance['CoreLoggerProvider'] = CoreLogger;
instance['signal'] = signal;
instance['computed'] = computed;
instance['moment'] = moment; instance['moment'] = moment;
instance['Md5'] = Md5; 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<CreateEffectOptions, 'injector'>,
): EffectRef =>
effect(effectFn, {
...options,
injector,
});
/** /**
* @deprecated since 4.1, plugins should use CoreNetwork instead. * @deprecated since 4.1, plugins should use CoreNetwork instead.
* Keeping this a bit more to avoid plugins breaking. * Keeping this a bit more to avoid plugins breaking.