{"name":"strict-event-emitter","version":"0.4.2","description":"Type-safe implementation of EventEmitter for browser and Node.js","main":"lib/index.js","typings":"lib/index.d.ts","repository":{"type":"git","url":"git@github.com:open-draft/strict-event-emitter.git"},"author":{"name":"Artem Zakharchenko","email":"kettanaito@gmail.com"},"license":"MIT","scripts":{"dev":"tsc -w","test":"jest","test:ts":"tsc -p test/typings.tsconfig.json","clean":"rimraf ./lib","build":"yarn clean && tsc","release":"release publish"},"devDependencies":{"@ossjs/release":"^0.4.0","@types/events":"^3.0.0","@types/jest":"^29.2.4","jest":"^29.3.1","jest-extended":"^3.2.0","rimraf":"^3.0.2","ts-jest":"^29.0.3","ts-node":"^9.1.1","typescript":"4.8"},"_id":"strict-event-emitter@0.4.2","dist":{"shasum":"a97985133145888ce15d913b6579846d989d2f81","integrity":"sha512-ycgUiHdPm0ePaNtGcQk68ZRQBiUtQXV0+agy7VyNnW5zge2bAfF0SpuBxUwmhBS+/uVk7J3Y52GIZSzcpbdNbw==","tarball":"http://registry-npm.ywwl.com/strict-event-emitter/download/strict-event-emitter-0.4.2.tgz","fileCount":10,"unpackedSize":14162,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDK8jihRsxYICaKsdrYatb9ulW8pRqBM83C6l4ZkxghzQIhAMZwtIE+eG3lqytzb4AbZe5AKDFG0BlEFKKdpNLLZbsg"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjoyQEACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmpzzQ/7BrUxb6tNd7TugZtA8nX0RUzBifrIlMcBiCwK2USVibKJ4E5/\r\nDgsNoT7vYbto+ey+IUrSZnLNNkwYXNAMX6XAfNWx8TCAnxcJ5UV0+fHJo6cf\r\nvKpUMMio34RJ2PB9Sm8E03Hc27x+chIE2e6RIo74/Oc1eIEKGAZgcCxyDXhj\r\nbfQeflLm+VQOY8/5uhSapGqC/6uiKddlw/9oFNELKKH6iZW6KRtyuoz5pJfJ\r\nXRbOpd708v+WcRQLT7qPz/RtJaodzLzO2wuM0qTcvKIVwRt7FDworqaxUyDU\r\nVl8PC96S3tTgg2OscLngB3rZsIXI4KQQx+J3dGA5CMtY5e6esZeOYHLtHpd1\r\n6t7ipuIm1cOC9FhvqUh9CpXq0sGqGvbdKMPc1rykryz/zsHjwEGQzF8/4us/\r\nA5mIEUhM7CzwDMqaSx/+FTH5UJ4n49nY/HhLKrHdB0yaK+coPXEE6fWLGbwq\r\ntRVm3Tsw863vJJU2psbawxqemODLD5TlLPGfH/Zt+6KHuznPoCh37O1q7vQg\r\nSIdfeHSk1ue5cULkNqYO9B4lMVUvM+NVL3BWMtj1rgzWaWm2bRhX+2NCLuMF\r\noyTr+arJFjXxIjXNs1CLomWQ01u0PwcwbNvaPPM5Kg56kfSo759PoFECzbXB\r\n9NXxPZEF851AsPdMRTiG48jtYZttHVEM4QA=\r\n=p1P9\r\n-----END PGP SIGNATURE-----\r\n","size":3968,"noattachment":false,"key":"/strict-event-emitter/-/strict-event-emitter-0.4.2.tgz"},"_npmUser":{"name":"kettanaito","email":"kettanaito@gmail.com"},"directories":{},"maintainers":[{"name":"kettanaito","email":"kettanaito@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/strict-event-emitter_0.4.2_1671635972385_0.6677095276350382"},"_hasShrinkwrap":false,"readme":"# Strict Event Emitter\n\nA type-safe implementation of `EventEmitter` for browser and Node.js.\n\n## Motivation\n\nDespite event emitters potentially accepting any runtime value, defining a strict event contract is crucial when developing complex event-driven architectures. Unfortunately, the native type definitions for Node's `EventEmitter` annotate event names as `string`, which forbids any stricter type validation.\n\n```js\n// index.js\nconst emitter = new EventEmitter()\n\n// Let's say our application expects a \"ping\"\n// event with the number payload.\nemitter.on('ping', (n: number) => {})\n\n// We can, however, emit a different event by mistake.\nemitter.emit('pong', 1)\n\n// Or even the correct event with the wrong data.\nemitter.emit('ping', 'wait, not a number')\n```\n\nThe purpose of this library is to provide an `EventEmitter` instance that can accept a generic describing the expected events contract.\n\n```ts\nimport { Emitter } from 'strict-event-emitter'\n\n// Define a strict events contract where keys\n// represent event names and values represent\n// the list of arguments expected in \".emit()\".\ntype Events = {\n  ping: [number]\n}\n\nconst emitter = new Emitter<Events>()\nemitter.addListener('ping', (n) => {\n  // \"n\" argument type is inferred as \"number'.\n})\n\nemitter.emit('ping', 10) // OK\nemitter.emit('unknown', 10) // TypeError (invalid event name)\nemitter.emit('ping', 'wait, not a number') // TypeError (invalid data)\n```\n\nThis library is also a custom `EventEmitter` implementation, which makes it compatible with other environments, like browsers or React Native.\n\n## Getting started\n\n### Install\n\n```bash\nnpm install strict-event-emitter\n```\n\n### Use\n\n```ts\nimport { Emitter } from 'strict-event-emitter'\n\n// 1. Define an interface that describes your events.\n// Set event names as the keys, and their expected payloads as values.\ninterface Events {\n  connect: [id: string]\n  disconnect: [id: string]\n}\n\n// 2. Create a strict emitter and pass the previously defined \"Events\"\n// as its first generic argument.\nconst emitter = new Emitter<Events>()\n\n// 3. Use the \"emitter\" the same way you'd use the regular \"EventEmitter\" instance.\nemitter.addListener('connect', (id) => {})\nemitter.emit('connect', 'abc-123')\n```\n\n## License\n\nMIT\n","publish_time":1671635972535,"_cnpm_publish_time":1671635972535,"dist-tags":{"latest":"0.5.1"}}