From 408caa4c509116b75780ab50fb3cfae72a943ae2 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 30 Oct 2016 08:40:37 -0400 Subject: [PATCH] fix events handling --- js/build/embark.bundle.js | 43 ++++++++++++++++++++++++++++++++++++++- js/embark.js | 43 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/js/build/embark.bundle.js b/js/build/embark.bundle.js index 94503606f..321bfd244 100644 --- a/js/build/embark.bundle.js +++ b/js/build/embark.bundle.js @@ -53,6 +53,7 @@ var EmbarkJS = EmbarkJS.Contract = function(options) { var self = this; + var i, abiElement; this.abi = options.abi; this.address = options.address; @@ -61,10 +62,50 @@ var EmbarkJS = var ContractClass = this.web3.eth.contract(this.abi); + this.eventList = []; + + if (this.abi) { + for (i = 0; i < this.abi.length; i++) { + abiElement = this.abi[i]; + if (abiElement.type === 'event') { + this.eventList.push(abiElement.name); + } + } + } + + var messageEvents = function() { + this.cb = function() {}; + }; + + messageEvents.prototype.then = function(cb) { + this.cb = cb; + }; + + messageEvents.prototype.error = function(err) { + return err; + }; + this._originalContractObject = ContractClass.at(this.address); this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function (p) { // TODO: check for forbidden properties - if (typeof self._originalContractObject[p] === 'function') { + if (self.eventList.indexOf(p) >= 0) { + + self[p] = function() { + var promise = new messageEvents(); + var args = Array.prototype.slice.call(arguments); + args.push(function(err, result) { + if (err) { + promise.error(err); + } else { + promise.cb(result); + } + }); + + self._originalContractObject[p].apply(self._originalContractObject[p], args); + return promise; + }; + return true; + } else if (typeof self._originalContractObject[p] === 'function') { self[p] = Promise.promisify(self._originalContractObject[p]); return true; } diff --git a/js/embark.js b/js/embark.js index 1b4d8d573..903e35b74 100644 --- a/js/embark.js +++ b/js/embark.js @@ -6,6 +6,7 @@ var EmbarkJS = { EmbarkJS.Contract = function(options) { var self = this; + var i, abiElement; this.abi = options.abi; this.address = options.address; @@ -14,10 +15,50 @@ EmbarkJS.Contract = function(options) { var ContractClass = this.web3.eth.contract(this.abi); + this.eventList = []; + + if (this.abi) { + for (i = 0; i < this.abi.length; i++) { + abiElement = this.abi[i]; + if (abiElement.type === 'event') { + this.eventList.push(abiElement.name); + } + } + } + + var messageEvents = function() { + this.cb = function() {}; + }; + + messageEvents.prototype.then = function(cb) { + this.cb = cb; + }; + + messageEvents.prototype.error = function(err) { + return err; + }; + this._originalContractObject = ContractClass.at(this.address); this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function (p) { // TODO: check for forbidden properties - if (typeof self._originalContractObject[p] === 'function') { + if (self.eventList.indexOf(p) >= 0) { + + self[p] = function() { + var promise = new messageEvents(); + var args = Array.prototype.slice.call(arguments); + args.push(function(err, result) { + if (err) { + promise.error(err); + } else { + promise.cb(result); + } + }); + + self._originalContractObject[p].apply(self._originalContractObject[p], args); + return promise; + }; + return true; + } else if (typeof self._originalContractObject[p] === 'function') { self[p] = Promise.promisify(self._originalContractObject[p]); return true; }