项目作者: mtoygar

项目描述 :
A poller service based on ember-concurrency
高级语言: JavaScript
项目地址: git://github.com/mtoygar/ember-poller.git
创建时间: 2019-03-01T22:06:43Z
项目社区:https://github.com/mtoygar/ember-poller

开源协议:MIT License

下载


Build Status
License: MIT

ember-poller

A polling addon for ember based on ember-concurrency

What does it offer?

  • Easy and handy polling state management
  • Multiple and isolated polling support
  • Automatic polling destruction upon the destruction of the object that pollings live on
  • Cancellable on demand
  • A test helper to increase testability

Installation

  1. ember install ember-poller

Sample Usages

You can initiate the poller using a ember-concurrency task.

  1. import { reject } from 'rsvp';
  2. import { task } from 'ember-concurrency';
  3. poller: service(),
  4. // Somewhere on the code call track method of the poller
  5. let pollerUnit = this.get('poller').track({
  6. pollingInterval: 1000,
  7. retryLimit: 30,
  8. pollTask: this.get('pollTask'),
  9. });
  10. this.set('pollerUnit', pollerUnit);
  11. pollTask: task(function*() {
  12. let response = yield this.get('someModel').reload();
  13. if (response.status == 'done') {
  14. return true; // if your task succeeds return true, so that poller service understands the task is successfully completed
  15. } else if (response == 'error') {
  16. return reject(); // if you have an error case basically reject the promise
  17. }
  18. // if polling needs to continue basically do nothing.
  19. })

If you don’t use ember-concurrency on your project, you can also provide an async function as a polling method.

  1. import { reject } from 'rsvp';
  2. poller: service(),
  3. // Somewhere on the code call track method of the poller
  4. let pollerUnit = this.get('poller').track({
  5. pollingInterval: 1000,
  6. retryLimit: 30,
  7. pollingFunction: () => this.pollingFunction(),
  8. });
  9. this.set('pollerUnit', pollerUnit);
  10. async pollingFunction() {
  11. let response = await this.get('someModel').reload();
  12. if (response.status == 'done') {
  13. return true; // if your task succeeds return true, so that poller service understands the task is successfully completed
  14. } else if (response == 'error') {
  15. return reject(); // if you have an error case basically reject the promise
  16. }
  17. // if polling needs to continue basically do nothing.
  18. }

Arguments other than option parameter will be passed directly to your pollingTask or pollingFunction.

  1. import { reject } from 'rsvp';
  2. import { task } from 'ember-concurrency';
  3. poller: service(),
  4. // Somewhere on the code call track method of the poller
  5. let pollerUnit = this.get('poller').track({
  6. pollingInterval: 1000,
  7. retryLimit: 30,
  8. pollTask: this.get('pollTask'),
  9. }, 17, 89);
  10. this.set('pollerUnit', pollerUnit);
  11. pollTask: task(function*(min, max) {
  12. let response = yield this.get('someModel').reload();
  13. console.log(min); // 17
  14. console.log(max); // 89
  15. if (response == 'error') {
  16. return reject(); // if you have an error case basically reject the promise
  17. } else if (response.get('anAttribute') > min && response.get('anAttribute') < max) {
  18. return true; // if your task succeeds return true, so that poller service understands the task is successfully completed
  19. }
  20. // if polling needs to continue basically do nothing.
  21. })

You can also cancel polling using the abort method.

  1. let pollerUnit = this.get('poller').track({ pollTask: this.get('pollTask') });
  2. pollerUnit.abort(); // cancels the polling
  3. pollerUnit.isCancelled; // returns true.

You can track the state of the polling with the attributes of PollerUnit.

  1. pollerUnit.get('isError'); // true if polling is failed(an exception throwed or promise rejected), false otherwise.
  2. pollerUnit.get('isFailed'); // alias of isError
  3. pollerUnit.get('isSuccessful'); // true if polling is succeeded(a `truthy` value is returned), false otherwise.
  4. pollerUnit.get('isRunning'); // true if polling is running, meaning it is not failed, succeeded, canceled or timed out.
  5. pollerUnit.get('isCanceled'); // true if polling is canceled using [abort()](#abort) method.
  6. pollerUnit.get('isCancelled'); // alias of isCanceled
  7. pollerUnit.get('isTimeout'); // true if polling terminates without success, failure and cancellation.
  8. pollerUnit.get('retryCount'); // returns the number of pollings made since polling started.
  9. `

For further reference, you may look API docs.

Testing

You can stub track method in your tests in your acceptance and integration tests.

  1. let pollerService = this.owner.lookup('service:poller');
  2. this.stub(pollerService, 'track').returns({ isRunning: true }); // sinon implementation

Test Helper

You 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.

  1. import injectPoller from 'ember-poller/test-helpers/poller-stub';
  2. test('it supports polling methods with arguments', async function(assert) {
  3. assert.expect(5);
  4. injectPoller(this);
  5. // Arrange
  6. // Act
  7. // Assert
  8. });

Optionally, you can also pass stubbedOptions to injectPoller. This will override your parameters specified in your code.

  1. import injectPoller from 'ember-poller/test-helpers/poller-stub';
  2. test('it supports polling methods with arguments', async function(assert) {
  3. assert.expect(5);
  4. injectPoller(this, {
  5. pollingInterval: 10,
  6. retryLimit: 5,
  7. });
  8. // Arrange
  9. // Act
  10. // Assert
  11. });