Skip to content

Commit d8c47b8

Browse files
committed
chore: track lastChangeTimestamp for each Document to make sense of ongoing changes
1 parent ce3c917 commit d8c47b8

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

src/document/Document.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ define(function (require, exports, module) {
119119
*/
120120
Document.prototype.diskTimestamp = null;
121121

122+
/**
123+
* Keeps a running timestamp of when the document was last changed. You can use this timestamp to see a
124+
* document was recently changed or not.
125+
* @type {number}
126+
*/
127+
Document.prototype.lastChangeTimestamp = null;
128+
122129
/**
123130
* The timestamp of the document at the point where the user last said to keep changes that conflict
124131
* with the current disk version. Can also be -1, indicating that the file was deleted on disk at the
@@ -337,6 +344,7 @@ define(function (require, exports, module) {
337344
* @param {Object} changeList Changelist in CodeMirror format
338345
*/
339346
Document.prototype._notifyDocumentChange = function (changeList) {
347+
this.lastChangeTimestamp = Date.now();
340348
this.trigger("change", this, changeList);
341349
exports.trigger("documentChange", this, changeList);
342350
};
@@ -501,6 +509,7 @@ define(function (require, exports, module) {
501509
*/
502510
Document.prototype._updateTimestamp = function (timestamp) {
503511
this.diskTimestamp = timestamp;
512+
this.lastChangeTimestamp = Date.now();
504513
// Clear the "keep changes" timestamp since it's no longer relevant.
505514
this.keepChangesTime = null;
506515
};

src/document/DocumentManager.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ define(function (require, exports, module) {
9494
ProjectManager = require("project/ProjectManager"),
9595
Strings = require("strings");
9696

97+
const EVENT_AFTER_DOCUMENT_CREATE = "afterDocumentCreate",
98+
EVENT_PATH_DELETED = "pathDeleted",
99+
EVENT_FILE_NAME_CHANGE = "fileNameChange",
100+
EVENT_BEFORE_DOCUMENT_DELETE = "beforeDocumentDelete",
101+
EVENT_DOCUMENT_REFRESHED = "documentRefreshed",
102+
EVENT_DOCUMENT_CHANGE = "documentChange",
103+
EVENT_DIRTY_FLAG_CHANGED = "dirtyFlagChange";
104+
97105

98106
/**
99107
* @private
@@ -472,7 +480,7 @@ define(function (require, exports, module) {
472480
*/
473481
function notifyFileDeleted(file) {
474482
// Notify all editors to close as well
475-
exports.trigger("pathDeleted", file.fullPath);
483+
exports.trigger(EVENT_PATH_DELETED, file.fullPath);
476484

477485
var doc = getOpenDocumentForPath(file.fullPath);
478486

@@ -515,7 +523,7 @@ define(function (require, exports, module) {
515523
// For images not open in the workingset,
516524
// FileSyncManager.syncOpenDocuments() will
517525
// not tell us to close those views
518-
exports.trigger("pathDeleted", fullPath);
526+
exports.trigger(EVENT_PATH_DELETED, fullPath);
519527
}
520528
}
521529

@@ -536,7 +544,7 @@ define(function (require, exports, module) {
536544
});
537545

538546
// Send a "fileNameChange" event. This will trigger the views to update.
539-
exports.trigger("fileNameChange", oldName, newName);
547+
exports.trigger(EVENT_FILE_NAME_CHANGE, oldName, newName);
540548
}
541549

542550

@@ -578,25 +586,27 @@ define(function (require, exports, module) {
578586
}
579587

580588
_openDocuments[doc.file.id] = doc;
581-
exports.trigger("afterDocumentCreate", doc);
589+
exports.trigger(EVENT_AFTER_DOCUMENT_CREATE, doc);
582590
})
583591
.on("_beforeDocumentDelete", function (event, doc) {
584592
if (!_openDocuments[doc.file.id]) {
585593
console.error("Document with references was not in _openDocuments!");
586594
return true;
587595
}
588596

589-
exports.trigger("beforeDocumentDelete", doc);
597+
exports.trigger(EVENT_BEFORE_DOCUMENT_DELETE, doc);
590598
delete _openDocuments[doc.file.id];
591599
})
592600
.on("_documentRefreshed", function (event, doc) {
593-
exports.trigger("documentRefreshed", doc);
601+
exports.trigger(EVENT_DOCUMENT_REFRESHED, doc);
602+
}).on(EVENT_DOCUMENT_CHANGE, function (event, doc, changelist) {
603+
exports.trigger(EVENT_DOCUMENT_CHANGE, doc, changelist);
594604
})
595605
.on("_dirtyFlagChange", function (event, doc) {
596606
// Modules listening on the doc instance notified about dirtyflag change
597607
// To be used internally by Editor
598608
doc.trigger("_dirtyFlagChange", doc);
599-
exports.trigger("dirtyFlagChange", doc);
609+
exports.trigger(EVENT_DIRTY_FLAG_CHANGED, doc);
600610
if (doc.isDirty) {
601611
MainViewManager.addToWorkingSet(MainViewManager.ACTIVE_PANE, doc.file);
602612

test/spec/Document-test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ define(function (require, exports, module) {
5858
}
5959
});
6060

61-
it("should do a single edit, tracking a beforeEdit selection and preserving reversed flag", function () {
61+
function _verifySingleEdit() {
6262
var result = myDocument.doMultipleEdits([{edit: {text: "new content", start: {line: 2, ch: 0}, end: {line: 2, ch: 14}},
6363
selection: {start: {line: 2, ch: 4}, end: {line: 2, ch: 4}, reversed: true, isBeforeEdit: true}}]);
6464
initialContentLines[2] = "new content";
@@ -69,6 +69,17 @@ define(function (require, exports, module) {
6969
expect(result[0].end.line).toEqual(2);
7070
expect(result[0].end.ch).toEqual(11);
7171
expect(result[0].reversed).toBe(true);
72+
}
73+
74+
it("should do a single edit, tracking a beforeEdit selection and preserving reversed flag", function () {
75+
_verifySingleEdit();
76+
});
77+
78+
it("should edit update change lastChangeTimestamp", function () {
79+
let lastTimestamp = myDocument.lastChangeTimestamp;
80+
expect(typeof myDocument.lastChangeTimestamp).toBe('number');
81+
_verifySingleEdit();
82+
expect(myDocument.lastChangeTimestamp).not.toBe(lastTimestamp);
7283
});
7384

7485
it("should do a single edit, leaving a non-beforeEdit selection untouched and preserving reversed flag", function () {

0 commit comments

Comments
 (0)