reduce calls to offlinestorage while uploading attachments using debounce

This commit is contained in:
Salvatore Giordano
2021-03-01 11:52:32 +01:00
parent 578c4efade
commit ecb559f687
4 changed files with 37 additions and 1 deletions
@@ -7,6 +7,7 @@ import 'package:dio/dio.dart';
import 'package:logging/logging.dart';
import 'package:rxdart/rxdart.dart';
import 'package:stream_chat/src/api/retry_queue.dart';
import 'package:stream_chat/src/debounce.dart';
import 'package:stream_chat/src/event_type.dart';
import 'package:stream_chat/src/models/attachment_file.dart';
import 'package:stream_chat/src/models/channel_state.dart';
@@ -1685,7 +1686,11 @@ class ChannelClientState {
set _channelState(ChannelState v) {
_channelStateController.add(v);
_channel._client.chatPersistenceClient?.updateChannelState(v);
debounce(
timeout: Duration(milliseconds: 500),
target: _channel._client.chatPersistenceClient?.updateChannelState,
arguments: [v],
);
}
/// The channel threads related to this channel
@@ -0,0 +1,23 @@
import 'dart:async';
import 'package:meta/meta.dart';
/// Map of timeouts being debounced
Map timeouts = {};
/// Runs a function avoiding calling it too many times in a [timeoutMS] window
void debounce({
@required Duration timeout,
@required Function target,
@required List arguments,
}) {
if (timeouts.containsKey(target)) {
timeouts[target].cancel();
}
final timer = Timer(timeout, () {
Function.apply(target, arguments);
});
timeouts[target] = timer;
}
@@ -48,6 +48,10 @@ dependencies:
path_provider: ^1.6.27
video_thumbnail: ^0.2.5+1
dependencies_override:
stream_chat_flutter_core:
path: ../stream_chat_flutter_core
flutter:
assets:
- images/
@@ -15,6 +15,10 @@ dependencies:
sdk: flutter
rxdart: ^0.25.0
dependencies_override:
stream_chat:
path: ../stream_chat
dev_dependencies:
mockito: ^4.1.4
flutter_test: