Skip to content

Commit

Permalink
feat: 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 9, 2021
1 parent a32f748 commit c89471e
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 3 deletions.
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/grpc-web-no-streaming-observable/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ export class DashStateClientImpl implements DashState {

constructor(rpc: Rpc) {
this.rpc = rpc;
this.UserSettings = this.UserSettings.bind(this);
}

UserSettings(request: DeepPartial<Empty>, metadata?: grpc.Metadata): Observable<DashUserSettingsState> {
Expand Down
1 change: 1 addition & 0 deletions integration/grpc-web-no-streaming/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ export class DashStateClientImpl implements DashState {

constructor(rpc: Rpc) {
this.rpc = rpc;
this.UserSettings = this.UserSettings.bind(this);
}

UserSettings(request: DeepPartial<Empty>, metadata?: grpc.Metadata): Promise<DashUserSettingsState> {
Expand Down
9 changes: 9 additions & 0 deletions integration/grpc-web/example-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@ describe('grpc-web', () => {
const client = new DashStateClientImpl(rpc);
client.UserSettings({});
});
it('binds rpc function', () => {
const rpc = {
unary: jest.fn(),
invoke: jest.fn(),
};
const client = new DashStateClientImpl(rpc);
const userSettings = client.UserSettings;
userSettings({});
})
});
5 changes: 5 additions & 0 deletions integration/grpc-web/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,8 @@ export class DashStateClientImpl implements DashState {

constructor(rpc: Rpc) {
this.rpc = rpc;
this.UserSettings = this.UserSettings.bind(this);
this.ActiveUserSettingsStream = this.ActiveUserSettingsStream.bind(this);
}

UserSettings(request: DeepPartial<Empty>, metadata?: grpc.Metadata): Promise<DashUserSettingsState> {
Expand Down Expand Up @@ -814,6 +816,9 @@ export class DashAPICredsClientImpl implements DashAPICreds {

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: DeepPartial<DashAPICredsCreateReq>, metadata?: grpc.Metadata): Promise<DashCred> {
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
12 changes: 12 additions & 0 deletions integration/simple/simple-service-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PingRequest, PingServiceClientImpl } from './simple';

describe('simple', () => {
it('binds rpc function', () => {
const rpc = {
request: jest.fn().mockResolvedValue({}),
};
const client = new PingServiceClientImpl(rpc);
const ping = client.ping;
ping(PingRequest.fromPartial({}));
})
})
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: 6 additions & 2 deletions src/generate-grpc-web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ export function generateGrpcClientImpl(
private readonly rpc: Rpc;
constructor(rpc: Rpc) {
this.rpc = rpc;
}
`);
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`}\n`);

// Create a method for each FooService method
for (const methodDesc of serviceDesc.method) {
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 c89471e

Please sign in to comment.