-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17191c9
commit dcd63f1
Showing
12 changed files
with
179 additions
and
10 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
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
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
107 changes: 107 additions & 0 deletions
107
lib/src/internal/main/chat_manager/collection_manager/auto_resend_manager.dart
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,107 @@ | ||
// Copyright (c) 2024 Sendbird, Inc. All rights reserved. | ||
|
||
import 'dart:async'; | ||
|
||
import 'package:sendbird_chat_sdk/sendbird_chat_sdk.dart'; | ||
import 'package:sendbird_chat_sdk/src/internal/main/chat/chat.dart'; | ||
import 'package:sendbird_chat_sdk/src/internal/main/logger/sendbird_logger.dart'; | ||
|
||
class AutoResendManager { | ||
AutoResendManager._(); | ||
|
||
static final AutoResendManager _instance = AutoResendManager._(); | ||
|
||
factory AutoResendManager() => _instance; | ||
|
||
static const int _delayForRateLimit = 200; // Check | ||
bool _isAutoResending = false; | ||
bool _stopAutoResending = false; | ||
|
||
void startAutoResend(Chat chat) async { | ||
if (!chat.chatContext.options.useAutoResend) { | ||
sbLog.i(StackTrace.current, 'Returned because of useAutoResend == false'); | ||
return; | ||
} | ||
|
||
if (_isAutoResending) { | ||
sbLog.i( | ||
StackTrace.current, 'Returned because of _isAutoResending == true'); | ||
return; | ||
} | ||
|
||
sbLog.i(StackTrace.current, 'Started'); | ||
_isAutoResending = true; | ||
|
||
try { | ||
for (final collection in chat.collectionManager.baseMessageCollections) { | ||
if (collection is MessageCollection) { | ||
if (collection.channel.isFrozen) { | ||
sbLog.i(StackTrace.current, | ||
'Skipped because of collection.channel.isFrozen == true'); | ||
continue; | ||
} | ||
|
||
final failedMessages = await collection.getFailedMessages(); | ||
|
||
for (final failedMessage in failedMessages) { | ||
if (failedMessage.isAutoResendable()) { | ||
// Resend a message | ||
Completer completer = Completer(); | ||
SendbirdException? exception; | ||
if (failedMessage is UserMessage) { | ||
collection.channel.resendUserMessage( | ||
failedMessage, | ||
handler: (UserMessage message, SendbirdException? e) { | ||
exception = e; | ||
completer.complete(); | ||
}, | ||
); | ||
} else if (failedMessage is FileMessage) { | ||
collection.channel.resendFileMessage( | ||
failedMessage, | ||
handler: (FileMessage message, SendbirdException? e) { | ||
exception = e; | ||
completer.complete(); | ||
}, | ||
); | ||
} else { | ||
// Defensive code | ||
completer.complete(); | ||
} | ||
await completer.future; | ||
|
||
if (exception != null) { | ||
sbLog.i( | ||
StackTrace.current, 'Stopped because of exception != null'); | ||
break; | ||
} | ||
|
||
if (_stopAutoResending) break; | ||
|
||
// Delay to avoid the rate limit | ||
await Future.delayed( | ||
const Duration(milliseconds: _delayForRateLimit)); | ||
} | ||
|
||
if (_stopAutoResending) break; | ||
} | ||
} | ||
|
||
if (_stopAutoResending) break; | ||
} | ||
} catch (e) { | ||
sbLog.e(StackTrace.current, e.toString()); | ||
} | ||
|
||
_stopAutoResending = false; | ||
_isAutoResending = false; | ||
sbLog.i(StackTrace.current, 'Stopped'); | ||
} | ||
|
||
void stopAutoResend() { | ||
if (_isAutoResending) { | ||
sbLog.i(StackTrace.current); | ||
_stopAutoResending = true; | ||
} | ||
} | ||
} |
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
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
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
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
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
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
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
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