-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
APP-7497: Add Button interface and client (#451)
- Loading branch information
1 parent
88206e2
commit f595ede
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export type { Button } from './button/button'; | ||
export { ButtonClient } from './button/client'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { Struct } from '@bufbuild/protobuf'; | ||
import type { Resource } from '../../types'; | ||
|
||
/** Represents a physical button. */ | ||
export interface Button extends Resource { | ||
/** Push the button. */ | ||
push: (extra?: Struct) => Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Struct, type JsonValue } from '@bufbuild/protobuf'; | ||
import type { CallOptions, PromiseClient } from '@connectrpc/connect'; | ||
import { ButtonService } from '../../gen/component/button/v1/button_connect'; | ||
import { PushRequest } from '../../gen/component/button/v1/button_pb'; | ||
import type { RobotClient } from '../../robot'; | ||
import type { Options } from '../../types'; | ||
import { doCommandFromClient } from '../../utils'; | ||
import type { Button } from './button'; | ||
|
||
/** | ||
* A gRPC-web client for the Button component. | ||
* | ||
* @group Clients | ||
*/ | ||
export class ButtonClient implements Button { | ||
private client: PromiseClient<typeof ButtonService>; | ||
private readonly name: string; | ||
private readonly options: Options; | ||
public callOptions: CallOptions = { headers: {} as Record<string, string> }; | ||
|
||
constructor(client: RobotClient, name: string, options: Options = {}) { | ||
this.client = client.createServiceClient(ButtonService); | ||
this.name = name; | ||
this.options = options; | ||
} | ||
|
||
async push(extra = {}, callOptions = this.callOptions) { | ||
const request = new PushRequest({ | ||
name: this.name, | ||
extra: Struct.fromJson(extra), | ||
}); | ||
|
||
this.options.requestLogger?.(request); | ||
|
||
await this.client.push(request, callOptions); | ||
} | ||
|
||
async doCommand( | ||
command: Struct, | ||
callOptions = this.callOptions | ||
): Promise<JsonValue> { | ||
return doCommandFromClient( | ||
this.client.doCommand, | ||
this.name, | ||
command, | ||
this.options, | ||
callOptions | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters