I have developed a service which load objects on start up but these object are not available in cache.
setupApp function above the NgModule
export function setupApp(setup: AppLoadService ) {
return () => setup.initliaze();
}
@NgModule({
imports: [
NgModule
providers: [
AppLoadService ,
{
provide: APP_INITIALIZER,
useFactory: setupApp,
deps: [AppLoadService ],
multi: true
},
The AppLoadService ...
@Injectable()
export class AppLoadService {
private url = ...
public countries: Country[] = [];
constructor(private http: HttpClient) {}
public initliaze() {
this.initCountries();
}
public initCountries() {
if (this.countries.length == 0) {
this.http.get<Country[]>(`${this.url}`)
.subscribe(data => { this.countries = data; },
err => console.error(err),
() => console.log('done load countries')
)
}
}
public getCountries() {
console.log(this.countries)
return this.countries;
}
}
When calling the getCountries() method it return empty array. I need the AppLoadService to be a cache service, any idea how to achieve this ?
UPDATE I've changed the initliaze function to this and now it's look like the cache is working:
initliaze(): Promise<any> {
return new Promise((resolve, reject) => {
console.log(`initializeApp:: inside promise`);
this.initCountries();
setTimeout(() => {
console.log(`initializeApp:: inside setTimeout`);
// doing something
resolve();
}, 3000);
});
}