From b79e068003cbf274f436233ae0eb1153069a640d Mon Sep 17 00:00:00 2001 From: Nika Hassani Date: Mon, 16 Oct 2023 11:21:41 -0700 Subject: [PATCH] fix(logging): use indexedDB on web if it is supported --- .../dart_queued_item_store.web.dart | 26 +++++++++++------ .../index_db/indexed_db_adapter.dart | 28 ++++--------------- .../lib/src/cloudwatch_logger_plugin.dart | 10 +++---- .../queued_item_store/queued_item_store.dart | 2 +- 4 files changed, 28 insertions(+), 38 deletions(-) diff --git a/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/dart_queued_item_store.web.dart b/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/dart_queued_item_store.web.dart index 7d0de342a8..9cc7c50b96 100644 --- a/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/dart_queued_item_store.web.dart +++ b/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/dart_queued_item_store.web.dart @@ -3,6 +3,8 @@ // ignore_for_file: implementation_imports +import 'dart:async'; + import 'package:amplify_core/amplify_core.dart'; import 'package:amplify_logging_cloudwatch/src/queued_item_store/index_db/indexed_db_adapter.dart'; import 'package:aws_logging_cloudwatch/src/queued_item_store/in_memory_queued_item_store.dart'; @@ -17,8 +19,8 @@ class DartQueuedItemStore // ignore: avoid_unused_constructor_parameters DartQueuedItemStore(String? storagePath); - late final QueuedItemStore _database = () { - if (IndexedDbAdapter.checkIsIndexedDBSupported()) { + late final Future _database = () async { + if (await IndexedDbAdapter.checkIsIndexedDBSupported()) { return IndexedDbAdapter(); } logger.warn( @@ -40,7 +42,8 @@ class DartQueuedItemStore String timestamp, { bool enableQueueRotation = false, }) async { - await _database.addItem( + final db = await _database; + await db.addItem( string, timestamp, enableQueueRotation: enableQueueRotation, @@ -49,29 +52,34 @@ class DartQueuedItemStore @override Future deleteItems(Iterable items) async { - await _database.deleteItems(items); + final db = await _database; + await db.deleteItems(items); } @override Future> getCount(int count) async { - return _database.getCount(count); + final db = await _database; + return db.getCount(count); } @override Future> getAll() async { - return _database.getAll(); + final db = await _database; + return db.getAll(); } @override - bool isFull(int maxSizeInMB) { - return _database.isFull(maxSizeInMB); + Future isFull(int maxSizeInMB) async { + final db = await _database; + return db.isFull(maxSizeInMB); } /// Clear IndexedDB data. @override @visibleForTesting Future clear() async { - return _database.clear(); + final db = await _database; + return db.clear(); } @override diff --git a/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/index_db/indexed_db_adapter.dart b/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/index_db/indexed_db_adapter.dart index 050e1ef1eb..7da69274ae 100644 --- a/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/index_db/indexed_db_adapter.dart +++ b/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/index_db/indexed_db_adapter.dart @@ -97,10 +97,10 @@ class IndexedDbAdapter implements QueuedItemStore { for (final elem in request.result) { final value = elem as Object; final id = getProperty(value, 'id'); - final string = getProperty(value, 'value'); + final itemValue = getProperty(value, 'value'); final timestamp = getProperty(value, 'timestamp'); readValues.add( - QueuedItem(id: id, value: string, timestamp: timestamp), + QueuedItem(id: id, value: itemValue, timestamp: timestamp), ); } return readValues; @@ -129,25 +129,7 @@ class IndexedDbAdapter implements QueuedItemStore { @override Future> getAll() async { - final readValues = []; - - await _databaseOpenEvent; - final store = _getObjectStore(); - final request = store.getAll(null, null); - - await request.future; - - for (final elem in request.result) { - final value = elem as Map; - final id = value['id'] as int; - final itemValue = value['value'] as String; - final timestamp = value['timestamp'] as String; - readValues.add( - QueuedItem(id: id, value: itemValue, timestamp: timestamp), - ); - } - - return readValues; + return getCount(); } @override @@ -168,14 +150,14 @@ class IndexedDbAdapter implements QueuedItemStore { void close() {} /// Check that IndexDB will work on this device. - static bool checkIsIndexedDBSupported() { + static Future checkIsIndexedDBSupported() async { if (indexedDB == null) { return false; } // indexedDB will be non-null in Firefox private browsing, // but will fail to open. try { - indexedDB!.open('test', 1).result; + await indexedDB!.open('test', 1).future; return true; } on Object { return false; diff --git a/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/cloudwatch_logger_plugin.dart b/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/cloudwatch_logger_plugin.dart index ce6fb512b1..1b03df8949 100644 --- a/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/cloudwatch_logger_plugin.dart +++ b/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/cloudwatch_logger_plugin.dart @@ -219,7 +219,7 @@ class CloudWatchLoggerPlugin extends AWSLoggerPlugin } on Exception catch (e) { logger.error('Failed to sync logs to CloudWatch.', e); } finally { - _handleFullLogStoreAfterSync( + await _handleFullLogStoreAfterSync( retryTime: nextRetry, ); _syncing = false; @@ -227,11 +227,11 @@ class CloudWatchLoggerPlugin extends AWSLoggerPlugin } } - void _handleFullLogStoreAfterSync({ + Future _handleFullLogStoreAfterSync({ DateTime? retryTime, - }) { + }) async { final isLogStoreFull = - _logStore.isFull(_pluginConfig.localStoreMaxSizeInMB); + await _logStore.isFull(_pluginConfig.localStoreMaxSizeInMB); if (!isLogStoreFull) { _retryCount = 0; _retryTime = null; @@ -347,7 +347,7 @@ class CloudWatchLoggerPlugin extends AWSLoggerPlugin } final item = logEntry.toQueuedItem(); final isLogStoreFull = - _logStore.isFull(_pluginConfig.localStoreMaxSizeInMB); + await _logStore.isFull(_pluginConfig.localStoreMaxSizeInMB); final shouldEnableQueueRotation = isLogStoreFull && _retryTime != null; await _logStore.addItem( diff --git a/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/queued_item_store/queued_item_store.dart b/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/queued_item_store/queued_item_store.dart index 9317aa4878..24e197d449 100644 --- a/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/queued_item_store/queued_item_store.dart +++ b/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/queued_item_store/queued_item_store.dart @@ -24,7 +24,7 @@ abstract interface class QueuedItemStore { FutureOr> getAll(); /// Whether the queue size is reached [maxSizeInMB]. - bool isFull(int maxSizeInMB); + FutureOr isFull(int maxSizeInMB); /// Clear the queue of items. FutureOr clear();