refactor setProvider

This commit is contained in:
Iuri Matias 2017-12-27 21:04:12 -05:00
parent 6786b47e77
commit 24c34fcbe3
3 changed files with 23 additions and 29 deletions

View File

@ -228,37 +228,15 @@ EmbarkJS.Messages.registerProvider = function(providerName, obj) {
};
EmbarkJS.Messages.setProvider = function(provider, options) {
var self = this;
var ipfs;
if (provider === 'whisper') {
let providerObj = this.Providers[provider];
let providerObj = this.Providers[provider];
if (!providerObj) {
throw new Error('Unknown storage provider');
}
if (!providerObj) {
throw new Error('Unknown messages provider');
}
this.currentMessages = providerObj;
this.currentMessages = providerObj;
return providerObj.setProvider(options);
} else if (provider === 'orbit') {
this.providerName = 'orbit';
this.currentMessages = EmbarkJS.Messages.Orbit;
if (options === undefined) {
ipfs = HaadIpfsApi('localhost', '5001');
} else {
ipfs = HaadIpfsApi(options.host, options.port);
}
this.currentMessages.orbit = new Orbit(ipfs);
if (typeof(web3) === "undefined") {
this.currentMessages.orbit.connect(Math.random().toString(36).substring(2));
} else {
this.currentMessages.orbit.connect(web3.eth.accounts[0]);
}
} else {
throw Error('Unknown message provider');
}
return providerObj.setProvider(options);
};
EmbarkJS.Messages.sendMessage = function(options) {

View File

@ -1,5 +1,21 @@
EmbarkJS.Messages.Orbit = {};
EmbarkJS.Messages.Orbit.setProvider = function(options) {
this.providerName = 'orbit';
this.currentMessages = EmbarkJS.Messages.Orbit;
if (options === undefined) {
ipfs = HaadIpfsApi('localhost', '5001');
} else {
ipfs = HaadIpfsApi(options.host, options.port);
}
this.currentMessages.orbit = new Orbit(ipfs);
if (typeof(web3) === "undefined") {
this.currentMessages.orbit.connect(Math.random().toString(36).substring(2));
} else {
this.currentMessages.orbit.connect(web3.eth.accounts[0]);
}
};
EmbarkJS.Messages.Orbit.sendMessage = function(options) {
var topics = options.topic || options.topics;
var data = options.data || options.payload;

View File

@ -293,7 +293,7 @@ class CodeGenerator {
code += "\n" + fs.readFileSync(fs.embarkPath('js/embarkjs/ipfs.js')).toString();
code += "\n" + fs.readFileSync(fs.embarkPath('js/embarkjs/whisper.js')).toString();
code += "\n" + fs.readFileSync(fs.embarkPath('js/embarkjs/orbit.js')).toString();
//code += "\n" + fs.readFileSync(fs.embarkPath('js/embarkjs/orbit.js')).toString();
code += this.generateCommunicationInitialization(true);
code += this.generateStorageInitialization(true);