mirror of https://github.com/status-im/web3.js.git
async getting properties, remove property setters
This commit is contained in:
parent
9812b01979
commit
80c0792e79
|
@ -3189,12 +3189,26 @@ Property.prototype.attachToObject = function (obj) {
|
|||
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);
|
||||
};
|
||||
|
||||
if (this.getter) {
|
||||
obj[toAsyncName('get', name)] = this.asyncGet.bind(this);
|
||||
}
|
||||
|
||||
if (this.setter) {
|
||||
obj[toAsyncName('set', name)] = this.asyncSet.bind(this);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -3223,6 +3237,35 @@ Property.prototype.set = function (value) {
|
|||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Should be used to asynchrounously get value of property
|
||||
*
|
||||
* @method asyncGet
|
||||
* @param {Function}
|
||||
*/
|
||||
Property.prototype.asyncGet = function (callback) {
|
||||
var self = this;
|
||||
RequestManager.getInstance().sendAsync({
|
||||
method: this.getter
|
||||
}, function (err, result) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
callback(err, self.formatOutput(result));
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Should be used to asynchronously set value of property
|
||||
*
|
||||
* @method asyncSet
|
||||
* @param {Any} new value
|
||||
* @param {Function} callback
|
||||
*/
|
||||
Property.prototype.asyncSet = function (value, callback) {
|
||||
RequestManager.getInstance().sendAsync(this.formatInput(value), callback);
|
||||
};
|
||||
|
||||
module.exports = Property;
|
||||
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -3189,12 +3189,26 @@ Property.prototype.attachToObject = function (obj) {
|
|||
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);
|
||||
};
|
||||
|
||||
if (this.getter) {
|
||||
obj[toAsyncName('get', name)] = this.asyncGet.bind(this);
|
||||
}
|
||||
|
||||
if (this.setter) {
|
||||
obj[toAsyncName('set', name)] = this.asyncSet.bind(this);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -3223,6 +3237,35 @@ Property.prototype.set = function (value) {
|
|||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Should be used to asynchrounously get value of property
|
||||
*
|
||||
* @method asyncGet
|
||||
* @param {Function}
|
||||
*/
|
||||
Property.prototype.asyncGet = function (callback) {
|
||||
var self = this;
|
||||
RequestManager.getInstance().sendAsync({
|
||||
method: this.getter
|
||||
}, function (err, result) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
callback(err, self.formatOutput(result));
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Should be used to asynchronously set value of property
|
||||
*
|
||||
* @method asyncSet
|
||||
* @param {Any} new value
|
||||
* @param {Function} callback
|
||||
*/
|
||||
Property.prototype.asyncSet = function (value, callback) {
|
||||
RequestManager.getInstance().sendAsync(this.formatInput(value), callback);
|
||||
};
|
||||
|
||||
module.exports = Property;
|
||||
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -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.asyncGet.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 asyncGet
|
||||
* @param {Function}
|
||||
*/
|
||||
Property.prototype.set = function (value) {
|
||||
return RequestManager.getInstance().send({
|
||||
method: this.setter,
|
||||
params: [this.formatInput(value)]
|
||||
Property.prototype.asyncGet = function (callback) {
|
||||
var self = this;
|
||||
RequestManager.getInstance().sendAsync({
|
||||
method: this.getter
|
||||
}, function (err, result) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
callback(err, self.formatOutput(result));
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue