{"name":"stream-demux","version":"7.0.1","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":"^1.0.0","writable-consumable-stream":"^1.1.1"},"gitHead":"e638f5b0517f35416b6f22ae96c298d058478810","_id":"stream-demux@7.0.1","_npmVersion":"5.6.0","_nodeVersion":"10.0.0","_npmUser":{"name":"topcloudsystems","email":"grosjona@yahoo.com.au"},"dist":{"integrity":"sha512-IEWUmiXyMPgAlCG+YhpZpG4S6PMhzYXZITWz0OrqXdzBGpV5vWYiLRc31CJhE9E95dIh4Slwuo0aG/RT/1OvJA==","shasum":"86e3b25d19173e11f84e4ef711a1675d3c09879a","tarball":"http://registry-npm.ywwl.com/stream-demux/download/stream-demux-7.0.1.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\nwsFcBAEBCAAQBQJceGsBCRA9TVsSAnZWagAA/DwQAJxXeEMp0wc9mmPkLLzu\nz0ea/AT+zNeGnf47r58tz+0lllVHKT0Dcb1ZLwPYpyuk1RObMJdvxOFEM6Yy\nWHrSCnUBd4HG8te+guI6ItM6+h7TsB0riPeIovzS1Yu+kz6C6cLMLAVohB1b\ngdxfFnNxnb0qJxyxpVVAGXhvrbPLRByhUNQHdceeG6ZPhH30MC6af4qTOzeu\n8ZVrQL+lu2wKB0hiw0V+valbfVb/N9KxO+qzHPdwMOr9zMlRUqgMKrRfkVSZ\nsfzj72eiAg3ppQLBRnZpk+bM+AM/bsHqtYxEGFvPbUApOwbsofu+tCgaQa8G\nbEl4bAz8Ee8eRcNsw6JZgSmXb2CeOT+HM16odBrlLPwyrr08mPzDsb6HXgqX\nve2Uu+nYDnX7SvvTNO5RDzao/FWoqnCcbX7dFmRLSFakrfqCXiP9rALMukLE\nEuDXuyVA49XI5UJzxFsuJ/DYNNiijcYy61yiAPUB+bGUaU/Z12IEvIOc1tUG\nvfKWuGWoDTnX9cB6i19eXN6un8P4r5BkM4/eBmE2wCIMRTH3A+vBtLa4TR6K\nlz0W40lHtIbpC/HvqycB/LsZ2AmUSiL2zk2o+G6BJ3dPyz9uPDyqvli+cUzD\nY8FETeqgWra0+aZDB99M8hXYO2SI0SWScdXJ8eTvS6zwitK4s3tgQL9iToJq\nw6I7\r\n=noNP\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCKzIO9OCYbC9YzKSjHv3YY0R3glslzpws+keUeHxYQKgIhAJq4GXmtMI/GFW5T1m2IGS+vYMxwL1j9POJXTHZE9zvr"}],"size":5496,"noattachment":false,"key":"/stream-demux/-/stream-demux-7.0.1.tgz"},"maintainers":[{"name":"topcloudsystems","email":"grosjona@yahoo.com.au"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/stream-demux_7.0.1_1551395584485_0.946329700609225"},"_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":1551395584623,"_cnpm_publish_time":1551395584623,"dist-tags":{"latest":"10.2.1"}}