UNPKG

5.5 kBTypeScriptView Raw
1/**
2 * Copyright 2020 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16/// <reference types="node" />
17import { EventEmitter } from 'events';
18import { ResultTuple } from '../apitypes';
19import { CancellablePromise } from '../call';
20import { BackoffSettings, CallOptions } from '../gax';
21import { GoogleError } from '../googleError';
22import { Metadata } from '../grpc';
23import { LongRunningDescriptor } from './longRunningDescriptor';
24import * as operationProtos from '../../protos/operations';
25/**
26 * @callback GetOperationCallback
27 * @param {?Error} error
28 * @param {?Object} result
29 * @param {?Object} metadata
30 * @param {?google.longrunning.Operation} rawResponse
31 */
32export interface GetOperationCallback {
33 (err?: Error | null, result?: {}, metadata?: {}, rawResponse?: LROOperation): void;
34}
35declare type LROOperation = operationProtos.google.longrunning.Operation;
36export declare class Operation extends EventEmitter {
37 completeListeners: number;
38 hasActiveListeners: boolean;
39 latestResponse: LROOperation;
40 longrunningDescriptor: LongRunningDescriptor;
41 result: {} | null;
42 metadata: Metadata | null;
43 backoffSettings: BackoffSettings;
44 _callOptions?: CallOptions;
45 currentCallPromise_?: CancellablePromise<ResultTuple>;
46 name?: string;
47 done?: boolean;
48 error?: GoogleError;
49 response?: {};
50 /**
51 * Wrapper for a google.longrunnung.Operation.
52 *
53 * @constructor
54 *
55 * @param {google.longrunning.Operation} grpcOp - The operation to be wrapped.
56 * @param {LongRunningDescriptor} longrunningDescriptor - This defines the
57 * operations service client and unpacking mechanisms for the operation.
58 * @param {BackoffSettings} backoffSettings - The backoff settings used in
59 * in polling the operation.
60 * @param {CallOptions} callOptions - CallOptions used in making get operation
61 * requests.
62 */
63 constructor(grpcOp: LROOperation, longrunningDescriptor: LongRunningDescriptor, backoffSettings: BackoffSettings, callOptions?: CallOptions);
64 /**
65 * Begin listening for events on the operation. This method keeps track of how
66 * many "complete" listeners are registered and removed, making sure polling
67 * is handled automatically.
68 *
69 * As long as there is one active "complete" listener, the connection is open.
70 * When there are no more listeners, the polling stops.
71 *
72 * @private
73 */
74 _listenForEvents(): void;
75 /**
76 * Cancels current polling api call and cancels the operation.
77 *
78 * @return {Promise} the promise of the OperationsClient#cancelOperation api
79 * request.
80 */
81 cancel(): Promise<operationProtos.google.protobuf.Empty>;
82 /**
83 * Get the updated status of the operation. If the Operation has previously
84 * completed, this will use the status of the cached completed operation.
85 *
86 * - callback(err): Operation failed
87 * - callback(null, result, metadata, rawResponse): Operation complete
88 * - callback(null, null, metadata, rawResponse): Operation incomplete
89 *
90 * @param {getOperationCallback} callback - Callback to handle the polled
91 * operation result and metadata.
92 * @return {Promise|undefined} - This returns a promise if a callback is not specified.
93 * The promise resolves to an array where the first element is the unpacked
94 * result, the second element is the metadata, and the third element is the
95 * raw response of the api call. The promise rejects if the operation returns
96 * an error.
97 */
98 getOperation(): Promise<{}>;
99 getOperation(callback: GetOperationCallback): void;
100 _unpackResponse(op: LROOperation, callback?: GetOperationCallback): void;
101 /**
102 * Poll `getOperation` to check the operation's status. This runs a loop to
103 * ping using the backoff strategy specified at initialization.
104 *
105 * Note: This method is automatically called once a "complete" event handler
106 * is registered on the operation.
107 *
108 * @private
109 */
110 startPolling_(): void;
111 /**
112 * Wraps the `complete` and `error` events in a Promise.
113 *
114 * @return {promise} - Promise that resolves on operation completion and rejects
115 * on operation error.
116 */
117 promise(): Promise<unknown>;
118}
119/**
120 * Method used to create Operation objects.
121 *
122 * @constructor
123 *
124 * @param {google.longrunning.Operation} op - The operation to be wrapped.
125 * @param {LongRunningDescriptor} longrunningDescriptor - This defines the
126 * operations service client and unpacking mechanisms for the operation.
127 * @param {BackoffSettings} backoffSettings - The backoff settings used in
128 * in polling the operation.
129 * @param {CallOptions=} callOptions - CallOptions used in making get operation
130 * requests.
131 */
132export declare function operation(op: LROOperation, longrunningDescriptor: LongRunningDescriptor, backoffSettings: BackoffSettings, callOptions?: CallOptions): Operation;
133export {};