lamnhan066 / isolate_manager

Create multiple long-lived isolates for the Functions, supports Worker on the Web (with the effective generator) and WASM compilation.
https://pub.dev/packages/isolate_manager
MIT License
35 stars 2 forks source link

Unable to Pass Map as Input/Output in isolate-manager Causing UI Freeze on jsonEncode/jsonDecode #31

Open abbashosseinii opened 4 days ago

abbashosseinii commented 4 days ago

Hello,

I encountered an issue while using the isolate-manager package. It seems that I’m unable to pass a Map as an input or output when using isolate-manager, which leads to problems when handling large data transfers.

As a workaround, I was able to send and receive a Map using a worker without any issues. However, when attempting the same process using isolate-manager, the error occurs, and the UI freezes during jsonEncode and jsonDecode.

Is there any way to resolve this issue or support passing a Map?

Thank you for your assistance!

Steps to Reproduce:

1.  Try passing a Map as an input or output in isolate-manager.
2.  Experience the No such method error.

Expected Behavior:

Be able to pass a Map without encountering an error.

Environment:

•   Flutter version: (3.24.3)
•   isolate-manager version: (^5.3.0)
•   Device: (chrome)
lamnhan066 commented 4 days ago

Hi @abbashosseinii

Can you let me know the way that you send and receive a Map via Worker? I think we have no similar Dart Map in JS so we cannot send a Map directly to the JS. The only way I can do is by using jsonEncode and jsonDecode.

abbashosseinii commented 3 days ago

Hello dear @lamnhan066 This is possible. We just need our models to work with Map<dynamic, dynamic> in the toJson and fromJson methods. I wrote a simple project where I implemented this using a basic worker.js. I’ll attach the project link below. Thank you for your quick response. Sample project

lamnhan066 commented 3 days ago

Hi @abbashosseinii

If you are using the toJson and fromJson the the package will work flawlessly when you're using String like this (I copied this from the document) :

main() async {
  final isolate = IsolateManager.create(
    aStringIntMap,
    workerName: 'aStringIntMap',
    isDebug: true,
  );
  await isolate.start();

  final map = {'a': 1, 'b': 2, 'c': 3};
  final result = await isolate.compute(jsonEncode(map));
  expect(jsonDecode(result), map);
}

@isolateManagerWorker
String aStringIntMap(String params) {
  return params;
}
abbashosseinii commented 2 days ago

Dear @lamnhan066

I opened this issue for three reasons:

1- The reason for choosing the package

The solution you provided for encoding and decoding is not compatible with the web platform, and the main reason I chose this package over conventional methods is to leverage multi-threading in the web environment.

2- Compatibility with very large data

In cases where large amounts of data need to be transferred, like in the project I’m currently working on, this encoding and decoding process becomes a very time-consuming operation.

3- Work smarter, not harder

We can only solve this issue if we remove the current limitations on input and output and handle it through Map, as I explained earlier. This way, we can significantly reduce the computational load and noticeably shorten task completion times.

lamnhan066 commented 2 days ago

Hi @abbashosseinii

I misunderstood your last opinion because at first, I thought your toJson returns a String instead of a Map. I just realized that when I looked into the worker.js. I think the issue was caused when compiling from Dart to JS (your script was written in JS native).

I think I will try to write a simple script in Dart using a Map, compile it to JS and let’s see if I can find any way to make it work.

Thank you for your information.

abbashosseinii commented 2 days ago

@lamnhan066 Thank you so much! Wish you success!