From 2a4c9ef6055bac0b51674bd35aa6a0370120a1e1 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Wed, 27 Sep 2023 10:03:28 +0200 Subject: [PATCH 1/2] fix(neon): cache initialization Signed-off-by: Nikolas Rimikis --- .../neon/lib/src/utils/request_manager.dart | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/neon/neon/lib/src/utils/request_manager.dart b/packages/neon/neon/lib/src/utils/request_manager.dart index ec1b96ed..d67dd3dc 100644 --- a/packages/neon/neon/lib/src/utils/request_manager.dart +++ b/packages/neon/neon/lib/src/utils/request_manager.dart @@ -28,8 +28,10 @@ class RequestManager { static RequestManager get instance => _requestManager ??= RequestManager(); Future initCache() async { - _cache = Cache(); - await _cache!.init(); + final cache = Cache(); + await cache.init(); + + _cache = cache; } Cache? _cache; @@ -209,13 +211,24 @@ class Cache { ); } + Database get _requireDatabase { + final database = _database; + if (database == null) { + throw StateError( + 'Cache has not been set up yet. Please make sure Cache.init() has been called before and completed.', + ); + } + + return database; + } + Future has(final String key) async => - Sqflite.firstIntValue(await _database!.rawQuery('SELECT COUNT(*) FROM cache WHERE key = ?', [key])) == 1; + Sqflite.firstIntValue(await _requireDatabase.rawQuery('SELECT COUNT(*) FROM cache WHERE key = ?', [key])) == 1; Future get(final String key) async => - (await _database!.rawQuery('SELECT value FROM cache WHERE key = ?', [key]))[0]['value'] as String?; + (await _requireDatabase.rawQuery('SELECT value FROM cache WHERE key = ?', [key]))[0]['value'] as String?; - Future set(final String key, final String value) async => _database!.rawQuery( + Future set(final String key, final String value) async => _requireDatabase.rawQuery( 'INSERT INTO cache (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value', [key, value], ); From df75a05f8cb8dfa48e118330dbd0136dee8b22b9 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Wed, 27 Sep 2023 10:04:00 +0200 Subject: [PATCH 2/2] feat(neon): skip platform initialization if already initialized Signed-off-by: Nikolas Rimikis --- packages/neon/neon/lib/src/platform/platform.dart | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/neon/neon/lib/src/platform/platform.dart b/packages/neon/neon/lib/src/platform/platform.dart index 8c541890..d4e53fd6 100644 --- a/packages/neon/neon/lib/src/platform/platform.dart +++ b/packages/neon/neon/lib/src/platform/platform.dart @@ -17,6 +17,10 @@ abstract interface class NeonPlatform { /// /// Required to be called before accessing [NeonPlatform.instance]. static Future setup() async { + if (_platform != null) { + return; + } + if (Platform.isAndroid) { _platform = const AndroidNeonPlatform(); } else if (Platform.isLinux) {