@@ -6,6 +6,7 @@ var EmbarkJS = {
66
77EmbarkJS . Contract = function ( options ) {
88 var self = this ;
9+ var i , abiElement ;
910
1011 this . abi = options . abi ;
1112 this . address = options . address ;
@@ -14,10 +15,50 @@ EmbarkJS.Contract = function(options) {
1415
1516 var ContractClass = this . web3 . eth . contract ( this . abi ) ;
1617
18+ this . eventList = [ ] ;
19+
20+ if ( this . abi ) {
21+ for ( i = 0 ; i < this . abi . length ; i ++ ) {
22+ abiElement = this . abi [ i ] ;
23+ if ( abiElement . type === 'event' ) {
24+ this . eventList . push ( abiElement . name ) ;
25+ }
26+ }
27+ }
28+
29+ var messageEvents = function ( ) {
30+ this . cb = function ( ) { } ;
31+ } ;
32+
33+ messageEvents . prototype . then = function ( cb ) {
34+ this . cb = cb ;
35+ } ;
36+
37+ messageEvents . prototype . error = function ( err ) {
38+ return err ;
39+ } ;
40+
1741 this . _originalContractObject = ContractClass . at ( this . address ) ;
1842 this . _methods = Object . getOwnPropertyNames ( this . _originalContractObject ) . filter ( function ( p ) {
1943 // TODO: check for forbidden properties
20- if ( typeof self . _originalContractObject [ p ] === 'function' ) {
44+ if ( self . eventList . indexOf ( p ) >= 0 ) {
45+
46+ self [ p ] = function ( ) {
47+ var promise = new messageEvents ( ) ;
48+ var args = Array . prototype . slice . call ( arguments ) ;
49+ args . push ( function ( err , result ) {
50+ if ( err ) {
51+ promise . error ( err ) ;
52+ } else {
53+ promise . cb ( result ) ;
54+ }
55+ } ) ;
56+
57+ self . _originalContractObject [ p ] . apply ( self . _originalContractObject [ p ] , args ) ;
58+ return promise ;
59+ } ;
60+ return true ;
61+ } else if ( typeof self . _originalContractObject [ p ] === 'function' ) {
2162 self [ p ] = Promise . promisify ( self . _originalContractObject [ p ] ) ;
2263 return true ;
2364 }
0 commit comments