Skip to content

Commit

Permalink
Improve jsify and dartify Type
Browse files Browse the repository at this point in the history
  • Loading branch information
lamnhan066 committed Oct 26, 2024
1 parent 906594a commit e63f285
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 9 additions & 6 deletions lib/src/base/contactor/utils/utils.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'dart:js_interop';

/// Returns Dart representation from JS Object.
dynamic dartify(dynamic object) {
Object? dartify(dynamic object) {
// Convert JSObject to Dart equivalents directly
// Cannot be done with Dart 3.2 constraints
// ignore: invalid_runtime_check_with_js_interop_types
if (object is! JSObject) {
return object;
return object as Object?;
}

final jsObject = object;
Expand All @@ -16,11 +16,12 @@ dynamic dartify(dynamic object) {
return convertNested(dartObject);
}

dynamic convertNested(dynamic object) {
/// Convert nested Map or List.
Object? convertNested(Object? object) {
if (object is List) {
return object.map(convertNested).toList();
} else if (object is Map) {
var map = <dynamic, dynamic>{};
final map = <dynamic, dynamic>{};
object.forEach((key, value) {
map[key] = convertNested(value);
});
Expand All @@ -34,15 +35,17 @@ dynamic convertNested(dynamic object) {
/// Returns the JS implementation from Dart Object.
JSAny? jsify(Object? dartObject) {
if (dartObject == null) {
return dartObject?.jsify();
return dartObject.jsify();
}

if (dartObject is List) {
return dartObject.map(jsify).toList().toJS;
}

if (dartObject is Map) {
return dartObject.map((key, value) => MapEntry(key, jsify(value))).jsify();
return dartObject
.map((key, value) => MapEntry(jsify(key), jsify(value)))
.jsify();
}

if (dartObject is JSAny Function()) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/isolate_manager_controller/web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class _IsolateManagerWorkerController<R, P>

_IsolateManagerWorkerController(this.self) {
self.onmessage = (MessageEvent event) {
_streamController.sink.add(dartify(event.data));
_streamController.sink.add(dartify(event.data) as P);
}.toJS;
}

Expand Down

0 comments on commit e63f285

Please sign in to comment.