Skip to content

Commit

Permalink
conditional require of isomorphic-fetch (#33)
Browse files Browse the repository at this point in the history
* conditional require of isomorphic-fetch

* lint
  • Loading branch information
wheresrhys authored Dec 12, 2016
1 parent d8a21c4 commit b319a62
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 234 deletions.
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
machine:
node:
version: 4.5.0
version: 6.0.0
14 changes: 6 additions & 8 deletions src/client.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
/*global CustomElement */
'use strict';
class EventEmitter {

constructor(){
constructor () {
this.el = document.body;
}

emit(eventName, data){
this.el.dispatchEvent(new CustomElement(eventName, { detail: data }));
emit (eventName, data) {
this.el.dispatchEvent (new CustomEvent (eventName, { detail: data }));
}

on(eventName, fn){
this.el.addEventListener(eventName, fn, true);
on (eventName, fn) {
this.el.addEventListener (eventName, fn, true);
}
}

const Poller = require('./poller')(EventEmitter);
const Poller = require ('./poller')(EventEmitter);

// eager fetch not yet supported on client side
Poller.prototype.eagerFetch = fetch;
Expand Down
80 changes: 40 additions & 40 deletions src/poller.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
'use strict';
require('isomorphic-fetch');

require ('isomorphic-fetch');

module.exports = EventEmitter => {

return class Poller extends EventEmitter {

constructor(config){
super();
constructor (config) {
super ();
if (!config.url) {
throw new Error('ft-poller expects a url');
throw new Error ('ft-poller expects a url');
}

this.url = config.url;
Expand All @@ -19,8 +17,6 @@ module.exports = EventEmitter => {

this.options.headers = this.options.headers || {};

this._fetch = this.options.retry ? this.eagerFetch : fetch;

if (!this.options.headers['Content-Type'] && this.options.method === 'POST') {
this.options.headers['Content-Type'] = 'application/json';
}
Expand All @@ -34,72 +30,76 @@ module.exports = EventEmitter => {
};
this.poller = undefined;
if (config.autostart) {
this.start({initialRequest: true})
this.start ({initialRequest: true})
}
}

isRunning(){
isRunning () {
return !!this.poller;
}

stop(){
clearInterval(this.poller);
stop () {
clearInterval (this.poller);
this.poller = undefined;
return true;
}

start(opts){
start (opts) {
opts = opts || {};
let initialPromise;
if (!!this.isRunning()) {
throw new Error('Could not start job because the service is already running');
if (!!this.isRunning ()) {
throw new Error ('Could not start job because the service is already running');
}

if (opts.initialRequest) {
initialPromise = this.fetch();
initialPromise = this.fetch ();
}

this.poller = setInterval( () => {
this.fetch();
this.poller = setInterval (() => {
this.fetch ();
}, this.refreshInterval);

return opts.initialRequest ? initialPromise : Promise.resolve();
return opts.initialRequest ? initialPromise : Promise.resolve ();
}

retry(){
this.fetch();
clearInterval(this.poller);
this.poller = setInterval( () => {
this.fetch();
retry () {
this.fetch ();
clearInterval (this.poller);
this.poller = setInterval (() => {
this.fetch ();
}, this.refreshInterval);
}

fetch(){
const time = new Date();
return this._fetch(this.url, this.options)
.then( (response) => {
const latency = new Date() - time;
fetch () {
const time = new Date ();
// Note - don't do this in the constructor as it means any instrumentation applied to fetch
// later is discarded within pollers
const _fetch = this.options.retry ? this.eagerFetch : fetch;

return _fetch (this.url, this.options)
.then ((response) => {
const latency = new Date () - time;
if (response.status === 200) {
this.emit('ok', response, latency);
this.emit ('ok', response, latency);
} else {
throw new Error(`Fetching ${response.url} failed with a ${response.status}, ${response.statusText}`);
throw new Error (`Fetching ${response.url} failed with a ${response.status}, ${response.statusText}`);
}
if ((response.headers.get('content-type') || '').indexOf('json') > -1) {
return response.json();
if ((response.headers.get ('content-type') || '').indexOf ('json') > -1) {
return response.json ();
} else {
return response.text();
return response.text ();
}
})
.then((s) => {
this.data = this.parseData(s);
this.emit('data', this.data);
.then ((s) => {
this.data = this.parseData (s);
this.emit ('data', this.data);
})
.catch( (err) => {
this.emit('error', err);
.catch ((err) => {
this.emit ('error', err);
});
}

getData(){
getData () {
return this.data;
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
const EventEmitter = require('events').EventEmitter;
const Poller = require('./poller')(EventEmitter);
const EventEmitter = require ('events').EventEmitter;
const Poller = require ('./poller')(EventEmitter);

Poller.prototype.eagerFetch = require('n-eager-fetch');
Poller.prototype.eagerFetch = require ('n-eager-fetch');

module.exports = Poller;
Loading

0 comments on commit b319a62

Please sign in to comment.