mirror of
https://github.com/wgh136/flutter_qjs.git
synced 2025-09-27 05:27:23 +00:00
remove handler when destroy
This commit is contained in:
@@ -3,8 +3,12 @@
|
||||
* @Author: ekibun
|
||||
* @Date: 2020-08-08 08:16:50
|
||||
* @LastEditors: ekibun
|
||||
* @LastEditTime: 2020-08-27 20:42:32
|
||||
* @LastEditTime: 2020-08-28 10:46:26
|
||||
-->
|
||||
## 0.0.6
|
||||
|
||||
* remove handler when destroy.
|
||||
|
||||
## 0.0.5
|
||||
|
||||
* add js module.
|
||||
|
@@ -3,7 +3,7 @@
|
||||
* @Author: ekibun
|
||||
* @Date: 2020-08-08 08:16:50
|
||||
* @LastEditors: ekibun
|
||||
* @LastEditTime: 2020-08-27 20:55:04
|
||||
* @LastEditTime: 2020-08-27 21:11:55
|
||||
-->
|
||||
# flutter_qjs
|
||||
|
||||
@@ -71,7 +71,8 @@ dart("http", "http://example.com/");
|
||||
|
||||
```dart
|
||||
await engine.setModuleHandler((String module) async {
|
||||
return await rootBundle.loadString("js/" + module.replaceFirst(new RegExp(r".js$"), "") + ".js");
|
||||
return await rootBundle.loadString(
|
||||
"js/" + module.replaceFirst(new RegExp(r".js$"), "") + ".js");
|
||||
});
|
||||
```
|
||||
|
||||
|
@@ -76,8 +76,9 @@ class _TestPageState extends State<TestPage> {
|
||||
}
|
||||
});
|
||||
await engine.setModuleHandler((String module) async {
|
||||
if(module == "test") return "export default '${new DateTime.now()}'";
|
||||
return await rootBundle.loadString("js/" + module.replaceFirst(new RegExp(r".js$"), "") + ".js");
|
||||
if (module == "test") return "export default '${new DateTime.now()}'";
|
||||
return await rootBundle.loadString(
|
||||
"js/" + module.replaceFirst(new RegExp(r".js$"), "") + ".js");
|
||||
});
|
||||
}
|
||||
|
||||
@@ -96,7 +97,8 @@ class _TestPageState extends State<TestPage> {
|
||||
scrollDirection: Axis.horizontal,
|
||||
child: Row(
|
||||
children: [
|
||||
FlatButton(child: Text("create engine"), onPressed: _createEngine),
|
||||
FlatButton(
|
||||
child: Text("create engine"), onPressed: _createEngine),
|
||||
FlatButton(
|
||||
child: Text("evaluate"),
|
||||
onPressed: () async {
|
||||
@@ -105,8 +107,9 @@ class _TestPageState extends State<TestPage> {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
resp =
|
||||
(await engine.evaluate(_controller.text ?? '', "<eval>")).toString();
|
||||
resp = (await engine.evaluate(
|
||||
_controller.text ?? '', "<eval>"))
|
||||
.toString();
|
||||
} catch (e) {
|
||||
resp = e.toString();
|
||||
}
|
||||
@@ -127,12 +130,11 @@ class _TestPageState extends State<TestPage> {
|
||||
color: Colors.grey.withOpacity(0.1),
|
||||
constraints: BoxConstraints(minHeight: 200),
|
||||
child: TextField(
|
||||
autofocus: true,
|
||||
controller: _controller,
|
||||
decoration: null,
|
||||
expands: true,
|
||||
maxLines: null
|
||||
),
|
||||
autofocus: true,
|
||||
controller: _controller,
|
||||
decoration: null,
|
||||
expands: true,
|
||||
maxLines: null),
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
Text("result:"),
|
||||
|
@@ -75,7 +75,7 @@ packages:
|
||||
path: ".."
|
||||
relative: true
|
||||
source: path
|
||||
version: "0.0.5"
|
||||
version: "0.0.6"
|
||||
flutter_test:
|
||||
dependency: "direct dev"
|
||||
description: flutter
|
||||
|
@@ -3,7 +3,7 @@
|
||||
* @Author: ekibun
|
||||
* @Date: 2020-08-08 08:29:09
|
||||
* @LastEditors: ekibun
|
||||
* @LastEditTime: 2020-08-27 18:23:47
|
||||
* @LastEditTime: 2020-08-28 10:45:14
|
||||
*/
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
@@ -23,6 +23,7 @@ class JsMethodHandlerNotImplement {}
|
||||
/// Make sure call `destroy` to terminate thread and release memory when you don't need it.
|
||||
class FlutterJs {
|
||||
dynamic _engine;
|
||||
dynamic get pointer => _engine;
|
||||
|
||||
_ensureEngine() async {
|
||||
if (_engine == null) {
|
||||
@@ -32,12 +33,16 @@ class FlutterJs {
|
||||
|
||||
/// Set a handler to manage js call with `dart(method, ...args)` function.
|
||||
setMethodHandler(JsMethodHandler handler) async {
|
||||
if (handler == null)
|
||||
return _FlutterJs.instance._methodHandlers.remove(_engine);
|
||||
await _ensureEngine();
|
||||
_FlutterJs.instance._methodHandlers[_engine] = handler;
|
||||
}
|
||||
|
||||
/// Set a handler to manage js module.
|
||||
setModuleHandler(JsModuleHandler handler) async {
|
||||
if (handler == null)
|
||||
return _FlutterJs.instance._moduleHandlers.remove(_engine);
|
||||
await _ensureEngine();
|
||||
_FlutterJs.instance._moduleHandlers[_engine] = handler;
|
||||
}
|
||||
@@ -45,8 +50,11 @@ class FlutterJs {
|
||||
/// Terminate thread and release memory.
|
||||
destroy() async {
|
||||
if (_engine != null) {
|
||||
await _FlutterJs.instance._channel.invokeMethod("close", _engine);
|
||||
await setMethodHandler(null);
|
||||
await setModuleHandler(null);
|
||||
var engine = _engine;
|
||||
_engine = null;
|
||||
await _FlutterJs.instance._channel.invokeMethod("close", engine);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +63,8 @@ class FlutterJs {
|
||||
await _ensureEngine();
|
||||
var arguments = {"engine": _engine, "script": command, "name": name};
|
||||
return _FlutterJs.instance._wrapFunctionArguments(
|
||||
await _FlutterJs.instance._channel.invokeMethod("evaluate", arguments), _engine);
|
||||
await _FlutterJs.instance._channel.invokeMethod("evaluate", arguments),
|
||||
_engine);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,15 +73,18 @@ class _FlutterJs {
|
||||
static _FlutterJs get instance => _getInstance();
|
||||
static _FlutterJs _instance;
|
||||
MethodChannel _channel = const MethodChannel('soko.ekibun.flutter_qjs');
|
||||
Map<dynamic, JsMethodHandler> _methodHandlers = Map<dynamic, JsMethodHandler>();
|
||||
Map<dynamic, JsModuleHandler> _moduleHandlers = Map<dynamic, JsModuleHandler>();
|
||||
Map<dynamic, JsMethodHandler> _methodHandlers =
|
||||
Map<dynamic, JsMethodHandler>();
|
||||
Map<dynamic, JsModuleHandler> _moduleHandlers =
|
||||
Map<dynamic, JsModuleHandler>();
|
||||
_FlutterJs._internal() {
|
||||
_channel.setMethodCallHandler((call) async {
|
||||
var engine = call.arguments["engine"];
|
||||
var args = call.arguments["args"];
|
||||
if (args is List) {
|
||||
if (_methodHandlers[engine] == null) return call.noSuchMethod(null);
|
||||
var ret = await _methodHandlers[engine](call.method, _wrapFunctionArguments(args, engine));
|
||||
var ret = await _methodHandlers[engine](
|
||||
call.method, _wrapFunctionArguments(args, engine));
|
||||
if (ret is JsMethodHandlerNotImplement) return call.noSuchMethod(null);
|
||||
return ret;
|
||||
} else {
|
||||
@@ -96,8 +108,13 @@ class _FlutterJs {
|
||||
if (val["__js_function__"] != null) {
|
||||
var functionId = val["__js_function__"];
|
||||
return (List<dynamic> args) async {
|
||||
var arguments = {"engine": engine, "function": functionId, "arguments": args};
|
||||
return _wrapFunctionArguments(await _channel.invokeMethod("call", arguments), engine);
|
||||
var arguments = {
|
||||
"engine": engine,
|
||||
"function": functionId,
|
||||
"arguments": args,
|
||||
};
|
||||
return _wrapFunctionArguments(
|
||||
await _channel.invokeMethod("call", arguments), engine);
|
||||
};
|
||||
} else
|
||||
for (var key in val.keys) {
|
||||
|
@@ -1,6 +1,6 @@
|
||||
name: flutter_qjs
|
||||
description: This plugin is a simple js engine for flutter using the `quickjs` project. Plugin currently supports Windows, Linux, and Android.
|
||||
version: 0.0.5
|
||||
version: 0.0.6
|
||||
homepage: https://github.com/ekibun/flutter_qjs
|
||||
|
||||
environment:
|
||||
|
Reference in New Issue
Block a user