GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — EventEmitter/JavaScript/a-prod.js at master · HowProgrammingWorks/EventEmitter Expand file tree/ Copy patha-prod.jsMore file actions 115 lines (95 loc) · 2.17 KB / Copy patha-prod.jsFile metadata and controls115 lines (95 loc) · 2.17 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 'use strict'; const emitter = () => { const events = new Map(); const wrapped = new Map(); const ee = { on: (name, f, timeout = 0) => { const event = events.get(name); if (event) event.push(f); else events.set(name, [f]); if (timeout) { setTimeout(() => { ee.remove(name, f); }, timeout); } }, emit: (name, ...data) => { const event = events.get(name); event?.forEach((f) => f(...data)); }, once: (name, f) => { const g = (...a) => { ee.remove(name, g); f(...a); }; wrapped.set(f, g); ee.on(name, g); }, remove: (name, f) => { const event = events.get(name); if (!event) return; let i = event.indexOf(f); if (i !== -1) { event.splice(i, 1); return; } const g = wrapped.get(f); if (g) { i = event.indexOf(g); if (i !== -1) event.splice(i, 1); if (!event.length) events.delete(name); } }, clear: (name) => { if (name) events.delete(name); else events.clear(); }, count: (name) => { const event = events.get(name); return event?.length ?? 0; }, listeners: (name) => { const event = events.get(name); return event.slice(); }, names: () => [...events.keys()], }; return ee; }; // Usage const ee = emitter(); // on and emit ee.on('e1', (data) => { console.dir(data); }); ee.emit('e1', { msg: 'e1 ok' }); // once ee.once('e2', (data) => { console.dir(data); }); ee.emit('e2', { msg: 'e2 ok' }); ee.emit('e2', { msg: 'e2 not ok' }); // remove const f3 = (data) => { console.dir(data); }; ee.on('e3', f3); ee.remove('e3', f3); ee.emit('e3', { msg: 'e3 not ok' }); // count ee.on('e4', () => {}); ee.on('e4', () => {}); console.log('e4 count', ee.count('e4')); // clear ee.clear('e4'); ee.emit('e4', { msg: 'e4 not ok' }); ee.emit('e1', { msg: 'e1 ok' }); ee.clear(); ee.emit('e1', { msg: 'e1 not ok' }); // listeners and names ee.on('e5', () => {}); ee.on('e5', () => {}); ee.on('e6', () => {}); ee.on('e7', () => {}); console.log('listeners', ee.listeners('e5')); console.log('names', ee.names()); |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |