Skip to content

Instantly share code, notes, and snippets.

View mantou132's full-sized avatar
🎯
Focusing

mantou mantou132

🎯
Focusing
View GitHub Profile
@mantou132
mantou132 / PortAwareSharedWorker.ts
Created September 29, 2022 12:52 — forked from Akxe/PortAwareSharedWorker.ts
PortAwareSharedWorker, shared worker that know who is still connected and who is not
/// <reference lib="webworker" />
type SharedWorkerPort = MessagePort | DedicatedWorkerGlobalScope;
class PortAwareSharedWorkerPort<T extends SharedWorkerPort = SharedWorkerPort, D = any> {
private readonly weakRef: WeakRef<T>;
private disconnected = false;
constructor(
port: T,
onMessage: (eventData: D) => void,
@mantou132
mantou132 / serviceworker.js
Last active June 5, 2020 18:53 — forked from ngokevin/serviceworker.js
Service Worker Template - cache-else-network + network-else-cache
const VERSION = '0.0.1';
const assetsToCache = [...new Set([self.location.pathname, '/'])].map(
path => self.location.origin + path,
);
self.addEventListener('install', () => {
self.skipWaiting();
self.caches.open(VERSION).then(cache => {
cache.addAll(assetsToCache);