Skip to content

Commit

Permalink
Prepare for 2024.1.1.beta release (protobuf)
Browse files Browse the repository at this point in the history
  • Loading branch information
buijs-dev committed Mar 15, 2024
1 parent db769cb commit b2720aa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.1.0
* Add support for protocol buffers through encodeBuffer and decodeBuffer fields in doEvent method.

## 1.0.1
* Bump SDK constraints to >=2.17.6 <4.0.0

Expand Down
49 changes: 36 additions & 13 deletions lib/src/publisher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,20 @@ Future<AdapterResponse<OUT>> doEvent<OUT>({
/// If not then callbacks are not executed.
State? state,

/// (Optional) Decoding function used to decode the received response.
/// (Optional) Decoding function used to decode the received JSON response.
OUT Function(String)? decode,

/// (Optional) Encoding function used to encode message if is not
/// a StandardMessageCodec Type.
String Function(dynamic)? encode,

/// (Optional) Decoding function used to decode the received buffer response.
OUT Function(List<int>)? decodeBuffer,

/// (Optional) Encoding function used to encode message as buffer if is not
/// a StandardMessageCodec Type.
Uint8List Function(dynamic)? encodeBuffer,

/// (Optional) Function to be executed if the event is processed successfully.
void Function(OUT)? onSuccess,

Expand All @@ -87,12 +94,13 @@ Future<AdapterResponse<OUT>> doEvent<OUT>({
/// Create a request message.
///
/// If [message] is null then [request] is also null.
final dynamic request = _toRequestMessage(message, encode);
final dynamic request = _toRequestMessage(message, encode, encodeBuffer);

/// Send the event and wait for a response.
final response = await _sendEvent(
sendRequest: () => channel.invokeMethod<dynamic>(event, request),
deserialize: decode,
decode: decode,
decodeBuffer: decodeBuffer
);

/// Check if state is mounted.
Expand All @@ -112,12 +120,14 @@ Future<AdapterResponse<OUT>> doEvent<OUT>({

Future<AdapterResponse<OUT>> _sendEvent<OUT>({
required dynamic Function() sendRequest,
OUT Function(String)? deserialize,
OUT Function(String)? decode,
OUT Function(List<int>)? decodeBuffer,
}) async {
try {
final dynamic responseMessage = _handleResult(
response: await sendRequest.call(),
deserialize: deserialize,
decode: decode,
decodeBuffer: decodeBuffer,
);

return AdapterResponse<OUT>.success(responseMessage as OUT);
Expand All @@ -130,22 +140,35 @@ Exception _failureToException(dynamic e) =>
e is Error ? Exception(e.stackTrace) : e as Exception;

dynamic _toRequestMessage(
dynamic message,
String Function(dynamic)? encode,
dynamic message,
String Function(dynamic)? encode,
Uint8List Function(dynamic)? encodeBuffer,
) {
if (message == null) {
return null;
}

if (encode == null) {
return message;
if(encodeBuffer != null) {
return encodeBuffer(message);
}

return encode.call(message);
if (encode != null) {
return encode(message);
}

return message;
}

dynamic _handleResult<OUT>({
dynamic response,
OUT Function(String)? deserialize,
}) =>
deserialize?.call(response.toString()) ?? response;
OUT Function(String)? decode,
OUT Function(List<int>)? decodeBuffer,
}) {
if (response == null) {
return response;
}
return decodeBuffer?.call(response as List<int>)
?? decode?.call(response.toString())
?? response;

}
5 changes: 3 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: klutter_ui
description: Flutter widgets to be used in conjunction with the klutter plugin.
version: 1.0.1
version: 1.1.0
homepage: https://buijs.dev
repository: https://github.com/buijs-dev/klutter-dart-ui

Expand All @@ -14,4 +14,5 @@ dependencies:
dev_dependencies:
coverage: ^1.6.4
dartdoc: ^6.3.0
test: ^1.24.7
test: ^1.24.7
pana: ^0.21.39

0 comments on commit b2720aa

Please sign in to comment.