Am new to angular service workers, and have the following needs
- Want to have a separate thread than the one in the app taking care of purging every n minutes an indexedDb containing stuff, and notify the tab/tabs that this has been done
- Would like that separate service/job to take care of doing some background calls that may fail and if so being retried till done, like posting photos to an API.
Am wondering which way to take for that with angular, the only approach I can see so far is having a PwaService injected into app.component.ts and then call the init() method, but seems to me its gonna just be yet another thing/component of my ng application
export class PwaService {
constructor(
private update: SwUpdate,
private appRef: ApplicationRef
) {}
init(): void {
if (!this.update.isEnabled) {
return;
}
console.info('Auto update is enabled');
const appIsStable$ = this.appRef.isStable.pipe(first((isStable) => isStable === true));
const everyHour$ = interval(60 * 60 * 1000);
concat(appIsStable$, everyHour$).subscribe(() => {
//do some scheduled job making sure its not done by ng application and blocking DOM and stuff...
});
}
}
app.component.ts
constructor(private pwaService: PwaService){this.pwaService.init();}
BTW I tested the above implementation with a loop and its blocking the DOM in the meantime... so that's probably not the way of doing things
You have to separate taks into a Web Worker for IndexedDB purging and use RxJS for background API calls with retry logic in Angular,
Create a worker
indexed-db-purge.worker.tsCreate a service
pwa.service.tsCall the services: