data-updater
Advanced tools
Comparing version 1.4.0 to 2.0.0
@@ -1,5 +0,7 @@ | ||
var DataUpdater = require('../lib/DataUpdater.js'); | ||
var dataUpdater = require('../index.js'); | ||
var updater = new DataUpdater('test-data.json', 5000); | ||
// var updater = dataUpdater.fromFile('test-data.json', 5000); | ||
var updater = dataUpdater.fromUrl('http://127.0.0.1:1488/test-data.json', 5000); | ||
updater.on('first-update', function(err) { | ||
@@ -6,0 +8,0 @@ console.log('first-update', err); |
22
index.js
@@ -1,1 +0,21 @@ | ||
module.exports = require('./lib/DataUpdater.js'); | ||
var debug = require('debug'); | ||
var DataUpdater = require('./lib/DataUpdater.js'); | ||
var FileDriver = require('./lib/FileDriver.js'); | ||
var UrlDriver = require('./lib/UrlDriver.js'); | ||
module.exports = { | ||
_debug: debug('data-updater'), | ||
fromFile: function(file, interval) { | ||
this._debug('fromFile'); | ||
return new DataUpdater(FileDriver, file, interval); | ||
}, | ||
fromUrl: function(url, interval) { | ||
this._debug('fromUrl'); | ||
return new DataUpdater(UrlDriver, url, interval); | ||
} | ||
}; |
var _ = require('lodash'); | ||
var debug = require('debug'); | ||
var events = require('events'); | ||
var fs = require('fs'); | ||
var util = require('util'); | ||
var EventEmitter = events.EventEmitter; | ||
function DataUpdater(dataFile, interval) { | ||
function DataUpdater(driverConstructor, driverOptions, interval) { | ||
EventEmitter.call(this); | ||
this._debug = debug('DataUpdater'); | ||
this._clearTimeout = clearTimeout; | ||
this._driverConstructor = driverConstructor; | ||
this._driverOptions = driverOptions; | ||
this._driver = null; | ||
this._unref = false; | ||
this._stopped = false; | ||
this._firstUpdate = true; | ||
this._lastCtime = 0; | ||
this._timeoutRef = null; | ||
this._updating = false; | ||
var onReadFile = _.bind(this._onReadFile, this); | ||
this._readFile = _.bind(fs.readFile, fs, dataFile, onReadFile); | ||
var onStat = _.bind(this._onStat, this); | ||
this._stat = _.bind(fs.stat, fs, dataFile, onStat); | ||
var update = _.bind(this._update, this); | ||
@@ -27,8 +22,12 @@ this._updateDelayed = _.partial(setTimeout, update, interval); | ||
util.inherits(DataUpdater, EventEmitter); | ||
var p = Object.create(EventEmitter.prototype); | ||
var p = DataUpdater.prototype; | ||
DataUpdater.prototype = p; | ||
p.start = function() { | ||
p.start = function(cb) { | ||
this._debug('start'); | ||
this._driver = new this._driverConstructor(this._driverOptions, this); | ||
if (cb !== undefined) { | ||
this.once('first-update', cb); | ||
} | ||
this._update(); | ||
@@ -77,48 +76,23 @@ }; | ||
this._updating = true; | ||
this._checkStat(); | ||
this._driver._update(); | ||
}; | ||
p._checkStat = function(cb) { | ||
this._debug('_checkStat'); | ||
this._stat(); | ||
p._emitError = function(err) { | ||
this._debug('_emitError'); | ||
this._updating = false; | ||
this._mayBeEmitFirstUpdate(err); | ||
this.emit('error', err); | ||
this._scheduleUpdate(); | ||
}; | ||
p._onStat = function(err, stats) { | ||
this._debug('_onStat'); | ||
if (err) { | ||
this._updating = false; | ||
this._mayBeEmitFirstUpdate(err); | ||
this.emit('error', err); | ||
this._scheduleUpdate(); | ||
return; | ||
} | ||
var time = stats.ctime.getTime(); | ||
if (this._lastCtime === time) { | ||
this._updating = false; | ||
this.emit('old'); | ||
this._scheduleUpdate(); | ||
return; | ||
} | ||
this._lastCtime = time; | ||
this._readFile(); | ||
p._emitOld = function() { | ||
this._debug('_emitOld'); | ||
this._updating = false; | ||
this.emit('old'); | ||
this._scheduleUpdate(); | ||
}; | ||
p._onReadFile = function(err, dataAsJson) { | ||
this._debug('_onReadFile'); | ||
p._emitUpdate = function(data) { | ||
this._debug('_emitUpdate'); | ||
this._updating = false; | ||
if (err) { | ||
this._mayBeEmitFirstUpdate(err); | ||
this.emit('error', err); | ||
this._scheduleUpdate(); | ||
return; | ||
} | ||
var data = null; | ||
try { | ||
data = this._parse(dataAsJson); | ||
} catch (e) { | ||
this._mayBeEmitFirstUpdate(e); | ||
this.emit('error', e); | ||
this._scheduleUpdate(); | ||
return; | ||
} | ||
this._mayBeEmitFirstUpdate(); | ||
@@ -125,0 +99,0 @@ this.emit('update', data); |
{ | ||
"name": "data-updater", | ||
"version": "1.4.0", | ||
"version": "2.0.0", | ||
"description": "", | ||
@@ -13,3 +13,5 @@ "main": "index.js", | ||
"debug": "^2.1.1", | ||
"lodash": "^2.4.1" | ||
"diver": "^1.0.0", | ||
"lodash": "^2.4.1", | ||
"request": "^2.55.0" | ||
}, | ||
@@ -16,0 +18,0 @@ "devDependencies": { |
7715
10
268
4
+ Addeddiver@^1.0.0
+ Addedrequest@^2.55.0
+ Addedajv@6.12.6(transitive)
+ Addedasn1@0.2.6(transitive)
+ Addedassert-plus@1.0.0(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaws-sign2@0.7.0(transitive)
+ Addedaws4@1.13.2(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedcaseless@0.12.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addeddashdash@1.14.1(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addeddiver@1.1.1(transitive)
+ Addedecc-jsbn@0.1.2(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedextsprintf@1.3.0(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedforever-agent@0.6.1(transitive)
+ Addedform-data@2.3.3(transitive)
+ Addedgetpass@0.1.7(transitive)
+ Addedhar-schema@2.0.0(transitive)
+ Addedhar-validator@5.1.5(transitive)
+ Addedhttp-signature@1.2.0(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedjsbn@0.1.1(transitive)
+ Addedjson-schema@0.4.0(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addedjsprim@1.4.2(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedoauth-sign@0.9.0(transitive)
+ Addedperformance-now@2.1.0(transitive)
+ Addedpsl@1.15.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedqs@6.5.3(transitive)
+ Addedrequest@2.88.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsshpk@1.18.0(transitive)
+ Addedtough-cookie@2.5.0(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addedtweetnacl@0.14.5(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedverror@1.10.0(transitive)