Scanning is the one thing in Munchable that has to feel instant. You are standing in a shop holding a packet, you point the camera at the barcode, and the answer about whether that product suits you should already be there. Everything else in the app can take a moment. This cannot.
So the scan path has three caches and a de-duplicator in front of one network call. They look like one optimisation. They are four different decisions, and only the first is about speed.
Layer 1: a Map, read synchronously
Products resolved during this session live in a plain Map , and the scan path peeks at it synchronously. No await , no storage round trip, no state update. Scan the same packet twice while comparing two brands on a shelf and the second scan costs nothing at all.
The peek has one rule that took a bug to find:
export function peekProduct ( barcode : string ) : ResolvedProduct | undefined { const hit = resolved . get ( barcode ) ?? resolved . get ( normalizeBarcode ( barcode ) ) ; if ( ! hit || hit .…