{"name":"@n1ru4l/push-pull-async-iterable-iterator","version":"3.1.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.1.0","_nodeVersion":"14.18.1","_npmVersion":"6.14.15","dist":{"integrity":"sha512-K4scWxGhdQM0masHHy4gIQs2iGiLEXCrXttumknyPJqtdl4J179BjpibWSSQ1fxKdCcHgIlCTKXJU6cMM6D6Wg==","shasum":"be450c97d1c7cd6af1a992d53232704454345df9","tarball":"http://registry-npm.ywwl.com/@n1ru4l/push-pull-async-iterable-iterator/download/@n1ru4l/push-pull-async-iterable-iterator-3.1.0.tgz","fileCount":22,"unpackedSize":27108,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBOu6HWpq6aEYeQfZNcFrVgnh9GO40z4c2ZmA3LmeP2VAiAZ7tweIA/4VcC4UMflB9kQmpru4HtCJL5oChmp8k9t2Q=="}],"size":6516,"noattachment":false,"key":"/@n1ru4l/push-pull-async-iterable-iterator/-/@n1ru4l/push-pull-async-iterable-iterator-3.1.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.1.0_1635537015261_0.8691394220580875"},"_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":1635537015418,"_cnpm_publish_time":1635537015418,"dist-tags":{"latest":"3.2.0","next":"1.0.0-alpha.2"}}