All Projects → mtoygar → ember-poller

mtoygar / ember-poller

Licence: MIT license
A poller service based on ember-concurrency

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects
Handlebars
879 projects
CSS
56736 projects

Projects that are alternatives of or similar to ember-poller

ember-fastboot-app-tests
FastBoot testing support for Ember apps
Stars: ✭ 17 (+13.33%)
Mutual labels:  ember
ember-event-helpers
Complimentary event template helpers to the {{on}} modifier
Stars: ✭ 33 (+120%)
Mutual labels:  ember
ember-concurrency-retryable
An Ember addon that adds retry strategies and a task modifier for automatically retrying ember-concurrency tasks.
Stars: ✭ 24 (+60%)
Mutual labels:  ember-concurrency
ember-simple-infinite-scroller
📜 Simple infinite scroll component for Ember apps
Stars: ✭ 35 (+133.33%)
Mutual labels:  ember
ember-link
Link primitive to pass around self-contained route references. It's {{link-to}}, but better!
Stars: ✭ 50 (+233.33%)
Mutual labels:  ember
ember-useragent
An Ember addon for Fastboot-enabled UserAgent parsing via UAParser.js.
Stars: ✭ 34 (+126.67%)
Mutual labels:  ember
mcp3008.js
A node.js module for querying an mcp3008 analog/digital converter.
Stars: ✭ 24 (+60%)
Mutual labels:  polling
ember-cli-ifa
Ember CLI addon for injecting fingerprinted asset map file into Ember app
Stars: ✭ 54 (+260%)
Mutual labels:  ember
ember-cordova
CLI for Ember/Cordova/Crosswalk Applications
Stars: ✭ 16 (+6.67%)
Mutual labels:  ember
ember-link-action
Fire an action when LinkTo component transition happens
Stars: ✭ 86 (+473.33%)
Mutual labels:  ember
ember-popper
Popper.js for Ember
Stars: ✭ 38 (+153.33%)
Mutual labels:  ember
ember-foxy-forms
Ember Addon for Making Foxy Forms
Stars: ✭ 27 (+80%)
Mutual labels:  ember
Prometheus
🌈 A Clean And Modern Ghost Theme with Progressive Web Apps (PWA)
Stars: ✭ 94 (+526.67%)
Mutual labels:  ember
ember-get-config
Get `config/environment` from anywhere, even addons!!!
Stars: ✭ 63 (+320%)
Mutual labels:  ember
terraform-aws-frontend
Collection of Terraform modules for frontend app deployment on AWS.
Stars: ✭ 31 (+106.67%)
Mutual labels:  ember
ember-cli-g-maps
Deprecated Google Maps Addon
Stars: ✭ 58 (+286.67%)
Mutual labels:  ember
ember-cli-yadda
Write cucumber specs for ember-cli applications
Stars: ✭ 41 (+173.33%)
Mutual labels:  ember
ember-named-yields
Named Yields for Ember Components
Stars: ✭ 17 (+13.33%)
Mutual labels:  ember
website
Comunidade Brasileira Ember
Stars: ✭ 14 (-6.67%)
Mutual labels:  ember
ember-ref-bucket
This is list of handy ember primitives, created to simplify class-based dom workflow
Stars: ✭ 31 (+106.67%)
Mutual labels:  ember

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

ember install ember-poller

Sample Usages

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

import { reject } from 'rsvp';
import { task } from 'ember-concurrency';

poller: service(),

// Somewhere on the code call track method of the poller
let pollerUnit = this.get('poller').track({
  pollingInterval: 1000,
  retryLimit: 30,
  pollTask: this.get('pollTask'),
});
this.set('pollerUnit', pollerUnit);


pollTask: task(function*() {
  let response = yield this.get('someModel').reload();
  if (response.status == 'done') {
    return true; // if your task succeeds return true, so that poller service understands the task is successfully completed
  } else if (response == 'error') {
    return reject(); // if you have an error case basically reject the promise
  }
  // if polling needs to continue basically do nothing.
})

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

import { reject } from 'rsvp';

poller: service(),

// Somewhere on the code call track method of the poller
let pollerUnit = this.get('poller').track({
  pollingInterval: 1000,
  retryLimit: 30,
  pollingFunction: () => this.pollingFunction(),
});
this.set('pollerUnit', pollerUnit);


async pollingFunction() {
  let response = await this.get('someModel').reload();
  if (response.status == 'done') {
    return true; // if your task succeeds return true, so that poller service understands the task is successfully completed
  } else if (response == 'error') {
    return reject(); // if you have an error case basically reject the promise
  }
  // if polling needs to continue basically do nothing.
}

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

import { reject } from 'rsvp';
import { task } from 'ember-concurrency';

poller: service(),

// Somewhere on the code call track method of the poller
let pollerUnit = this.get('poller').track({
  pollingInterval: 1000,
  retryLimit: 30,
  pollTask: this.get('pollTask'),
}, 17, 89);
this.set('pollerUnit', pollerUnit);


pollTask: task(function*(min, max) {
  let response = yield this.get('someModel').reload();
  console.log(min); // 17
  console.log(max); // 89
  if (response == 'error') {
    return reject(); // if you have an error case basically reject the promise
  } else if (response.get('anAttribute') > min && response.get('anAttribute') < max) {
    return true; // if your task succeeds return true, so that poller service understands the task is successfully completed
  }
  // if polling needs to continue basically do nothing.
})

You can also cancel polling using the abort method.

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

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

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

For further reference, you may look API docs.

Testing

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

let pollerService = this.owner.lookup('service:poller');
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.

import injectPoller from 'ember-poller/test-helpers/poller-stub';

test('it supports polling methods with arguments', async function(assert) {
  assert.expect(5);

  injectPoller(this);
  // Arrange
  // Act
  // Assert
});

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

import injectPoller from 'ember-poller/test-helpers/poller-stub';

test('it supports polling methods with arguments', async function(assert) {
  assert.expect(5);

  injectPoller(this, {
    pollingInterval: 10,
    retryLimit: 5,
  });
  // Arrange
  // Act
  // Assert
});
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].