{"id":17561675,"url":"https://github.com/inspmoore/pollinator","last_synced_at":"2025-04-28T10:47:01.093Z","repository":{"id":37096294,"uuid":"332328258","full_name":"inspmoore/pollinator","owner":"inspmoore","description":"Lightweight js library to poll any function. Node and browser compatible.","archived":false,"fork":false,"pushed_at":"2025-04-06T06:21:30.000Z","size":361,"stargazers_count":12,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-12T20:17:57.163Z","etag":null,"topics":["javascript","js","polling"],"latest_commit_sha":null,"homepage":"","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/inspmoore.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-01-23T23:21:03.000Z","updated_at":"2025-04-12T05:27:04.000Z","dependencies_parsed_at":"2022-06-24T13:17:33.848Z","dependency_job_id":null,"html_url":"https://github.com/inspmoore/pollinator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inspmoore%2Fpollinator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inspmoore%2Fpollinator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inspmoore%2Fpollinator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inspmoore%2Fpollinator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/inspmoore","download_url":"https://codeload.github.com/inspmoore/pollinator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251298145,"owners_count":21566960,"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":["javascript","js","polling"],"created_at":"2024-10-21T12:07:14.928Z","updated_at":"2025-04-28T10:47:01.073Z","avatar_url":"https://github.com/inspmoore.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![npm bundle size (version)](https://img.shields.io/bundlephobia/minzip/pollinator/0.3.2?style=flat-square)\n![GitHub](https://img.shields.io/github/license/inspmoore/pollinator?style=flat-square)\n![GitHub Repo stars](https://img.shields.io/github/stars/inspmoore/pollinator?style=flat-square)\n![npm](https://img.shields.io/npm/v/pollinator?style=flat-square)\n![npm type definitions](https://img.shields.io/npm/types/pollinator?style=flat-square)\n\n![Pollinator](https://github.com/inspmoore/pollinator/raw/master/assets/pollinator_logo@2x.png)\n\n---\n\n🐝 Pollinator is a super lightweight library for lazy people to poll any function\n(API gateway anyone?). Supports retries, pausing, cancelling, emits events you\ncan subscribe to. It has a nice, natural API and works in Node and browsers.\n\n## Main features\n\n- simple API 🔨\n- start, pause and stop at any time 🎮\n- add event listeners to receive polling results and polling status changes 🎭\n- written in _TypeScript_ 👷‍♂️\n- cancel polling when certain conditions are met based on current or previous\n  results 🔚\n- supports sync and async functions 🎛\n- configurable delay between polling ⏲\n- configurable retry attempts in case of error 🔄\n- well tested, no unnecessary calls ⛰\n- no bs - does only one thing and gets the job done 👍🏻\n\n## Installation\n\nFor node just do your:\n\n```shell\nyarn add pollinator\n#or\nnpm install pollinator\n```\n\nFor the browsers add the CDN link:\n\n```html\n\u003c!-- either use unpkg.com --\u003e\n\u003cscript src=\"https://unpkg.com/pollinator@0.3.2/dist/index.umd.min.js\"\u003e\u003c/script\u003e\n\n\u003c!-- or use JSDelivr --\u003e\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/pollinator@0.3.2/dist/index.umd.min.js\"\u003e\u003c/script\u003e\n```\n\n## Usage\n\nHere's a minimal example\n\n```javascript\n// es6\nimport Pollinator from 'pollinator'\n\nfunction pollingFunction() {\n  return fetch('http://some-url.com/endpoint')\n}\n\n// configure your poller\nconst poller = new Pollinator(pollingFunction)\n// remember to start\npoller.start()\n```\n\nThis will poll `pollingFunction` indefinitely.\n\nTo get the return from the polling function you need subscribe to an event.\n\n```javascript\npoller.on(Pollinator.Event.POLL, handlePoll)\n\nfunction handlePoll(response, status) {\n  // do something with the response and current polling status\n  console.log('The response is:', response)\n  console.log('Current polling status is:', status)\n}\n```\n\nSay you want to stop polling when a specific response comes back from the\npolling function.\n\n```javascript\nconst poller = new Pollinator(pollingFunction, {\n  conditionFn: stopPollingOnCondition,\n})\n\nfunction stopPollingOnCondition(currentResponse, previousResponse) {\n  // return true if you wish to stop polling\n  if (currentResponse === 'Half a Bee' \u0026\u0026 previousResponse === 'Eric')\n    return true\n\n  // return false if you don't want to stop and keep on polling\n  return false\n}\n```\n\n## Reference\n\n### `Pollinator` constructor params\n\n|    Name    |        Type        |  Required  | Description                                        | Default                           |\n| :--------: | :----------------: | :--------: | :------------------------------------------------- | :-------------------------------- |\n| **pollFn** |     `function`     | `required` | A function you want to poll. Can be sync or async. |                                   |\n| **config** | `PollinatorConfig` | `optional` | Optional configuration. Check details below        | see `PollinatorConfig` type below |\n\n### `PollinatorConfig` type\n\n|        Name        |    Type    |  Required  | Description                                                                                                                                                                                                                                                              | Default       |\n| :----------------: | :--------: | :--------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------ |\n|  **polFnParams**   | `unknown`  | `optional` | Use this option to pass some parameters to the `pollFn`                                                                                                                                                                                                                  | `undefined`   |\n|  **conditionFn**   | `function` | `optional` | Use this function to stop polling. The function will be called with two parameters of current and previous response from the `pollFn`. Must return a `boolean` where `true` stops polling.\u003cbr\u003e`(current: unkown, previous: unkown) =\u003e bool`                              | `() =\u003e false` |\n|     **delay**      |  `number`  | `optional` | A value in milliseconds setting the timeout between consequent `pollFn` calls                                                                                                                                                                                            | `5000`        |\n| **failRetryCount** |  `number`  | `optional` | A value that indicates the number of attempts to call `pollFn` after catching an error. Zero means that polling will fail immediately after catching error. Any positive number means that Pollinator will try to poll that many times until it emits the `Event.ERROR`. | `3`           |\n\n### `Status` enum\n\n```javascript\n{\n  IDLE, POLLING, FINISHED\n}\n```\n\n### `Event` enum\n\n```javascript\nenum Event {\n  POLL = 'poll',\n  END = 'end',\n  ERROR = 'error',\n  STATUS_CHANGE = 'statusChange',\n}\n```\n\n### `Pollinator` instance methods and properties\n\n|    Name    |    Type    |                    Params                    | Description                                                  |\n| :--------: | :--------: | :------------------------------------------: | :----------------------------------------------------------- |\n| **start**  |  `method`  |                                              | Starts polling and changes `status` to `Status.POLLING`      |\n|  **stop**  |  `method`  |                                              | Stops polling and changes `status` to `Status.FINISHED`      |\n| **pause**  |  `method`  |                                              | Pauses polling and `status` changes to `Status.IDLE`         |\n|   **on**   |  `method`  | `(event: Event, listener: function) =\u003e void` | Registers an event listener for a given `Event` type.        |\n|  **off**   |  `method`  | `(event: Event, listener: function) =\u003e void` | Removes an event listener for a given `Event` type.          |\n| **status** | `property` |                                              | Get current status of your poller. Value is of type `Status` |\n\n## Building and contributing\n\nIf you want to contribute then please do. PRs are welcome. Before squashing any bug or adding a new feature please create an issue first. Also add a test case(s) before contributing any code.\n\nAfter cloning the repo do the usual:\n\n```shell\nyarn\n#or\nnpm i\n```\n\nTo run tests use this script:\n\n```shell\nyarn test\n```\n\nTo build run:\n\n```shell\nyarn build\n```\n\n## To do\n\n- max attempts - stop polling when a max number of attempts is achieved\n- current attempts number in the event data\n- timeout - configure max amount of time after which polling stops\n- total polling time in the event data\n\n## Spread the pollen 🌻\n\nIf you like this lib consider hitting that star ⭐️ button.\n\nYou can hit me up here\n\n![Twitter Follow](https://img.shields.io/twitter/follow/pirx__?label=Follow\u0026style=social)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finspmoore%2Fpollinator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finspmoore%2Fpollinator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finspmoore%2Fpollinator/lists"}