forked from adobe/brackets
-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */ | ||
/*global define, brackets */ | ||
|
||
define(function (require, exports, module) { | ||
"use strict"; | ||
|
||
/** | ||
* Adds automatic saving to all documents in the editor. A document | ||
* gets queued for saving when it is marked as dirty by the DocumentManager. | ||
* We then wait a certain number of ms until we trigger a save for the document. | ||
* You can change `WRITE_DELAY_MS` below to whatever number you think reasonable. | ||
*/ | ||
|
||
// Time in ms to wait after a dirtyFlagChange event before autosaving file | ||
var WRITE_DELAY_MS = 15 * 1000; | ||
|
||
var FILE_SAVE = brackets.getModule("command/Commands").FILE_SAVE, | ||
SaveCommand = brackets.getModule("command/CommandManager").get(FILE_SAVE), | ||
DocumentManager = brackets.getModule("document/DocumentManager"); | ||
|
||
// In progress saves | ||
var inProgress = {}; | ||
|
||
function scheduleSave(doc) { | ||
var path = doc.file.fullPath; | ||
|
||
// Skip subsequent attempts to save, if we're already waiting on a save | ||
if(inProgress[path]) { | ||
return; | ||
} | ||
|
||
doc.addRef(); | ||
inProgress[path] = true; | ||
|
||
function finish() { | ||
doc.releaseRef(); | ||
delete inProgress[path]; | ||
} | ||
|
||
setTimeout(function() { | ||
SaveCommand.execute({doc: doc}) | ||
.done(finish) | ||
.fail(function(err) { | ||
console.error('[Autosave] failed write for `' + path + '`', err); | ||
finish(); | ||
}); | ||
}, WRITE_DELAY_MS); | ||
} | ||
|
||
// When the editor's document is flagged as having changes, schedule a save | ||
DocumentManager.on("dirtyFlagChange", function(evt, doc) { | ||
scheduleSave(doc); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters