{"name":"@n1ru4l/push-pull-async-iterable-iterator","version":"3.0.0","repository":{"type":"git","url":"git+https://github.com/n1ru4l/push-pull-async-iterable-iterator.git"},"author":{"name":"Laurin Quast","email":"laurinquast@googlemail.com","url":"https://github.com/n1ru4l"},"license":"MIT","engines":{"node":">=12"},"main":"index.js","module":"index.mjs","typings":"index.d.ts","typescript":{"definition":"index.d.ts"},"exports":{".":{"require":"./index.js","import":"./index.mjs"},"./*":{"require":"./*.js","import":"./*.mjs"}},"description":"[![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-%230074c1.svg)](http://www.typescriptlang.org/) [![npm version](https://img.shields.io/npm/v/@n1ru4l/push-pull-async-iterable-iterator)](https://www.npmjs.com/package/@n1ru4l/push-pull-async","bugs":{"url":"https://github.com/n1ru4l/push-pull-async-iterable-iterator/issues"},"homepage":"https://github.com/n1ru4l/push-pull-async-iterable-iterator#readme","_id":"@n1ru4l/push-pull-async-iterable-iterator@3.0.0","_nodeVersion":"14.17.1","_npmVersion":"6.14.13","dist":{"integrity":"sha512-gwoIwo/Dt1GOI+lbcG1G7IeRM2K+Fo0op3OGyFJ4tXUCf2a3Q8lUCm81aoevrXC0nu4gbAXeOWy7wWxjpSvZUw==","shasum":"22dc34094c2de5f21b9a798d0ffab16b45de0eb7","tarball":"http://registry-npm.ywwl.com/@n1ru4l/push-pull-async-iterable-iterator/download/@n1ru4l/push-pull-async-iterable-iterator-3.0.0.tgz","fileCount":23,"unpackedSize":43460,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg7Fe8CRA9TVsSAnZWagAAoq4P/2e4uQK2kPaqdzd12lfU\ncb3QWSs5+Gm6P95ySe2wlQdGCNhPkvmssQyF3MGUe1KdLR1yNQz5Y43cWrx2\nRnneHOBb3ZtTMQg/9DK2oXV1TKIXIlvvI0YlsvAWZf+k3xJ184GJpROMtYQN\nnMUhQlN8/AxAM7iBXk1xhm/w+TMB1eUXtGiUQmNWrNS56/3UNiC/3/kO8lEy\nuOr7FuoWe2j1UtsvWrDyF2Bu6KH6rjN7UTRyEiMXl18Lx0TqrXewou75HEl9\nTZQsF5YZV7ru8Y2PHXBlW5isjJAq2ou29VT3f6OqElN7iHN8FSzBJketes0j\nut1WPqjbYNGtuC3npu8W+ksiBaqq/yjvjbN4XtLxXNRgLlYoyI0i7CkZcGrF\nssTXqRFJtnYHACnOwW9HwYxaD4E6d4SJngZeb5Gy/3u4UIIkv8CcoSGVA/Jc\noI+Hiud2VEQ7IYcvddjc4WrRNUQGugBknMZuTX5yMI+f0fnrHjgY0t0Q5wwX\n3jxl4hrN0NIWnBvWHuutONLjuVYuWelW0fVN6gj017x+637/rtt++8GkZRSr\n0K+oDam/wRdrfOFZaZih7bnaAKnXe88pE6Hju/6S7Sj9x8Dsnm3oSiM8JjNM\nP+Q1KoZHdfZcBJ8zTAe4n2wzHp0qKZwylV+msRaOKsbaUuxN74Zi+5ODXD1c\n0hU/\r\n=oSGd\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCJqlgwTS5647IATgVeJA3WwNt6kyl217LsTzgEZDCcNwIhAN9ZdK4uWmYjMc8c0dzIgfGUjFhBsBTNdI4YbuIQ5pHO"}],"size":8345,"noattachment":false,"key":"/@n1ru4l/push-pull-async-iterable-iterator/-/@n1ru4l/push-pull-async-iterable-iterator-3.0.0.tgz"},"_npmUser":{"name":"n1ru4l","email":"laurinquast@googlemail.com"},"directories":{},"maintainers":[{"name":"n1ru4l","email":"laurinquast@googlemail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/push-pull-async-iterable-iterator_3.0.0_1626101692171_0.2859616048728555"},"_hasShrinkwrap":false,"readme":"# `@n1ru4l/push-pull-async-iterable-iterator`\n\n[![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-%230074c1.svg)](http://www.typescriptlang.org/)\n[![npm version](https://img.shields.io/npm/v/@n1ru4l/push-pull-async-iterable-iterator)](https://www.npmjs.com/package/@n1ru4l/push-pull-async-iterable-iterator)\n[![npm downloads](https://img.shields.io/npm/dm/@n1ru4l/push-pull-async-iterable-iterator)](https://www.npmjs.com/package/@n1ru4l/push-pull-async-iterable-iterator)\n[![Dependents](https://img.shields.io/librariesio/dependents/npm/@n1ru4l/push-pull-async-iterable-iterator)](https://www.npmjs.com/browse/depended/@n1ru4l/push-pull-async-iterable-iterator)\n[![Build Status](https://img.shields.io/github/workflow/status/n1ru4l/push-pull-async-iterable-iterator/CI)](https://github.com/n1ru4l/push-pull-async-iterable-iterator/actions)\n\nCreate an AsyncIterableIterator from anything (on any modern platform) while handling back-pressure!\n\n```bash\nyarn install -E @n1ru4l/push-pull-async-iterable-iterator\n```\n\n**Standalone Usage**\n\n```ts\nimport { makePushPullAsyncIterableIterator } from \"@n1ru4l/push-pull-async-iterable-iterator\";\n\nconst {\n  pushValue,\n  asyncIterableIterator\n} = makePushPullAsyncIterableIterator();\npushValue(1);\npushValue(2);\npushValue(3);\n\n// prints 1, 2, 3\nfor await (const value of asyncIterableIterator) {\n  console.log(value);\n}\n```\n\n**Check if something is an AsyncIterable**\n\n```ts\nimport { isAsyncIterable } from \"@n1ru4l/push-pull-async-iterable-iterator\";\n\nif (isAsyncIterable(something)) {\n  for await (const value of something) {\n    console.log(value);\n  }\n}\n```\n\n_Note:_ On Safari iOS [`Symbol.asyncIterator` is not available](https://caniuse.com/mdn-javascript_builtins_symbol_asynciterator), therefore all async iterators used must be build using [AsyncGenerators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of#Iterating_over_async_generators).\nIf a AsyncIterable that is NO AsyncGenerator is passed to `isAsyncIterable` on the Safari iOS environment, it will return the value `false`.\n\n**Wrap a Sink**\n\n```ts\nimport { makeAsyncIterableIteratorFromSink } from \"@n1ru4l/push-pull-async-iterable-iterator\";\n// let's use some GraphQL client :)\nimport { createClient } from \"graphql-ws/lib/use/ws\";\n\nconst client = createClient({\n  url: \"ws://localhost:3000/graphql\"\n});\n\nconst asyncIterableIterator = makeAsyncIterableIteratorFromSink(sink => {\n  const dispose = client.subscribe(\n    {\n      query: \"{ hello }\"\n    },\n    {\n      next: sink.next,\n      error: sink.error,\n      complete: sink.complete\n    }\n  );\n  return () => dispose();\n});\n\nfor await (const value of asyncIterableIterator) {\n  console.log(value);\n}\n```\n\n**Apply an AsyncIterableIterator to a sink**\n\n```tsx\nimport Observable from \"zen-observable\";\nimport {\n  makePushPullAsyncIterableIterator,\n  applyAsyncIterableIteratorToSink\n} from \"@n1ru4l/push-pull-async-iterable-iterator\";\n\nconst { asyncIterableIterator } = makePushPullAsyncIterableIterator();\n\nconst observable = new Observable(sink => {\n  const dispose = applyAsyncIterableIteratorToSink(asyncIterableIterator, sink);\n  // dispose will be called when the observable subscription got destroyed\n  // the dispose call will ensure that the async iterator is completed.\n  return () => dispose();\n});\n\nconst subscription = observable.subscribe({\n  next: console.log,\n  complete: () => console.log(\"done.\"),\n  error: () => console.log(\"error.\")\n});\n\nconst interval = setInterval(() => {\n  iterator.push(\"hi\");\n}, 1000);\n\nsetTimeout(() => {\n  subscription.unsubscribe();\n  clearInterval(interval);\n}, 5000);\n```\n\n**Put it all together**\n\n```tsx\nimport { Observable, RequestParameters, Variables } from \"relay-runtime\";\nimport { createClient } from \"graphql-ws/lib/use/ws\";\nimport {\n  makeAsyncIterableFromSink,\n  applyAsyncIterableIteratorToSink\n} from \"@n1ru4l/push-pull-async-iterable-iterator\";\nimport { createApplyLiveQueryPatch } from \"@n1ru4l/graphql-live-query-patch\";\n\nconst client = createClient({\n  url: \"ws://localhost:3000/graphql\"\n});\n\nexport const execute = (request: RequestParameters, variables: Variables) => {\n  if (!request.text) {\n    throw new Error(\"Missing document.\");\n  }\n  const query = request.text;\n\n  return Observable.create<GraphQLResponse>(sink => {\n    // Create our asyncIterator from a Sink\n    const executionResultIterator = makeAsyncIterableFromSink(wsSink => {\n      const dispose = client.subscribe({ query }, wsSink);\n      return () => dispose();\n    });\n\n    const applyLiveQueryPatch = createApplyLiveQueryPatch();\n\n    // apply some middleware to our asyncIterator\n    const compositeIterator = applyLiveQueryPatch(executionResultIterator);\n\n    // Apply our async iterable to the relay sink\n    // unfortunately relay cannot consume an async iterable right now.\n    const dispose = applyAsyncIterableIteratorToSink(compositeIterator, sink);\n    // dispose will be called by relay when the observable is disposed\n    // the dispose call will ensure that the async iterator is completed.\n    return () => dispose();\n  });\n};\n```\n\n## Operators\n\nThis package also ships a few utilities that make your life easier!\n\n### `map`\n\nMap a source\n\n```ts\nimport { map } from \"@n1ru4l/push-pull-async-iterable-iterator\";\n\nasync function* source() {\n  yield 1;\n  yield 2;\n  yield 3;\n}\n\nconst square = map((value: number): number => value * value);\n\nfor await (const value of square(source())) {\n  console.log(value);\n}\n// logs 1, 4, 9\n```\n\n### `filter`\n\nFilter a source\n\n```ts\nimport { filter } from \"@n1ru4l/push-pull-async-iterable-iterator\";\n\nasync function* source() {\n  yield 1;\n  yield 2;\n  yield 3;\n}\n\nconst biggerThan1 = filter((value: number): number => value > 1);\n\nfor await (const value of biggerThan1(source())) {\n  console.log(value);\n}\n// logs 2, 3\n```\n\n## Other helpers\n\n### `withHandlers`\n\nAttach a return and throw handler to a source.\n\n```ts\nimport { withReturn } from \"@n1ru4l/push-pull-async-iterable-iterator\";\n\nasync function* source() {\n  yield 1;\n  yield 2;\n  yield 3;\n}\n\nconst sourceInstance = source();\n\nconst newSourceWithHandlers = withHandlers(\n  sourceInstance,\n  () => sourceInstance.return(),\n  err => sourceInstance.throw(err)\n);\n\nfor await (const value of stream) {\n  // ...\n}\n```\n","publish_time":1626101692326,"_cnpm_publish_time":1626101692326,"dist-tags":{"latest":"3.2.0","next":"1.0.0-alpha.2"}}