Skip to content

Commit

Permalink
feat: added SurrealWasmMutex class
Browse files Browse the repository at this point in the history
  • Loading branch information
limcheekin committed Aug 24, 2024
1 parent 26d2a0c commit e5f08ff
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 7 deletions.
4 changes: 2 additions & 2 deletions example/flutter_console_widget/lib/flutter_console.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion integration_test/patch_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion integration_test/surrealdb_wasm_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion integration_test/transaction_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
163 changes: 163 additions & 0 deletions lib/src/surreal_wasm_mutex.dart
Original file line number Diff line number Diff line change
@@ -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<void> let(String key, Object value) async {
await _mutex.protect(() async {
return super.let(key, value);
});
}

@override
Future<void> unset(String key) async {
await _mutex.protect(() async {
return super.unset(key);
});
}

@override
Future<Object?> signup(Map<String, dynamic> credentials) async {
return _mutex.protect(() async {
return super.signup(credentials);
});
}

@override
Future<Object?> signin(Map<String, dynamic> credentials) async {
return _mutex.protect(() async {
return super.signin(credentials);
});
}

@override
Future<void> invalidate() async {
await _mutex.protect(() async {
return super.invalidate();
});
}

@override
Future<void> authenticate(String token) async {
await _mutex.protect(() async {
return super.authenticate(token);
});
}

@override
Future<Object?> info() async {
return _mutex.protect(() async {
return super.info();
});
}

@override
Future<Object?> patch(
String resource,
List<Map<String, dynamic>> data,
) async {
return _mutex.protect(() async {
return super.patch(resource, data);
});
}

@override
Future<String> version() async {
return _mutex.protect(() async {
return super.version();
});
}

@override
Future<void> connect(
String endpoint, {
Map<String, dynamic> options = const {},
}) async {
await _mutex.protect(() async {
return super.connect(endpoint, options: options);
});
}

@override
Future<void> close() async {
await _mutex.protect(() async {
return super.close();
});
}

@override
Future<void> use({String? namespace, String? database}) async {
await _mutex.protect(() async {
return super.use(namespace: namespace, database: database);
});
}

@override
Future<Object?> create(String resource, Object data) async {
return _mutex.protect(() async {
return super.create(resource, data);
});
}

@override
Future<Object?> update(String resource, Object data) async {
return _mutex.protect(() async {
return super.update(resource, data);
});
}

@override
Future<Object?> merge(String resource, Object data) async {
return _mutex.protect(() async {
return super.merge(resource, data);
});
}

@override
Future<Object?> select(String resource) async {
return _mutex.protect(() async {
return super.select(resource);
});
}

@override
Future<Object?> query(
String sql, {
Map<String, dynamic> bindings = const {},
}) async {
return _mutex.protect(() async {
return super.query(sql, bindings: bindings);
});
}

@override
Future<Object?> delete(String resource) async {
return _mutex.protect(() async {
return super.delete(resource);
});
}

@override
Future<Object?> transaction(
Future<void> Function(Transaction txn) action, {
Duration timeout = const Duration(seconds: 5),
}) async {
return _mutex.protect(() async {
return super.transaction(action, timeout: timeout);
});
}
}
1 change: 1 addition & 0 deletions lib/surrealdb_wasm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ library surrealdb_wasm;

export 'src/js.dart';
export 'src/surreal_wasm.dart';
export 'src/surreal_wasm_mutex.dart';
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ environment:
dependencies:
flutter:
sdk: flutter
mutex: ^3.1.0
surrealdb_js: ^1.0.0-beta.20+4

dev_dependencies:
Expand Down

0 comments on commit e5f08ff

Please sign in to comment.