Skip to content

allow custom ID generators in Clients, and support String ids #2077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rename some thigns
  • Loading branch information
jakemac53 committed Apr 21, 2025
commit 8660fde312694cfe7e3dadd86d92e64491d78675
22 changes: 12 additions & 10 deletions pkgs/json_rpc_2/lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Client {
final StreamChannel<dynamic> _channel;

/// A function to generate the next request id.
Object Function() _generateId;
Object Function() _idGenerator;

/// The current batch of requests to be sent together.
///
Expand Down Expand Up @@ -52,13 +52,13 @@ class Client {
/// Note that the client won't begin listening to [channel] until
/// [Client.listen] is called.
///
/// If [generateId] is passed, it will be called to generate an ID for each
/// If [idGenerator] is passed, it will be called to generate an ID for each
/// request. Defaults to an auto-incrementing `int`. The value returned must
/// be either an `int` or `String`.
Client(StreamChannel<String> channel, {Object Function()? generateId})
Client(StreamChannel<String> channel, {Object Function()? idGenerator})
: this.withoutJson(
jsonDocument.bind(channel).transformStream(ignoreFormatExceptions),
generateId: generateId);
idGenerator: idGenerator);

/// Creates a [Client] that communicates using decoded messages over
/// [_channel].
Expand All @@ -69,11 +69,11 @@ class Client {
/// Note that the client won't begin listening to [_channel] until
/// [Client.listen] is called.
///
/// If [generateId] is passed, it will be called to generate an ID for each
/// If [_idGenerator] is passed, it will be called to generate an ID for each
/// request. Defaults to an auto-incrementing `int`. The value returned must
/// be either an `int` or `String`.
Client.withoutJson(this._channel, {Object Function()? generateId})
: _generateId = generateId ?? _incrementingIdGenerator {
Client.withoutJson(this._channel, {Object Function()? idGenerator})
: _idGenerator = idGenerator ?? _createIncrementingIdGenerator {
done.whenComplete(() {
for (var request in _pendingRequests.values) {
request.completer.completeError(StateError(
Expand Down Expand Up @@ -127,7 +127,7 @@ class Client {
/// Throws a [StateError] if the client is closed while the request is in
/// flight, or if the client is closed when this method is called.
Future<Object?> sendRequest(String method, [Object? parameters]) {
var id = _generateId();
var id = _idGenerator();
_send(method, parameters, id);

var completer = Completer<Object?>.sync();
Expand Down Expand Up @@ -255,8 +255,10 @@ class _Request {
_Request(this.method, this.completer, this.chain);
}

/// The default ID generator, returns an auto incrementing integer.
int Function() get _incrementingIdGenerator {
/// The default ID generator, uses an auto incrementing integer.
///
/// Each call returns a new function which starts back a `0`.
int Function() _createIncrementingIdGenerator() {
var nextId = 0;
return () => nextId++;
}
12 changes: 6 additions & 6 deletions pkgs/json_rpc_2/lib/src/peer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@ class Peer implements Client, Server {
/// specification. In particular, requests missing the `jsonrpc` parameter
/// will be accepted.
///
/// If [generateId] is passed, it will be called to generate an ID for each
/// If [idGenerator] is passed, it will be called to generate an ID for each
/// request. Defaults to an auto-incrementing `int`. The value returned must
/// be either an `int` or `String`.
Peer(
StreamChannel<String> channel, {
ErrorCallback? onUnhandledError,
bool strictProtocolChecks = true,
Object Function()? generateId,
Object Function()? idGenerator,
}) : this.withoutJson(
jsonDocument.bind(channel).transform(respondToFormatExceptions),
onUnhandledError: onUnhandledError,
strictProtocolChecks: strictProtocolChecks,
generateId: generateId);
idGenerator: idGenerator);

/// Creates a [Peer] that communicates using decoded messages over [_channel].
///
Expand All @@ -90,13 +90,13 @@ class Peer implements Client, Server {
/// specification. In particular, requests missing the `jsonrpc` parameter
/// will be accepted.
///
/// If [generateId] is passed, it will be called to generate an ID for each
/// If [idGenerator] is passed, it will be called to generate an ID for each
/// request. Defaults to an auto-incrementing `int`. The value returned must
/// be either an `int` or `String`.
Peer.withoutJson(this._channel,
{ErrorCallback? onUnhandledError,
bool strictProtocolChecks = true,
Object Function()? generateId}) {
Object Function()? idGenerator}) {
_server = Server.withoutJson(
StreamChannel(_serverIncomingForwarder.stream, _channel.sink),
onUnhandledError: onUnhandledError,
Expand All @@ -106,7 +106,7 @@ class Peer implements Client, Server {
_clientIncomingForwarder.stream,
_channel.sink,
),
generateId: generateId);
idGenerator: idGenerator);
}

// Client methods.
Expand Down
4 changes: 2 additions & 2 deletions pkgs/json_rpc_2/test/client/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ void main() {

test('with custom String ids', () {
var id = 0;
controller = ClientController(generateId: () => 'ID-${id++}');
controller = ClientController(idGenerator: () => 'ID-${id++}');
controller.expectRequest((request) {
expect(
request,
Expand All @@ -228,7 +228,7 @@ void main() {

test('String ids are not parsed as ints', () {
var id = 0;
controller = ClientController(generateId: () => '${id++}');
controller = ClientController(idGenerator: () => '${id++}');
controller.expectRequest((request) {
expect(
request,
Expand Down
4 changes: 2 additions & 2 deletions pkgs/json_rpc_2/test/client/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class ClientController {
/// The client.
late final json_rpc.Client client;

ClientController({Object Function()? generateId}) {
ClientController({Object Function()? idGenerator}) {
client = json_rpc.Client(
StreamChannel(_responseController.stream, _requestController.sink),
generateId: generateId);
idGenerator: idGenerator);
client.listen();
}

Expand Down
Loading