Skip to content

Commit

Permalink
fix(logging): use indexedDB on web if it is supported (#3961)
Browse files Browse the repository at this point in the history
  • Loading branch information
NikaHsn authored Oct 18, 2023
1 parent d48f1bf commit c4467c0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 38 deletions.
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();
}

@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

0 comments on commit c4467c0

Please sign in to comment.