{"name":"stream-demux","version":"8.0.0","description":"A consumable 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":{"consumable-stream":"^2.0.0","writable-consumable-stream":"^2.0.0"},"gitHead":"fd6542a6208f0962824f490e9e2195222cd48d47","_id":"stream-demux@8.0.0","_nodeVersion":"11.13.0","_npmVersion":"6.7.0","dist":{"integrity":"sha512-vt6P85yEUxiapK9F+ET5BLyto1DnCD4Sl7MlDklDgcb4K5SGXPtOPz9DiP7vTooJ1wW1F/VRPlcSV22Coeim1A==","shasum":"d7d33bf47dfaeb2e94f1966e990d206b6d26ef1e","tarball":"http://registry-npm.ywwl.com/stream-demux/download/stream-demux-8.0.0.tgz","fileCount":6,"unpackedSize":30819,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJePaZNCRA9TVsSAnZWagAA5s8QAIT5TJhpNdLJBT0qXjrh\nyI5LBrTkd7RjrNCj+02GmXW1pPXcq2vwyH3+y6E8ksvHfzM5zhKfgIrnTmrm\nsI9RMToMbmhWrWnDOj3g7eaTFXlEc44WlSqQbB3gHjWN2nUrgMYJVkQF92A/\ngvdCnr5fhMUvrOyyhlBSDmNX6qsIT970CzWYEfOMDBHxQjA31rS2uOvroYSp\nHsIxmMNim+u8PJ5apo1Ir/sHIvspA+SaL8Lp7NWUaFY43GS9cRAjMRG///EM\nejGmjPSMIs+fD7MyWjO5ZxTNW55B310rWx/lwJfkKAduDvYBqQSPO513lxXD\n8hhDW9qxmvMlLswiByZWx0dijjEsQm1C4eBUi8Hxu/FaegSiMcI7fHTdoT6l\nzvj3VaLKvt8yCr5nIV+3eGYRtuChre1RbM3rCD/ObmgFQvtb41kR9TH3vafT\no3rulUOGkqd6rM8+vG7ibg/fjxf3ql2IDi8Wi9CHHFIgUUb8A67vuwZb0zFo\nK82ysOZXkIM129/OoWMHI4rSNtVGCw4efXbO4YvhZux6ExJeTuTIjTpwEGRX\n4Qaaezicq6RTDxrvlQBBr+81Bx2/SqguPbWL/Q6kmiezlx2qXgtJUoFvDpn8\ntw156dSpVMXusMdYyeKjj7/NiPXxN+uyg4FOKF5zEw+EZuqwU3ezeFc2ZdZi\nJY4z\r\n=VvVd\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIG3Ua15KKd2Bx/qo+W6rlzw0o+dkET7hLfvv2lyNpTuQAiARVv0kr0GIjxEqBxlzm9z3ZvOA4Z733GDBIxbg+QSY3A=="}],"size":5453,"noattachment":false,"key":"/stream-demux/-/stream-demux-8.0.0.tgz"},"maintainers":[{"name":"topcloudsystems","email":"grosjona@yahoo.com.au"}],"_npmUser":{"name":"topcloudsystems","email":"grosjona@yahoo.com.au"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/stream-demux_8.0.0_1581098573062_0.0579207193538025"},"_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":1581098573153,"_cnpm_publish_time":1581098573153,"dist-tags":{"latest":"10.2.1"}}