by chrismorgan · 1y ago · 0 comments
let t={};
sub=(e,c)=>((e=t[e]??=new Set).add(c),()=>e.delete(c));
pub=(n,d)=>t[n]?.forEach(f=>f(d))
The original was 149 bytes; this is 97.(The nullish coalescing assignment operator ??= has been supported across the board for 4½ years. Avoiding it will cost six more bytes.)
Well then, a few alternatives to replace f=>f(d), each with slightly different semantics:
• async f=>f(d) (+6, 103 bytes).
• f=>{try{f(d)}catch{}} (+14, 111 bytes).
• f=>setTimeout(()=>f(d)) (+16 bytes, 113 bytes).
• f=>queueMicrotask(()=>f(d)) (+20 bytes, 117 bytes).