diff --git a/integration/batching-with-context/batching.ts b/integration/batching-with-context/batching.ts index 9c5ef06d4..00edf4034 100644 --- a/integration/batching-with-context/batching.ts +++ b/integration/batching-with-context/batching.ts @@ -662,6 +662,10 @@ export class EntityServiceClientImpl implements Ent private readonly rpc: Rpc; constructor(rpc: Rpc) { this.rpc = rpc; + this.BatchQuery = this.BatchQuery.bind(this); + this.BatchMapQuery = this.BatchMapQuery.bind(this); + this.GetOnlyMethod = this.GetOnlyMethod.bind(this); + this.WriteMethod = this.WriteMethod.bind(this); } GetQuery(ctx: Context, id: string): Promise { const dl = ctx.getDataLoader('batching.EntityService.BatchQuery', () => { diff --git a/integration/batching/batching.ts b/integration/batching/batching.ts index 90ad9a610..25a025910 100644 --- a/integration/batching/batching.ts +++ b/integration/batching/batching.ts @@ -658,6 +658,10 @@ export class EntityServiceClientImpl implements EntityService { private readonly rpc: Rpc; constructor(rpc: Rpc) { this.rpc = rpc; + this.BatchQuery = this.BatchQuery.bind(this); + this.BatchMapQuery = this.BatchMapQuery.bind(this); + this.GetOnlyMethod = this.GetOnlyMethod.bind(this); + this.WriteMethod = this.WriteMethod.bind(this); } BatchQuery(request: BatchQueryRequest): Promise { const data = BatchQueryRequest.encode(request).finish(); diff --git a/integration/grpc-web-go-server/example.ts b/integration/grpc-web-go-server/example.ts index ecdd0b973..1135f99ba 100644 --- a/integration/grpc-web-go-server/example.ts +++ b/integration/grpc-web-go-server/example.ts @@ -735,6 +735,8 @@ export class DashStateClientImpl implements DashState { private readonly rpc: Rpc; constructor(rpc: Rpc) { this.rpc = rpc; + this.UserSettings = this.UserSettings.bind(this); + this.ActiveUserSettingsStream = this.ActiveUserSettingsStream.bind(this); } UserSettings(request: Empty): Promise { const data = Empty.encode(request).finish(); @@ -764,6 +766,9 @@ export class DashAPICredsClientImpl implements DashAPICreds { private readonly rpc: Rpc; constructor(rpc: Rpc) { this.rpc = rpc; + this.Create = this.Create.bind(this); + this.Update = this.Update.bind(this); + this.Delete = this.Delete.bind(this); } Create(request: DashAPICredsCreateReq): Promise { const data = DashAPICredsCreateReq.encode(request).finish(); diff --git a/integration/meta-typings/simple.ts b/integration/meta-typings/simple.ts index 4737c9dcf..11bbdda36 100644 --- a/integration/meta-typings/simple.ts +++ b/integration/meta-typings/simple.ts @@ -1216,6 +1216,7 @@ export class PingServiceClientImpl implements PingService { private readonly rpc: Rpc; constructor(rpc: Rpc) { this.rpc = rpc; + this.ping = this.ping.bind(this); } ping(request: PingRequest): Promise { const data = PingRequest.encode(request).finish(); diff --git a/integration/no-proto-package/no-proto-package.ts b/integration/no-proto-package/no-proto-package.ts index 316b20a1e..4b5c6a19a 100644 --- a/integration/no-proto-package/no-proto-package.ts +++ b/integration/no-proto-package/no-proto-package.ts @@ -112,6 +112,7 @@ export class UserStateClientImpl implements UserState { private readonly rpc: Rpc; constructor(rpc: Rpc) { this.rpc = rpc; + this.GetUsers = this.GetUsers.bind(this); } GetUsers(request: Empty): Promise { const data = Empty.encode(request).finish(); diff --git a/integration/simple-optionals/simple.ts b/integration/simple-optionals/simple.ts index 901cfa1fe..f68098d33 100644 --- a/integration/simple-optionals/simple.ts +++ b/integration/simple-optionals/simple.ts @@ -1900,6 +1900,7 @@ export class PingServiceClientImpl implements PingService { private readonly rpc: Rpc; constructor(rpc: Rpc) { this.rpc = rpc; + this.ping = this.ping.bind(this); } ping(request: PingRequest): Promise { const data = PingRequest.encode(request).finish(); diff --git a/integration/simple-snake/simple.ts b/integration/simple-snake/simple.ts index 7b83c8e77..fbf6776f5 100644 --- a/integration/simple-snake/simple.ts +++ b/integration/simple-snake/simple.ts @@ -1900,6 +1900,7 @@ export class PingServiceClientImpl implements PingService { private readonly rpc: Rpc; constructor(rpc: Rpc) { this.rpc = rpc; + this.ping = this.ping.bind(this); } ping(request: PingRequest): Promise { const data = PingRequest.encode(request).finish(); diff --git a/integration/simple-unrecognized-enum/simple.ts b/integration/simple-unrecognized-enum/simple.ts index 49e358e3c..3a309d4cd 100644 --- a/integration/simple-unrecognized-enum/simple.ts +++ b/integration/simple-unrecognized-enum/simple.ts @@ -1891,6 +1891,7 @@ export class PingServiceClientImpl implements PingService { private readonly rpc: Rpc; constructor(rpc: Rpc) { this.rpc = rpc; + this.ping = this.ping.bind(this); } ping(request: PingRequest): Promise { const data = PingRequest.encode(request).finish(); diff --git a/integration/simple/simple.ts b/integration/simple/simple.ts index 317faa5c5..a5fe5d61d 100644 --- a/integration/simple/simple.ts +++ b/integration/simple/simple.ts @@ -2562,6 +2562,7 @@ export class PingServiceClientImpl implements PingService { private readonly rpc: Rpc; constructor(rpc: Rpc) { this.rpc = rpc; + this.ping = this.ping.bind(this); } ping(request: PingRequest): Promise { const data = PingRequest.encode(request).finish(); diff --git a/src/generate-services.ts b/src/generate-services.ts index e814b08c5..61ccedb33 100644 --- a/src/generate-services.ts +++ b/src/generate-services.ts @@ -148,7 +148,13 @@ export function generateServiceClientImpl( // Create the constructor(rpc: Rpc) const rpcType = options.context ? 'Rpc' : 'Rpc'; chunks.push(code`private readonly rpc: ${rpcType};`); - chunks.push(code`constructor(rpc: ${rpcType}) { this.rpc = rpc; }`); + chunks.push(code`constructor(rpc: ${rpcType}) {`); + chunks.push(code`this.rpc = rpc;`) + // Bind each FooService method to the FooServiceImpl class + for (const methodDesc of serviceDesc.method) { + chunks.push(code`this.${methodDesc.name} = this.${methodDesc.name}.bind(this);`); + } + chunks.push(code`}`); // Create a method for each FooService method for (const methodDesc of serviceDesc.method) {