main: use deepCopy instead of JSON trick

This commit is contained in:
jean-emmanuel 2018-08-26 18:57:46 +02:00
parent 769b332436
commit 9eae6e3c91
2 changed files with 28 additions and 3 deletions

View file

@ -3,6 +3,7 @@ var path = require('path'),
settings = require('./settings'),
osc = require('./osc'),
{ipc} = require('./server'),
{deepCopy} = require('./utils'),
theme = require('./theme'),
chokidar = require('chokidar')
@ -103,7 +104,7 @@ module.exports = {
var watchFile = ()=>{
var watcher = chokidar.watch(data.path)
watcher.on('change',()=>{
var openedSessionsClone = JSON.parse(JSON.stringify(openedSessions[data.path]))
var openedSessionsClone = deepCopy(openedSessions[data.path])
for (var k in openedSessionsClone) {
if (openedSessionsClone[k] != lastSavingClient) {
module.exports.sessionOpen({path:data.path}, openedSessionsClone[k])
@ -166,7 +167,7 @@ module.exports = {
var value = shortdata.v,
data = widgetHashTable[clientId][shortdata.h]
data = JSON.parse(JSON.stringify(data))
data = deepCopy(data)
for (var k in shortdata) {
data[k] = shortdata[k]
}
@ -185,7 +186,7 @@ module.exports = {
var value = shortdata.v,
data = widgetHashTable[clientId][shortdata.h]
data = JSON.parse(JSON.stringify(data))
data = deepCopy(data)
for (var k in shortdata) {
data[k] = shortdata[k]
}

24
app/main/utils.js Normal file
View file

@ -0,0 +1,24 @@
module.exports = {
deepCopy: function(obj, precision) {
var copy = obj
if (obj === null) {
return obj
}
if (typeof obj === 'object') {
copy = Array.isArray(obj) ? [] : {}
for (let key in obj) {
copy[key] = module.exports.deepCopy(obj[key], precision)
}
} else if (typeof obj == 'number') {
return precision === undefined ? copy : parseFloat(copy.toFixed(precision))
}
return copy
}
}