Created
December 6, 2019 23:11
-
-
Save kmaraz/bea955a4c6d4bbb14c54cc94a9f52ec2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class WorkerService { | |
initialize() { | |
if ('serviceWorker' in navigator && 'BroadcastChannel' in window) { | |
// Initialize worker | |
this.getWorkboxInstance() | |
.subscribe((Workbox) => { | |
if (Workbox) { | |
const wb = new Workbox(`/${WORKER_NAME}`); | |
wb.register(); | |
} | |
}); | |
} | |
} | |
private getWorkboxInstance(): Observable<any> { | |
return new Observable((observer: Observer<any>) => { | |
import( | |
/* webpackChunkName: "workboxWindow" */ | |
'workbox-window' as any | |
) | |
.then(({ Workbox }) => observer.next(Workbox)) | |
.catch(() => observer.next(null)); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment