{"id":17440234,"url":"https://github.com/mtoygar/ember-poller","last_synced_at":"2025-04-19T07:48:32.766Z","repository":{"id":34245813,"uuid":"173374310","full_name":"mtoygar/ember-poller","owner":"mtoygar","description":"A poller service based on ember-concurrency","archived":false,"fork":false,"pushed_at":"2022-12-09T14:28:18.000Z","size":2573,"stargazers_count":16,"open_issues_count":15,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-29T06:51:38.604Z","etag":null,"topics":["ember","ember-concurrency","ember-poller","polling"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/mtoygar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-01T22:06:43.000Z","updated_at":"2024-05-30T03:01:53.000Z","dependencies_parsed_at":"2023-01-15T05:45:42.277Z","dependency_job_id":null,"html_url":"https://github.com/mtoygar/ember-poller","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtoygar%2Fember-poller","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtoygar%2Fember-poller/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtoygar%2Fember-poller/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtoygar%2Fember-poller/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mtoygar","download_url":"https://codeload.github.com/mtoygar/ember-poller/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248597075,"owners_count":21130806,"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":["ember","ember-concurrency","ember-poller","polling"],"created_at":"2024-10-17T14:07:49.479Z","updated_at":"2025-04-19T07:48:32.738Z","avatar_url":"https://github.com/mtoygar.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.com/mtoygar/ember-poller.svg?branch=master)](https://travis-ci.com/mtoygar/ember-poller)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nember-poller\n==============================================================================\n\nA polling addon for ember based on [ember-concurrency](https://github.com/machty/ember-concurrency)\n\nWhat does it offer?\n------------------------------------------------------------------------------\n* Easy and handy polling state management\n* Multiple and isolated polling support\n* Automatic polling destruction upon the destruction of the object that pollings live on\n* Cancellable on demand\n* A test helper to increase testability\n\nInstallation\n------------------------------------------------------------------------------\n\n```bash\nember install ember-poller\n```\n\nSample Usages\n------------------------------------------------------------------------------\n\nYou can initiate the poller using a ember-concurrency task.\n\n```javascript\nimport { reject } from 'rsvp';\nimport { task } from 'ember-concurrency';\n\npoller: service(),\n\n// Somewhere on the code call track method of the poller\nlet pollerUnit = this.get('poller').track({\n  pollingInterval: 1000,\n  retryLimit: 30,\n  pollTask: this.get('pollTask'),\n});\nthis.set('pollerUnit', pollerUnit);\n\n\npollTask: task(function*() {\n  let response = yield this.get('someModel').reload();\n  if (response.status == 'done') {\n    return true; // if your task succeeds return true, so that poller service understands the task is successfully completed\n  } else if (response == 'error') {\n    return reject(); // if you have an error case basically reject the promise\n  }\n  // if polling needs to continue basically do nothing.\n})\n```\n\nIf you don't use ember-concurrency on your project, you can also provide an async function as a polling method.\n\n```javascript\nimport { reject } from 'rsvp';\n\npoller: service(),\n\n// Somewhere on the code call track method of the poller\nlet pollerUnit = this.get('poller').track({\n  pollingInterval: 1000,\n  retryLimit: 30,\n  pollingFunction: () =\u003e this.pollingFunction(),\n});\nthis.set('pollerUnit', pollerUnit);\n\n\nasync pollingFunction() {\n  let response = await this.get('someModel').reload();\n  if (response.status == 'done') {\n    return true; // if your task succeeds return true, so that poller service understands the task is successfully completed\n  } else if (response == 'error') {\n    return reject(); // if you have an error case basically reject the promise\n  }\n  // if polling needs to continue basically do nothing.\n}\n```\n\nArguments other than option parameter will be passed directly to your pollingTask or pollingFunction.\n\n```javascript\nimport { reject } from 'rsvp';\nimport { task } from 'ember-concurrency';\n\npoller: service(),\n\n// Somewhere on the code call track method of the poller\nlet pollerUnit = this.get('poller').track({\n  pollingInterval: 1000,\n  retryLimit: 30,\n  pollTask: this.get('pollTask'),\n}, 17, 89);\nthis.set('pollerUnit', pollerUnit);\n\n\npollTask: task(function*(min, max) {\n  let response = yield this.get('someModel').reload();\n  console.log(min); // 17\n  console.log(max); // 89\n  if (response == 'error') {\n    return reject(); // if you have an error case basically reject the promise\n  } else if (response.get('anAttribute') \u003e min \u0026\u0026 response.get('anAttribute') \u003c max) {\n    return true; // if your task succeeds return true, so that poller service understands the task is successfully completed\n  }\n  // if polling needs to continue basically do nothing.\n})\n```\n\nYou can also cancel polling using the `abort` method.\n\n```javascript\nlet pollerUnit = this.get('poller').track({ pollTask: this.get('pollTask') });\npollerUnit.abort(); // cancels the polling\npollerUnit.isCancelled; // returns true.\n```\n\nYou can track the state of the polling with the attributes of PollerUnit.\n\n```javascript\npollerUnit.get('isError'); // true if polling is failed(an exception throwed or promise rejected), false otherwise.\npollerUnit.get('isFailed'); // alias of isError\npollerUnit.get('isSuccessful'); // true if polling is succeeded(a `truthy` value is returned), false otherwise.\npollerUnit.get('isRunning'); // true if polling is running, meaning it is not failed, succeeded, canceled or timed out.\npollerUnit.get('isCanceled'); // true if polling is canceled using [abort()](#abort) method.\npollerUnit.get('isCancelled'); // alias of isCanceled\npollerUnit.get('isTimeout'); // true if polling terminates without success, failure and cancellation.\npollerUnit.get('retryCount'); // returns the number of pollings made since polling started.\n````\n\nFor further reference, you may look [API](https://github.com/mtoygar/ember-poller/blob/master/API.md) docs.\n\nTesting\n------------------------------------------------------------------------------\nYou can stub track method in your tests in your acceptance and integration tests.\n```javascript\nlet pollerService = this.owner.lookup('service:poller');\nthis.stub(pollerService, 'track').returns({ isRunning: true }); // sinon implementation\n```\n\n#### Test Helper\nYou can also inject a stubbed poller to your tests and set the pollingInterval to zero. To test your success, error, timeout case all you need is to arrange your data/mocks as intended. An example can be found below.\n\n```javascript\nimport injectPoller from 'ember-poller/test-helpers/poller-stub';\n\ntest('it supports polling methods with arguments', async function(assert) {\n  assert.expect(5);\n\n  injectPoller(this);\n  // Arrange\n  // Act\n  // Assert\n});\n```\n\nOptionally, you can also pass `stubbedOptions` to `injectPoller`. This will override your parameters specified in your code.\n```javascript\nimport injectPoller from 'ember-poller/test-helpers/poller-stub';\n\ntest('it supports polling methods with arguments', async function(assert) {\n  assert.expect(5);\n\n  injectPoller(this, {\n    pollingInterval: 10,\n    retryLimit: 5,\n  });\n  // Arrange\n  // Act\n  // Assert\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmtoygar%2Fember-poller","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmtoygar%2Fember-poller","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmtoygar%2Fember-poller/lists"}