File

src/longRunningCalls/longRunningApiCaller.ts

Implements

APICaller

Index

Properties
Methods

Constructor

constructor(longrunningDescriptor: LongRunningDescriptor)

Creates an API caller that performs polling on a long running operation.

Parameters :
Name Type Optional Description
longrunningDescriptor LongRunningDescriptor No
  • Holds the decoders used for unpacking responses and the operationsClient used for polling the operation.

Properties

longrunningDescriptor
Type : LongRunningDescriptor

Methods

call
call(apiCall: SimpleCallbackFunction, argument: literal type, settings: CallOptions, canceller: OngoingCallPromise)
Parameters :
Name Type Optional
apiCall SimpleCallbackFunction No
argument literal type No
settings CallOptions No
canceller OngoingCallPromise No
Returns : void
fail
fail(canceller: OngoingCallPromise, err: GoogleError)
Parameters :
Name Type Optional
canceller OngoingCallPromise No
err GoogleError No
Returns : void
init
init(settings: ApiCallerSettings, callback?: APICallback)
Parameters :
Name Type Optional
settings ApiCallerSettings No
callback APICallback Yes
Returns : OngoingCallPromise | OngoingCall
result
result(canceller: OngoingCallPromise)
Parameters :
Name Type Optional
canceller OngoingCallPromise No
Returns : any
wrap
wrap(func: GRPCCall)
Parameters :
Name Type Optional
func GRPCCall No
Returns : GRPCCall
import {APICaller, ApiCallerSettings} from '../apiCaller';
import {APICallback, GRPCCall, SimpleCallbackFunction} from '../apitypes';
import {OngoingCall, OngoingCallPromise} from '../call';
import {
  BackoffSettings,
  CallOptions,
  CallSettings,
  createBackoffSettings,
  createDefaultBackoffSettings,
} from '../gax';
import {GoogleError} from '../googleError';

import {Operation} from './longrunning';
import {LongRunningDescriptor} from './longRunningDescriptor';

export class LongrunningApiCaller implements APICaller {
  longrunningDescriptor: LongRunningDescriptor;
  /**
   * Creates an API caller that performs polling on a long running operation.
   *
   * @private
   * @constructor
   * @param {LongRunningDescriptor} longrunningDescriptor - Holds the
   * decoders used for unpacking responses and the operationsClient
   * used for polling the operation.
   */
  constructor(longrunningDescriptor: LongRunningDescriptor) {
    this.longrunningDescriptor = longrunningDescriptor;
  }

  init(
    settings: ApiCallerSettings,
    callback?: APICallback
  ): OngoingCallPromise | OngoingCall {
    if (callback) {
      return new OngoingCall(callback);
    }
    return new OngoingCallPromise(settings.promise);
  }

  wrap(func: GRPCCall): GRPCCall {
    return func;
  }

  call(
    apiCall: SimpleCallbackFunction,
    argument: {},
    settings: CallOptions,
    canceller: OngoingCallPromise
  ) {
    canceller.call((argument, callback) => {
      return this._wrapOperation(apiCall, settings, argument, callback);
    }, argument);
  }

  private _wrapOperation(
    apiCall: SimpleCallbackFunction,
    settings: CallOptions,
    argument: {},
    callback: APICallback
  ) {
    let backoffSettings: BackoffSettings | undefined = settings.longrunning;
    if (!backoffSettings) {
      backoffSettings = createDefaultBackoffSettings();
    }

    const longrunningDescriptor = this.longrunningDescriptor;
    return apiCall(
      argument,
      (err: GoogleError | null, rawResponse: {} | null | undefined) => {
        if (err) {
          callback(err, null, null, rawResponse as Operation);
          return;
        }

        const operation = new Operation(
          rawResponse as Operation,
          longrunningDescriptor,
          backoffSettings!,
          settings
        );

        callback(null, operation, rawResponse);
      }
    );
  }

  fail(canceller: OngoingCallPromise, err: GoogleError): void {
    canceller.callback!(err);
  }

  result(canceller: OngoingCallPromise) {
    return canceller.promise;
  }
}

result-matching ""

    No results matching ""