Skip to content

Commit

Permalink
Bind service methods to class
Browse files Browse the repository at this point in the history
This makes it easier to pass around service function references.
  • Loading branch information
Eric Butler committed May 8, 2021
1 parent a32f748 commit b315261
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions integration/batching-with-context/batching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,10 @@ export class EntityServiceClientImpl<Context extends DataLoaders> implements Ent
private readonly rpc: Rpc<Context>;
constructor(rpc: Rpc<Context>) {
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<Entity> {
const dl = ctx.getDataLoader('batching.EntityService.BatchQuery', () => {
Expand Down
4 changes: 4 additions & 0 deletions integration/batching/batching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BatchQueryResponse> {
const data = BatchQueryRequest.encode(request).finish();
Expand Down
5 changes: 5 additions & 0 deletions integration/grpc-web-go-server/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DashUserSettingsState> {
const data = Empty.encode(request).finish();
Expand Down Expand Up @@ -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<DashCred> {
const data = DashAPICredsCreateReq.encode(request).finish();
Expand Down
1 change: 1 addition & 0 deletions integration/meta-typings/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PingResponse> {
const data = PingRequest.encode(request).finish();
Expand Down
1 change: 1 addition & 0 deletions integration/no-proto-package/no-proto-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<User> {
const data = Empty.encode(request).finish();
Expand Down
1 change: 1 addition & 0 deletions integration/simple-optionals/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PingResponse> {
const data = PingRequest.encode(request).finish();
Expand Down
1 change: 1 addition & 0 deletions integration/simple-snake/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PingResponse> {
const data = PingRequest.encode(request).finish();
Expand Down
1 change: 1 addition & 0 deletions integration/simple-unrecognized-enum/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PingResponse> {
const data = PingRequest.encode(request).finish();
Expand Down
1 change: 1 addition & 0 deletions integration/simple/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PingResponse> {
const data = PingRequest.encode(request).finish();
Expand Down
8 changes: 7 additions & 1 deletion src/generate-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,13 @@ export function generateServiceClientImpl(
// Create the constructor(rpc: Rpc)
const rpcType = options.context ? 'Rpc<Context>' : '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) {
Expand Down

0 comments on commit b315261

Please sign in to comment.