Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(logging): use indexedDB on web if it is supported #3961

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -17,8 +19,8 @@ class DartQueuedItemStore
// ignore: avoid_unused_constructor_parameters
DartQueuedItemStore(String? storagePath);

late final QueuedItemStore _database = () {
if (IndexedDbAdapter.checkIsIndexedDBSupported()) {
late final Future<QueuedItemStore> _database = () async {
if (await IndexedDbAdapter.checkIsIndexedDBSupported()) {
return IndexedDbAdapter();
}
logger.warn(
Expand All @@ -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,
Expand All @@ -49,29 +52,34 @@ class DartQueuedItemStore

@override
Future<void> deleteItems(Iterable<QueuedItem> items) async {
await _database.deleteItems(items);
final db = await _database;
await db.deleteItems(items);
}

@override
Future<Iterable<QueuedItem>> getCount(int count) async {
return _database.getCount(count);
final db = await _database;
return db.getCount(count);
}

@override
Future<Iterable<QueuedItem>> getAll() async {
return _database.getAll();
final db = await _database;
return db.getAll();
}

@override
bool isFull(int maxSizeInMB) {
return _database.isFull(maxSizeInMB);
Future<bool> isFull(int maxSizeInMB) async {
final db = await _database;
return db.isFull(maxSizeInMB);
}

/// Clear IndexedDB data.
@override
@visibleForTesting
Future<void> clear() async {
return _database.clear();
final db = await _database;
return db.clear();
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ class IndexedDbAdapter implements QueuedItemStore {
for (final elem in request.result) {
final value = elem as Object;
final id = getProperty<int>(value, 'id');
final string = getProperty<String>(value, 'value');
final itemValue = getProperty<String>(value, 'value');
final timestamp = getProperty<String>(value, 'timestamp');
readValues.add(
QueuedItem(id: id, value: string, timestamp: timestamp),
QueuedItem(id: id, value: itemValue, timestamp: timestamp),
);
}
return readValues;
Expand Down Expand Up @@ -129,25 +129,7 @@ class IndexedDbAdapter implements QueuedItemStore {

@override
Future<Iterable<QueuedItem>> getAll() async {
final readValues = <QueuedItem>[];

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<String, dynamic>;
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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have 2 separate methods if they do the same exact thing?

Copy link
Member Author

@NikaHsn NikaHsn Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the case only for IDBObjectStore. other implementations of QueuedItemStore have different impl for getCount and getAll.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok thanks for clarifying

}

@override
Expand All @@ -168,14 +150,14 @@ class IndexedDbAdapter implements QueuedItemStore {
void close() {}

/// Check that IndexDB will work on this device.
static bool checkIsIndexedDBSupported() {
static Future<bool> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,19 @@ 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;
}
}
}

void _handleFullLogStoreAfterSync({
Future<void> _handleFullLogStoreAfterSync({
DateTime? retryTime,
}) {
}) async {
final isLogStoreFull =
_logStore.isFull(_pluginConfig.localStoreMaxSizeInMB);
await _logStore.isFull(_pluginConfig.localStoreMaxSizeInMB);
if (!isLogStoreFull) {
_retryCount = 0;
_retryTime = null;
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ abstract interface class QueuedItemStore {
FutureOr<Iterable<QueuedItem>> getAll();

/// Whether the queue size is reached [maxSizeInMB].
bool isFull(int maxSizeInMB);
FutureOr<bool> isFull(int maxSizeInMB);

/// Clear the queue of items.
FutureOr<void> clear();
Expand Down
Loading