浏览代码

Prevent multiple syncs from triggering

Vishnu Mohandas 5 年之前
父节点
当前提交
d84456bd5c
共有 1 个文件被更改,包括 9 次插入4 次删除
  1. 9 4
      lib/folder_service.dart

+ 9 - 4
lib/folder_service.dart

@@ -17,6 +17,8 @@ class FolderSharingService {
   final _dio = Dio();
   static final _diffLimit = 100;
 
+  bool _isSyncInProgress = false;
+
   FolderSharingService._privateConstructor() {
     Bus.instance.on<UserAuthenticatedEvent>().listen((event) {
       sync();
@@ -26,11 +28,12 @@ class FolderSharingService {
   static final FolderSharingService instance =
       FolderSharingService._privateConstructor();
 
-  void sync() {
-    if (!Configuration.instance.hasConfiguredAccount()) {
-      return;
+  Future<void> sync() {
+    if (_isSyncInProgress || !Configuration.instance.hasConfiguredAccount()) {
+      return Future.value();
     }
-    getFolders().then((f) async {
+    _isSyncInProgress = true;
+    return getFolders().then((f) async {
       var folders = f.toSet();
       var currentFolders = await FolderDB.instance.getFolders();
       for (final currentFolder in currentFolders) {
@@ -47,6 +50,8 @@ class FolderSharingService {
         }
       }
       Bus.instance.fire(RemoteSyncEvent(true));
+      _isSyncInProgress = false;
+      return Future.value();
     });
   }