mirror of https://github.com/embarklabs/embark.git
Initial refactoring of EmbarkJS to support multiple storage providers
This commit is contained in:
parent
e8c8a85713
commit
494e776f05
|
@ -211,11 +211,40 @@ EmbarkJS.Contract.prototype.deploy = function(args, _options) {
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Storage = {
|
EmbarkJS.Storage = {
|
||||||
IPFS : 'ipfs'
|
};
|
||||||
|
|
||||||
|
EmbarkJS.Storage.Providers = {
|
||||||
|
IPFS : 'ipfs',
|
||||||
|
SWARM: 'swarm'
|
||||||
|
};
|
||||||
|
|
||||||
|
EmbarkJS.Storage.IPFS = {
|
||||||
|
};
|
||||||
|
|
||||||
|
EmbarkJS.Storage.IPFS.saveText = function(text) {
|
||||||
|
var self = this;
|
||||||
|
if (!this.ipfsConnection) {
|
||||||
|
this.setProvider('ipfs');
|
||||||
|
}
|
||||||
|
var promise = new Promise(function(resolve, reject) {
|
||||||
|
self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) {
|
||||||
|
if (err) {
|
||||||
|
reject(err);
|
||||||
|
} else {
|
||||||
|
resolve(result[0].path);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return promise;
|
||||||
|
};
|
||||||
|
|
||||||
|
EmbarkJS.Storage.saveText = function(text) {
|
||||||
|
return this.currentStorage.sendMessage(text);
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Storage.setProvider = function(provider, options) {
|
EmbarkJS.Storage.setProvider = function(provider, options) {
|
||||||
if (provider.toLowerCase() === EmbarkJS.Storage.IPFS) {
|
if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) {
|
||||||
//I don't think currentStorage is used anywhere, this might not be needed
|
//I don't think currentStorage is used anywhere, this might not be needed
|
||||||
//for now until additional storage providers are supported. But keeping it
|
//for now until additional storage providers are supported. But keeping it
|
||||||
//anyways
|
//anyways
|
||||||
|
@ -225,7 +254,17 @@ EmbarkJS.Storage.setProvider = function(provider, options) {
|
||||||
} else {
|
} else {
|
||||||
this.ipfsConnection = IpfsApi(options.server, options.port);
|
this.ipfsConnection = IpfsApi(options.server, options.port);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else if (provider.toLowerCase() === EmbarkJS.Storage.SWARM){
|
||||||
|
throw Error('Swarm not implemented');
|
||||||
|
this.currentStorage = EmbarkJS.Storage.SWARM;
|
||||||
|
if (options === undefined) {
|
||||||
|
//Connect to default Swarm node
|
||||||
} else {
|
} else {
|
||||||
|
//Connect using options
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
throw Error('Unknown storage provider');
|
throw Error('Unknown storage provider');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -336,7 +375,7 @@ EmbarkJS.Messages.setProvider = function(provider, options) {
|
||||||
this.currentMessages.orbit = new Orbit(ipfs);
|
this.currentMessages.orbit = new Orbit(ipfs);
|
||||||
this.currentMessages.orbit.connect(web3.eth.accounts[0]);
|
this.currentMessages.orbit.connect(web3.eth.accounts[0]);
|
||||||
} else {
|
} else {
|
||||||
throw Error('unknown provider');
|
throw Error('Unknown message provider');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
45
js/embark.js
45
js/embark.js
|
@ -138,11 +138,40 @@ EmbarkJS.Contract.prototype.deploy = function(args, _options) {
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Storage = {
|
EmbarkJS.Storage = {
|
||||||
IPFS : 'ipfs'
|
};
|
||||||
|
|
||||||
|
EmbarkJS.Storage.Providers = {
|
||||||
|
IPFS : 'ipfs',
|
||||||
|
SWARM: 'swarm'
|
||||||
|
};
|
||||||
|
|
||||||
|
EmbarkJS.Storage.IPFS = {
|
||||||
|
};
|
||||||
|
|
||||||
|
EmbarkJS.Storage.IPFS.saveText = function(text) {
|
||||||
|
var self = this;
|
||||||
|
if (!this.ipfsConnection) {
|
||||||
|
this.setProvider('ipfs');
|
||||||
|
}
|
||||||
|
var promise = new Promise(function(resolve, reject) {
|
||||||
|
self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) {
|
||||||
|
if (err) {
|
||||||
|
reject(err);
|
||||||
|
} else {
|
||||||
|
resolve(result[0].path);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return promise;
|
||||||
|
};
|
||||||
|
|
||||||
|
EmbarkJS.Storage.saveText = function(text) {
|
||||||
|
return this.currentStorage.sendMessage(text);
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Storage.setProvider = function(provider, options) {
|
EmbarkJS.Storage.setProvider = function(provider, options) {
|
||||||
if (provider.toLowerCase() === EmbarkJS.Storage.IPFS) {
|
if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) {
|
||||||
//I don't think currentStorage is used anywhere, this might not be needed
|
//I don't think currentStorage is used anywhere, this might not be needed
|
||||||
//for now until additional storage providers are supported. But keeping it
|
//for now until additional storage providers are supported. But keeping it
|
||||||
//anyways
|
//anyways
|
||||||
|
@ -152,7 +181,17 @@ EmbarkJS.Storage.setProvider = function(provider, options) {
|
||||||
} else {
|
} else {
|
||||||
this.ipfsConnection = IpfsApi(options.server, options.port);
|
this.ipfsConnection = IpfsApi(options.server, options.port);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else if (provider.toLowerCase() === EmbarkJS.Storage.SWARM){
|
||||||
|
throw Error('Swarm not implemented');
|
||||||
|
this.currentStorage = EmbarkJS.Storage.SWARM;
|
||||||
|
if (options === undefined) {
|
||||||
|
//Connect to default Swarm node
|
||||||
} else {
|
} else {
|
||||||
|
//Connect using options
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
throw Error('Unknown storage provider');
|
throw Error('Unknown storage provider');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -263,7 +302,7 @@ EmbarkJS.Messages.setProvider = function(provider, options) {
|
||||||
this.currentMessages.orbit = new Orbit(ipfs);
|
this.currentMessages.orbit = new Orbit(ipfs);
|
||||||
this.currentMessages.orbit.connect(web3.eth.accounts[0]);
|
this.currentMessages.orbit.connect(web3.eth.accounts[0]);
|
||||||
} else {
|
} else {
|
||||||
throw Error('unknown provider');
|
throw Error('Unknown message provider');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue