Merge pull request #202 from debris/async_properties

tested it manually
This commit is contained in:
Fabian Vogelsteller 2015-05-12 10:17:17 +02:00
commit fa8db32bd5
8 changed files with 106 additions and 50 deletions

40
dist/web3-light.js vendored
View File

@ -3186,16 +3186,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);
};
/**
@ -3211,15 +3218,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));
});
};

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

40
dist/web3.js vendored
View File

@ -3186,16 +3186,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);
};
/**
@ -3211,15 +3218,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));
});
};

4
dist/web3.js.map vendored

File diff suppressed because one or more lines are too long

4
dist/web3.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -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);
};
/**
@ -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));
});
};

View File

@ -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();
});
});
});
});
});