From 310712567f8c17b8949173cabf072c221ccb611c Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 28 Dec 2017 12:40:11 -0500 Subject: [PATCH] move whisper to a module --- lib/contracts/code_generator.js | 1 - lib/core/engine.js | 12 ++--- .../modules/whisper/embarkjs.js | 0 lib/modules/whisper/index.js | 47 +++++++++++++++++++ 4 files changed, 51 insertions(+), 9 deletions(-) rename js/embarkjs/whisper.js => lib/modules/whisper/embarkjs.js (100%) create mode 100644 lib/modules/whisper/index.js diff --git a/lib/contracts/code_generator.js b/lib/contracts/code_generator.js index d8577b5f0..801a07ac0 100644 --- a/lib/contracts/code_generator.js +++ b/lib/contracts/code_generator.js @@ -294,7 +294,6 @@ class CodeGenerator { } } - code += "\n" + fs.readFileSync(fs.embarkPath('js/embarkjs/whisper.js')).toString(); //code += "\n" + fs.readFileSync(fs.embarkPath('js/embarkjs/orbit.js')).toString(); code += this.generateCommunicationInitialization(true); diff --git a/lib/core/engine.js b/lib/core/engine.js index 5a2927e95..7cbc199a3 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -200,14 +200,10 @@ class Engine { } }); - self.servicesMonitor.addCheck('Whisper', function (cb) { - self.web3.version.getWhisper(function (err, version) { - if (err) { - return cb({name: 'Whisper', status: 'off'}); - } else { - return cb({name: 'Whisper (version ' + version + ')', status: 'on'}); - } - }); + this.registerModule('whisper', { + addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor), + communicationConfig: this.config.communicationConfig, + web3: this.web3 }); } } diff --git a/js/embarkjs/whisper.js b/lib/modules/whisper/embarkjs.js similarity index 100% rename from js/embarkjs/whisper.js rename to lib/modules/whisper/embarkjs.js diff --git a/lib/modules/whisper/index.js b/lib/modules/whisper/index.js new file mode 100644 index 000000000..788e99821 --- /dev/null +++ b/lib/modules/whisper/index.js @@ -0,0 +1,47 @@ +let utils = require('../../utils/utils.js'); +let fs = require('../../core/fs.js'); + +class Whisper { + + constructor(embark, options) { + this.logger = embark.logger; + this.events = embark.events; + this.communicationConfig = options.communicationConfig; + this.addCheck = options.addCheck; + this.web3 = options.web3; + this.embark = embark; + + this.setServiceCheck(); + this.addWhisperToEmbarkJS(); + } + + setServiceCheck() { + const self = this; + self.addCheck('Whisper', function (cb) { + self.web3.version.getWhisper(function (err, version) { + if (err) { + return cb({name: 'Whisper', status: 'off'}); + } else { + return cb({name: 'Whisper (version ' + version + ')', status: 'on'}); + } + }); + }); + } + + addWhisperToEmbarkJS() { + if (this.communicationConfig === {}) { + return; + } + if(this.communicationConfig.provider !== 'whisper' || this.communicationConfig.enabled !== true) { + return; + } + + let code = ""; + code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString(); + code += "\nEmbarkJS.Storage.registerProvider('ipfs', __embarkWhisper);"; + + this.embark.addCodeToEmbarkJS(code); + } +} + +module.exports = Whisper;