From 4fbca892d2010373d5ba28806c15aa33a159ac3c Mon Sep 17 00:00:00 2001 From: ckcz123 Date: Wed, 21 Oct 2020 11:43:11 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E5=86=99?= =?UTF-8?q?=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _server/editor_config.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/_server/editor_config.js b/_server/editor_config.js index 497c246e..188ecfde 100644 --- a/_server/editor_config.js +++ b/_server/editor_config.js @@ -1,5 +1,6 @@ function editor_config() { this.address = "_server/config.json"; + this._isWriting = false; } editor_config.prototype.load = function(callback) { @@ -27,8 +28,18 @@ editor_config.prototype.set = function(key, value, 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(); - }) + } }