1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-08-07 22:33:50 +00:00

Fix for autosave

Re-introduces the “tw-auto-save-wiki” message. The previous approach of
automatically triggering autosave whenever a tiddler changed meant that
changing configuration changes in control panel was triggering an
autosave. Using the explicit message gives us better control of the
situations in which we’ll autosave.

Now we solve the earlier problem of there being outstanding tiddler
change events at the time that we process the “tw-auto-save-wiki” by
deferring the autosave until the outstanding change event comes in.
This commit is contained in:
Jermolene 2014-08-29 09:58:30 +01:00
parent 48312272ad
commit 2952afe7af
5 changed files with 155 additions and 121 deletions

View File

@ -21,6 +21,7 @@ function SaverHandler(options) {
var self = this;
this.wiki = options.wiki;
this.dirtyTracking = options.dirtyTracking;
this.pendingAutoSave = false;
// Make a logger
this.logger = new $tw.utils.Logger("saver-handler");
// Initialise our savers
@ -43,12 +44,34 @@ function SaverHandler(options) {
});
self.numTasksInQueue += filteredChanges.length;
self.updateDirtyStatus();
// Do any autosave if one is pending and there's no more change events
if(self.pendingAutoSave && self.wiki.getSizeOfTiddlerEventQueue() === 0) {
// Check if we're dirty
if(self.numTasksInQueue > 0) {
self.saveWiki({method: "autosave"});
self.saveWiki({
method: "autosave",
downloadType: "text/plain"
});
}
self.pendingAutoSave = false;
}
});
// Listen for the autosave event
$tw.rootWidget.addEventListener("tw-auto-save-wiki",function(event) {
// Do the autosave unless there are outstanding tiddler change events
if(self.wiki.getSizeOfTiddlerEventQueue() === 0) {
// Check if we're dirty
if(self.numTasksInQueue > 0) {
self.saveWiki({
method: "autosave",
downloadType: "text/plain"
});
}
} else {
// Otherwise put ourselves in the "pending autosave" state and wait for the change event before we do the autosave
self.pendingAutoSave = true;
}
});
// Browser event handlers
if($tw.browser) {
// Set up our beforeunload handler
window.addEventListener("beforeunload",function(event) {
var confirmationMessage = undefined;
@ -59,7 +82,6 @@ function SaverHandler(options) {
return confirmationMessage;
});
}
}
// Install the save action handlers
if($tw.browser) {
$tw.rootWidget.addEventListener("tm-save-wiki",function(event) {

View File

@ -71,6 +71,8 @@ exports.repackPlugin = function(title,additionalTiddlers,excludeTiddlers) {
$tw.wiki.deleteTiddler(title);
}
});
// Trigger an autosave
$tw.rootWidget.dispatchEvent({type: "tw-auto-save-wiki"});
// Return a heartwarming confirmation
return "Plugin " + title + " successfully saved";
}

View File

@ -243,6 +243,8 @@ NavigatorWidget.prototype.handleDeleteTiddlerEvent = function(event) {
// Remove the closed tiddler from the story
this.removeTitleFromStory(storyList,title);
this.saveStoryList(storyList);
// Trigger an autosave
$tw.rootWidget.dispatchEvent({type: "tw-auto-save-wiki"});
return false;
};
@ -331,6 +333,8 @@ NavigatorWidget.prototype.handleSaveTiddlerEvent = function(event) {
if(draftTitle !== this.storyTitle) {
this.saveStoryList(storyList);
}
// Trigger an autosave
$tw.rootWidget.dispatchEvent({type: "tw-auto-save-wiki"});
}
}
}
@ -478,6 +482,8 @@ NavigatorWidget.prototype.handlePerformImportEvent = function(event) {
}));
// Navigate to the $:/Import tiddler
this.addToHistory([IMPORT_TITLE]);
// Trigger an autosave
$tw.rootWidget.dispatchEvent({type: "tw-auto-save-wiki"});
};
exports.navigator = NavigatorWidget;

View File

@ -152,6 +152,10 @@ exports.enqueueTiddlerEvent = function(title,isDeleted) {
}
};
exports.getSizeOfTiddlerEventQueue = function() {
return $tw.utils.count(this.changedTiddlers);
};
exports.clearTiddlerEventQueue = function() {
this.changedTiddlers = Object.create(null);
this.changeCount = Object.create(null)

View File

@ -1,3 +1,3 @@
title: $:/config/SaverFilter
[!is[shadow]] -[[$:/HistoryList]] -[[$:/StoryList]] -[[$:/Import]] -[[$:/isEncrypted]] -[[$:/UploadName]] -[prefix[$:/status]] -[prefix[$:/state]] -[prefix[$:/temp]] -[has[draft.of]]
[all[]] -[[$:/HistoryList]] -[[$:/StoryList]] -[[$:/Import]] -[[$:/isEncrypted]] -[[$:/UploadName]] -[prefix[$:/state]] -[prefix[$:/temp]] -[has[draft.of]]