{"name":"writable-consumable-stream","version":"1.0.2","keywords":["async","generator","stream","data","for-await-of"],"author":{"name":"Jonathan Gros-Dubois"},"license":"MIT","_id":"writable-consumable-stream@1.0.2","maintainers":[{"name":"topcloudsystems","email":"grosjona@yahoo.com.au"}],"homepage":"https://github.com/SocketCluster/writable-consumable-stream#readme","bugs":{"url":"https://github.com/SocketCluster/writable-consumable-stream/issues"},"dist":{"shasum":"aa879af58530f7864fdb1c116a7215a359083e6c","tarball":"http://registry-npm.ywwl.com/writable-consumable-stream/download/writable-consumable-stream-1.0.2.tgz","fileCount":6,"integrity":"sha512-UAcmyq7ld5bXJxW0zV0ujJ3BDqT9+njkiqBSr7XNdp9EiVxoD9IWbtbhKBDeHlT1E1t8ykxG/cHwkcxPEN4zAg==","signatures":[{"sig":"MEYCIQC7tO9BfoeZjBFdMzjp4Et+EFiNe041T6NyV+ZXxrRi9QIhAOoynN2XaG0hl0wUnnXKJ5UJUB0hOAwGA5DP9FT7Mvul","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":51769,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJcaf+WCRA9TVsSAnZWagAAu6oQAIpFZ6cWdWP/1eilCdDd\nPahchLfvA7AMQ8I1seBKAsjou2nf4PVhsv6APZa1sOFH1ZIyCy+/jlYU9E9l\nZ7ob/G/d4hgm3g6BTHRAIWJqQ0WleUykh7MXm5zFbAp64sbHYSaBOWuppRbk\nDi8cRphJElyI/HeIeRqsnhxfraZzaxq3mU0HihFh598DU9dfOLyYe5GXo61l\nu0UFRomtny3BT1WwRj3ExXH5TdLHe2k6WFHCXDZM2DDRwiLX5n1LqBRh25CM\nbhox8K3y3GWXfKf5miyo0gYdm4b2zMgdjQNKqhBhuy0ouw4UwlfbqO0kuSff\nLFM106Z3u1R1Ghxk47tOIlSjrhbmrZaeV3yzOD9HGA0mYWp5BZP9Fq6fGvKM\ngtZs/nEGdw/lPUY9EWh3KeEjRuPtgjnNZDTl+S2ToT/F/1Vmbh5agshK5K/v\nQ3B6fe65px6KkzrAZgI2idarwU7dGod6gdBaiKrChbJwKapAPqSz9tyO8Mj+\npbF55/YqfZ2D9BmnfM51ecdMXgKaN/ftqJNlloVxRmSsLWELb5T72ua4kitI\nwNMLXvmf6t6zZMFMEKvUCr6LKitM8fxJAvYL6tiFBYVECxeqHHrt8lSuTk+h\n5M7mcnjQMRz/MZ5O2QPg4iGVCsQuX6uZ2LkHmMUnve/hTx5slQAx93WnC8SM\nL/0p\r\n=2fsN\r\n-----END PGP SIGNATURE-----\r\n","size":6883,"noattachment":false,"key":"/writable-consumable-stream/-/writable-consumable-stream-1.0.2.tgz"},"main":"index.js","gitHead":"431fe41e733ddfe8168788dbb5da8bc9e4fbb10d","scripts":{"test":"./node_modules/mocha/bin/_mocha --reporter spec --timeout 10000 --slow 10000"},"_npmUser":{"name":"topcloudsystems","email":"grosjona@yahoo.com.au"},"repository":{"url":"git+ssh://git@github.com/SocketCluster/writable-consumable-stream.git","type":"git"},"_npmVersion":"5.6.0","description":"An async stream which can be iterated over using a for-await-of loop.","directories":{},"_nodeVersion":"10.0.0","dependencies":{"consumable-stream":"^1.0.0"},"_hasShrinkwrap":false,"devDependencies":{"mocha":"^5.2.0"},"_npmOperationalInternal":{"tmp":"tmp/writable-consumable-stream_1.0.2_1550450581682_0.9971650812376369","host":"s3://npm-registry-packages"},"readme":"# writable-consumable-stream\nAn async stream which can be iterated over using a for-await-of loop and which can be written to.\n\nThe `WritableConsumableStream` class extends the `ConsumableStream` class.  \nSee https://github.com/SocketCluster/consumable-stream\n\n## Installation\n\n```\nnpm install writable-consumable-stream\n```\n\n## Usage\n\n### Require\n\n```js\nconst WritableConsumableStream = require('writable-consumable-stream');\n```\n\n### Consume a stream and write to it asynchronously:\n\n```js\nlet consumableStream = new WritableConsumableStream();\n\nasync function consumeAsyncIterable(asyncIterable) {\n  // Consume iterable data asynchronously.\n  for await (let packet of asyncIterable) {\n    console.log('Packet:', packet);\n  }\n}\nconsumeAsyncIterable(consumableStream);\n\nsetInterval(() => {\n  // Write data to the stream asynchronously,\n  consumableStream.write(`Timestamp: ${Date.now()}`);\n}, 100);\n```\n\n### Consume a stream using a while loop:\n\n```js\nlet consumableStream = new WritableConsumableStream();\n\nasync function consumeAsyncIterable(asyncIterable) {\n  // Consume iterable data asynchronously.\n  // Works in older environments.\n  let asyncIterator = asyncIterable.createConsumer();\n  while (true) {\n    let packet = await asyncIterator.next();\n    if (packet.done) break;\n    console.log('Packet:', packet.value);\n  }\n}\nconsumeAsyncIterable(consumableStream);\n\nsetInterval(() => {\n  // Write data to the stream asynchronously,\n  consumableStream.write(`Timestamp: ${Date.now()}`);\n}, 100);\n```\n\n### Consume a filtered stream using an async generator:\n\n```js\nlet consumableStream = new WritableConsumableStream();\n\n// Creates an async generator which only produces packets which are allowed by the\n// specified filterFunction.\nasync function* createFilteredStreamGenerator(fullStream, filterFunction) {\n  for await (let packet of fullStream) {\n    if (filterFunction(packet)) {\n      yield packet;\n    }\n  }\n}\n\nasync function consumeAsyncIterable(asyncIterable) {\n  // Consume iterable data asynchronously.\n  for await (let packet of asyncIterable) {\n    console.log('Packet:', packet);\n  }\n}\n\n// The filter function will only include strings which end with the number 5.\nfunction filterFn(data) {\n  return /5$/.test(data);\n}\nlet filteredStreamGenerator = createFilteredStreamGenerator(consumableStream, filterFn);\n\nconsumeAsyncIterable(filteredStreamGenerator);\n\nsetInterval(() => {\n  // Write data to the stream asynchronously,\n  consumableStream.write(`Timestamp: ${Date.now()}`);\n}, 100);\n```\n\n### Consume only the next data object which will be written to the stream:\n\n```js\nlet consumableStream = new WritableConsumableStream();\n\n(async () => {\n  let data = await consumableStream.once();\n  console.log(data);\n})();\n\nsetInterval(() => {\n  // Write data to the stream asynchronously,\n  consumableStream.write(`Timestamp: ${Date.now()}`);\n}, 100);\n```\n\n### Releasing a consumer\n\nKilling or closing a consumer removes it from the stream but does not discard the\npackets which it has not consumed yet - this is what allows a consumer to resume\nlater without missing any messages. For as long as a consumer is still reachable,\nthose packets, and everything written to the stream afterwards, stay in memory.\n\nSo once you are finished with a consumer, `break` out of its `for await...of` loop,\nor kill it with `stream.killConsumer(consumer.id)` (or `stream.kill()` for all\nconsumers) and discard any remaining references to it.\n\nSee `test/` directory for additional examples.\n","publish_time":1550450581829,"_cnpm_publish_time":1550450581829,"dist-tags":{"latest":"5.0.1"}}