{"id":13448617,"url":"https://github.com/repeaterjs/repeater","last_synced_at":"2025-03-22T09:31:30.625Z","repository":{"id":34566302,"uuid":"180010496","full_name":"repeaterjs/repeater","owner":"repeaterjs","description":"The missing constructor for creating safe async iterators","archived":false,"fork":false,"pushed_at":"2024-05-08T19:59:16.000Z","size":2070,"stargazers_count":454,"open_issues_count":18,"forks_count":12,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-05-18T17:45:02.683Z","etag":null,"topics":["async-iterable","async-iterator","async-iterators","repeater"],"latest_commit_sha":null,"homepage":"https://repeater.js.org","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/repeaterjs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-04-07T19:04:37.000Z","updated_at":"2024-06-18T12:34:48.573Z","dependencies_parsed_at":"2024-01-26T13:08:26.320Z","dependency_job_id":"0e294262-a88b-4fc4-ad6d-4d46a9b1527f","html_url":"https://github.com/repeaterjs/repeater","commit_stats":{"total_commits":301,"total_committers":5,"mean_commits":60.2,"dds":"0.029900332225913595","last_synced_commit":"219a0c8faf2c2768d234ecfe8dd21d455a4a98fe"},"previous_names":["channeljs/channel"],"tags_count":40,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/repeaterjs%2Frepeater","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/repeaterjs%2Frepeater/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/repeaterjs%2Frepeater/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/repeaterjs%2Frepeater/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/repeaterjs","download_url":"https://codeload.github.com/repeaterjs/repeater/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244937751,"owners_count":20535124,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async-iterable","async-iterator","async-iterators","repeater"],"created_at":"2024-07-31T05:01:50.682Z","updated_at":"2025-03-22T09:31:29.951Z","avatar_url":"https://github.com/repeaterjs.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# Repeater.js\nThe missing constructor for creating safe async iterators.\n\nFor more information, visit [repeater.js.org](https://repeater.js.org).\n\n## Installation\n\nRepeater.js is available on [npm](https://www.npmjs.com/package/@repeaterjs/repeater) in the CommonJS and ESModule formats.\n\n`$ npm install @repeaterjs/repeater`\n\n`$ yarn add @repeaterjs/repeater`\n\n## Requirements\n\nThe core `@repeaterjs/repeater` module has no dependencies, but requires the following globals in order to work:\n- `Promise`\n- `WeakMap`\n- `Symbol`\n  - `Symbol.iterator`\n  - `Symbol.asyncIterator`\n\nIn addition, repeaters are most useful when used via `async/await` and `for await…of` syntax. You can transpile your code with babel or typescript to support enviroments which lack these features.\n\n## Examples\n\n\u003ch4 id=\"timestamps\"\u003eLogging timestamps with setInterval\u003c/h4\u003e\n\n```js\nimport { Repeater } from \"@repeaterjs/repeater\";\n\nconst timestamps = new Repeater(async (push, stop) =\u003e {\n  push(Date.now());\n  const interval = setInterval(() =\u003e push(Date.now()), 1000);\n  await stop;\n  clearInterval(interval);\n});\n\n(async function() {\n  let i = 0;\n  for await (const timestamp of timestamps) {\n    console.log(timestamp);\n    i++;\n    if (i \u003e= 10) {\n      console.log(\"ALL DONE!\");\n      break; // triggers clearInterval above\n    }\n  }\n})();\n```\n\n\u003ch4 id=\"websocket\"\u003eCreating a repeater from a websocket\u003c/h4\u003e\n\n```js\nimport { Repeater } from \"@repeaterjs/repeater\";\n\nconst socket = new WebSocket(\"ws://echo.websocket.org\");\nconst messages = new Repeater(async (push, stop) =\u003e {\n  socket.onmessage = (ev) =\u003e push(ev.data);\n  socket.onerror = () =\u003e stop(new Error(\"WebSocket error\"));\n  socket.onclose = () =\u003e stop();\n  await stop;\n  socket.close();\n});\n\n(async function() {\n  for await (const message of messages) {\n    console.log(message);\n    if (message === \"close\") {\n      console.log(\"Closing!\");\n      break; // closes the socket\n    }\n  }\n})();\n\nsocket.onopen = () =\u003e {\n  socket.send(\"hello\"); // \"hello\"\n  socket.send(\"world\"); // \"world\"\n  socket.send(\"close\"); // \"close\", \"Closing!\"\n};\n```\n\n\u003ch4 id=\"konami-code\"\u003eListening for the \u003ca href=\"https://en.wikipedia.org/wiki/Konami_Code\"\u003eKonami Code\u003c/a\u003e and canceling if \u003ckbd\u003eEscape\u003c/kbd\u003e is pressed\u003c/h4\u003e\n\n```js\nimport { Repeater } from \"@repeaterjs/repeater\";\n\nconst keys = new Repeater(async (push, stop) =\u003e {\n  const listener = (ev) =\u003e {\n    if (ev.key === \"Escape\") {\n      stop();\n    } else {\n      push(ev.key);\n    }\n  };\n  window.addEventListener(\"keyup\", listener);\n  await stop;\n  window.removeEventListener(\"keyup\", listener);\n});\n\nconst konami = [\"ArrowUp\", \"ArrowUp\", \"ArrowDown\", \"ArrowDown\", \"ArrowLeft\", \"ArrowRight\", \"ArrowLeft\", \"ArrowRight\", \"b\", \"a\"];\n\n(async function() {\n  let i = 0;\n  for await (const key of keys) {\n    if (key === konami[i]) {\n      i++;\n    } else {\n      i = 0;\n    }\n    if (i \u003e= konami.length) {\n      console.log(\"KONAMI!!!\");\n      break; // removes the keyup listener\n    }\n  }\n})();\n```\n\n\u003ch4 id=\"observables\"\u003eConverting an observable to an async iterator\u003c/h4\u003e\n\n```js\nimport { Subject } from \"rxjs\";\nimport { Repeater } from \"@repeaterjs/repeater\";\n\nconst observable = new Subject();\nconst repeater = new Repeater(async (push, stop) =\u003e {\n  const subscription = observable.subscribe({\n    next: (value) =\u003e push(value),\n    error: (err) =\u003e stop(err),\n    complete: () =\u003e stop(),\n  });\n  await stop;\n  subscription.unsubscribe();\n});\n\n(async function() {\n  try {\n    for await (const value of repeater) {\n      console.log(\"Value: \", value);\n    }\n  } catch (err) {\n    console.log(\"Error caught: \", err);\n  }\n})();\nobservable.next(1);\n// Value: 1\nobservable.next(2);\n// Value: 2\nobservable.error(new Error(\"Hello from observable\"));\n// Error caught: Error: Hello from observable\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frepeaterjs%2Frepeater","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frepeaterjs%2Frepeater","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frepeaterjs%2Frepeater/lists"}