Skip to content

Instantly share code, notes, and snippets.

@joliss
Created December 1, 2016 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joliss/d6068382cb60499fb82ff9fdebded911 to your computer and use it in GitHub Desktop.
Save joliss/d6068382cb60499fb82ff9fdebded911 to your computer and use it in GitHub Desktop.
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