Skip to content

Commit

Permalink
feat(polling): Added an optional ability to specify a polling interval
Browse files Browse the repository at this point in the history
getStream now accepts an optional second parameter that must be a positive number. If not specified,
it defaults to an 8s polling interval.
  • Loading branch information
mmmoli committed Oct 13, 2015
1 parent 07e3a8b commit 8c61827
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import {Observable} from 'rx-lite';
import fetchJsonp from 'fetch-jsonp';

const getStream = (collectionId) => {
const getStream = (collectionId, intervalSeconds = 8) => {

if (!collectionId) {
throw new Error('Please specify a collectionId');
}

const pollRate = 8 * 1000;
if (typeof(intervalSeconds) !== 'number') {
throw new Error(`Polling interval should be a positive integer. A ${typeof(intervalSeconds)} was provided.`);
}

if (intervalSeconds <= 0) {
throw new Error(`Polling interval should be a positive integer. ${intervalSeconds} was specified.`);
}

const pollRate = intervalSeconds * 1000;

const requestStream = Observable.just(`http://app.climb.social/api/v1/collections/${collectionId}`);

Expand Down
41 changes: 41 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,47 @@ describe('Climb.social library', () => {

});

it('should accept an optional polling interval value', () => {

expect(() => {
const stream = climb.getStream(collectionId, 0.001);
}).to.not.throw();

expect(() => {
const stream = climb.getStream(collectionId, 5);
}).to.not.throw();

expect(() => {
const stream = climb.getStream(collectionId, 50000);
}).to.not.throw();

});

it('should error with a negative polling value', () => {

expect(() => {
const stream = climb.getStream(collectionId, -200);
}).to.throw();

expect(() => {
const stream = climb.getStream(collectionId, 0);
}).to.throw();

expect(() => {
const stream = climb.getStream(collectionId, -0.00001);
}).to.throw();

});


it('should error if interval length is not a interval', () => {

expect(() => {
const stream = climb.getStream(collectionId, '8');
}).to.throw();

});

it('should return an object with a `subscribe()` method (an Observable)', () => {

const stream = climb.getStream(collectionId);
Expand Down

0 comments on commit 8c61827

Please sign in to comment.