This commit is contained in:
ekibun
2021-01-24 21:12:13 +08:00
parent 6b0bab2faf
commit ee110f55e1
12 changed files with 202 additions and 170 deletions

View File

@@ -60,23 +60,6 @@ Data conversion between dart and js are implemented as follow:
**notice:** Dart function parameter `thisVal` is used to store `this` in js.
### Set into global object
Method `setToGlobalObject` is presented to set dart object into global object.
For example, you can pass a function implement http in JavaScript with `Dio`:
```dart
engine.setToGlobalObject("http", (String url) {
return Dio().get(url).then((response) => response.data);
});
```
then, in java script you can use `http` function to invoke dart function:
```javascript
http("http://example.com/");
```
### Use modules
ES6 module with `import` function is supported and can be managed in dart with `moduleHandler`:
@@ -105,8 +88,6 @@ To use async function in module handler, try [Run on isolate thread](#Run-on-iso
Create a `IsolateQjs` object, pass handlers to resolving modules. Async function such as `rootBundle.loadString` can be used now to get modules:
The `methodHandler` is used in isolate, so **the handler function must be a top-level function or a static method**.
```dart
dynamic methodHandler(String method, List arg) {
switch (method) {
@@ -126,21 +107,11 @@ final engine = IsolateQjs(
// not need engine.dispatch();
```
Method `setToGlobalObject` is still here to set dart object into global object. Use `await` to make sure it is finished.
**Make sure the object that can pass through isolate**, For example, a top level function:
```dart
dynamic http(String url) {
return Dio().get(url).then((response) => response.data);
}
await engine.setToGlobalObject("http", http);
```
Same as run on main thread, use `evaluate` to run js script. In this way, `Promise` return by `evaluate` will be automatically tracked and return the resolved data:
```dart
try {
print(await engine.evaluate(code ?? '', "<eval>"));
print(await engine.evaluate(code ?? ''));
} catch (e) {
print(e.toString());
}
@@ -148,4 +119,22 @@ try {
Method `close` can destroy quickjs runtime that can be recreated again if you call `evaluate`.
**notice:** Make sure arguments passed to `IsolateJSFunction` are avaliable for isolate, such as primities and top level function. Method `bind` can help to pass instance function to isolate:
```dart
await setToGlobalObject("func", await engine.bind(() {
// DO SOMETHING
}))
```
[This example](example/lib/main.dart) contains a complete demonstration on how to use this plugin.
## Breaking change in v0.3.0
`channel` function is no longer utilized by default.
Use js function to set to global:
```dart
final setToGlobalObject = await engine.evaluate("(key, val) => this[key] = val;");
setToGlobalObject("channel", methodHandler);
```