mirror of
https://github.com/status-im/embark-area-51.git
synced 2025-02-02 02:15:58 +00:00
add orbit to embarkjs
This commit is contained in:
parent
5dabf8198a
commit
13cf92a4f5
@ -28,7 +28,7 @@ Table of Contents
|
||||
* [Using and Configuring Contracts](#dapp-structure)
|
||||
* [EmbarkJS](#embarkjs)
|
||||
* [EmbarkJS - Storage (IPFS)](#embarkjs---storage)
|
||||
* [EmbarkJS - Communication (Whisper)](#embarkjs---communication)
|
||||
* [EmbarkJS - Communication (Whisper/Orbit)](#embarkjs---communication)
|
||||
* [Testing Contracts](#tests)
|
||||
* [Working with different chains](#working-with-different-chains)
|
||||
* [Custom Application Structure](#structuring-application)
|
||||
@ -351,7 +351,7 @@ EmbarkJS - Communication
|
||||
|
||||
**initialization**
|
||||
|
||||
The current available communication is Whisper.
|
||||
The current available communications are Whisper and Orbit. Whisper is supported in go-ethereum. To run Orbit ```ipfs daemon --enable-pubsub-experiment```.
|
||||
|
||||
**listening to messages**
|
||||
|
||||
|
@ -246,20 +246,30 @@ var EmbarkJS =
|
||||
EmbarkJS.Messages = {
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.setProvider = function(provider) {
|
||||
EmbarkJS.Messages.setProvider = function(provider, options) {
|
||||
var ipfs;
|
||||
if (provider === 'whisper') {
|
||||
this.currentMessages = EmbarkJS.Messages.Whisper;
|
||||
} else if (provider === 'orbit') {
|
||||
this.currentMessages = EmbarkJS.Messages.Orbit;
|
||||
if (options === undefined) {
|
||||
ipfs = HaadIpfsApi('localhost', '5001');
|
||||
} else {
|
||||
ipfs = HaadIpfsApi(options.server, options.port);
|
||||
}
|
||||
this.currentMessages.orbit = new Orbit(ipfs);
|
||||
this.currentMessages.orbit.connect(web3.eth.accounts[0]);
|
||||
} else {
|
||||
throw Error('unknown provider');
|
||||
}
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.sendMessage = function(options) {
|
||||
return EmbarkJS.Messages.Whisper.sendMessage(options);
|
||||
return this.currentMessages.sendMessage(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.listenTo = function(options) {
|
||||
return EmbarkJS.Messages.Whisper.listenTo(options);
|
||||
return this.currentMessages.listenTo(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Whisper = {
|
||||
@ -349,6 +359,74 @@ var EmbarkJS =
|
||||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Orbit = {
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Orbit.sendMessage = function(options) {
|
||||
var topics = options.topic || options.topics;
|
||||
var data = options.data || options.payload;
|
||||
|
||||
if (topics === undefined) {
|
||||
throw new Error("missing option: topic");
|
||||
}
|
||||
|
||||
if (data === undefined) {
|
||||
throw new Error("missing option: data");
|
||||
}
|
||||
|
||||
// do fromAscii to each topics unless it's already a string
|
||||
if (typeof topics === 'string') {
|
||||
topics = topics;
|
||||
} else {
|
||||
// TODO: better to just send to different channels instead
|
||||
topics = topics.join(',');
|
||||
}
|
||||
|
||||
// TODO: if it's array then join and send ot several
|
||||
// keep list of channels joined
|
||||
this.orbit.join(topics);
|
||||
|
||||
var payload = JSON.stringify(data);
|
||||
|
||||
this.orbit.send(topics, data);
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Orbit.listenTo = function(options) {
|
||||
var self = this;
|
||||
var topics = options.topic || options.topics;
|
||||
|
||||
// do fromAscii to each topics unless it's already a string
|
||||
if (typeof topics === 'string') {
|
||||
topics = topics;
|
||||
} else {
|
||||
// TODO: better to just send to different channels instead
|
||||
topics = topics.join(',');
|
||||
}
|
||||
|
||||
var messageEvents = function() {
|
||||
this.cb = function() {};
|
||||
};
|
||||
|
||||
messageEvents.prototype.then = function(cb) {
|
||||
this.cb = cb;
|
||||
};
|
||||
|
||||
messageEvents.prototype.error = function(err) {
|
||||
return err;
|
||||
};
|
||||
|
||||
var promise = new messageEvents();
|
||||
|
||||
this.orbit.events.on('message', (topics, message) => {
|
||||
// Get the actual content of the message
|
||||
self.orbit.getPost(message.payload.value, true).then((post) => {
|
||||
promise.cb(post);
|
||||
});
|
||||
});
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
module.exports = EmbarkJS;
|
||||
|
||||
|
||||
|
84
js/embark.js
84
js/embark.js
@ -199,20 +199,30 @@ EmbarkJS.Storage.getUrl = function(hash) {
|
||||
EmbarkJS.Messages = {
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.setProvider = function(provider) {
|
||||
EmbarkJS.Messages.setProvider = function(provider, options) {
|
||||
var ipfs;
|
||||
if (provider === 'whisper') {
|
||||
this.currentMessages = EmbarkJS.Messages.Whisper;
|
||||
} else if (provider === 'orbit') {
|
||||
this.currentMessages = EmbarkJS.Messages.Orbit;
|
||||
if (options === undefined) {
|
||||
ipfs = HaadIpfsApi('localhost', '5001');
|
||||
} else {
|
||||
ipfs = HaadIpfsApi(options.server, options.port);
|
||||
}
|
||||
this.currentMessages.orbit = new Orbit(ipfs);
|
||||
this.currentMessages.orbit.connect(web3.eth.accounts[0]);
|
||||
} else {
|
||||
throw Error('unknown provider');
|
||||
}
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.sendMessage = function(options) {
|
||||
return EmbarkJS.Messages.Whisper.sendMessage(options);
|
||||
return this.currentMessages.sendMessage(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.listenTo = function(options) {
|
||||
return EmbarkJS.Messages.Whisper.listenTo(options);
|
||||
return this.currentMessages.listenTo(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Whisper = {
|
||||
@ -302,4 +312,72 @@ EmbarkJS.Messages.Whisper.listenTo = function(options) {
|
||||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Orbit = {
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Orbit.sendMessage = function(options) {
|
||||
var topics = options.topic || options.topics;
|
||||
var data = options.data || options.payload;
|
||||
|
||||
if (topics === undefined) {
|
||||
throw new Error("missing option: topic");
|
||||
}
|
||||
|
||||
if (data === undefined) {
|
||||
throw new Error("missing option: data");
|
||||
}
|
||||
|
||||
// do fromAscii to each topics unless it's already a string
|
||||
if (typeof topics === 'string') {
|
||||
topics = topics;
|
||||
} else {
|
||||
// TODO: better to just send to different channels instead
|
||||
topics = topics.join(',');
|
||||
}
|
||||
|
||||
// TODO: if it's array then join and send ot several
|
||||
// keep list of channels joined
|
||||
this.orbit.join(topics);
|
||||
|
||||
var payload = JSON.stringify(data);
|
||||
|
||||
this.orbit.send(topics, data);
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Orbit.listenTo = function(options) {
|
||||
var self = this;
|
||||
var topics = options.topic || options.topics;
|
||||
|
||||
// do fromAscii to each topics unless it's already a string
|
||||
if (typeof topics === 'string') {
|
||||
topics = topics;
|
||||
} else {
|
||||
// TODO: better to just send to different channels instead
|
||||
topics = topics.join(',');
|
||||
}
|
||||
|
||||
var messageEvents = function() {
|
||||
this.cb = function() {};
|
||||
};
|
||||
|
||||
messageEvents.prototype.then = function(cb) {
|
||||
this.cb = cb;
|
||||
};
|
||||
|
||||
messageEvents.prototype.error = function(err) {
|
||||
return err;
|
||||
};
|
||||
|
||||
var promise = new messageEvents();
|
||||
|
||||
this.orbit.events.on('message', (topics, message) => {
|
||||
// Get the actual content of the message
|
||||
self.orbit.getPost(message.payload.value, true).then((post) => {
|
||||
promise.cb(post);
|
||||
});
|
||||
});
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
module.exports = EmbarkJS;
|
||||
|
61
js/ipfs-api.min.js
vendored
Normal file
61
js/ipfs-api.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
60
js/orbit.min.js
vendored
Normal file
60
js/orbit.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
@ -84,6 +84,9 @@ Config.prototype.loadFiles = function(files) {
|
||||
if (file === 'embark.js') {
|
||||
readFiles.push({filename: 'web3.js', content: fs.readFileSync(path.join(__dirname, "/../js/web3.js")).toString()});
|
||||
readFiles.push({filename: 'ipfs.js', content: fs.readFileSync(path.join(__dirname, "/../js/ipfs.js")).toString()});
|
||||
// TODO: remove duplicated files if funcitonality is the same for storage and orbit
|
||||
readFiles.push({filename: 'ipfs-api.js', content: fs.readFileSync(path.join(__dirname, "/../js/ipfs-api.min.js")).toString()});
|
||||
readFiles.push({filename: 'orbit.js', content: fs.readFileSync(path.join(__dirname, "/../js/orbit.min.js")).toString()});
|
||||
readFiles.push({filename: 'embark.js', content: fs.readFileSync(path.join(__dirname, "/../js/build/embark.bundle.js")).toString()});
|
||||
} else {
|
||||
readFiles.push({filename: file, content: fs.readFileSync(file).toString()});
|
||||
|
Loading…
x
Reference in New Issue
Block a user