Created
December 6, 2019 23:15
-
-
Save kmaraz/b17853cbf00a313f5f1719e122fac54c 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
initialize() { | |
if ('serviceWorker' in navigator && 'BroadcastChannel' in window) { | |
// ... | |
// Subscribe to broadcast channel from worker | |
const apiUpdates = new BroadcastChannel(BROADCAST_CHANNEL); | |
apiUpdates.addEventListener('message', (event: any) => { | |
if (event.data.type === CACHE_UPDATED) { | |
const { cacheName, updatedURL } = event.data.payload; | |
this.retrieveAndSendData(cacheName, updatedURL); | |
} | |
}); | |
} | |
} | |
private dispatchData(cacheName: string, data: any) { | |
switch (cacheName) { | |
case 'api-things': { | |
return this.store$.dispatch(ThingsActions.update(data)); | |
} | |
} | |
} | |
private async retrieveAndSendData(cacheName: string, updatedURL: string) { | |
const cache = await caches.open(cacheName); | |
const response = await cache.match(updatedURL); | |
if (response) { | |
const responseJson = await response.json(); | |
this.dispatchData(cacheName, responseJson); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment