Defines a ChangedDocumentTracker class to monitor changes to files in the current project.
Tracks "change" events on opened Documents. Used to monitor changes to documents in-memory and update caches. Assumes all documents have changed when the Brackets window loses and regains focus. Does not read timestamps of files on disk. Clients may optionally track file timestamps on disk independently.
function ChangedDocumentTracker() {
var self = this;
this._changedPaths = {};
this._windowFocus = true;
this._addListener = this._addListener.bind(this);
this._removeListener = this._removeListener.bind(this);
this._onChange = this._onChange.bind(this);
this._onWindowFocus = this._onWindowFocus.bind(this);
DocumentManager.on("afterDocumentCreate", function (event, doc) {
// Only track documents in the current project
if (ProjectManager.isWithinProject(doc.file.fullPath)) {
self._addListener(doc);
}
});
DocumentManager.on("beforeDocumentDelete", function (event, doc) {
// In case a document somehow remains loaded after its project
// has been closed, unconditionally attempt to remove the listener.
self._removeListener(doc);
});
$(window).focus(this._onWindowFocus);
}
ChangedDocumentTracker.prototype._addListener = function (doc) {
doc.on("change", this._onChange);
};
ChangedDocumentTracker.prototype._onChange = function (event, doc) {
// if it was already changed, and the client hasn't reset the tracker,
// then leave it changed.
this._changedPaths[doc.file.fullPath] = true;
};
ChangedDocumentTracker.prototype._onWindowFocus = function (event, doc) {
this._windowFocus = true;
};
ChangedDocumentTracker.prototype._removeListener = function (doc) {
doc.off("change", this._onChange);
};
Get the set of changed paths since the last reset.
ChangedDocumentTracker.prototype.getChangedPaths = function () {
return $.makeArray(this._changedPaths);
};
module.exports = ChangedDocumentTracker;
});