2018-05-28 12:59:18 +00:00
|
|
|
/* global EmbarkJS */
|
|
|
|
|
|
|
|
import {findSeries} from 'p-iteration';
|
|
|
|
|
|
|
|
let __embarkStorage = {};
|
|
|
|
|
|
|
|
__embarkStorage.setProviders = async function (dappConnOptions) {
|
2018-05-28 12:59:18 +00:00
|
|
|
var self = this;
|
2018-05-28 12:59:18 +00:00
|
|
|
try {
|
|
|
|
let workingConnFound = await findSeries(dappConnOptions, async (dappConn) => {
|
|
|
|
if(dappConn === '$BZZ' || dappConn.provider === 'swarm'){
|
2018-05-28 12:59:18 +00:00
|
|
|
let options = dappConnOptions;
|
|
|
|
options.useOnlyCurrentProvider = dappConn === '$BZZ';
|
|
|
|
await EmbarkJS.Storage.setProvider('swarm', options);
|
|
|
|
return EmbarkJS.Storage.isAvailable();
|
2018-05-28 12:59:18 +00:00
|
|
|
}
|
|
|
|
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);
|
2018-05-28 12:59:18 +00:00
|
|
|
return EmbarkJS.Storage.isAvailable();
|
2018-05-28 12:59:18 +00:00
|
|
|
} catch(err) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if(!workingConnFound) throw new Error('Could not connect to a storage provider using any of the dappConnections in the storage config');
|
|
|
|
} catch (err) {
|
2018-05-28 12:59:18 +00:00
|
|
|
self.ipfsConnection = null;
|
|
|
|
throw new Error('Failed to connect to IPFS: ' + err.message);
|
2018-05-28 12:59:18 +00:00
|
|
|
}
|
|
|
|
};
|