lohnn / integral_isolates

Dart package making isolates easy
MIT License
12 stars 2 forks source link

[FEATURE REQUEST] Plugin support #34

Open deckerst opened 1 year ago

deckerst commented 1 year ago

Is your feature request related to a problem? Please describe. Trying to call a plugin within a spawn isolate fails. This limits use cases for this package.

Describe the solution you'd like Since 13937 it's possible to pass a root token and call BackgroundIsolateBinaryMessenger.ensureInitialized within the isolate. But it only works for some plugins (those that do dart -> platform messenging, but not platform -> dart messenging). Hopefully this package can do the necessary plumbing so that it works whatever the plugin.

Describe alternatives you've considered flutter_isolate is another package trying to make isolates nicer, and it supports plugins, but it has its own issues and limitations.

lohnn commented 1 year ago

Hi!

This is definitely something I've intended to look at at some point. I'll have to look at what the overhead for this would be, and what I'd need to do to make integral_isolate still be able to run in a pure Dart environment too. I'd be more than happy to get feedback on this and discuss my options/potential solutions to be able to bring interop with packages to integral_isolates. ☺️

lohnn commented 8 months ago

Hello again!

I am sorry for being so slow at investigating this but I've had a lot of stuff on my plate, and to be honest, this feature request was not on top of my list.

I have tried something that seems to work for me, but I also want to check if it will solve it for you too.

If you try calling this piece of code before making any calls to a plugin:

// Define as a top level function or a static function, just as normal
void _initPluginForIsolate(RootIsolateToken rootIsolateToken) {
  BackgroundIsolateBinaryMessenger.ensureInitialized(rootIsolateToken);
  print('I am init!');
}
...
// Creating the isolate
isolate = StatefulIsolate();
final rootIsolateToken = RootIsolateToken.instance!;
await isolate.compute(_initPluginForIsolate, rootIsolateToken);

Are you able to call the plugins you wish to work then? I seem to be able to call at least the shared_preferences plugin, but I would love to hear how well this workaround works.

If it works well, I'll try to neatly wrap it in a nice package and do it automatically in Flutter apps.

lohnn commented 7 months ago

I realise now that I repeated exactly what you said and didn't actually solve your problem 🙈 But I am looking into how to fix this and I'm working on a wrapping package that sets up and allows for this functionality.

deckerst commented 7 months ago

Glad you're looking into it. However, to be perfectly candid, when I opened this issue I was investigating something and trying out several isolate related packages, but I can't remember what I was searching then... So I can't really follow up on my initial request or help you push this further.

lohnn commented 7 months ago

I believe I've come to the conclusion that I will need to implement native code for all supported platforms in order to support hooking up native calls to push data to Flutter. I have decided that I will create the package anyway, but initially only support dart -> native. I will then create a follow up issue in the backlog to try to implement native -> dart. At least I think that I know how I want the API surface to look when I eventually cross that road.

Thank you for the feature request, it is definitely something that I really want to have eventually!