0

I have the following class (only the relevant part):

import { AppInjector } from '@app/shared/utility/app-injector';
import { HttpService } from '@app/shared/services/http.service';

export class Lazy<T> {

    private readonly _http: HttpService = AppInjector.get(HttpService);

    private _init() {
        // Usage of this._http
    }
}

So basically, this is a simple type but that needs to use the Angular HttpService. To do so, I created a file with the following code:

import { Injector } from '@angular/core';

export let AppInjector: Injector;

export function setAppInjector(injector: Injector) {
    if (AppInjector) {
        console.error('Programming error: AppInjector was already set');
    }
    else {
        AppInjector = injector;
    }
}

And I'm setting it in the constructor of my AppModule:

export class AppModule {
    constructor(injector: Injector) {
        setAppInjector(injector);
    }
}

Finally, I use it for example in a service:

@Injectable({
    providedIn: 'root',
})
export class FooService {

    private readonly ROUTE: string = "/api/foo";

    private _campaigns: Lazy<ICampaign[]> = new Lazy<ICampaign[]>(`${this.ROUTE}/campaigns`);

    public get campaigns(): Lazy<ICampaign[]> {
        return this._campaigns;
    }
}

And in a component, I can do something like:

export class FooComponent implements OnInit {
    constructor(private _fooService: FooService) {}

    public async ngOnInit(): Promise<void> {
        await this._fooService.campaigns.load();
    }
}

This works pretty well but I'm wondering how I could make this more generic. Indeed, say I wanted to create a separate npm package with just this class, I can't tell users to create the setAppInjector function and to register it in the AppModule (and I could not even use it from my npm package anyway...).

So I'm searching for an Angular way to make this generic, something like passing something in the providers property of the decorator of the AppModule.

Any idea?

ssougnez
  • 5,315
  • 11
  • 46
  • 79
  • That class can not be a service? – Stefan Mar 24 '20 at 21:41
  • I edited my question by explaining how I use this class. I think it should answer your question, if not, maybe you're onto something ;-) – ssougnez Mar 24 '20 at 21:51
  • To be honest I will still make it as service. Could you say way you do not want to make it as service? – Stefan Mar 24 '20 at 21:56
  • Well, maybe there is something I don't know but would you be able to use a service like I'm using the class right now ? Note that you could have multiple variable of type `Lazy` that differs only by the parameters sent to its constructor. I don't really see how you would use a service in here. – ssougnez Mar 24 '20 at 21:58
  • Ok I see now your point that you expect to have multiple instances of Lazy within single component, which will use different url. But it still not be a problem to use service with methods which will as parameter get an url and get and than set proper variables. – Stefan Mar 24 '20 at 22:12

0 Answers0