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

Support isolate #52

Closed
623637646 opened this issue Jul 31, 2021 · 4 comments
Closed

Support isolate #52

623637646 opened this issue Jul 31, 2021 · 4 comments

Comments

@623637646
Copy link

623637646 commented Jul 31, 2021

My JS code is enormous computation. I have to call it on non-main isolate. But I can't do it cause of a crash: "Unhandled Exception: Exception: Null check operator used on a null value". The reason is spawned isolates do not have access to the root bundle flutter/flutter#61480 (comment)

This demo shows what I want.

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_js/flutter_js.dart';

void main() async {
  runApp(MaterialApp(
    home: Scaffold(
      body: Center(child: Text("hello word")),
    ),
  ));

  final jsCode = "2+3;";

  // This code has error: "Unhandled Exception: Exception: Null check operator used on a null value".
  final String result = await compute(_execute, jsCode);

  // This code works fine.
  // final int result = await _execute(jsCode);
  print(result);
}

Future<T> _execute<T>(String jsCode) async {
  final javascriptRuntime = getJavascriptRuntime();
  final result = javascriptRuntime.evaluate(jsCode);
  return javascriptRuntime.convertValue(result);
}
@wardy484
Copy link

This would be great, I have a process that does a lot of processing (unfortuantely in JS).

@jpolstre
Copy link

jpolstre commented May 10, 2022

I had some problems with this package and on reviewing the code I found that it depends on Quickjs Dart, which apparently already performs the operations in a separate isolation.
This is an image

@sirenoremac
Copy link

If you do not need xhr, you can pass false to the xhr parameter of getJavascriptRuntime() and it will not try to access the root bundle:
final javascriptRuntime = getJavascriptRuntime(xhr: false);

If you do this, it will work in an isolate.

@abner
Copy link
Owner

abner commented May 23, 2022

If you do not need xhr, you can pass false to the xhr parameter of getJavascriptRuntime() and it will not try to access the root bundle: final javascriptRuntime = getJavascriptRuntime(xhr: false);

If you do this, it will work in an isolate.

By now I think it is the best suitable solution. I would have to dig more and change the way we load the polyfills to xhr and fetch, so it doesn't rely in access rootBundle (put them fixed as strings into the code would be the solution)

@abner abner closed this as completed May 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants