File

src/streamingCalls/streamingApiCaller.ts

Implements

APICaller

Index

Properties
Methods

Constructor

constructor(descriptor: StreamDescriptor)

An API caller for methods of gRPC streaming.

Parameters :
Name Type Optional Description
descriptor StreamDescriptor No
  • the descriptor of the method structure.

Properties

descriptor
Type : StreamDescriptor

Methods

call
call(apiCall: SimpleCallbackFunction, argument: literal type, settings: literal type, stream: StreamProxy)
Parameters :
Name Type Optional
apiCall SimpleCallbackFunction No
argument literal type No
settings literal type No
stream StreamProxy No
Returns : void
fail
fail(stream: CancellableStream, err: Error)
Parameters :
Name Type Optional
stream CancellableStream No
err Error No
Returns : void
init
init(settings: ApiCallerSettings, callback: APICallback)
Parameters :
Name Type Optional
settings ApiCallerSettings No
callback APICallback No
Returns : StreamProxy
result
result(stream: CancellableStream)
Parameters :
Name Type Optional
stream CancellableStream No
Returns : any
wrap
wrap(func: GRPCCall)
Parameters :
Name Type Optional
func GRPCCall No
Returns : GRPCCall
import {APICaller, ApiCallerSettings} from '../apiCaller';
import {
  APICallback,
  BiDiStreamingCall,
  CancellableStream,
  ClientStreamingCall,
  GRPCCall,
  ServerStreamingCall,
  SimpleCallbackFunction,
} from '../apitypes';
import {warn} from '../warnings';

import {StreamDescriptor} from './streamDescriptor';
import {StreamProxy, StreamType} from './streaming';

export class StreamingApiCaller implements APICaller {
  descriptor: StreamDescriptor;

  /**
   * An API caller for methods of gRPC streaming.
   * @private
   * @constructor
   * @param {StreamDescriptor} descriptor - the descriptor of the method structure.
   */
  constructor(descriptor: StreamDescriptor) {
    this.descriptor = descriptor;
  }

  init(settings: ApiCallerSettings, callback: APICallback): StreamProxy {
    return new StreamProxy(this.descriptor.type, callback);
  }

  wrap(func: GRPCCall): GRPCCall {
    switch (this.descriptor.type) {
      case StreamType.SERVER_STREAMING:
        return (argument: {}, metadata: {}, options: {}) => {
          return (func as ServerStreamingCall)(argument, metadata, options);
        };
      case StreamType.CLIENT_STREAMING:
        return (
          argument: {},
          metadata: {},
          options: {},
          callback?: APICallback
        ) => {
          return (func as ClientStreamingCall)(metadata, options, callback);
        };
      case StreamType.BIDI_STREAMING:
        return (argument: {}, metadata: {}, options: {}) => {
          return (func as BiDiStreamingCall)(metadata, options);
        };
      default:
        warn(
          'streaming_wrap_unknown_stream_type',
          `Unknown stream type: ${this.descriptor.type}`
        );
    }
    return func;
  }

  call(
    apiCall: SimpleCallbackFunction,
    argument: {},
    settings: {},
    stream: StreamProxy
  ) {
    stream.setStream(apiCall, argument);
  }

  fail(stream: CancellableStream, err: Error) {
    stream.emit('error', err);
  }

  result(stream: CancellableStream) {
    return stream;
  }
}

result-matching ""

    No results matching ""