feat(llc): added synchronization to the StreamChatClient.sync api.
Signed-off-by: xsahil03x <[email protected]>
This commit is contained in:
@@ -8,6 +8,8 @@
|
|||||||
✅ Added
|
✅ Added
|
||||||
|
|
||||||
- Expose `ChannelMute` class. [#1473](https://github.com/GetStream/stream-chat-flutter/issues/1473)
|
- Expose `ChannelMute` class. [#1473](https://github.com/GetStream/stream-chat-flutter/issues/1473)
|
||||||
|
- Added synchronization to the `StreamChatClient.sync`
|
||||||
|
api. [#1392](https://github.com/GetStream/stream-chat-flutter/issues/1392)
|
||||||
|
|
||||||
## 6.0.0
|
## 6.0.0
|
||||||
|
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ import 'package:stream_chat/src/event_type.dart';
|
|||||||
import 'package:stream_chat/src/ws/connection_status.dart';
|
import 'package:stream_chat/src/ws/connection_status.dart';
|
||||||
import 'package:stream_chat/src/ws/websocket.dart';
|
import 'package:stream_chat/src/ws/websocket.dart';
|
||||||
import 'package:stream_chat/version.dart';
|
import 'package:stream_chat/version.dart';
|
||||||
|
import 'package:synchronized/extension.dart';
|
||||||
|
|
||||||
/// Handler function used for logging records. Function requires a single
|
/// Handler function used for logging records. Function requires a single
|
||||||
/// [LogRecord] as the only parameter.
|
/// [LogRecord] as the only parameter.
|
||||||
@@ -488,37 +489,40 @@ class StreamChatClient {
|
|||||||
|
|
||||||
/// Get the events missed while offline to sync the offline storage
|
/// Get the events missed while offline to sync the offline storage
|
||||||
/// Will automatically fetch [cids] and [lastSyncedAt] if [persistenceEnabled]
|
/// Will automatically fetch [cids] and [lastSyncedAt] if [persistenceEnabled]
|
||||||
Future<void> sync({List<String>? cids, DateTime? lastSyncAt}) async {
|
Future<void> sync({List<String>? cids, DateTime? lastSyncAt}) {
|
||||||
cids ??= await _chatPersistenceClient?.getChannelCids();
|
return synchronized(() async {
|
||||||
if (cids == null || cids.isEmpty) {
|
final channels = cids ?? await _chatPersistenceClient?.getChannelCids();
|
||||||
return;
|
if (channels == null || channels.isEmpty) {
|
||||||
}
|
return;
|
||||||
|
|
||||||
lastSyncAt ??= await _chatPersistenceClient?.getLastSyncAt();
|
|
||||||
if (lastSyncAt == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
final res = await _chatApi.general.sync(cids, lastSyncAt);
|
|
||||||
final events = res.events
|
|
||||||
..sort((a, b) => a.createdAt.compareTo(b.createdAt));
|
|
||||||
|
|
||||||
for (final event in events) {
|
|
||||||
logger.fine('event.type: ${event.type}');
|
|
||||||
final messageText = event.message?.text;
|
|
||||||
if (messageText != null) {
|
|
||||||
logger.fine('event.message.text: $messageText');
|
|
||||||
}
|
|
||||||
handleEvent(event);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final now = DateTime.now();
|
final syncAt =
|
||||||
_lastSyncedAt = now;
|
lastSyncAt ?? await _chatPersistenceClient?.getLastSyncAt();
|
||||||
_chatPersistenceClient?.updateLastSyncAt(now);
|
if (syncAt == null) {
|
||||||
} catch (e, stk) {
|
return;
|
||||||
logger.severe('Error during sync', e, stk);
|
}
|
||||||
}
|
|
||||||
|
try {
|
||||||
|
final res = await _chatApi.general.sync(channels, syncAt);
|
||||||
|
final events = res.events
|
||||||
|
..sort((a, b) => a.createdAt.compareTo(b.createdAt));
|
||||||
|
|
||||||
|
for (final event in events) {
|
||||||
|
logger.fine('event.type: ${event.type}');
|
||||||
|
final messageText = event.message?.text;
|
||||||
|
if (messageText != null) {
|
||||||
|
logger.fine('event.message.text: $messageText');
|
||||||
|
}
|
||||||
|
handleEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
final now = DateTime.now();
|
||||||
|
_lastSyncedAt = now;
|
||||||
|
_chatPersistenceClient?.updateLastSyncAt(now);
|
||||||
|
} catch (e, stk) {
|
||||||
|
logger.severe('Error during sync', e, stk);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
final _queryChannelsStreams = <String, Future<List<Channel>>>{};
|
final _queryChannelsStreams = <String, Future<List<Channel>>>{};
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ dependencies:
|
|||||||
mime: ^1.0.4
|
mime: ^1.0.4
|
||||||
rate_limiter: ^1.0.0
|
rate_limiter: ^1.0.0
|
||||||
rxdart: ^0.27.7
|
rxdart: ^0.27.7
|
||||||
|
synchronized: ^3.0.0
|
||||||
uuid: ^3.0.7
|
uuid: ^3.0.7
|
||||||
web_socket_channel: ^2.3.0
|
web_socket_channel: ^2.3.0
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user