static setCallback_(stream, callback, useUnaryResponse) {
    let isResponseReceived = false;
    let responseReceived = null;
    let errorEmitted = false;

    stream.on('data', function(response) {
      isResponseReceived = true;
      responseReceived = response;
    });

    stream.on('error', function(error) {
      if (error.code != StatusCode.OK && !errorEmitted) {
        errorEmitted = true;
        callback(error, null);
      }
    });

    stream.on('status', function(status) {
      if (status.code != StatusCode.OK && !errorEmitted) {
        errorEmitted = true;
        callback(
            {
              code: status.code,
              message: status.details,
              metadata: status.metadata
            },
            null);
      } else if (useUnaryResponse) {
        callback(null, null, status);
      }
    });

    if (useUnaryResponse) {
      stream.on('metadata', function(metadata) {
        callback(null, null, null, metadata);
      });
    }

    stream.on('end', function() {
      if (!errorEmitted) {
        if (!isResponseReceived) {
          callback({
            code: StatusCode.UNKNOWN,
            message: 'Incomplete response',
          });
        } else if (useUnaryResponse) {
          callback(
            null, responseReceived, null, null,
            /* unaryResponseReceived= */ true);
        } else {
          callback(null, responseReceived);
        }
      }
      if (useUnaryResponse) {
        callback(null, null);
      }
    });
  }
@private
@static
@template RESPONSE
@param {!ClientReadableStream<RESPONSE>} stream
@param {function(?RpcError, ?RESPONSE, ?Status=, ?Object<string, string>=, ?boolean)|
    function(?RpcError,?RESPONSE)} callback
@param {boolean} useUnaryResponse Pass true to have the client make
multiple calls to the callback, using (error, response, status,
metadata, unaryResponseReceived) arguments. One of error, status,
metadata, or unaryResponseReceived will be truthy to indicate which piece
of information the client is providing in that call. After the stream
ends, it will call the callback an additional time with all falsy
arguments. Pass false to have the client make one call to the callback
using (error, response) arguments.