1 | ;
|
2 | /**
|
3 | * Copyright 2020 Google LLC
|
4 | *
|
5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | * you may not use this file except in compliance with the License.
|
7 | * You may obtain a copy of the License at
|
8 | *
|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
10 | *
|
11 | * Unless required by applicable law or agreed to in writing, software
|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 | * See the License for the specific language governing permissions and
|
15 | * limitations under the License.
|
16 | */
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.OngoingCallPromise = exports.OngoingCall = void 0;
|
19 | const status_1 = require("./status");
|
20 | const googleError_1 = require("./googleError");
|
21 | class OngoingCall {
|
22 | /**
|
23 | * OngoingCall manages callback, API calls, and cancellation
|
24 | * of the API calls.
|
25 | * @param {APICallback=} callback
|
26 | * The callback to be called asynchronously when the API call
|
27 | * finishes.
|
28 | * @constructor
|
29 | * @property {APICallback} callback
|
30 | * The callback function to be called.
|
31 | * @private
|
32 | */
|
33 | constructor(callback) {
|
34 | this.callback = callback;
|
35 | this.completed = false;
|
36 | }
|
37 | /**
|
38 | * Cancels the ongoing promise.
|
39 | */
|
40 | cancel() {
|
41 | if (this.completed) {
|
42 | return;
|
43 | }
|
44 | this.completed = true;
|
45 | if (this.cancelFunc) {
|
46 | this.cancelFunc();
|
47 | }
|
48 | else {
|
49 | const error = new googleError_1.GoogleError('cancelled');
|
50 | error.code = status_1.Status.CANCELLED;
|
51 | this.callback(error);
|
52 | }
|
53 | }
|
54 | /**
|
55 | * Call calls the specified function. Result will be used to fulfill
|
56 | * the promise.
|
57 | *
|
58 | * @param {SimpleCallbackFunction} func
|
59 | * A function for an API call.
|
60 | * @param {Object} argument
|
61 | * A request object.
|
62 | */
|
63 | call(func, argument) {
|
64 | if (this.completed) {
|
65 | return;
|
66 | }
|
67 | const canceller = func(argument, (err, response, next, rawResponse) => {
|
68 | this.completed = true;
|
69 | setImmediate(this.callback, err, response, next, rawResponse);
|
70 | });
|
71 | this.cancelFunc = () => canceller.cancel();
|
72 | }
|
73 | }
|
74 | exports.OngoingCall = OngoingCall;
|
75 | class OngoingCallPromise extends OngoingCall {
|
76 | /**
|
77 | * GaxPromise is GRPCCallbackWrapper, but it holds a promise when
|
78 | * the API call finishes.
|
79 | * @constructor
|
80 | * @private
|
81 | */
|
82 | constructor() {
|
83 | let resolveCallback;
|
84 | let rejectCallback;
|
85 | const callback = (err, response, next, rawResponse) => {
|
86 | if (err) {
|
87 | // If gRPC metadata exist, parsed google.rpc.status details.
|
88 | if (err.metadata) {
|
89 | rejectCallback(googleError_1.GoogleError.parseGRPCStatusDetails(err));
|
90 | }
|
91 | else {
|
92 | rejectCallback(err);
|
93 | }
|
94 | }
|
95 | else if (response !== undefined) {
|
96 | resolveCallback([response, next || null, rawResponse || null]);
|
97 | }
|
98 | else {
|
99 | throw new googleError_1.GoogleError('Neither error nor response are defined');
|
100 | }
|
101 | };
|
102 | const promise = new Promise((resolve, reject) => {
|
103 | resolveCallback = resolve;
|
104 | rejectCallback = reject;
|
105 | });
|
106 | super(callback);
|
107 | this.promise = promise;
|
108 | this.promise.cancel = () => {
|
109 | this.cancel();
|
110 | };
|
111 | }
|
112 | }
|
113 | exports.OngoingCallPromise = OngoingCallPromise;
|
114 | //# sourceMappingURL=call.js.map |
\ | No newline at end of file |