Created
December 1, 2016 18:00
-
-
Save joliss/d6068382cb60499fb82ff9fdebded911 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var helpers = require('broccoli-kitchen-sink-helpers') | |
var RSVP = require('rsvp') | |
module.exports = PollWatcher | |
RSVP.EventTarget.mixin(PollWatcher.prototype) | |
function PollWatcher(options) { | |
this.options = options || {} | |
if (this.options.interval == null) this.options.interval = 100 | |
this.treeHash = null | |
} | |
PollWatcher.prototype.watch = function(watchedPaths) { | |
this.watchedPaths = watchedPaths | |
this.intervalID = setInterval(this._check.bind(this), this.options.interval) | |
} | |
PollWatcher.prototype.quit = function() { | |
clearTimeout(this.intervalID) | |
} | |
PollWatcher.prototype._computeTreeHash = function() { | |
return helpers.hashStrings(this.watchedPaths.map(function(watchedPath) { | |
return helpers.hashTree(watchedPath) | |
})) | |
} | |
PollWatcher.prototype._check = function() { | |
try { | |
var newTreeHash = this._computeTreeHash() | |
// Check if anything changed or if we're running for the first time | |
if (newTreeHash !== this.treeHash) { | |
this.treeHash = newTreeHash | |
this.trigger('change') | |
} | |
} catch (err) { | |
// This can happen if one of the watchedPaths is missing | |
this.trigger('error', err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment