-
Notifications
You must be signed in to change notification settings - Fork 216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass watchedNodes to Watcher/WatcherAdapter #403
Changes from all commits
e69e76c
6499bca
2cc64f6
5920132
037d403
e8aa89c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,28 +10,30 @@ const logger = require('heimdalljs-logger')('broccoli:watcher'); | |
// principle. | ||
|
||
module.exports = class Watcher extends EventEmitter { | ||
constructor(builder, options) { | ||
constructor(builder, watchedNodes = [], options = {}) { | ||
super(); | ||
this.options = options || {}; | ||
this.options = options; | ||
if (this.options.debounce == null) { | ||
this.options.debounce = 100; | ||
} | ||
this.builder = builder; | ||
this.watcherAdapter = | ||
this.options.watcherAdapter || new WatcherAdapter(this.options.saneOptions); | ||
this.options.watcherAdapter || | ||
new WatcherAdapter(watchedNodes || [], this.options.saneOptions); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we default |
||
|
||
this.currentBuild = null; | ||
this._rebuildScheduled = false; | ||
this._ready = false; | ||
this._quittingPromise = null; | ||
this._lifetimeDeferred = null; | ||
this._lifetime = null; | ||
} | ||
|
||
start() { | ||
if (this._lifetimeDeferred != null) { | ||
if (this._lifetime != null) { | ||
throw new Error('Watcher.prototype.start() must not be called more than once'); | ||
} | ||
|
||
let lifetime = (this._lifetimeDeferred = {}); | ||
let lifetime = (this._lifetime = {}); | ||
lifetime.promise = new Promise((resolve, reject) => { | ||
lifetime.resolve = resolve; | ||
lifetime.reject = reject; | ||
|
@@ -41,11 +43,7 @@ module.exports = class Watcher extends EventEmitter { | |
this.watcherAdapter.on('error', this._error.bind(this)); | ||
this.currentBuild = Promise.resolve() | ||
.then(() => { | ||
let watchedSourceNodes = this.builder.nodeWrappers.filter(nw => { | ||
return nw.nodeInfo.nodeType === 'source' && nw.nodeInfo.watched; | ||
}); | ||
|
||
return this.watcherAdapter.watch(watchedSourceNodes); | ||
return this.watcherAdapter.watch(); | ||
}) | ||
.then(() => { | ||
logger.debug('ready'); | ||
|
@@ -55,7 +53,7 @@ module.exports = class Watcher extends EventEmitter { | |
.catch(err => this._error(err)) | ||
.then(() => this.currentBuild); | ||
|
||
return this._lifetimeDeferred.promise; | ||
return this._lifetime.promise; | ||
} | ||
|
||
_change(event, filePath, root) { | ||
|
@@ -127,7 +125,7 @@ module.exports = class Watcher extends EventEmitter { | |
this.emit('error', err); | ||
return this._quit() | ||
.catch(() => {}) | ||
.then(() => this._lifetimeDeferred.reject(err)); | ||
.then(() => this._lifetime.reject(err)); | ||
} | ||
|
||
quit() { | ||
|
@@ -136,10 +134,14 @@ module.exports = class Watcher extends EventEmitter { | |
return this._quittingPromise; | ||
} | ||
|
||
return this._quit().then( | ||
() => this._lifetimeDeferred.resolve(), | ||
err => this._lifetimeDeferred.reject(err) | ||
); | ||
let quitting = this._quit(); | ||
|
||
if (this._lifetime) { | ||
this._lifetime.resolve(quitting); | ||
return this._lifetime.promise; | ||
} else { | ||
return quitting; | ||
} | ||
} | ||
|
||
_quit() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
const EventEmitter = require('events').EventEmitter; | ||
const sane = require('sane'); | ||
const logger = require('heimdalljs-logger')('broccoli:watcherAdapter'); | ||
const SourceNode = require('./wrappers/source-node'); | ||
|
||
function defaultFilterFunction(name) { | ||
return /^[^.]/.test(name); | ||
|
@@ -18,20 +19,29 @@ function bindFileEvent(adapter, watcher, node, event) { | |
} | ||
|
||
module.exports = class WatcherAdapter extends EventEmitter { | ||
constructor(options) { | ||
constructor(watchedNodes = [], options) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO, we should just require an arg here. This defaulting is a bit odd (e.g. it only supports |
||
super(); | ||
if (!Array.isArray(watchedNodes)) { | ||
throw new TypeError( | ||
`WatcherAdapter's first argument must be an array of SourceNodeWrapper nodes` | ||
); | ||
} | ||
for (const node of watchedNodes) { | ||
if (!(node instanceof SourceNode)) { | ||
throw new Error(`${node} is not a SourceNode`); | ||
} | ||
if (node.nodeInfo.watched !== true) { | ||
throw new Error(`'${node.nodeInfo.sourceDirectory}' is not watched`); | ||
} | ||
} | ||
this.watchedNodes = watchedNodes; | ||
this.options = options || {}; | ||
this.options.filter = this.options.filter || defaultFilterFunction; | ||
this.watchers = []; | ||
} | ||
|
||
watch(watchedSourceNodes) { | ||
if (!Array.isArray(watchedSourceNodes)) { | ||
throw new TypeError( | ||
`WatcherAdapter#watch's first argument must be an array of WatchedDir nodes` | ||
); | ||
} | ||
let watchers = watchedSourceNodes.map(node => { | ||
watch() { | ||
let watchers = this.watchedNodes.map(node => { | ||
const watchedPath = node.nodeInfo.sourceDirectory; | ||
const watcher = new sane(watchedPath, this.options); | ||
this.watchers.push(watcher); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the watched nodes are required, why do we default them?