move setProviders to EmbarkJS

This commit is contained in:
Iuri Matias 2018-07-07 18:29:45 +03:00
parent 9b1e6a36a6
commit 2e21dfa08a
2 changed files with 37 additions and 20 deletions

View File

@ -1,3 +1,5 @@
import {detectSeries} from 'async';
var EmbarkJS = { var EmbarkJS = {
onReady: function (cb) { onReady: function (cb) {
if (typeof (__embarkContext) === 'undefined') { if (typeof (__embarkContext) === 'undefined') {
@ -300,6 +302,40 @@ EmbarkJS.Storage.isAvailable = function () {
return this.currentStorage.isAvailable(); return this.currentStorage.isAvailable();
}; };
let __embarkStorage = {};
EmbarkJS.Storage.setProviders = async function (dappConnOptions) {
try {
await detectSeries(dappConnOptions, async (dappConn, callback) => {
if(dappConn === '$BZZ' || dappConn.provider === 'swarm'){
let options = dappConn;
if(dappConn === '$BZZ') options = {"useOnlyGivenProvider": true};
try{
await EmbarkJS.Storage.setProvider('swarm', options);
let isAvailable = await EmbarkJS.Storage.isAvailable();
callback(null, isAvailable);
}catch(err){
callback(null, false); // catch errors for when bzz object not initialised but config has requested it to be used
}
}
else if(dappConn.provider === 'ipfs') {
// set the provider then check the connection, if true, use that provider, else, check next provider
try{
await EmbarkJS.Storage.setProvider('ipfs', dappConn);
let isAvailable = await EmbarkJS.Storage.isAvailable();
callback(null, isAvailable);
} catch(err) {
callback(null, false); // catch but keep looping by not passing err to callback
}
}
}, function(err, result){
if(!result) throw new Error('Could not connect to a storage provider using any of the dappConnections in the storage config');
});
} catch (err) {
throw new Error('Failed to connect to a storage provider: ' + err.message);
}
};
EmbarkJS.Messages = {}; EmbarkJS.Messages = {};
EmbarkJS.Messages.Providers = {}; EmbarkJS.Messages.Providers = {};

View File

@ -127,29 +127,10 @@ class Storage {
if(typeof storageProvider.addProviderToEmbarkJS == 'function') storageProvider.addProviderToEmbarkJS(); if(typeof storageProvider.addProviderToEmbarkJS == 'function') storageProvider.addProviderToEmbarkJS();
}); });
// add the storage provider code (__embarkStorage) to embarkjs
this.addProviderToEmbarkJS();
// add the code to call setProviders in embarkjs after embark is ready // add the code to call setProviders in embarkjs after embark is ready
this.addSetProviders(); this.addSetProviders();
} }
/**
* Adds the storage provider code (__embarkStorage) to embarkjs
*
* @returns {void}
*/
addProviderToEmbarkJS(){
// TODO: make this a shouldAdd condition
if (this._storageConfig === {} || !this._storageConfig.dappConnection || !this._storageConfig.dappConnection.length) {
return;
}
let code = "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString();
this._embark.addCodeToEmbarkJS(code);
}
/** /**
* Adds the code to call setProviders in embarkjs after embark is ready * Adds the code to call setProviders in embarkjs after embark is ready
* *
@ -157,7 +138,7 @@ class Storage {
*/ */
addSetProviders() { addSetProviders() {
let code = `\n__embarkStorage.setProviders(${JSON.stringify(this._validDappProviders)});`; let code = `\nEmbarkJS.Storage.setProviders(${JSON.stringify(this._validDappProviders)});`;
let shouldInit = (storageConfig) => { let shouldInit = (storageConfig) => {
return (this._validDappProviders !== undefined && this._validDappProviders.length > 0 && storageConfig.enabled === true); return (this._validDappProviders !== undefined && this._validDappProviders.length > 0 && storageConfig.enabled === true);
}; };