From 41b2741ba59369c6e7d3f34a1bce2288489062d2 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 2 May 2018 12:10:11 -0400 Subject: [PATCH 1/2] add optionnal callback --- js/embark.js | 4 ++-- lib/modules/whisper/js/embarkjs.js | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/js/embark.js b/js/embark.js index d8632b87..912ab415 100644 --- a/js/embark.js +++ b/js/embark.js @@ -263,11 +263,11 @@ EmbarkJS.Messages.sendMessage = function(options) { return this.currentMessages.sendMessage(options); }; -EmbarkJS.Messages.listenTo = function(options) { +EmbarkJS.Messages.listenTo = function(options, callback) { if (!this.currentMessages) { throw new Error('Messages provider not set; e.g EmbarkJS.Messages.setProvider("whisper")'); } - return this.currentMessages.listenTo(options); + return this.currentMessages.listenTo(options, callback); }; EmbarkJS.Utils = { diff --git a/lib/modules/whisper/js/embarkjs.js b/lib/modules/whisper/js/embarkjs.js index 27bd1f7b..4cfa98fe 100644 --- a/lib/modules/whisper/js/embarkjs.js +++ b/lib/modules/whisper/js/embarkjs.js @@ -64,7 +64,7 @@ __embarkWhisperNewWeb3.sendMessage = function (options) { }); }; -__embarkWhisperNewWeb3.listenTo = function (options) { +__embarkWhisperNewWeb3.listenTo = function (options, callback) { var topics = options.topic || options.topics; let promise = new __MessageEvents(); @@ -82,12 +82,15 @@ __embarkWhisperNewWeb3.listenTo = function (options) { var payload = JSON.parse(EmbarkJS.Utils.toAscii(result.payload)); var data; data = { - topic: result.topic, + topic: EmbarkJS.Utils.toAscii(result.topic), data: payload, //from: result.from, time: result.timestamp }; + if (callback) { + return callback(null, data); + } promise.cb(payload, data, result); }); From f303d3d8397cfbbb8eb325d72bb82dceb7eadfc3 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 2 May 2018 12:24:16 -0400 Subject: [PATCH 2/2] fix whisper demo --- templates/demo/app/components/whisper.js | 197 ++++++++++++----------- 1 file changed, 105 insertions(+), 92 deletions(-) diff --git a/templates/demo/app/components/whisper.js b/templates/demo/app/components/whisper.js index be78c62e..3c056bf2 100644 --- a/templates/demo/app/components/whisper.js +++ b/templates/demo/app/components/whisper.js @@ -1,105 +1,118 @@ import EmbarkJS from 'Embark/EmbarkJS'; import React from 'react'; -import { Alert, Form, FormGroup, FormControl, HelpBlock, Button } from 'react-bootstrap'; - +import {Alert, Form, FormGroup, FormControl, Button} from 'react-bootstrap'; + class Whisper extends React.Component { - constructor(props) { - super(props); + constructor (props) { + super(props); - this.state = { - listenTo: '', - channel: '', - message: '', - subscribedChannels: [], - messageList: [], - logs: [] + this.state = { + listenTo: '', + channel: '', + message: '', + subscribedChannels: [], + messageList: [], + logs: [] + }; + } + + handleChange (e, name) { + this.state[name] = e.target.value; + this.setState(this.state); + } + + sendMessage (e) { + e.preventDefault(); + EmbarkJS.Messages.sendMessage({topic: this.state.channel, data: this.state.message}); + this.addToLog("EmbarkJS.Messages.sendMessage({topic: '" + this.state.channel + "', data: '" + this.state.message + "'})"); + } + + listenToChannel (e) { + e.preventDefault(); + + const subscribedChannels = this.state.subscribedChannels; + subscribedChannels.push(Subscribed to {this.state.listenTo}. Now try sending a message); + this.setState({ + subscribedChannels + }); + + EmbarkJS.Messages.listenTo({topic: [this.state.listenTo]}, (error, message) => { + const messageList = this.state.messageList; + if (error) { + messageList.push(Error: {error}); + } else { + messageList.push(Channel: {message.topic} | Message: {message.data}); } - } + this.setState({ + messageList + }); + }); - handleChange(e, name){ - this.state[name] = e.target.value; - this.setState(this.state); - } + this.addToLog("EmbarkJS.Messages.listenTo({topic: ['" + this.state.listenTo + "']}).then(function(message) {})"); + } - sendMessage(e){ - e.preventDefault(); - EmbarkJS.Messages.sendMessage({topic: this.state.channel, data: this.state.message}); - this.addToLog("EmbarkJS.Messages.sendMessage({topic: '" + this.state.channel + "', data: '" + this.state.message + "'})"); - } + addToLog (txt) { + this.state.logs.push(txt); + this.setState({logs: this.state.logs}); + } - listenToChannel(e){ - e.preventDefault(); - - this.state.subscribedChannels.push(`subscribed to ${this.state.listenTo} now try sending a message`); - - EmbarkJS.Messages.listenTo({topic: [this.state.listenTo]}) - .then(message => this.state.messageList.push(`channel: ${this.state.listenTo} message: ${message}`)) - - this.addToLog("EmbarkJS.Messages.listenTo({topic: ['" + this.state.listenTo + "']}).then(function(message) {})"); - } - - addToLog(txt){ - this.state.logs.push(txt); - this.setState({logs: this.state.logs}); - } - - render(){ - return ( - - { - !this.props.enabled ? - - The node you are using does not support Whisper - The node uses an unsupported version of Whisper - : '' - } -

Listen To channel

-
- - this.handleChange(e, 'listenTo')} /> - -
- { this.state.subscribedChannels.map((item, i) =>

{item}

) } -
-

messages received:

-
- { this.state.messageList.map((item, i) =>

{item}

) } -
-
-
- -

Send Message

-
- - this.handleChange(e, 'channel')} /> - this.handleChange(e, 'message')} /> - - -
- -

Javascript calls being made:

-
-

EmbarkJS.Messages.setProvider('whisper')

- { - this.state.logs.map((item, i) =>

{item}

) - } + render () { + return ( + + { + !this.props.enabled ? + + The node you are using does not support Whisper + The node uses an unsupported version of Whisper + : '' + } +

Listen To channel

+
+ + this.handleChange(e, 'listenTo')}/> + +
+ {this.state.subscribedChannels.map((item, i) =>

{item}

)}
- - ); - } +

messages received:

+
+ {this.state.messageList.map((item, i) =>

{item}

)} +
+
+
+ +

Send Message

+
+ + this.handleChange(e, 'channel')}/> + this.handleChange(e, 'message')}/> + + +
+ +

Javascript calls being made:

+
+

EmbarkJS.Messages.setProvider('whisper')

+ { + this.state.logs.map((item, i) =>

{item}

) + } +
+
+ ); + } } export default Whisper;