配置文件写锁
This commit is contained in:
parent
78f4ee4ad6
commit
4fbca892d2
@ -1,5 +1,6 @@
|
|||||||
function editor_config() {
|
function editor_config() {
|
||||||
this.address = "_server/config.json";
|
this.address = "_server/config.json";
|
||||||
|
this._isWriting = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
editor_config.prototype.load = function(callback) {
|
editor_config.prototype.load = function(callback) {
|
||||||
@ -27,8 +28,18 @@ editor_config.prototype.set = function(key, value, callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
editor_config.prototype.save = function(callback) {
|
editor_config.prototype.save = function(callback) {
|
||||||
fs.writeFile(this.address, JSON.stringify(this.config) ,'utf-8', function(e) {
|
// 读写锁防止写文件冲突
|
||||||
if (e) console.error("写入配置文件失败");
|
if (this._isWriting) return;
|
||||||
|
try {
|
||||||
|
this._isWriting = true;
|
||||||
|
fs.writeFile(this.address, JSON.stringify(this.config) ,'utf-8', function(e) {
|
||||||
|
this._isWriting = false;
|
||||||
|
if (e) console.error("写入配置文件失败");
|
||||||
|
if (callback instanceof Function) callback();
|
||||||
|
})
|
||||||
|
} catch (e) {
|
||||||
|
this._isWriting = false;
|
||||||
|
console.error(e);
|
||||||
if (callback instanceof Function) callback();
|
if (callback instanceof Function) callback();
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user