Skip to content

Commit

Permalink
Merge pull request #202 from debris/async_properties
Browse files Browse the repository at this point in the history
tested it manually
  • Loading branch information
frozeman committed May 12, 2015
2 parents 9812b01 + 5f9272c commit fa8db32
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 50 deletions.
40 changes: 26 additions & 14 deletions dist/web3-light.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/web3-light.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/web3-light.min.js

Large diffs are not rendered by default.

40 changes: 26 additions & 14 deletions dist/web3.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/web3.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/web3.min.js

Large diffs are not rendered by default.

40 changes: 26 additions & 14 deletions lib/web3/property.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,23 @@ Property.prototype.formatOutput = function (result) {
Property.prototype.attachToObject = function (obj) {
var proto = {
get: this.get.bind(this),
set: this.set.bind(this)
};

var name = this.name.split('.');
if (name.length > 1) {
obj[name[0]] = obj[name[0]] || {};
Object.defineProperty(obj[name[0]], name[1], proto);
} else {
Object.defineProperty(obj, name[0], proto);
var names = this.name.split('.');
var name = names[0];
if (names.length > 1) {
obj[names[0]] = obj[names[0]] || {};
obj = obj[names[0]];
name = names[1];
}

Object.defineProperty(obj, name, proto);

var toAsyncName = function (prefix, name) {
return prefix + name.charAt(0).toUpperCase() + name.slice(1);
};

obj[toAsyncName('get', name)] = this.getAsync.bind(this);
};

/**
Expand All @@ -88,15 +95,20 @@ Property.prototype.get = function () {
};

/**
* Should be used to set value of the property
* Should be used to asynchrounously get value of property
*
* @method set
* @param {Object} new value of the property
* @method getAsync
* @param {Function}
*/
Property.prototype.set = function (value) {
return RequestManager.getInstance().send({
method: this.setter,
params: [this.formatInput(value)]
Property.prototype.getAsync = function (callback) {
var self = this;
RequestManager.getInstance().sendAsync({
method: this.getter
}, function (err, result) {
if (err) {
return callback(err);
}
callback(err, self.formatOutput(result));
});
};

Expand Down
20 changes: 20 additions & 0 deletions test/web3.eth.blockNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ describe('web3.eth', function () {
// then
assert.strictEqual(test.formattedResult, result);
});

it('async get property test: ' + index, function (done) {

// given
var provider = new FakeHttpProvider();
web3.setProvider(provider);
provider.injectResult(test.result);
provider.injectValidation(function (payload) {
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, test.call);
assert.deepEqual(payload.params, []);
});

// when
web3.eth.getBlockNumber(function (err, result) {
assert.strictEqual(test.formattedResult, result);
done();
});

});
});
});
});
Expand Down

0 comments on commit fa8db32

Please sign in to comment.