{"name":"strict-event-emitter","version":"0.5.0","description":"Type-safe implementation of EventEmitter for browser and Node.js","main":"lib/index.js","module":"lib/index.mjs","typings":"lib/index.d.ts","exports":{".":{"types":"./lib/index.d.ts","require":"./lib/index.js","default":"./lib/index.mjs"}},"repository":{"type":"git","url":"git+ssh://git@github.com/open-draft/strict-event-emitter.git"},"author":{"name":"Artem Zakharchenko","email":"kettanaito@gmail.com"},"license":"MIT","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","tsup":"^6.2.3","typescript":"4.8"},"scripts":{"dev":"tsc -w","test":"jest","test:ts":"tsc -p test/typings.tsconfig.json","clean":"rimraf ./lib","build":"pnpm clean && tsup","release":"release publish"},"bugs":{"url":"https://github.com/open-draft/strict-event-emitter/issues"},"homepage":"https://github.com/open-draft/strict-event-emitter#readme","_id":"strict-event-emitter@0.5.0","_integrity":"sha512-sqnMpVJLSB3daNO6FcvsEk4Mq5IJeAwDeH80DP1S8+pgxrF6yZnE1+VeapesGled7nEcIkz1Ax87HzaIy+02kA==","_resolved":"/private/var/folders/mh/mfyzksxs34jfv5znybsfp_fr0000gn/T/5b4c7ae608499e987bea7c3433efc9ec/strict-event-emitter-0.5.0.tgz","_from":"file:strict-event-emitter-0.5.0.tgz","_nodeVersion":"18.8.0","_npmVersion":"8.18.0","dist":{"integrity":"sha512-sqnMpVJLSB3daNO6FcvsEk4Mq5IJeAwDeH80DP1S8+pgxrF6yZnE1+VeapesGled7nEcIkz1Ax87HzaIy+02kA==","shasum":"a4aa84a3d9b4a9be12e750a75e089cabb3dbc0e2","tarball":"http://registry-npm.ywwl.com/strict-event-emitter/download/strict-event-emitter-0.5.0.tgz","fileCount":7,"unpackedSize":44440,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIFD4H+pcviySgqyIkw5kyqi10RPPJkXfh8Of/kZgYO7FAiBdgMH0sufPMqhVfFye6PtrjixTQ+XOOxy1QEs5idHhcg=="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJkGYu+ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmrWQA/7BXXiXsHvKajj2vdtYg5N/7ZI/hf5clIt0Tekz2WaevcBYx6e\r\nhQDqhm21cV6MOWTmKXN62PaqMicIbfKbMj8DFwC3vMcSMOIGnhH55RWOdL5B\r\nB8Pt9KrOQ0zLb7YMiWSHUFhpb5Qn0z8mg32NQ8lFiHNtwpN2IFux/WVXA8bG\r\n1F+J83s36rXD5hQ4yq6u/pJ9zjOMgDBerq6wlCCnKEf0Hp5DmpfYdx7jmMmU\r\ndODg19RZiQdmtBIMhgiX+hQ2n4dvj7bZj+FvB+5UrZBYkRVn9pZeFiz2iuYb\r\nGSq3/xJYVZGEilg9i7mBCtCzxT/JFQYF5EtF2ZK5cCmnYyfL/jzyc3l0HI9h\r\nQ4ZpNwXF7aFhmaDHdapyQRcQZr2bVwWCLw3SKu/5rCJ9MPf8NYamlDycNb54\r\nU35DwQDCNbImB7Wh8CASIeXV92iVnmsZTwv3CANMxB0c9ArHdG8CdTHI+KT5\r\nDBYfn9nAUwtYqsb42cT6RV5ybLpphZy2/B7bPdGvMnFbHwpkZyGspupJlPgB\r\nkFfMOiDJGqcro7R1nH08d6PQzGdQjM2aAc9tJkzKkrAfXOHbb5/gJJ6EIQRv\r\nrXkKRtTWcVPc4AnrovSPy7QoxLactahN69kJJZL0kExzKT+p+r5dHYMAXezf\r\nDk7k4hizAtcDTu9lzeTnrXPCKU03WFHLPos=\r\n=FzOy\r\n-----END PGP SIGNATURE-----\r\n","size":6963,"noattachment":false,"key":"/strict-event-emitter/-/strict-event-emitter-0.5.0.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.5.0_1679395774467_0.07376569191551297"},"_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":1679395774638,"_cnpm_publish_time":1679395774638,"dist-tags":{"latest":"0.5.1"}}