Modules (180)

ChangedDocumentTracker

Description

Defines a ChangedDocumentTracker class to monitor changes to files in the current project.

Dependencies

Classes

Constructor

ChangedDocumentTracker

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);
    }

Methods

Private

_addListener

    ChangedDocumentTracker.prototype._addListener = function (doc) {
        doc.on("change", this._onChange);
    };
Private

_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;
    };
Private

_onWindowFocus

    ChangedDocumentTracker.prototype._onWindowFocus = function (event, doc) {
        this._windowFocus = true;
    };
Private

_removeListener

    ChangedDocumentTracker.prototype._removeListener = function (doc) {
        doc.off("change", this._onChange);
    };

getChangedPaths

Get the set of changed paths since the last reset.

Returns: Array.<string>
Changed file paths
    ChangedDocumentTracker.prototype.getChangedPaths = function () {
        return $.makeArray(this._changedPaths);
    };

    module.exports = ChangedDocumentTracker;
});

isPathChanged

Check if a file path is dirty.

file non-nullable string
path
Returns: !boolean
Returns true if the file was dirtied since the last reset.
    ChangedDocumentTracker.prototype.isPathChanged = function (path) {
        return this._windowFocus || this._changedPaths[path];
    };

reset

Empty the set of dirty paths. Begin tracking new dirty documents.

    ChangedDocumentTracker.prototype.reset = function () {
        this._changedPaths = {};
        this._windowFocus = false;
    };