{"name":"stream-demux","version":"4.0.4","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":"^3.0.1","writable-async-iterable-stream":"^4.0.1"},"gitHead":"9394af529e5acf6e84ffa282120829ab3c5e3b21","_id":"stream-demux@4.0.4","_npmVersion":"5.6.0","_nodeVersion":"10.0.0","_npmUser":{"name":"topcloudsystems","email":"grosjona@yahoo.com.au"},"dist":{"integrity":"sha512-aSB1jS+0tPCnijI9BqEXbV9xC0JqkXnO869JIU4bC2isYMZizUdWm9g7NV0U9D4yaZ+K+HYtgI9XxAebOpkZjw==","shasum":"099a57e9335070d54b363f80dd1103394a0e89bc","tarball":"http://registry-npm.ywwl.com/stream-demux/download/stream-demux-4.0.4.tgz","fileCount":6,"unpackedSize":15657,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJcDYeTCRA9TVsSAnZWagAAfGwP/jQ+bpOB3LN0z8ZN1UNq\nu6guVT1H7r5n7jzHAdyxHPkL82Ae4/nPN7KwDwHaB2/zZrdxnoYCTVag5upZ\nT7XX9NobazDCNsCbF9oLEJh4b0cQZ3DoOvqsjtF8SiZ1ZwVqp4+b5A6zCJTT\nMNs0xcCc4sBOKN2Me3kYoWC6EqyCrHc8BhcBFSPfsXDRBQ8dcpJI1gzaxXXC\nWBiijLaZHHnDcTMNE4q5VOsZ4ZEMqwABq0YhVfWEswhjAhG2X/YxKdUpyA8G\n1ezrFKeRUKpfIswSfDKRWsqBrsqqfv5oATaWEqPUjpO+1LruOp1X04++/j+M\ntkFuIxXSykXimBXaypmm/ZttrasOoPbYwgmNIzkOqJCWK/jnh2P8/91e2zht\nkHH+OAKYLE7tuE7L94d0MLB+5VRZCDuofZ4mZ7JojJW8HKCN2oEgFIwDm0Sq\nK8wzaEDx/fLUyLQkfrAyhAaelux/tom/z2dMe+gf6NXZi0qzVFhCxAk8bMeL\nriYWZG7cuDBUv+rR2pHUBNybCj+tmNaDQTrpEJ8O5dqbICPLgs+Eohe3cac7\n30LpETrq5GtZfurfiBQzZvZb3eqZLD5khdmf0uPZmCjaf7Hn8SDOMfWOJRPJ\nqTFlM1oI0NKjOQEHNJ9oOfvKdZCbmFwe+gOI30p5bgW0jG0hqZJGJGcvBWc8\n0VYE\r\n=XSv3\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIAbS+ryR+GZrpvPbwoRILhw9qJtN98YuVrxhdAydY53pAiAU6GInC3mhXstKeDe00yLTignm24t+w4uSlKxzZX7hZg=="}],"size":3954,"noattachment":false,"key":"/stream-demux/-/stream-demux-4.0.4.tgz"},"maintainers":[{"name":"topcloudsystems","email":"grosjona@yahoo.com.au"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/stream-demux_4.0.4_1544390546823_0.5442722708568624"},"_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":1544390546987,"_cnpm_publish_time":1544390546987,"dist-tags":{"latest":"10.2.1"}}