Currently, I am utilizing RxJS within the context of an Angular application.
Within my service, there is functionality to reinitialize the entire application with different settings as needed.
@Injectable()
class BootstrapService{
public initApplication(config: appConfig): Observable<boolean>{
return someObservable;
};
}
The intended use case scenario would be:
///.... code in part of application that allows user to reinit app
this.bootstrapService.initApplication({someconfig}).subscribe(result=>{
if(result){
//report success
}else report failure
}
}
This functions similarly to a cold observable, resembling http calls.
Now, I have a requirement to send additional notifications to specific components (such as updating user menus, toolbars, etc. with new configuration from the server).
I plan to achieve this by emitting an event at the end of the pipe inside BoostrapService#initApplicaiton
, like so:
public initApplication(config: appConfig): Observable<boolean>{
return someObservable().pipe(tap(result=>if(result)this.subject.next(someEvent))
};
This can be seen as a side effect, which is typically discouraged in functional programming, based on information available online.
So the question arises - is it acceptable to emit events as side effects, or should a different approach be taken?
Another potential solution that comes to mind involves creating a "hot" observable that isn't returned to the caller, but rather a shared stream is used instead:
appInitResult: Subject<boolean>
public initApplication(config: appConfig): Observable<boolean>{
someObservable().subscribe(r-> this.appInitResult.next(r));
return this.appInitResult.asObservable();
};
This way, everything can subscribe to the same stream, including the method caller.