{"name":"stream-demux","version":"2.0.0","description":"An iterable asynchronous stream demultiplexer.","main":"index.js","scripts":{"test":"./node_modules/mocha/bin/_mocha --reporter spec --timeout 10000 --slow 10000"},"repository":{"type":"git","url":"git+https://github.com/SocketCluster/stream-demux.git"},"keywords":["iterable","async","stream","demux","demultiplexer","for-await-of"],"author":{"name":"Jonathan Gros-Dubois"},"license":"MIT","bugs":{"url":"https://github.com/SocketCluster/stream-demux/issues"},"homepage":"https://github.com/SocketCluster/stream-demux#readme","devDependencies":{"mocha":"^5.0.5"},"dependencies":{"async-iterable-stream":"^1.2.1","writable-async-iterable-stream":"^2.0.1"},"gitHead":"060cc3e60ca2b890b559df45904f1457718d2f49","_id":"stream-demux@2.0.0","_npmVersion":"5.6.0","_nodeVersion":"10.0.0","_npmUser":{"name":"topcloudsystems","email":"grosjona@yahoo.com.au"},"dist":{"integrity":"sha512-JH7LpnvZnynAruGOSdJ3fiLdH6A8hckddkigDjcDa91uUozueJf1/0jEwPqf9kcBY6wiK9n+IPUa4dZyQAdwCQ==","shasum":"2aaef8718ddc0142b95fefffa972caa7657b3900","tarball":"http://registry-npm.ywwl.com/stream-demux/download/stream-demux-2.0.0.tgz","fileCount":5,"unpackedSize":10958,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJcA+/UCRA9TVsSAnZWagAA4ZYP/iE9Y2lhYtrk9DPC1Axh\nGOgeiEZuZb37uzzZq2We1HpRDry9DDmv98fwe62E25YucSVwjbQxfMWpeash\nw9o76BeH0hPfa/DW1Rj8zpLQ+1E1LmBW/HKfS6AohqcGIpE1QPu60FburY7L\ngajnGToPL3yXn6kE9Ifg/HUMu06CwFt8tVPtnlwa9XdcYGkzyX8w8AruUJop\n4GI4kGuRXDBDXLgVB/JocDipKY4s2/qd1wXWKQWHqBeRCyuwkXadOJdr5a2o\n87IFZQzVW5F8bhWc5IR56/Px0EuBTzeqZ+VGqpZuMIF74vFO0g561avbJqlq\nQkI3uFNSjLcvDPPQaraHBaeRkd/Gy/ChkqSlaQntHXth0YgTEMOnW0OQ4QCC\nLx/4im/iSNw8fl347JtyTvmHOJB9SqpSBXx5t8z7wLxQI+wJphilP1Pf3Aom\nBMoghRF/XXI9bwIR2Yvk+zDYV9bj85xsrSqtJgI9clV/6yQPLmZFdMh73/0U\n2fXmHq0cht1ISYMnDEON6/pfrDhqHNPjVfJM2H3SRR44GkxDi84C+xc/B5k1\nwQopr0Y7su8Vs6TFXnleqAkWRrMZW3Kv8Yxcf0sFAVdFaiKQ+mpMTHBtKuib\nOWJBIjqaF+PXn6r0Sz1NUVy19TwC9gGUwiuICY4jtMf2wrY2FvKZZxrArzPm\ntov/\r\n=oKp5\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCICI8E9sJ4FbgXHPXSbuz+cSKpEYoJafeh9jj9R5UAhjMAiAnngjVoLAEnpbKbVJTt7rmfjYsEk+O+NabljjfB4lIQw=="}],"size":3108,"noattachment":false,"key":"/stream-demux/-/stream-demux-2.0.0.tgz"},"maintainers":[{"name":"topcloudsystems","email":"grosjona@yahoo.com.au"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/stream-demux_2.0.0_1543761876097_0.03115023497471947"},"_hasShrinkwrap":false,"readme":"# stream-demux\nAn consumable stream demultiplexer.\n\nLets you write data to multiple consumable streams from a central place without keeping any references to those streams.\nThe `StreamDemux` class returns streams of class `DemuxedConsumableStream` (base class `ConsumableStream`).  \nSee https://github.com/SocketCluster/consumable-stream\n\nThis library uses a queue which is implemented as a singly-linked list; this allows each loop to consume at its own pace without missing any events (supports nested await statements). An 'event' in the queue can be garbage-collected as soon as the slowest consumer moves its pointer past it.\n\n## Installation\n\n```\nnpm install stream-demux\n```\n\n## Usage\n\n### Consuming using async loops\n\n```js\nlet demux = new StreamDemux();\n\n(async () => {\n  // Consume data from 'abc' stream.\n  let substream = demux.stream('abc');\n  for await (let packet of substream) {\n    console.log('ABC:', packet);\n  }\n})();\n\n(async () => {\n  // Consume data from 'def' stream.\n  let substream = demux.stream('def');\n  for await (let packet of substream) {\n    console.log('DEF:', packet);\n  }\n})();\n\n(async () => {\n  // Consume data from 'def' stream.\n  // Can also work with a while loop for older environments.\n  // Can have multiple loops consuming the same stream at\n  // the same time.\n  // Note that you can optionally pass a number n to the\n  // createConsumer(n) method to force the iteration to\n  // timeout after n milliseconds of inactivity.\n  let consumer = demux.stream('def').createConsumer();\n  while (true) {\n    let packet = await consumer.next();\n    if (packet.done) break;\n    console.log('DEF (while loop):', packet.value);\n  }\n})();\n\n(async () => {\n  for (let i = 0; i < 10; i++) {\n    await wait(10);\n    demux.write('abc', 'message-abc-' + i);\n    demux.write('def', 'message-def-' + i);\n  }\n  demux.close('abc');\n  demux.close('def');\n})();\n\n// Utility function for using setTimeout() with async/await.\nfunction wait(duration) {\n  return new Promise((resolve) => {\n    setTimeout(() => {\n      resolve();\n    }, duration);\n  });\n}\n```\n\n### Consuming using the once method\n\n```js\n// Log the next received packet from the abc stream.\n(async () => {\n  // The returned promise never times out.\n  let packet = await demux.stream('abc').once();\n  console.log('Packet:', packet);\n})();\n\n// Same as above, except with a timeout of 10 seconds.\n(async () => {\n  try {\n    let packet = await demux.stream('abc').once(10000);\n    console.log('Packet:', packet);\n  } catch (err) {\n    // If no packets are written to the 'abc' stream before\n    // the timeout, an error will be thrown and handled here.\n    // The err.name property will be 'TimeoutError'.\n    console.log('Error:', err);\n  }\n})();\n```\n\n## Goal\n\nThe goal of this module is to facilitate functional programming patterns which decrease the probability of memory leaks and race conditions.\nIt serves as an alternative to callback-based event handling.\n","publish_time":1543761876251,"_cnpm_publish_time":1543761876251,"dist-tags":{"latest":"10.2.1"}}