Last active
August 6, 2018 20:17
-
-
Save carlosdelfino/929d7918e3d3a6172fdd47a59d25b150 to your computer and use it in GitHub Desktop.
Um sistema que permite manter as propriedades atualizadas conforme alterações de registros no banco de dados
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const BaseManager = require('./base-manager'); | |
const Config = require('../models/config'); | |
let count = 0; | |
function checkProp(key) { | |
return typeof key === 'symbol' || | |
(typeof key === 'object' && Object.prototype.toString.call(key) === '[object Symbol]') || ['model', 'isNew', 'errors'].indexOf(key.toString()) > -1 || | |
!isNaN(key) || | |
/^\$/.test(key) || | |
/^\_/.test(key); | |
} | |
function criarProxy(parametro, valor) { | |
if (process.env.DEBUG_DETAIL > 4) console.log("Antes do for") | |
Object.entries(valor).forEach(([key, value]) => { | |
if (count > 4) return; | |
if (checkProp(key)) return; | |
if (process.env.DEBUG_DETAIL > 4) console.log(key + ' ' + value); // "a 5", "b 7", "c 9" | |
count++; | |
valor[key] = criarProxy(key, value); | |
count-- | |
}); | |
if (process.env.DEBUG_DETAIL > 4) console.log("Depois do for"); | |
if (process.env.DEBUG_DETAIL > 4) console.log(typeof valor); | |
if (typeof valor === 'object') | |
return new Proxy(valor, { | |
get: function (target, property, receiver) { | |
if (checkProp(property)) return; | |
if (process.env.DEBUG_DETAIL > 2) console.log("ConfigManager Acessando Propriedade de " + parametro); | |
if (process.env.DEBUG_DETAIL > 3) console.dir(target); | |
if (target[property]) return target[property]; | |
}, | |
set: null | |
}); | |
else | |
return valor; | |
} | |
let wait; | |
class ConfigManager extends BaseManager { | |
constructor() { | |
super(); | |
this.config = []; | |
let resolv; | |
let reject; | |
this.connection.once('open', () => { | |
let cursor = Config.find({}).cursor(); | |
cursor.on('data', (doc) => { | |
this.config[doc.parametro] = criarProxy(doc.parametro, doc.valor); | |
if (doc.watch) { | |
console.log("Preparando para Monitorar %s", doc.parametro); | |
function watch(configManager, doc) { | |
console.log("Monitorando parametro: %s a cada %d ms", doc.parametro, doc.watch); | |
if (doc.watch) setTimeout(() => { | |
Config.findOne({ | |
parametro: doc.parametro | |
}).then((doc) => { | |
console.dir(doc); | |
if (doc) { | |
if (doc.valor != configManager.config[doc.parametro]) { | |
console.log("Parametro monitorado: %s, foi alterado!", doc.parametro); | |
configManager.config[doc.parametro] = criarProxy(doc.parametro, doc.valor); | |
} else | |
console.log("Parametro monitorado %s, não foi alterado", doc.parametro); | |
watch(configManager, doc); | |
} else | |
console.log("Verifique o parametro: %s") | |
}) | |
}, | |
doc.watch * 1000) | |
} | |
watch(this, doc); | |
} | |
}); | |
cursor.on('close', () => { | |
if (process.env.DEBUG_DETAIL > 2) console.log("ConfigManager closed cursor data"); | |
resolv(); | |
}); | |
cursor.on('end', () => { | |
if (process.env.DEBUG_DETAIL > 2) console.log("ConfigManager end data"); | |
}); | |
}); | |
this.connection.on('error', () => { | |
reject(); | |
}) | |
wait = new Promise((r, j) => { | |
resolv = r; | |
reject = j; | |
}); | |
} | |
then(cb) { | |
wait.then(cb); | |
} | |
catch (cb) { | |
wait.then(cb); | |
} | |
} | |
var manager = new Proxy(new ConfigManager, { | |
get: function (target, property, receiver) { | |
if (target[property]) return target[property]; | |
var config = target.config[property]; | |
if (!config) console.log("Propriedade não encontrada: " + property); | |
if (process.env.DEBUG_DETAIL > 1) console.log("ConfigManager Acessando Propriedade raiz: " + property); | |
return config; | |
} | |
}); | |
module.exports = manager; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment