From e5f08ffb9fd6c455052146935874adcc7b373b0b Mon Sep 17 00:00:00 2001 From: Lim Chee Kin Date: Sat, 24 Aug 2024 12:41:18 +0800 Subject: [PATCH] feat: added `SurrealWasmMutex` class --- .../lib/flutter_console.dart | 4 +- .../lib/selectable_colored_text.dart | 4 +- integration_test/patch_test.dart | 2 +- integration_test/surrealdb_wasm_test.dart | 2 +- integration_test/transaction_test.dart | 2 +- lib/src/surreal_wasm_mutex.dart | 163 ++++++++++++++++++ lib/surrealdb_wasm.dart | 1 + pubspec.yaml | 1 + 8 files changed, 172 insertions(+), 7 deletions(-) create mode 100644 lib/src/surreal_wasm_mutex.dart diff --git a/example/flutter_console_widget/lib/flutter_console.dart b/example/flutter_console_widget/lib/flutter_console.dart index bb0cff9..efeed09 100644 --- a/example/flutter_console_widget/lib/flutter_console.dart +++ b/example/flutter_console_widget/lib/flutter_console.dart @@ -16,10 +16,10 @@ class FlutterConsole extends StatelessWidget { required this.height, this.inputBackground = const Color(0xff333333), this.inputTextColor = Colors.white, - Key? key, + super.key, this.scrollColor = Colors.grey, required this.width, - }) : super(key: key); + }); final Color consoleBackground; final Color consoleTextColor; diff --git a/example/flutter_console_widget/lib/selectable_colored_text.dart b/example/flutter_console_widget/lib/selectable_colored_text.dart index 54efda3..d73c3dd 100644 --- a/example/flutter_console_widget/lib/selectable_colored_text.dart +++ b/example/flutter_console_widget/lib/selectable_colored_text.dart @@ -10,11 +10,11 @@ class SelectableColoredText extends StatefulWidget { const SelectableColoredText( this.text, { - Key? key, + super.key, this.style, required this.selectedTextColor, required this.selectedTextBackgroundColor, - }) : super(key: key); + }); @override // ignore: library_private_types_in_public_api diff --git a/integration_test/patch_test.dart b/integration_test/patch_test.dart index 259115e..d8893b8 100644 --- a/integration_test/patch_test.dart +++ b/integration_test/patch_test.dart @@ -5,7 +5,7 @@ import 'package:surrealdb_wasm/surrealdb_wasm.dart'; void main({bool wasm = false}) { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); - final db = SurrealWasm.getInstance(); + final db = SurrealWasmMutex.getInstance(); setUpAll(() async { if (wasm) { diff --git a/integration_test/surrealdb_wasm_test.dart b/integration_test/surrealdb_wasm_test.dart index 20f5b71..42c731a 100644 --- a/integration_test/surrealdb_wasm_test.dart +++ b/integration_test/surrealdb_wasm_test.dart @@ -8,7 +8,7 @@ import 'package:surrealdb_wasm/surrealdb_wasm.dart'; void main({bool wasm = false}) { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); - final db = SurrealWasm.getInstance(); + final db = SurrealWasmMutex.getInstance(); //Tests run with local SurrealDB instance started with the command below: //surreal start memory --log trace --allow-all --auth --user root --pass root setUpAll(() async { diff --git a/integration_test/transaction_test.dart b/integration_test/transaction_test.dart index 081907b..f7a2fbd 100644 --- a/integration_test/transaction_test.dart +++ b/integration_test/transaction_test.dart @@ -10,7 +10,7 @@ import 'package:surrealdb_wasm/surrealdb_wasm.dart'; void main({bool wasm = false}) { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); - final db = SurrealWasm.getInstance(); + final db = SurrealWasmMutex.getInstance(); setUpAll(() async { if (wasm) { diff --git a/lib/src/surreal_wasm_mutex.dart b/lib/src/surreal_wasm_mutex.dart new file mode 100644 index 0000000..7f986fb --- /dev/null +++ b/lib/src/surreal_wasm_mutex.dart @@ -0,0 +1,163 @@ +import 'package:mutex/mutex.dart'; +import 'package:surrealdb_js/surrealdb_js.dart'; +import 'package:surrealdb_wasm/surrealdb_wasm.dart'; + +/// The class implemented to workaround the following issue +/// of the surrealdb.wasm: +/// https://github.com/surrealdb/surrealdb.wasm/issues/87. +class SurrealWasmMutex extends Surreal { + /// Instantiation of the [Surreal] class with the [WasmEngine]. + SurrealWasmMutex() : super({'engines': WasmEngine()}); + final _mutex = Mutex(); + + /// Returns a new instance of the [Surreal] class + /// with the [SurrealWasmMutex] implementation. + static Surreal getInstance() { + return SurrealWasmMutex(); + } + + @override + Future let(String key, Object value) async { + await _mutex.protect(() async { + return super.let(key, value); + }); + } + + @override + Future unset(String key) async { + await _mutex.protect(() async { + return super.unset(key); + }); + } + + @override + Future signup(Map credentials) async { + return _mutex.protect(() async { + return super.signup(credentials); + }); + } + + @override + Future signin(Map credentials) async { + return _mutex.protect(() async { + return super.signin(credentials); + }); + } + + @override + Future invalidate() async { + await _mutex.protect(() async { + return super.invalidate(); + }); + } + + @override + Future authenticate(String token) async { + await _mutex.protect(() async { + return super.authenticate(token); + }); + } + + @override + Future info() async { + return _mutex.protect(() async { + return super.info(); + }); + } + + @override + Future patch( + String resource, + List> data, + ) async { + return _mutex.protect(() async { + return super.patch(resource, data); + }); + } + + @override + Future version() async { + return _mutex.protect(() async { + return super.version(); + }); + } + + @override + Future connect( + String endpoint, { + Map options = const {}, + }) async { + await _mutex.protect(() async { + return super.connect(endpoint, options: options); + }); + } + + @override + Future close() async { + await _mutex.protect(() async { + return super.close(); + }); + } + + @override + Future use({String? namespace, String? database}) async { + await _mutex.protect(() async { + return super.use(namespace: namespace, database: database); + }); + } + + @override + Future create(String resource, Object data) async { + return _mutex.protect(() async { + return super.create(resource, data); + }); + } + + @override + Future update(String resource, Object data) async { + return _mutex.protect(() async { + return super.update(resource, data); + }); + } + + @override + Future merge(String resource, Object data) async { + return _mutex.protect(() async { + return super.merge(resource, data); + }); + } + + @override + Future select(String resource) async { + return _mutex.protect(() async { + return super.select(resource); + }); + } + + @override + Future query( + String sql, { + Map bindings = const {}, + }) async { + return _mutex.protect(() async { + return super.query(sql, bindings: bindings); + }); + } + + @override + Future delete(String resource) async { + return _mutex.protect(() async { + return super.delete(resource); + }); + } + + @override + Future transaction( + Future Function(Transaction txn) action, { + Duration timeout = const Duration(seconds: 5), + }) async { + return _mutex.protect(() async { + return super.transaction(action, timeout: timeout); + }); + } +} diff --git a/lib/surrealdb_wasm.dart b/lib/surrealdb_wasm.dart index 53536b3..6d13175 100644 --- a/lib/surrealdb_wasm.dart +++ b/lib/surrealdb_wasm.dart @@ -3,3 +3,4 @@ library surrealdb_wasm; export 'src/js.dart'; export 'src/surreal_wasm.dart'; +export 'src/surreal_wasm_mutex.dart'; diff --git a/pubspec.yaml b/pubspec.yaml index 3a86dae..52c6a96 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -10,6 +10,7 @@ environment: dependencies: flutter: sdk: flutter + mutex: ^3.1.0 surrealdb_js: ^1.0.0-beta.20+4 dev_dependencies: