Skip to content

Commit 9501d31

Browse files
committed
Prevent users from saving a document multiple times at once.
1 parent 8a6a669 commit 9501d31

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

src/xwiki/RTFrontend/ErrorBox/content.xwiki2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ We were unable to parse the server's response.
6666
= Save Error =
6767

6868
We were unable to save your document. Try again shortly.
69-
If this message is displayed everytime you try to save the document, you should consider to **disable the realtime engine** (uncheck the "Allow Realtime Collaboration" checkbox) and **save** again.
69+
If this message is displayed everytime you try to save the document, you should consider **disabling the realtime engine** (uncheck the "Allow Realtime Collaboration" checkbox) and **save** again.
7070

7171
)))
7272

@@ -75,7 +75,7 @@ If this message is displayed everytime you try to save the document, you should
7575
= Critical Save Error =
7676

7777
The realtime saver/merger script has encountered an unexpected error and **the save was not performed**.
78-
If this message is displayed everytime you try to save the document, you probably should **disable the realtime engine** (uncheck the "Allow Realtime Collaboration" checkbox) and **save** again.
78+
If this message is displayed everytime you try to save the document, you should consider **disabling the realtime engine** (uncheck the "Allow Realtime Collaboration" checkbox) and **save** again.
7979

8080
)))
8181

src/xwiki/RTFrontend/WebHome/attachments/saver.js

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ define([
3232
wasEditedLocally: false,
3333
receivedISAVE: false,
3434
shouldRedirect: false,
35-
isavedSignature: ''
35+
isavedSignature: '',
36+
mergeMessage: function () {}
3637
};
3738

3839
var isSaving = false;
@@ -159,6 +160,7 @@ define([
159160
},
160161
data: stats,
161162
error: function (err) {
163+
isSaving = false;
162164
cb(err, null);
163165
}
164166
});
@@ -242,11 +244,9 @@ define([
242244

243245
var ISAVED = 1;
244246
// sends an ISAVED message
245-
var saveMessage=function (wc, channel, version, hash) {
247+
var saveMessage = function (wc, channel, version, hash) {
246248
if (!wc) { return; }
247-
debug("saved document"); // RT_event-on_save
248249
// show(saved(version))
249-
lastSaved.mergeMessage('saved', [version]);
250250
var msg = [ISAVED, mainConfig.userName, version, hash, mainConfig.editorType, mainConfig.editorName];
251251
wc.bcast(JSON.stringify(msg)).then(function() {
252252
// Send the message back to Chainpad once it is sent to the recipients.
@@ -328,16 +328,11 @@ define([
328328

329329
mergedContent = merge.content;
330330
if (mergedContent === lastSaved.content) {
331-
debug("Merging didn't result in a change.");
332-
/* FIXME merge on load isn't working
333-
if (force) {
334-
debug("Force option was passed, merging anyway.");
335-
} else { */
336-
// don't dead end, but indicate that you shouldn't save.
337-
andThen("Merging didn't result in a change.", false);
338-
isSaving = false;
339-
return;
340-
// }
331+
// don't dead end, but indicate that you shouldn't save.
332+
andThen("Merging didn't result in a change.", false);
333+
setLocalEditFlag(false);
334+
isSaving = false;
335+
return;
341336
}
342337

343338
var continuation = function (callback) {
@@ -625,7 +620,8 @@ define([
625620
}
626621
};
627622

628-
var saveRoutine = function (andThen, force) {
623+
var resetIsSaving;
624+
var saveRoutine = function (andThen, force, autosave) {
629625
// if this is ever true in your save routine, complain and abort
630626
lastSaved.receivedISAVE = false;
631627

@@ -636,8 +632,22 @@ define([
636632
return;
637633
}
638634

635+
if (isSaving) {
636+
if (!autosave) { warn("You tried to save while the document is already being saved. "+
637+
"Aborting the new instruction."); }
638+
return;
639+
}
639640
isSaving = true;
640-
if (isSaving) { return; }
641+
642+
// Set a timeout to enable the save after 10sec if there was an unexpected error
643+
if (resetIsSaving) { clearTimeout(resetIsSaving); }
644+
resetIsSaving = window.setTimeout(function() {
645+
if (isSaving) {
646+
ErrorBox.show('save');
647+
warn("TIMEOUT: Save lock removed after 5sec. The previous save has failed and it is now possible to try again.");
648+
isSaving = false;
649+
}
650+
}, 5000);
641651

642652
if (mainConfig.mergeContent) {
643653
mergeRoutine(andThen);
@@ -703,10 +713,12 @@ define([
703713
ajaxVersion(function (e, out) {
704714
if (e) {
705715
// there was an error (probably ajax)
716+
isSaving = false;
706717
warn(e);
707718
ErrorBox.show('save');
708719
} else if (out.isNew) {
709720
// it didn't actually save?
721+
isSaving = false;
710722
ErrorBox.show('save');
711723
} else {
712724
lastSaved.onReceiveOwnIsave = function () {
@@ -718,6 +730,7 @@ define([
718730
debug("redirecting!");
719731
redirectToView();
720732
} else {
733+
isSaving = false;
721734
debug('createSaver.saveandcontinue.receivedOwnIsaved');
722735
}
723736
// clean up after yourself..
@@ -734,7 +747,6 @@ define([
734747
lastSaved.mergeMessage("saved", [out.version]);
735748
}, out);
736749
}
737-
isSaving = false;
738750
});
739751
return true;
740752
};

0 commit comments

Comments
 (0)