Skip to content

Commit

Permalink
fix: fix a type of AnyDecoder (#619)
Browse files Browse the repository at this point in the history
* fix: fix a type of AnyDecoder

* fix LRO types
  • Loading branch information
alexander-fenster authored Oct 10, 2019
1 parent 73b3d8f commit ed46692
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/longRunningCalls/longRunningApiCaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
} from '../gax';
import {GoogleError} from '../googleError';

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

export class LongrunningApiCaller implements APICaller {
Expand Down Expand Up @@ -105,7 +105,7 @@ export class LongrunningApiCaller implements APICaller {
}

const operation = new Operation(
rawResponse as Operation,
rawResponse as LROOperation,
longrunningDescriptor,
backoffSettings!,
settings
Expand Down
4 changes: 2 additions & 2 deletions src/longRunningCalls/longRunningDescriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import * as protobuf from 'protobufjs';
import {Descriptor} from '../descriptor';
import {CallSettings} from '../gax';
import {Metadata} from '../grpc';
import {OperationsClient} from '../operationsClient';

import {LongrunningApiCaller} from './longRunningApiCaller';

/**
* A callback to upack a google.protobuf.Any message.
*/
export interface AnyDecoder {
(message: {}): Metadata;
(reader: protobuf.Reader | Uint8Array, length?: number): protobuf.Message<{}>;
}

/**
Expand Down
34 changes: 22 additions & 12 deletions src/longRunningCalls/longrunning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ import {GaxCallPromise, ResultTuple} from '../apitypes';
import {CancellablePromise} from '../call';
import {BackoffSettings, CallOptions} from '../gax';
import {GoogleError} from '../googleError';
import {Metadata, MetadataValue} from '../grpc';

import {Metadata} from '../grpc';
import {LongRunningDescriptor} from './longRunningDescriptor';

/**
Expand All @@ -52,14 +51,25 @@ export interface GetOperationCallback {
err?: Error | null,
result?: {},
metadata?: {},
rawResponse?: Operation
rawResponse?: LROOperation
): void;
}

// This interface is essentially the TypeScript representation of google.longrunning.Operation.
// TODO: we'll replace it with the proper type generated by `pbts`.
export interface LROOperation {
name: string;
metadata: {value: Uint8Array};
done: boolean;
result: string;
error: GoogleError;
response: {value: Uint8Array};
}

export class Operation extends EventEmitter {
completeListeners: number;
hasActiveListeners: boolean;
latestResponse: Operation;
latestResponse: LROOperation;
longrunningDescriptor: LongRunningDescriptor;
result: {} | null;
metadata: Metadata | null;
Expand All @@ -69,7 +79,7 @@ export class Operation extends EventEmitter {
name?: string;
done?: boolean;
error?: GoogleError;
response?: {value: {}};
response?: {};

/**
* Wrapper for a google.longrunnung.Operation.
Expand All @@ -85,7 +95,7 @@ export class Operation extends EventEmitter {
* requests.
*/
constructor(
grpcOp: Operation,
grpcOp: LROOperation,
longrunningDescriptor: LongRunningDescriptor,
backoffSettings: BackoffSettings,
callOptions?: CallOptions
Expand Down Expand Up @@ -201,8 +211,8 @@ export class Operation extends EventEmitter {
);

const noCallbackPromise = this.currentCallPromise_!.then(responses => {
self.latestResponse = responses[0] as Operation;
self._unpackResponse(responses[0] as Operation, callback);
self.latestResponse = responses[0] as LROOperation;
self._unpackResponse(responses[0] as LROOperation, callback);
return promisifyResponse()!;
});

Expand All @@ -211,7 +221,7 @@ export class Operation extends EventEmitter {
}
}

_unpackResponse(op: Operation, callback?: GetOperationCallback) {
_unpackResponse(op: LROOperation, callback?: GetOperationCallback) {
const responseDecoder = this.longrunningDescriptor.responseDecoder;
const metadataDecoder = this.longrunningDescriptor.metadataDecoder;
let response: {};
Expand All @@ -237,7 +247,7 @@ export class Operation extends EventEmitter {
}

if (metadataDecoder && op.metadata) {
metadata = metadataDecoder(op.metadata.value);
metadata = (metadataDecoder(op.metadata.value) as unknown) as Metadata;
this.metadata = metadata;
}
if (callback) {
Expand Down Expand Up @@ -265,7 +275,7 @@ export class Operation extends EventEmitter {
if (this.backoffSettings.totalTimeoutMillis) {
deadline = now.getTime() + this.backoffSettings.totalTimeoutMillis;
}
let previousMetadataBytes: MetadataValue;
let previousMetadataBytes: Uint8Array;
if (this.latestResponse.metadata) {
previousMetadataBytes = this.latestResponse.metadata.value;
}
Expand Down Expand Up @@ -378,7 +388,7 @@ export class Operation extends EventEmitter {
* requests.
*/
export function operation(
op: Operation,
op: LROOperation,
longrunningDescriptor: LongRunningDescriptor,
backoffSettings: BackoffSettings,
callOptions?: CallOptions
Expand Down
4 changes: 2 additions & 2 deletions test/unit/longrunning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ describe('longrunning', () => {
totalTimeoutMillis
);
const operation = longrunning.operation(
(SUCCESSFUL_OP as {}) as longrunning.Operation,
(SUCCESSFUL_OP as {}) as longrunning.LROOperation,
desc,
backoff
);
Expand Down Expand Up @@ -488,7 +488,7 @@ describe('longrunning', () => {
return promise;
}
const operation = longrunning.operation(
(SUCCESSFUL_OP as {}) as longrunning.Operation,
(SUCCESSFUL_OP as {}) as longrunning.LROOperation,
desc,
backoff,
{
Expand Down

0 comments on commit ed46692

Please sign in to comment.