{"name":"strict-event-emitter","version":"0.2.6","description":"Type-safe \"EventEmitter\" for everyday use","main":"lib/index.js","typings":"lib/index.js","repository":{"type":"git","url":"git@github.com:open-draft/strict-event-emitter.git"},"author":{"name":"Artem Zakharchenko","email":"kettanaito@gmail.com"},"license":"MIT","scripts":{"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/jest":"^26.0.19","@types/node":"^14.14.14","jest":"^26.6.3","rimraf":"^3.0.2","ts-jest":"^26.4.4","ts-node":"^9.1.1","typescript":"^4.1.3"},"dependencies":{"events":"^3.3.0"},"_id":"strict-event-emitter@0.2.6","dist":{"shasum":"7bb2022bdabcbf0058cec7118a7bbbfd64367366","integrity":"sha512-qDZOqEBoNtKLPb/qAutkXUt7hs3zXgYA1xX4pVa+gZHCZZVLr2r81AzHsK5YrQQhRNphMtkOUyAyOr9e1IxJTw==","tarball":"http://registry-npm.ywwl.com/strict-event-emitter/download/strict-event-emitter-0.2.6.tgz","fileCount":8,"unpackedSize":8610,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDPb22CYOdf9f9R4zNmS8H8vdzN1/nTB14ouPuYk8T/6QIgCQg9l8lTks05sc/KIwt0SnZuiiudJKr9GUEIFSuCjWo="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjOCKaACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmq0hQ/9E9eR4K2JR0N3Uz+47WX06vG25SfJVWIsgDwfG968ibjKZk5N\r\nzLxKIw6kISgHwbmtVi8Ao9E1b7KMt01wewaxaSKFoDW7URNAXCbDIUYbApW5\r\nL2anynf2XrpthVJ4Sxp2YKNnzZm4qLUdY/ZPj4i6jhK+RUWo3CeThzjvPD46\r\nd0gL1TX8OOmDE3TOwbNGZoOzPrWXRpehdElOBK7ZRC2RW+fKHr1MucFqpk0L\r\nXl4b7xOjoWTFosJ/NSloNc6AHKiYIyacsAFJzzlz4HBbvLchpm9DjLzbsrGW\r\nAKasudANpAALLAtWr9hFMPh7nBiwaF785NRC2Y0PhDgFwdqys4B9z5VpmaYZ\r\nieLYfj8a3R2owNsgSCMqu9WBncUpfNwLm2nl6QDfPeWWFYhJAVoa6NKe/BMO\r\nMctA+tCsJV/BBpZWsTF4m1CmSUpEtQHlZ+oeyZ+bxFXw42MU8OKB7ya8E9lE\r\nu1pxx8QKzrDp1dGH2c2dcWJURDkamdT7G2pu8DSnJotewBMfx6toBWeVa22Z\r\nhfShNXCTh2Yfk6+vmN0RxQoP7gIYo6hEGVkcEqwJviWzLrfmNbHq49hp8u19\r\np8VJyPfOoVI+mmm4+R2B8M1p0yjWhCBMnIeyzkX30D9gdoepAJ4T2LqUm05M\r\nqNiYoBF5R429WXsyqYPltY55MqUW/hqG+p0=\r\n=OeUc\r\n-----END PGP SIGNATURE-----\r\n","size":2813,"noattachment":false,"key":"/strict-event-emitter/-/strict-event-emitter-0.2.6.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.2.6_1664623257897_0.13323268504676689"},"_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":1664623258056,"_cnpm_publish_time":1664623258056,"dist-tags":{"latest":"0.5.1"}}