Modified watchState functions to match C API

pull/2696/head
murgatroid99 9 years ago
parent 937ac9b023
commit 371354673f
  1. 25
      src/node/ext/channel.cc
  2. 22
      src/node/src/client.js
  3. 9
      src/node/test/surface_test.js

@ -62,25 +62,6 @@ using v8::Persistent;
using v8::String;
using v8::Value;
class ConnectivityStateOp : public Op {
public:
Handle<Value> GetNodeValue() const {
return NanNew<Number>(new_state);
}
bool ParseOp(Handle<Value> value, grpc_op *out,
shared_ptr<Resources> resources) {
return true;
}
grpc_connectivity_state new_state;
protected:
std::string GetTypeString() const {
return "new_state";
}
};
NanCallback *Channel::constructor;
Persistent<FunctionTemplate> Channel::fun_tpl;
@ -252,12 +233,10 @@ NAN_METHOD(Channel::WatchConnectivityState) {
Handle<Function> callback_func = args[2].As<Function>();
NanCallback *callback = new NanCallback(callback_func);
Channel *channel = ObjectWrap::Unwrap<Channel>(args.This());
ConnectivityStateOp *op = new ConnectivityStateOp();
unique_ptr<OpVec> ops(new OpVec());
ops->push_back(unique_ptr<Op>(op));
grpc_channel_watch_connectivity_state(
channel->wrapped_channel, last_state, &op->new_state,
MillisecondsToTimespec(deadline), CompletionQueueAsyncWorker::GetQueue(),
channel->wrapped_channel, last_state, MillisecondsToTimespec(deadline),
CompletionQueueAsyncWorker::GetQueue(),
new struct tag(callback,
ops.release(),
shared_ptr<Resources>(nullptr)));

@ -562,9 +562,8 @@ exports.makeClientConstructor = function(methods, serviceName) {
* Wait for the client to be ready. The callback will be called when the
* client has successfully connected to the server, and it will be called
* with an error if the attempt to connect to the server has unrecoverablly
* failed or if the deadline expires. This function does not automatically
* attempt to initiate the connection, so the callback will not be called
* unless you also start a method call or call $tryConnect.
* failed or if the deadline expires. This function will make the channel
* start connecting if it has not already done so.
* @param {(Date|Number)} deadline When to stop waiting for a connection. Pass
* Infinity to wait forever.
* @param {function(Error)} callback The callback to call when done attempting
@ -572,12 +571,11 @@ exports.makeClientConstructor = function(methods, serviceName) {
*/
Client.prototype.$waitForReady = function(deadline, callback) {
var self = this;
var checkState = function(err, result) {
var checkState = function(err) {
if (err) {
callback(new Error('Failed to connect before the deadline'));
}
var new_state = result.new_state;
console.log(result);
var new_state = this.channel.getConnectivityState(true);
if (new_state === grpc.connectivityState.READY) {
callback();
} else if (new_state === grpc.connectivityState.FATAL_FAILURE) {
@ -586,17 +584,7 @@ exports.makeClientConstructor = function(methods, serviceName) {
self.channel.watchConnectivityState(new_state, deadline, checkState);
}
};
checkState(null, {new_state: this.channel.getConnectivityState()});
};
/**
* Attempt to connect to the server. That will happen automatically if
* you try to make a method call with this client, so this function should
* only be used if you want to know that you have a connection before making
* any calls.
*/
Client.prototype.$tryConnect = function() {
this.channel.getConnectivityState(true);
checkState();
};
_.each(methods, function(attrs, name) {

@ -149,20 +149,19 @@ describe('Client#$waitForReady', function() {
after(function() {
server.shutdown();
});
it('should complete when a call is initiated', function(done) {
it('should complete when called alone', function(done) {
client.$waitForReady(Infinity, function(error) {
assert.ifError(error);
done();
});
var call = client.div({}, function(err, response) {});
call.cancel();
});
it('should complete if $tryConnect is called', function(done) {
it('should complete when a call is initiated', function(done) {
client.$waitForReady(Infinity, function(error) {
assert.ifError(error);
done();
});
client.$tryConnect();
var call = client.div({}, function(err, response) {});
call.cancel();
});
it('should complete if called more than once', function(done) {
done = multiDone(done, 2);

Loading…
Cancel
Save