{"name":"@tootallnate/once","version":"3.0.0","description":"Creates a Promise that waits for a single event","type":"module","main":"./dist/index.js","types":"./dist/index.d.ts","scripts":{"prebuild":"rimraf dist","build":"tsc","test":"jest","prepublishOnly":"npm run build"},"repository":{"type":"git","url":"git://github.com/TooTallNate/once.git"},"keywords":[],"author":{"name":"Nathan Rajlich","email":"nathan@tootallnate.net","url":"http://n8.io/"},"license":"MIT","bugs":{"url":"https://github.com/TooTallNate/once/issues"},"devDependencies":{"@types/jest":"^27.0.2","@types/node":"^12.12.11","abort-controller":"^3.0.0","jest":"^27.2.1","rimraf":"^3.0.0","ts-jest":"^27.0.5","typescript":"^4.4.3"},"engines":{"node":">= 10"},"jest":{"preset":"ts-jest","globals":{"ts-jest":{"diagnostics":false,"isolatedModules":true}},"verbose":false,"testEnvironment":"node","testMatch":["<rootDir>/test/**/*.test.ts"]},"gitHead":"2bc2e134b7728a6b37c6838a3b5d5c290e60a539","homepage":"https://github.com/TooTallNate/once#readme","_id":"@tootallnate/once@3.0.0","_nodeVersion":"12.22.6","_npmVersion":"6.14.15","dist":{"integrity":"sha512-OAdBVB7rlwvLD+DiecSAyVKzKVmSfXbouCyM5I6wHGi4MGXIyFqErg1IvyJ7PI1e+GYZuZh7cCHV/c4LA8SKMw==","shasum":"d52238c9052d746c9689523e650160e70786bc9a","tarball":"http://registry-npm.ywwl.com/@tootallnate/once/download/@tootallnate/once-3.0.0.tgz","fileCount":15,"unpackedSize":25011,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJh2ekhCRA9TVsSAnZWagAAk+sP/jPqUJNTMe4Q0cgzmuUH\n33zRhJSAWr/jKLz//AnY4kaZnKtMpob18BRdST+9efxKxpDSC05K3ZHboukm\nZlK+x2mMcSScVEPMppSMweJYpxGLlUW7puT7URGfNTrHvL6R1b6p5UT9iVRA\nYsDFduuuCCc7WBxQcGilTPIOlnxiZy4w1I9FMdmpk6nHeINIraWotcXsalgL\nJq3QsqL+O9f/PsApWhBP+viBUbGzsdsvW6+37U6+JHI/obJoTjdRc488ic2M\nckT3ESFpD2BSztEYiC+A6I8xE4trbbXB2I0dY2mRYH+5JqTXHPRnit+cCihH\ntC4ntATQgMK6MX7aCz0ZX+pzH+ZZrMhPGmCpzc4L93t6mxm89JV/6M9Ba7KE\nfRN/Qnr90cBfH3I1a/okhVKBPeimLckeAjFy16YsuavKsRFu/zgDeh2DbYxw\nKpy3MrZZSY0RlY/Z8FeEY+N/SqaO8D7Xql6Z1noCS35e4GepoM9d3xEa9axL\nXR/VtQqBQQYWg0opqCOpj0ivuKlq/10BFXfByzJeXTJgTHkr7Xs2uhbxodMs\n3QGeWF1Xx6AZVXO8KDnX3Vz64THev0RFR48lNGq3zBGm7MvEVtm4ArNgmVLl\nXJMGTq2JoSFXh/cSZrcZ1tDZ2CLcQqEgcwVC5HpBnmtkf6uvDcs9PHtobYvk\nrCzz\r\n=1uQi\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIAKqgfeKpxNBpZ2qzfI+LmrCMk/kj9hxnKBoT9lIDcbfAiBzi8T/WuQoZ/qPbm1Q2J9AyS6XEbRDzVYCMk8zPbCw6Q=="}],"size":4447,"noattachment":false,"key":"/@tootallnate/once/-/@tootallnate/once-3.0.0.tgz"},"_npmUser":{"name":"tootallnate","email":"nathan@tootallnate.net"},"directories":{},"maintainers":[{"name":"tootallnate","email":"nathan@tootallnate.net"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/once_3.0.0_1632734034811_0.33679291306743675"},"_hasShrinkwrap":false,"readme":"# @tootallnate/once\n\n### Creates a Promise that waits for a single event\n\n## Installation\n\nInstall with `npm`:\n\n```bash\n$ npm install @tootallnate/once\n```\n\n## API\n\n### once(emitter: EventEmitter, name: string, opts?: OnceOptions): Promise&lt;[...Args]&gt;\n\nCreates a Promise that waits for event `name` to occur on `emitter`, and resolves\nthe promise with an array of the values provided to the event handler. If an\n`error` event occurs before the event specified by `name`, then the Promise is\nrejected with the error argument.\n\n```typescript\nimport once from '@tootallnate/once';\nimport { EventEmitter } from 'events';\n\nconst emitter = new EventEmitter();\n\nsetTimeout(() => {\n    emitter.emit('foo', 'bar');\n}, 100);\n\nconst [result] = await once(emitter, 'foo');\nconsole.log({ result });\n// { result: 'bar' }\n```\n\n#### Promise Strong Typing\n\nThe main feature that this module provides over other \"once\" implementations is that\nthe Promise that is returned is _**strongly typed**_ based on the type of `emitter`\nand the `name` of the event. Some examples are shown below.\n\n_The process \"exit\" event contains a single number for exit code:_\n\n```typescript\nconst [code] = await once(process, 'exit');\n//     ^ number\n```\n_A child process \"exit\" event contains either an exit code or a signal:_\n\n```typescript\nconst child = spawn('echo', []);\nconst [code, signal] = await once(child, 'exit');\n//     ^ number | null\n//           ^ string | null\n```\n\n_A forked child process \"message\" event is type `any`, so you can cast the Promise directly:_\n\n```typescript\nconst child = fork('file.js');\n\n// With `await`\nconst [message, _]: [WorkerPayload, unknown] = await once(child, 'message');\n\n// With Promise\nconst messagePromise: Promise<[WorkerPayload, unknown]> = once(child, 'message');\n\n// Better yet would be to leave it as `any`, and validate the payload\n// at runtime with i.e. `ajv` + `json-schema-to-typescript`\n```\n\n_If the TypeScript definition does not contain an overload for the specified event name, then the Promise will have type `unknown[]` and your code will need to narrow the result manually:_\n\n```typescript\ninterface CustomEmitter extends EventEmitter {\n    on(name: 'foo', listener: (a: string, b: number) => void): this;\n}\n\nconst emitter: CustomEmitter = new EventEmitter();\n\n// \"foo\" event is a defined overload, so it's properly typed\nconst fooPromise = once(emitter, 'foo');\n//    ^ Promise<[a: string, b: number]>\n\n// \"bar\" event in not a defined overload, so it gets `unknown[]`\nconst barPromise = once(emitter, 'bar');\n//    ^ Promise<unknown[]>\n```\n\n### OnceOptions\n\n-   `signal` - `AbortSignal` instance to unbind event handlers before the Promise has been fulfilled.\n","publish_time":1632734034973,"_cnpm_publish_time":1632734034973,"dist-tags":{"latest":"3.0.1","legacy":"2.0.1"}}