File

src/call.ts

Extends

Promise

Index

Methods

Methods

cancel
cancel()
Defined in src/call.ts:106
Returns : void
import {status} from '@grpc/grpc-js';

import {
  APICallback,
  NextPageRequestType,
  RawResponseType,
  RequestType,
  ResponseType,
  ResultTuple,
  SimpleCallbackFunction,
} from './apitypes';
import {GoogleError} from './googleError';

export class OngoingCall {
  callback?: APICallback;
  cancelFunc?: () => void;
  completed: boolean;

  /**
   * OngoingCall manages callback, API calls, and cancellation
   * of the API calls.
   * @param {APICallback=} callback
   *   The callback to be called asynchronously when the API call
   *   finishes.
   * @constructor
   * @property {APICallback} callback
   *   The callback function to be called.
   * @private
   */
  constructor(callback?: APICallback) {
    this.callback = callback;
    this.completed = false;
  }

  /**
   * Cancels the ongoing promise.
   */
  cancel(): void {
    if (this.completed) {
      return;
    }
    this.completed = true;
    if (this.cancelFunc) {
      this.cancelFunc();
    } else {
      const error = new GoogleError('cancelled');
      error.code = status.CANCELLED;
      this.callback!(error);
    }
  }

  /**
   * Call calls the specified function. Result will be used to fulfill
   * the promise.
   *
   * @param {SimpleCallbackFunction} func
   *   A function for an API call.
   * @param {Object} argument
   *   A request object.
   */
  call(func: SimpleCallbackFunction, argument: RequestType): void {
    if (this.completed) {
      return;
    }
    // tslint:disable-next-line no-any
    const canceller = func(argument, (...args: any[]) => {
      this.completed = true;
      setImmediate(this.callback!, ...args);
    });
    this.cancelFunc = () => canceller.cancel();
  }
}

export interface CancellablePromise<T> extends Promise<T> {
  cancel(): void;
}

export class OngoingCallPromise extends OngoingCall {
  promise: CancellablePromise<ResultTuple>;
  /**
   * GaxPromise is GRPCCallbackWrapper, but it holds a promise when
   * the API call finishes.
   * @param {Function} PromiseCtor - A constructor for a promise that implements
   * the ES6 specification of promise.
   * @constructor
   * @private
   */
  // tslint:disable-next-line variable-name
  constructor(PromiseCtor: PromiseConstructor) {
    super();
    this.promise = new PromiseCtor((resolve, reject) => {
      this.callback = (
        err: GoogleError | null,
        response?: ResponseType,
        next?: NextPageRequestType | null,
        rawResponse?: RawResponseType
      ) => {
        if (err) {
          reject(err);
        } else if (response !== undefined) {
          resolve([response, next, rawResponse]);
        } else {
          throw new GoogleError('Neither error nor response are defined');
        }
      };
    }) as CancellablePromise<ResultTuple>;
    this.promise.cancel = () => {
      this.cancel();
    };
  }
}

result-matching ""

    No results matching ""