问题描述
我的应用中有一个 save
函数,可以手动调用,还有一个 autosave
函数,每 60 秒运行一次.
I have a save
function in my app which can be called manually and an autosave
function which runs every 60 seconds.
为了防止两个操作试图同时访问同一个文件,我将一个名为 isSaving
的标志设置为 true
当一个开始运行时,并设置为 false
之后再次.如果 open
或 save
检测到 autosave
正在运行,它们会等待 1000 毫秒并重试.如果他们在那之后失败了,我认为这是一个错误.
To prevent the two ops trying to access the same file at the same instant, I set a flag called isSaving
to true
when one starts running, and to false
again afterward. If open
or save
detect that autosave
is running, they wait 1000ms and try again. If they fail after that I consider it an error.
自动保存:
setInterval(autosave, 1000 * 60);
isSaving = false;
function autosave()
{
return new WinJS.Promise(function (complete, error, progress)
{
if(isSaving == false) // no saving op in progress
{
// set saving flag on
isSaving = true;
// write file
return writeFile(currentFile)
.then(function () {
// saving flag off
isSaving = false;
complete();
});
}
else {
// silently abort
complete();
}
});
}
手动保存:
var saveFileAttempts = 0;
function save()
{
return new WinJS.Promise(function (complete, error, progress)
{
if (isSaving == false) // no saving op in progress
{
// set saving flag on
isSaving = true;
// write file
return writeFile(currentFile)
.then(function () {
// show notification to user "file saved"
return showSaveNotification()
})
.then(function () {
// set saving flag off
isSaving = false;
complete();
});
}
else if (saveFileAttempts < 10) {
// try again in 1000ms, up to 10 times
saveFileAttempts++;
setTimeout(function () { save(); }, 1000);
}
else{
error();
}
});
}
打开:
var openFileAttempts = 0;
function open()
{
return new WinJS.Promise(function (complete, error, progress)
{
if (isSaving == false)
{
return readFile()
.then(function (file) {
currentFile = file;
openFileAttempts = 0;
complete();
});
}
else if (openFileAttempts < 10) {
// try again in 1000ms, up to 10 times
openFileAttempts++;
setTimeout(function () { open(); }, 1000);
}
else{
error();
}
});
}
这感觉就像一个黑客.有没有更好的方法来实现我想要做的事情?
This feels like a hack. Is there a better way to achieve what I'm trying to do?
仅供参考:这些函数返回承诺,因为还有其他函数调用它们.
FYI: These functions return promises because there are other functions that call them.
推荐答案
与其等待 1000 毫秒再试一次,我建议您使用 promise 来表示保存正在进行以及何时结束.
Instead of waiting 1000ms and trying again, I'd recommend using a promise to represent that a save is ongoing and when it will end.
var saving = null;
setInterval(function() {
if (!saving) // ignore autosave when already triggered
save().then(showAutoSafeNotification);
}, 60e3);
function save() {
if (saving)
return saving.then(save); // queue
// else
var written = writeFile(currentFile);
saving = written.then(function() {
saving = null;
}, function() {
saving = null;
});
return written;
}
你可以用 open
做同样的事情(并且可能想要抽象出 written
部分),尽管我看不出它是如何干扰(自动)保存的.如果您担心在保存时读取已打开的文件,我会让文件系统处理并捕获错误.
You can do the same with open
(and might want to abstract the written
part out), although I fail to see how it interferes with an (auto)save. If you're concerned about reading the file that is already open while it is saved, I'd let the filesystem handle that and catch the error.
这篇关于避免在 js 中发生冲突的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!