From 97be8b7463fcd746cd21f89632012d49ca63446d Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 8 Jul 2018 21:14:44 +0300 Subject: [PATCH] refactor storage config check --- lib/modules/ipfs/index.js | 25 +++++++++---------------- lib/modules/swarm/index.js | 21 +++++++-------------- 2 files changed, 16 insertions(+), 30 deletions(-) diff --git a/lib/modules/ipfs/index.js b/lib/modules/ipfs/index.js index b7e0d0a5d..c2edc9fb8 100644 --- a/lib/modules/ipfs/index.js +++ b/lib/modules/ipfs/index.js @@ -2,7 +2,6 @@ const UploadIPFS = require('./upload.js'); const utils = require('../../utils/utils.js'); const fs = require('../../core/fs.js'); const IpfsApi = require('ipfs-api'); -const _ = require('underscore'); const StorageProcessesLauncher = require('../../processes/storageProcesses/storageProcessesLauncher'); class IPFS { @@ -35,14 +34,8 @@ class IPFS { setServiceCheck() { let self = this; - let storageConfig = this.storageConfig; - - if (!storageConfig.enabled) { - return; - } - - if (_.findWhere(this.storageConfig.dappConnection, {'provider': 'ipfs'}) === undefined && (storageConfig.upload.provider !== 'ipfs' || storageConfig.available_providers.indexOf("ipfs") < 0)) { - return; + if (!this.isIpfsEnabledInTheConfig()) { + return } self.events.on('check:backOnline:IPFS', function () { @@ -96,13 +89,8 @@ class IPFS { addProviderToEmbarkJS() { const self = this; - // TODO: make this a shouldAdd condition - if (this.storageConfig === {}) { - return; - } - - if (this.storageConfig.available_providers.indexOf('ipfs') < 0 || _.findWhere(this.storageConfig.dappConnection, {'provider': 'ipfs'}) === undefined || this.storageConfig.enabled !== true) { - return; + if (!this.isIpfsEnabledInTheConfig()) { + return } self.events.request("version:get:ipfs-api", function(ipfsApiVersion) { @@ -152,6 +140,11 @@ class IPFS { }); } + isIpfsEnabledInTheConfig() { + let { enabled, available_providers, dappConnection } = this.storageConfig; + return enabled && (available_providers.indexOf('ipfs') > 0 || dappConnection.find(c => c.provider === 'ipfs')); + } + } module.exports = IPFS; diff --git a/lib/modules/swarm/index.js b/lib/modules/swarm/index.js index 135f9dedf..db91d3cbb 100644 --- a/lib/modules/swarm/index.js +++ b/lib/modules/swarm/index.js @@ -2,7 +2,6 @@ const UploadSwarm = require('./upload.js'); const utils = require('../../utils/utils.js'); const fs = require('../../core/fs.js'); const Web3Bzz = require('web3-bzz'); -const _ = require('underscore'); const StorageProcessesLauncher = require('../../processes/storageProcesses/storageProcessesLauncher'); class Swarm { @@ -42,12 +41,7 @@ class Swarm { setServiceCheck() { let self = this; - let storageConfig = this.storageConfig; - - if (!storageConfig.enabled) { - return; - } - if (_.findWhere(this.storageConfig.dappConnection, {'provider': 'swarm'}) === undefined && (storageConfig.upload.provider !== 'swarm' || storageConfig.available_providers.indexOf("swarm") < 0)) { + if (!this.isSwarmEnabledInTheConfig()) { return; } @@ -79,12 +73,7 @@ class Swarm { } addProviderToEmbarkJS() { - // TODO: make this a shouldAdd condition - if (this.storageConfig === {}) { - return; - } - - if (this.storageConfig.available_providers.indexOf('swarm') < 0 || _.findWhere(this.storageConfig.dappConnection, {'provider': 'swarm'}) === undefined || this.storageConfig.enabled !== true) { + if (!this.isSwarmEnabledInTheConfig()) { return; } @@ -122,7 +111,11 @@ class Swarm { }); } + isSwarmEnabledInTheConfig() { + let { enabled, available_providers, dappConnection } = this.storageConfig; + return enabled && (available_providers.indexOf('swarm') > 0 || dappConnection.find(c => c.provider === 'swarm')); + } + } module.exports = Swarm; -