From 403831ccd0f81c8ab622461baff2fa402da7a259 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Sat, 21 Mar 2020 01:32:24 +0100 Subject: [PATCH] NotesFolderFS: Avoid multiple load operations in parallel --- lib/core/notes_folder_fs.dart | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/core/notes_folder_fs.dart b/lib/core/notes_folder_fs.dart index f3f078b2..607f9b26 100644 --- a/lib/core/notes_folder_fs.dart +++ b/lib/core/notes_folder_fs.dart @@ -3,6 +3,7 @@ import 'dart:io'; import 'package:fimber/fimber.dart'; import 'package:path/path.dart' as p; import 'package:path/path.dart'; +import 'package:synchronized/synchronized.dart'; import 'note.dart'; import 'notes_folder.dart'; @@ -11,6 +12,7 @@ import 'notes_folder_notifier.dart'; class NotesFolderFS with NotesFolderNotifier implements NotesFolder { final NotesFolderFS _parent; String _folderPath; + var _lock = Lock(); List _notes = []; List _folders = []; @@ -137,8 +139,14 @@ class NotesFolderFS with NotesFolderNotifier implements NotesFolder { return Future.wait(futures); } - // FIXME: This should not reconstruct the Notes or NotesFolders once constructed. Future load() async { + return _lock.synchronized(() async { + return _load(); + }); + } + + // FIXME: This should not reconstruct the Notes or NotesFolders once constructed. + Future _load() async { Set pathsFound = {}; final dir = Directory(folderPath);