From e66c4390bbd9de245fbb318e62e120e12e62d387 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Tue, 17 Sep 2024 17:09:00 +0530 Subject: [PATCH] [mob] Fix delay --- mobile/lib/services/magic_cache_service.dart | 36 ++++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/mobile/lib/services/magic_cache_service.dart b/mobile/lib/services/magic_cache_service.dart index 0b062bc76b..f002a0de3e 100644 --- a/mobile/lib/services/magic_cache_service.dart +++ b/mobile/lib/services/magic_cache_service.dart @@ -6,6 +6,8 @@ import "package:computer/computer.dart"; import "package:flutter/foundation.dart"; import "package:logging/logging.dart"; import "package:path_provider/path_provider.dart"; +import "package:photos/core/event_bus.dart"; +import "package:photos/events/file_uploaded_event.dart"; import "package:photos/extensions/stop_watch.dart"; import "package:photos/models/file/extensions/file_props.dart"; import "package:photos/models/file/file.dart"; @@ -94,6 +96,8 @@ class MagicCacheService { Future>? _magicCacheFuture; Future>? _promptFuture; + final Set _pendingUpdateReason = {}; + bool _isUpdateInProgress = false; static final MagicCacheService instance = MagicCacheService._privateConstructor(); @@ -104,7 +108,9 @@ class MagicCacheService { Future.delayed(_kCacheUpdateDelay, () { _updateCacheIfTheTimeHasCome(); }); - _updateCacheIfTheTimeHasCome(); + Bus.instance.on().listen((event) { + _pendingUpdateReason.add("File uploaded"); + }); } Future _resetLastMagicCacheUpdateTime() async { @@ -125,14 +131,14 @@ class MagicCacheService { final updatedJSONFile = await RemoteAssetsService.instance .getAssetIfUpdated(_kMagicPromptsDataUrl); if (updatedJSONFile != null) { - unawaited(updateCache()); - return; - } - if (lastMagicCacheUpdateTime < - DateTime.now() - .subtract(const Duration(days: 3)) - .millisecondsSinceEpoch) { - unawaited(updateCache()); + _pendingUpdateReason.add("Prompts data updated"); + } else { + if (lastMagicCacheUpdateTime < + DateTime.now() + .subtract(const Duration(days: 3)) + .millisecondsSinceEpoch) { + _pendingUpdateReason.add("Cache is old"); + } } } @@ -142,7 +148,15 @@ class MagicCacheService { Future updateCache() async { try { - _logger.info("updating magic cache"); + if (_pendingUpdateReason.isEmpty || _isUpdateInProgress) { + _logger.info( + "No update needed as ${_pendingUpdateReason.toList()} and isUpdateInProgress $_isUpdateInProgress", + ); + return; + } + _logger.info("updating magic cache ${_pendingUpdateReason.toList()}"); + _pendingUpdateReason.clear(); + _isUpdateInProgress = true; final EnteWatch? w = kDebugMode ? EnteWatch("magicCacheWatch") : null; w?.start(); final magicPromptsData = await getPrompts(); @@ -162,6 +176,8 @@ class MagicCacheService { w?.logAndReset('done'); } catch (e, s) { _logger.info("Error updating magic cache", e, s); + } finally { + _isUpdateInProgress = false; } }