Skip to content

Commit

Permalink
TableSchema | Field
Browse files Browse the repository at this point in the history
  • Loading branch information
a-givertzman committed Dec 10, 2024
1 parent ba36332 commit fce6c27
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 47 deletions.
99 changes: 54 additions & 45 deletions lib/src/api_client/request/api_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'package:hmi_core/hmi_core_failure.dart';
import 'package:hmi_core/hmi_core_log.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:hmi_core/hmi_core_result.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
// import 'package:web_socket_channel/web_socket_channel.dart';
// import 'package:web_socket_channel/io.dart';
///
Expand Down Expand Up @@ -84,6 +85,7 @@ class ApiRequest {
Future<Result<ApiReply, Failure>> fetch() async {
final queryJson = _query.buildJson(authToken: _authToken, debug: _debug, keep: _keep);
final bytes = utf8.encode(queryJson);
_log.info('.fetch | platform: ${kIsWeb ? 'Web' : 'NonWeb'}');
if (kIsWeb) {
return _fetchWebSocket(bytes);
} else {
Expand All @@ -96,6 +98,7 @@ class ApiRequest {
Future<Result<ApiReply, Failure>> fetchWith(ApiQueryType query) async {
final queryJson = query.buildJson(authToken: _authToken, debug: _debug, keep: _keep);
final bytes = utf8.encode(queryJson);
_log.info('.fetchWith | platform: ${kIsWeb ? 'Web' : 'NonWeb'}');
if (kIsWeb) {
return _fetchWebSocket(bytes);
} else {
Expand All @@ -118,45 +121,49 @@ class ApiRequest {
}
///
/// Fetching on web socket
Future<Result<ApiReply, Failure>> _fetchWebSocket(Bytes bytes) {
return WebSocket.connect('ws://${_address.host}:${_address.port}')
.then((wSocket) async {
return _sendWeb(wSocket, bytes)
.then((result) {
return switch(result) {
Ok() => _readWeb(wSocket)
.then((result) {
final Result<ApiReply, Failure> r = switch(result) {
Ok(:final value) => Ok(
ApiReply.fromJson(
utf8.decode(value),
),
Future<Result<ApiReply, Failure>> _fetchWebSocket(Bytes bytes) async {
return Future.microtask(() async {
final wSocket = WebSocketChannel.connect(Uri.parse('wss://${_address.host}:${_address.port}'));
_log.warning('._fetchWebSocket | wSocket connecting to ${_address.host}:${_address.port}...');
try {
await wSocket.ready;
} on SocketException catch (err) {
_log.warning('._fetchWebSocket | wSocket connection error $err');
return Err(Failure(message: 'ApiRequest._fetchWebSocket | Connection error $err', stackTrace: StackTrace.current));
} on WebSocketChannelException catch (err) {
_log.warning('._fetchWebSocket | wSocket connection error $err');
return Err(Failure(message: 'ApiRequest._fetchWebSocket | Connection error $err', stackTrace: StackTrace.current));
}
_log.warning('._fetchWebSocket | wSocket connected to: ${wSocket}');
return _sendWeb(wSocket, bytes)
.then((result) {
return switch(result) {
Ok() => _readWeb(wSocket)
.then((result) {
final Result<ApiReply, Failure> r = switch(result) {
Ok(:final value) => Ok(
ApiReply.fromJson(
utf8.decode(value),
),
Err(:final error) => Err(error),
};
return r;
}),
Err(:final error) => Future<Result<ApiReply, Failure>>.value(
Err(error),
),
};
});
})
.catchError((error) {
return Err<ApiReply, Failure>(
Failure(
message: '.fetch | web socket error: $error',
stackTrace: StackTrace.current,
),
);
});
),
Err(:final error) => Err(error),
};
return r;
}),
Err(:final error) => Future<Result<ApiReply, Failure>>.value(
Err(error),
),
};
});

});
}
///
/// Reads bytes from web socket
Future<Result<List<int>, Failure>> _readWeb(WebSocket socket) async {
Future<Result<List<int>, Failure>> _readWeb(WebSocketChannel socket) async {
try {
List<int> message = [];
final subscription = socket
final subscription = socket.stream
.timeout(
_timeout,
onTimeout: (sink) {
Expand All @@ -183,16 +190,19 @@ class ApiRequest {
}
///
/// Sends bytes over WEB socket
Future<Result<bool, Failure>> _sendWeb(WebSocket socket, Bytes bytes) async {
final message = MessageBuild(
syn: FieldSyn.def(),
id: FieldId.def(),
kind: FieldKind.bytes,
size: FieldSize.def(),
data: FieldData([]),
);
Future<Result<bool, Failure>> _sendWeb(WebSocketChannel socket, Bytes bytes) async {
// final message = MessageBuild(
// syn: FieldSyn.def(),
// id: FieldId.def(),
// kind: FieldKind.bytes,
// size: FieldSize.def(),
// data: FieldData([]),
// );
try {
socket.add(message.build(bytes));
_id++;
// final msgBytes = message.build(bytes, id: _id);
// _log.info('._send | Web socket bytes: ${msgBytes.sublist(0, 16)}');
socket.sink.add(bytes);
return Future.value(const Ok(true));
} catch (error) {
_log.warning('._send | Web socket error: $error');
Expand All @@ -206,10 +216,9 @@ class ApiRequest {
}
///
/// Closes web socket
Future<void> _closeSocketWeb(WebSocket? socket) async {
Future<void> _closeSocketWeb(WebSocketChannel? socket) async {
try {
socket?.close();
// socket?.destroy();
socket?.sink.close();
} catch (error) {
_log.warning('[.close] error: $error');
}
Expand Down
7 changes: 5 additions & 2 deletions lib/src/table_schema/schema_entry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SchemaEntry implements SchemaEntryAbstract {
final _log = Log("$SchemaEntry");
final _id = const Uuid().v1(); // v1 time-based id
final Map<String, FieldValue> _map;
final bool _isEmpty;
bool _isEmpty;
bool _isChanged = false;
bool _isSelected = false;
///
Expand Down Expand Up @@ -95,6 +95,9 @@ class SchemaEntry implements SchemaEntryAbstract {
if (field != null) {
final changed = field.update(value);
_isChanged = _isChanged || changed;
if (_isChanged) {
_isEmpty = false;
}
}
_log.debug('.update | key: $key, \t field: $field, isChanged: $_isChanged');
}
Expand All @@ -114,6 +117,6 @@ class SchemaEntry implements SchemaEntryAbstract {
//
@override
String toString() {
return '$runtimeType{ isChanged: $_isChanged, isSelected: $_isSelected, map: $_map}';
return '$runtimeType{ isEmpty: $_isEmpty, isChanged: $_isChanged, isSelected: $_isSelected, map: $_map}';
}
}

0 comments on commit fce6c27

Please sign in to comment.