Skip to content

Commit

Permalink
Allow canceling unary calls (improbable-eng#124)
Browse files Browse the repository at this point in the history
* Allow canceling unary calls

- Fixes improbable-eng/grpc-web#252

* Update examples
  • Loading branch information
virtuald authored and jonnyreeves committed Oct 16, 2018
1 parent b7267ea commit 7846e2f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions examples/generated/proto/examplecom/simple_service_pb_service.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions examples/generated/proto/orphan_pb_service.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion examples/generated/proto/orphan_pb_service.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions src/service/grpcweb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ function generateTypescriptDefinition(fileDescriptor: FileDescriptorProto, expor
printer.printLn(`export type Status = { details: string, code: number; metadata: grpc.Metadata }`);
printer.printLn(`export type ServiceClientOptions = { transport: grpc.TransportConstructor; debug?: boolean }`);
printer.printEmptyLn();
printer.printLn("interface UnaryResponse {");
printer.printIndentedLn("cancel(): void;");
printer.printLn("}");
printer.printLn(`interface ResponseStream<T> {`);
printer.printIndentedLn(`cancel(): void;`);
printer.printIndentedLn(`on(type: 'data', handler: (message: T) => void): ResponseStream<T>;`);
Expand Down Expand Up @@ -325,7 +328,7 @@ function printUnaryStubMethod(printer: CodePrinter, method: RPCMethodDescriptor)
.indent().printLn(`if (arguments.length === 2) {`)
.indent().printLn(`callback = arguments[1];`)
.dedent().printLn("}")
.printLn(`grpc.unary(${method.serviceName}.${method.nameAsPascalCase}, {`)
.printLn(`var client = grpc.unary(${method.serviceName}.${method.nameAsPascalCase}, {`)
.indent().printLn(`request: requestMessage,`)
.printLn(`host: this.serviceHost,`)
.printLn(`metadata: metadata,`)
Expand All @@ -344,7 +347,13 @@ function printUnaryStubMethod(printer: CodePrinter, method: RPCMethodDescriptor)
.dedent().printLn(`}`)
.dedent().printLn(`}`)
.dedent().printLn(`});`)
.dedent().printLn(`};`);
.printLn(`return {`)
.indent().printLn(`cancel: function () {`)
.indent().printLn(`callback = null;`)
.printLn(`client.close();`)
.dedent().printLn(`}`)
.dedent().printLn(`};`)
.dedent().printLn(`};`);
}

function printServerStreamStubMethod(printer: CodePrinter, method: RPCMethodDescriptor) {
Expand Down Expand Up @@ -510,11 +519,11 @@ function printUnaryStubMethodTypes(printer: CodePrinter, method: RPCMethodDescri
.indent().printLn(`requestMessage: ${method.requestType},`)
.printLn(`metadata: grpc.Metadata,`)
.printLn(`callback: (error: ServiceError|null, responseMessage: ${method.responseType}|null) => void`)
.dedent().printLn(`): void;`)
.dedent().printLn(`): UnaryResponse;`)
.printLn(`${method.nameAsCamelCase}(`)
.indent().printLn(`requestMessage: ${method.requestType},`)
.printLn(`callback: (error: ServiceError|null, responseMessage: ${method.responseType}|null) => void`)
.dedent().printLn(`): void;`);
.dedent().printLn(`): UnaryResponse;`);
}

function printServerStreamStubMethodTypes(printer: CodePrinter, method: RPCMethodDescriptor) {
Expand Down

0 comments on commit 7846e2f

Please sign in to comment.