In my recent project, I have developed an interface along with two classes, A and B, that implement this interface. To handle the creation of objects of these classes based on the result of an asynchronous method, I have also created a factory service. This factory service, named SomeFactory, returns an object of either class A or B, depending on the result of an Observable of type boolean returned by the asynchronous method. However, I am facing a challenge in ensuring that the response from this method is obtained before an object of class A or B is created synchronously. How can I achieve this?
As I explore possible solutions, I am considering the limitations of using APP_INITIALIZER in this scenario. Since the described implementation resides in a library that will be imported into the main application, I am hesitant to introduce a dependency from the library to the main application. Here is a snippet of the SomeFactory code:
@Injectable({
providedIn: 'root'
})
export class SomeFactory {
private static isEnabled : boolean = false;
constructor(private otherService: OtherService){}
loadState(){
this.otherService.isEnabled().subscribe((isEnabled) => {
SomeFactory.isEnabled = isEnabled
})
}
static create(): Interface {
if(SomeFactory.isEnabled) {
return new A()
} else {
return new B()
}
}
}