d-markey / squadron_builder

Dart code generator for Squadron workers. Implement your worker service and let squadron_builder bridge the gap with Web Workers and Isolates!
https://pub.dev/packages/squadron_builder
MIT License
15 stars 3 forks source link

[Web] UnimplementedError: structured clone of other type #10

Closed abdelaziz-mahdy closed 10 months ago

abdelaziz-mahdy commented 11 months ago

Hello,

Firstly, I want to express my gratitude for the awesome squadron package and its builder. It's been incredibly useful in my project. However, I've encountered an issue when running my Flutter web app. I'm getting an UnimplementedError: structured clone of other type. This error doesn't occur on macOS, but it appears on the web.

From my research, it seems the problem is related to using classes that are not serializable by JavaScript. Could you provide guidance on how to make these classes serializable for the web? I read something about marshallers in the issues section; can they be used to resolve this issue?

For reference, here's the link to the code: bouncy_ball_physics on GitHub.

Thank you for your help and the fantastic work on this package!

d-markey commented 11 months ago

Hello,

you're right, not all Dart classes can be transfered to Web workers. Base types (lists, ints, strings...) will happily cross Web worker boundaries, but custom types will not.

To work around this issue, you should implement your own serializers (marshalers in Squadron, see class SquadronMarshaler). You will find examples here: https://github.com/d-markey/squadron_sample/tree/main/lib/src/json/marshalers. These serializers should make sure only base types are used.

abdelaziz-mahdy commented 11 months ago

Just to be sure the builder does not automatically generate those classes? And is there an annotations to make it auto generated by builder?

Or do I have to implement each class marshalers by myself

d-markey commented 10 months ago

Nope, serialization is up to you and can be a sensitive topic for performance.

Depending on the "shape" of your data, you might want to serialize using a binary buffer eg Int32List or to string eg Json. If your data contains other classes / list of objects, the nested level and/or the list length can impact perf depending on the serialization strategy. squadron_builder cannot make that choice.

You mighty be interested in https://github.com/d-markey/squadron_builder/issues/9 for performance aspects on VM vs Web.

abdelaziz-mahdy commented 10 months ago

Nope, serialization is up to you and can be a sensitive topic for performance.

Depending on the "shape" of your data, you might want to serialize using a binary buffer eg Int32List or to string eg Json. If your data contains other classes / list of objects, the nested level and/or the list length can impact perf depending on the serialization strategy. squadron_builder cannot make that choice.

You mighty be interested in https://github.com/d-markey/squadron_builder/issues/9 for performance aspects on VM vs Web.

Thank you for mentioning the issue, this is the issue I checked before, when I got stuck,

I think I will go with converting the parameters to map and using that map in the function

If possible for the sake of future devs using the builder can a warning be added for this problem if they are not using basic types?

d-markey commented 10 months ago

Could be helpful indeed, I'll keep that in mind for a future release. Closing this issue as resolved, don't hesitate to open another one or reopen this one if you need more help!

abdelaziz-mahdy commented 10 months ago

Thank you very much for your help, and I will see if I could implement a pr