57eb412b6d
The UI only noticed a removed card when some user action ran a real card command (e.g. Check status); /api/status is a cached-state read that kept returning the old card, and _handle_card_disconnect() did not clear app.card/rs. - start_card_monitor() registers a pyscard CardObserver for our reader; it only polls SCardGetStatusChange (no APDU, no connection, no extra process), and on removal sets server.card_present=False and calls _handle_card_disconnect() under _CARD_LOCK - /api/status now exposes connected (session usable) and card_present (physically inserted) and masks card/profile/atr/selection when not connected; _CARD_CONNECTED is initialized from card presence instead of being unconditionally True - the 2s UI poll includes /api/status; on disconnect it switches to the existing 'No card detected. Insert card and click Equip' state, or the new 'Card inserted — press Equip' hint when the card is back; the old _hadData heuristic is gone Tests for the observer (filtering, removal, insertion) and the UI state transitions. SW cache v90 -> v91.
56 lines
1.4 KiB
JavaScript
56 lines
1.4 KiB
JavaScript
const CACHE = 'otaman-v91';
|
|
const URLS = [
|
|
'index.html',
|
|
'help.html',
|
|
'help-ru.html',
|
|
'style.css',
|
|
'sim.png',
|
|
'favicon.svg',
|
|
'icon-192.png',
|
|
'icon-512.png',
|
|
'manifest.json',
|
|
'des-bundle.js',
|
|
'aes-bundle.js',
|
|
'sim.svg',
|
|
'sim_anim.svg',
|
|
'nosim.svg',
|
|
];
|
|
|
|
self.addEventListener('install', e => {
|
|
e.waitUntil(
|
|
caches.open(CACHE).then(c => c.addAll(URLS))
|
|
);
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener('activate', e => {
|
|
e.waitUntil(
|
|
caches.keys().then(keys =>
|
|
Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k)))
|
|
).then(() => clients.claim())
|
|
);
|
|
});
|
|
|
|
self.addEventListener('fetch', e => {
|
|
if (!e.request.url.startsWith('http')) return;
|
|
if (new URL(e.request.url).pathname.startsWith('/api/')) return; // live data, never cache
|
|
if (e.request.method !== 'GET') return;
|
|
const isNavigate = e.request.mode === 'navigate' || e.request.url.endsWith('sw.js');
|
|
if (isNavigate) {
|
|
e.respondWith(
|
|
fetch(e.request).then(res => {
|
|
const clone = res.clone();
|
|
caches.open(CACHE).then(c => c.put(e.request, clone));
|
|
return res;
|
|
}).catch(() => caches.match(e.request))
|
|
);
|
|
} else {
|
|
e.respondWith(
|
|
caches.match(e.request).then(r => r || fetch(e.request).then(res => {
|
|
const clone = res.clone();
|
|
caches.open(CACHE).then(c => c.put(e.request, clone));
|
|
return res;
|
|
}))
|
|
);
|
|
}
|
|
}); |