{"_id":"@acdibble/emitter","name":"@acdibble/emitter","dist-tags":{"latest":"0.1.0"},"versions":{"0.1.0":{"name":"@acdibble/emitter","version":"0.1.0","description":"A zero-overhead, type-safe event emitter","type":"module","main":"./dist/TypeSafeEventEmitter.js","exports":"./dist/TypeSafeEventEmitter.js","scripts":{"prepublishOnly":"rm -rf dist && npx tsc"},"author":"","license":"ISC","peerDependencies":{"@types/node":"^16.0.0"},"devDependencies":{"typescript":"^4.7.3"},"types":"./dist/TypeSafeEventEmitter.d.ts","gitHead":"7aa658cf31fc4ea86be6f77ae5580589e65b0ad5","_id":"@acdibble/emitter@0.1.0","_nodeVersion":"16.10.0","_npmVersion":"7.24.0","dist":{"integrity":"sha512-roBj8rv19AhK79XKfUaGLGGHNJhl8VFIy9R1vSo/2Z/Wn7O/3xy6lOsnFeytBdySN4FuredF7movrjg5bgbAAA==","shasum":"9ab29466a6fabf950d520ae301ebabaf170224e8","tarball":"https://registry.npmjs.org/@acdibble/emitter/-/emitter-0.1.0.tgz","fileCount":5,"unpackedSize":6130,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDdZMBENKdeQ3pILr7Pk/kqUSeilVmpneE4HVsEPFWhPgIgA71HBXKhVTf4mkahATcmtt8so4Xtr2HtYKjLHtDabgo="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJioPWbACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqviA//Zw2pwD8GY5HiS+CZMlWFZr7KXt8/zLqMsjBkJTX2fviEELUh\r\nlEdUqa0sfiKGFKnln9q7V3Qh2AErfb9f/0+F+44hnU7GfD0MQQKlfo932yZ7\r\nGbUylgcfQ9miDKLUXqvL+VSEtj8BeM73RvMJXRFVlwp6el2Ju2lCIO5MvTJ/\r\nZdBwng1qK2HTfVonW6/11LGQYLqnyOTRHUgg/5G0FcSQLUqW5WAGLjdt6xcG\r\nyy2n2dV6REKJhWEa3Pv4Reu1xtU6zzWXVUOOSGStYEIIQPPvnvueTMflLzZU\r\nejKex1Ywfhnr/DqEt5pBl8l3N0auDgH2G3R0RXOr0ZedcpQ9tt9oSjUSmfeC\r\nxoBzXtJgUJRQFp7qvcWlG/btzkHCoQMvaF94nVAvKbC/TJsTSzySI6CO/cJR\r\nuVXIOkPOLv2tKNkgdsOMojmyUGC+qJnOqXUBik2NOT0/p3GpW6T+mmqkObSG\r\nJoezW5Sp9ek09fTzg3Ff9xAcUo94ct89UXPJffBVggLHZQFnrGobX+i2nr3Q\r\ni0lOWnIqvmCcITVriv7mMGplN3EpQaGYCWNjCZbY0X2Kxdb2SHc3jwwTU381\r\nOcw+KFzO6RibRoxmsDq0dvT6EjnzXGWdOUDVKes9GOOJVhVWif/t56Idv9do\r\n4kdia4pfkLekOBamBd6iRibzBUPIwJmVZhw=\r\n=RrGf\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"acdibble","email":"acdibble@gmail.com"},"directories":{},"maintainers":[{"name":"acdibble","email":"acdibble@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/emitter_0.1.0_1654715803081_0.006587295355185985"},"_hasShrinkwrap":false}},"time":{"created":"2022-06-08T19:16:43.023Z","0.1.0":"2022-06-08T19:16:43.248Z","modified":"2022-06-08T19:16:43.427Z"},"maintainers":[{"name":"acdibble","email":"acdibble@gmail.com"}],"description":"A zero-overhead, type-safe event emitter","license":"ISC","readme":"# @acdibble/emitter\n\nA zero-overhead, type-safe event emitter\n\n## Description\n\nThis package doesn't contain any logic. It simply reexports Node.js's native\n`EventEmitter` from the `events` module with type info that allows for type-safe\nusage.\n\nThe following functions have been made type-safe:\n\n```\naddListener\nemit\noff\non\nonce\nprependListener\nprependOnceListener\nremoveAllListeners\nremoveListener\n```\n\n## Usage\n\nSimply define a type/interface of listener functions with the event name (or\nchannel) as the name of the function, e.g.:\n\n```ts\ninterface MessageMap {\n  connect: () => void;\n  disconnect: (code: number, reason?: string) => void;\n}\n```\n\nThis interface is passed as the generic paramater to the constructor function of\n`TypeSafeEventEmitter`. `TypeSafeEventEmitter` will take all of those functions\nand feed the type information into the `emit` and `on` calls.\n\n## Examples\n\nWith the following setup:\n\n```typescript\nimport TypeSafeEventEmitter from \"@acdibble/emitter\";\n\ninterface MessageMap {\n  connect: () => void;\n  disconnect: (code: number, reason?: string) => void;\n  message: (content: string) => void;\n}\n\nconst emitter = new TypeSafeEventEmitter<MessageMap>();\n```\n\n### Listening\n\nWe can see all possible events are suggested by intellisense:\n<img width=\"578\" alt=\"image\" src=\"https://user-images.githubusercontent.com/21286068/172693932-56b18002-cce3-4ecb-8b56-87f716ae8152.png\">\n\nAfter entering the event, intellisense gives the callback signature:\n<img width=\"682\" alt=\"image\" src=\"https://user-images.githubusercontent.com/21286068/172694522-0e4f26c2-3924-4295-bf72-856a17a75e69.png\">\n\nAll parameters are given by intellisense for the listener function:\n<img width=\"766\" alt=\"image\" src=\"https://user-images.githubusercontent.com/21286068/172694680-bec5bbc3-ebca-491d-8625-c36254464455.png\">\n\nListeners can be declared in a non-anonymous way using the message interface:\n<img width=\"539\" alt=\"image\" src=\"https://user-images.githubusercontent.com/21286068/172694958-535b535b-fd5c-4ab6-a510-ec25bcff709e.png\">\n\nAn incorrect listener cannot be used:\n<img width=\"892\" alt=\"image\" src=\"https://user-images.githubusercontent.com/21286068/172695677-140fdded-b8de-414f-9b14-9468a5dc00a4.png\">\n\n### Emitting\n\nEmitting events provides the same intellisense as listening:\n<img width=\"601\" alt=\"image\" src=\"https://user-images.githubusercontent.com/21286068/172695850-f8eb84c6-f86a-4d45-9c62-ce67e20c924a.png\">\n\nWhen emitting events, all arguments are given with their proper names and types:\n<img width=\"683\" alt=\"image\" src=\"https://user-images.githubusercontent.com/21286068/172698006-460f7fbc-32af-47f6-9e7b-74adad189118.png\">\n\nAll expected errors are reported, such as incorrect arity:\n<img width=\"551\" alt=\"image\" src=\"https://user-images.githubusercontent.com/21286068/172695981-69a612b6-de41-4f9e-b046-2e67d10d00f6.png\">\n<img width=\"760\" alt=\"image\" src=\"https://user-images.githubusercontent.com/21286068/172696137-73df1407-bddc-468d-97d4-ac46d7cabba7.png\">\n\nAnd incorrect types being emitted:\n<img width=\"885\" alt=\"image\" src=\"https://user-images.githubusercontent.com/21286068/172696735-08713c45-ef63-4f7f-bbf2-d7c8bd787054.png\">\n","readmeFilename":"README.md"}