I am facing an issue with a root service that contains composition within it, as shown below:
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class MapService {
private rmap: RMap;
init(props: Props) {
this.rmap = new RMap(props);
}
getMapLayers() {
return this.rmap.layers;
}
}
The problem arises because new RMap();
includes async functions. When I call the getMapLayers()
method from the service, I encounter an error of undefined
, since some functionalities within new RMap()
are not yet ready due to being asynchronous.
What would be a proper solution to this?
As mentioned earlier, I receive props through input:
@Input() props: Props;
constructor(private rMap: RMap) {
this.rMap.init(this.props) {
}
}
However, in another root service, I face the issue of receiving undefined
on the line
this.rMap.interactiveMap.getSourceId();
:
@Injectable({ providedIn: 'root' })
export class SearchyService {
constructor(private rMap: RMap) {
this.drawResourceId = this.rMap.interactiveMap.getSourceId();
}
}
This is because RMap
involves some asynchronous operations.