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