http sync api
This commit is contained in:
96
lib/web3.js
96
lib/web3.js
@@ -169,22 +169,32 @@ var shhWatchMethods = function () {
|
||||
var setupMethods = function (obj, methods) {
|
||||
methods.forEach(function (method) {
|
||||
obj[method.name] = function () {
|
||||
return flattenPromise(Array.prototype.slice.call(arguments)).then(function (args) {
|
||||
var call = typeof method.call === "function" ? method.call(args) : method.call;
|
||||
return {call: call, args: args};
|
||||
}).then(function (request) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
web3.provider.send(request, function (err, result) {
|
||||
if (!err) {
|
||||
resolve(result);
|
||||
return;
|
||||
}
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}).catch(function(err) {
|
||||
console.error(err);
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
var call = typeof method.call === 'function' ? method.call(args) : method.call;
|
||||
var result = web3.provider.send({
|
||||
call: call,
|
||||
args: args
|
||||
});
|
||||
|
||||
result = JSON.parse(result);
|
||||
return result.result;
|
||||
|
||||
//return flattenPromise(Array.prototype.slice.call(arguments)).then(function (args) {
|
||||
//var call = typeof method.call === "function" ? method.call(args) : method.call;
|
||||
//return {call: call, args: args};
|
||||
//}).then(function (request) {
|
||||
//return new Promise(function (resolve, reject) {
|
||||
//web3.provider.send(request, function (err, result) {
|
||||
//if (!err) {
|
||||
//resolve(result);
|
||||
//return;
|
||||
//}
|
||||
//reject(err);
|
||||
//});
|
||||
//});
|
||||
//}).catch(function(err) {
|
||||
//console.error(err);
|
||||
//});
|
||||
};
|
||||
});
|
||||
};
|
||||
@@ -195,31 +205,47 @@ var setupProperties = function (obj, properties) {
|
||||
properties.forEach(function (property) {
|
||||
var proto = {};
|
||||
proto.get = function () {
|
||||
return new Promise(function(resolve, reject) {
|
||||
web3.provider.send({call: property.getter}, function(err, result) {
|
||||
if (!err) {
|
||||
resolve(result);
|
||||
return;
|
||||
}
|
||||
reject(err);
|
||||
});
|
||||
var result = web3.provider.send({
|
||||
call: property.getter
|
||||
});
|
||||
|
||||
result = JSON.parse(result);
|
||||
return result.result;
|
||||
|
||||
|
||||
//return new Promise(function(resolve, reject) {
|
||||
//web3.provider.send({call: property.getter}, function(err, result) {
|
||||
//if (!err) {
|
||||
//resolve(result);
|
||||
//return;
|
||||
//}
|
||||
//reject(err);
|
||||
//});
|
||||
//});
|
||||
};
|
||||
if (property.setter) {
|
||||
proto.set = function (val) {
|
||||
return flattenPromise([val]).then(function (args) {
|
||||
return new Promise(function (resolve) {
|
||||
web3.provider.send({call: property.setter, args: args}, function (err, result) {
|
||||
if (!err) {
|
||||
resolve(result);
|
||||
return;
|
||||
}
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}).catch(function (err) {
|
||||
console.error(err);
|
||||
var result = web3.provider.send({
|
||||
call: property.setter,
|
||||
args: [val]
|
||||
});
|
||||
|
||||
result = JSON.parse(result);
|
||||
return result.result;
|
||||
|
||||
//return flattenPromise([val]).then(function (args) {
|
||||
//return new Promise(function (resolve) {
|
||||
//web3.provider.send({call: property.setter, args: args}, function (err, result) {
|
||||
//if (!err) {
|
||||
//resolve(result);
|
||||
//return;
|
||||
//}
|
||||
//reject(err);
|
||||
//});
|
||||
//});
|
||||
//}).catch(function (err) {
|
||||
//console.error(err);
|
||||
//});
|
||||
};
|
||||
}
|
||||
Object.defineProperty(obj, property.name, proto);
|
||||
|
Reference in New Issue
Block a user