Skip to content

Commit

Permalink
v0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ekibun committed Jan 25, 2021
1 parent ee110f5 commit 6fb2c47
Show file tree
Hide file tree
Showing 12 changed files with 452 additions and 505 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
* @LastEditTime: 2020-12-02 11:36:40
-->

## 0.3.1

* code clean up.
* fix isolate wrap error.

## 0.3.0

* breakdown change to remove `channel`.
Expand Down
48 changes: 30 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,31 @@ engine = null;

Data conversion between dart and js are implemented as follow:

| dart | js |
| --------- | ------------------ |
| Bool | boolean |
| Int | number |
| Double | number |
| String | string |
| Uint8List | ArrayBuffer |
| List | Array |
| Map | Object |
| Function | function(....args) |
| Future | Promise |
| Object | DartObject |

**notice:** Dart function parameter `thisVal` is used to store `this` in js.
| dart | js |
| ----------------------- | ------------------ |
| Bool | boolean |
| Int | number |
| Double | number |
| String | string |
| Uint8List | ArrayBuffer |
| List | Array |
| Map | Object |
| Function<br>JSInvokable | function(....args) |
| Future | Promise |
| Object | DartObject |

**notice:** `JSInvokable` does not extend `Function`, but can be used same as `Function`.
Dart function uses named argument `thisVal` to manage js function `this`:

```dart
func(arg1, arg2, {thisVal});
```

or use `invoke` method to pass list parameters:

```dart
(func as JSInvokable).invoke([arg1, arg2], thisVal);
```

### Use modules

Expand Down Expand Up @@ -119,10 +130,11 @@ 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:
**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(() {
await setToGlobalObject("func", await engine.bind(({thisVal}) {
// DO SOMETHING
}))
```
Expand All @@ -131,8 +143,8 @@ await setToGlobalObject("func", await engine.bind(() {

## Breaking change in v0.3.0

`channel` function is no longer utilized by default.
Use js function to set to global:
`channel` function is no longer included by default.
Use js function to set dart object globally:

```dart
final setToGlobalObject = await engine.evaluate("(key, val) => this[key] = val;");
Expand Down
13 changes: 0 additions & 13 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
# flutter_qjs_example

Demonstrates how to use the flutter_qjs plugin.

## Getting Started

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab)
- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook)

For help getting started with Flutter, view our
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
27 changes: 0 additions & 27 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
* @LastEditTime: 2020-12-02 11:28:06
*/
import 'package:flutter/material.dart';
import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:flutter/services.dart';
import 'package:flutter_qjs/isolate.dart';

Expand Down Expand Up @@ -44,28 +42,6 @@ class TestPage extends StatefulWidget {
State<StatefulWidget> createState() => _TestPageState();
}

dynamic methodHandler(String method, List arg) {
switch (method) {
case "http":
return Dio().get(arg[0]).then((response) => response.data);
case "test":
return arg[0]([
true,
1,
0.5,
"str",
{"key": "val", 0: 1},
Uint8List(2),
Int32List(2),
Int64List(2),
Float64List(2),
Float32List(2)
]);
default:
throw Exception("No such method");
}
}

class _TestPageState extends State<TestPage> {
String resp;
IsolateQjs engine;
Expand All @@ -82,9 +58,6 @@ class _TestPageState extends State<TestPage> {
"js/" + module.replaceFirst(new RegExp(r".js$"), "") + ".js");
},
);
final setToGlobalObject =
await engine.evaluate("(key, val) => this[key] = val;");
setToGlobalObject("channel", methodHandler);
}

@override
Expand Down
16 changes: 1 addition & 15 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.15.0-nullsafety.5"
dio:
dependency: "direct main"
description:
name: dio
url: "https://pub.flutter-io.cn"
source: hosted
version: "3.0.10"
fake_async:
dependency: transitive
description:
Expand Down Expand Up @@ -82,7 +75,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.3.0"
version: "0.3.1"
flutter_test:
dependency: "direct dev"
description: flutter
Expand All @@ -95,13 +88,6 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.6.0"
http_parser:
dependency: transitive
description:
name: http_parser
url: "https://pub.flutter-io.cn"
source: hosted
version: "3.1.4"
matcher:
dependency: transitive
description:
Expand Down
1 change: 0 additions & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ dependencies:

highlight: 0.6.0
flutter_highlight: 0.6.0
dio: 3.0.10

dev_dependencies:
flutter_test:
Expand Down
Loading

0 comments on commit 6fb2c47

Please sign in to comment.