async getting properties, remove property setters

This commit is contained in:
Marek Kotewicz 2015-05-12 08:26:27 +02:00
parent 9812b01979
commit 80c0792e79
8 changed files with 154 additions and 34 deletions

55
dist/web3-light.js vendored
View File

@ -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

55
dist/web3.js vendored
View File

@ -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;

4
dist/web3.js.map vendored

File diff suppressed because one or more lines are too long

5
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.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));
});
};

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