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 7d0de342a8b..028ff6c43ba 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'; @@ -15,18 +17,23 @@ class DartQueuedItemStore implements QueuedItemStore, Closeable { /// {@macro amplify_logging_cloudwatch.index_db_queued_item_store} // ignore: avoid_unused_constructor_parameters - DartQueuedItemStore(String? storagePath); + DartQueuedItemStore(String? storagePath) { + scheduleMicrotask(_init); + } - late final QueuedItemStore _database = () { - if (IndexedDbAdapter.checkIsIndexedDBSupported()) { - return IndexedDbAdapter(); + late final QueuedItemStore _database; + + Future _init() async { + if (await IndexedDbAdapter.checkIsIndexedDBSupported()) { + _database = IndexedDbAdapter(); + return; } logger.warn( 'IndexedDB is not available. ' 'Falling back to in-memory storage.', ); - return InMemoryQueuedItemStore(); - }(); + _database = InMemoryQueuedItemStore(); + } @override String get runtimeTypeName => 'DartQueuedItemStore'; 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 050e1ef1eb8..7da69274aec 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;