Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Posting dynamic type message to Web Worker. #242

Open
ebala96 opened this issue May 22, 2024 · 5 comments
Open

Posting dynamic type message to Web Worker. #242

ebala96 opened this issue May 22, 2024 · 5 comments
Labels
type-question A question about expected behavior or functionality

Comments

@ebala96
Copy link

ebala96 commented May 22, 2024

I have written dart side post message to send dart Map to web worker postMessage.

Future<void> postMessageDynamic(int id, message) async{
    final worker = _workers[id];
    Map<String , dynamic> messageMap = message as Map<String,dynamic>;

    worker.postMessage(messageMap.toJSBox);
}

But toJsBox creates an empty object without the actual Map data I want to send.
Let me know how can I send dart Map to postMessage without using json encoding.

@srujzs
Copy link
Contributor

srujzs commented May 22, 2024

Dart Maps are not JS types and they can not be passed directly to postMessage. In order to clone a Map into a JS object, use jsify e.g. messageMap.jsify().

@srujzs srujzs added the type-question A question about expected behavior or functionality label May 22, 2024
@ebala96
Copy link
Author

ebala96 commented May 22, 2024

A follow up on js interop.

How can I define list of any type inside interop definiftion?

@srujzs
Copy link
Contributor

srujzs commented May 22, 2024

Lists also are not JS types so they'll need to be converted to a JSArray, either through a manual conversion with List<JSAny?>.toJS or using jsify. The appropriate type for a JSArray of any type is JSArray<JSAny?>.

@ebala96
Copy link
Author

ebala96 commented May 24, 2024

I am sending dartMap.jsify() to web workers and getting back JS object from web worker.
On receiving back from web worker I do a message.data.dartify() and convert it into Map<String, dynamic> using
Map<String, dynamic>.from(message.data.dartify() as Map)

I am expecting my Map data to be Map<String, List<String>>
Since dartify map conversion mentioned above which I am doing gives Map<String, List<Object?>>, I have to do one more iteration in List to do a List conversion.

What is the better way to type convert this?

@srujzs
Copy link
Contributor

srujzs commented May 28, 2024

Right, the generic cast will fail because the type parameters are just Object?. JS arrays and objects are allowed to contain values of different types, and therefore the resulting generics are Object?. If you control the code that iterates through the map you can use the cast function to do the cast without having to iterate over the list e.g.

final map = (original.jsify().dartify() as Map).cast<String, List>();
for (final key in map.keys()) {
  final list = map[key].cast<String>();
  ...
}

If you want to avoid that extra iteration, you can manually convert the map yourself using interop (which will be faster), but that'll require more code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-question A question about expected behavior or functionality
Projects
None yet
Development

No branches or pull requests

2 participants