mirror of
https://gitlab.com/veilid/veilid.git
synced 2024-12-23 22:39:36 -05:00
wasm work
This commit is contained in:
parent
736e7a0cb0
commit
a39b38cc16
@ -89,7 +89,7 @@ class _MyAppState extends State<MyApp> with UiLoggy {
|
||||
// Platform messages may fail, so we use a try/catch PlatformException.
|
||||
// We also handle the message potentially returning null.
|
||||
try {
|
||||
veilidVersion = Veilid.instance.veilidVersionString();
|
||||
veilidVersion = await Veilid.instance.veilidVersionString();
|
||||
} on Exception {
|
||||
veilidVersion = 'Failed to get veilid version.';
|
||||
}
|
||||
|
@ -149,6 +149,13 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.7.0"
|
||||
mutex:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: mutex
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "3.0.0"
|
||||
path:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -301,6 +308,13 @@ packages:
|
||||
relative: true
|
||||
source: path
|
||||
version: "0.0.1"
|
||||
wasm_interop:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: wasm_interop
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.0.1"
|
||||
win32:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
@ -996,6 +996,6 @@ abstract class Veilid {
|
||||
Future<void> changeLogLevel(VeilidConfigLogLevel logLevel);
|
||||
Future<void> shutdownVeilidCore();
|
||||
Future<String> debug(String command);
|
||||
String veilidVersionString();
|
||||
VeilidVersion veilidVersion();
|
||||
Future<String> veilidVersionString();
|
||||
Future<VeilidVersion> veilidVersion();
|
||||
}
|
||||
|
@ -326,7 +326,7 @@ class VeilidFFI implements Veilid {
|
||||
}
|
||||
|
||||
@override
|
||||
String veilidVersionString() {
|
||||
Future<String> veilidVersionString() async {
|
||||
final versionString = _veilidVersionString();
|
||||
String ret = versionString.toDartString();
|
||||
_freeString(versionString);
|
||||
@ -334,7 +334,7 @@ class VeilidFFI implements Veilid {
|
||||
}
|
||||
|
||||
@override
|
||||
VeilidVersion veilidVersion() {
|
||||
Future<VeilidVersion> veilidVersion() async {
|
||||
final version = _veilidVersion();
|
||||
return VeilidVersion(
|
||||
version.major,
|
||||
|
@ -1,46 +1,82 @@
|
||||
import 'veilid.dart';
|
||||
|
||||
import 'dart:js';
|
||||
import 'dart:js_util';
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/services.dart' show NetworkAssetBundle;
|
||||
import 'package:wasm_interop/wasm_interop.dart';
|
||||
import 'package:mutex/mutex.dart';
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
Veilid getVeilid() => VeilidJS();
|
||||
|
||||
Instance? _wasmInstance;
|
||||
final _wasmInstanceMutex = Mutex();
|
||||
|
||||
Future<Instance> getWasmInstance() async {
|
||||
await _wasmInstanceMutex.acquire();
|
||||
var _wi = _wasmInstance;
|
||||
if (_wi == null) {
|
||||
final bytes = await http???.get(Uri.parse("/wasm/veilid_wasm.wasm"));
|
||||
_wi = await Instance.fromBufferAsync(bytes.buffer);
|
||||
_wasmInstance = _wi;
|
||||
}
|
||||
_wasmInstanceMutex.release();
|
||||
return _wi;
|
||||
}
|
||||
|
||||
class VeilidJS implements Veilid {
|
||||
@override
|
||||
Stream<VeilidUpdate> startupVeilidCore(VeilidConfig config) {
|
||||
throw UnimplementedError();
|
||||
Stream<VeilidUpdate> startupVeilidCore(VeilidConfig config) async* {
|
||||
var wasm = (await getWasmInstance());
|
||||
var streamController = StreamController<VeilidUpdate>();
|
||||
await promiseToFuture(
|
||||
wasm.functions["startup_veilid_core"]!.call((String update) {
|
||||
streamController.add(VeilidUpdate.fromJson(jsonDecode(update)));
|
||||
}, jsonEncode(config.json, toEncodable: veilidApiToEncodable)));
|
||||
yield* streamController.stream;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<VeilidState> getVeilidState() {
|
||||
throw UnimplementedError();
|
||||
Future<VeilidState> getVeilidState() async {
|
||||
var wasm = (await getWasmInstance());
|
||||
return VeilidState.fromJson(jsonDecode(
|
||||
await promiseToFuture(wasm.functions["get_veilid_state"]!.call())));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> changeLogLevel(VeilidConfigLogLevel logLevel) {
|
||||
throw UnimplementedError();
|
||||
Future<void> changeLogLevel(VeilidConfigLogLevel logLevel) async {
|
||||
var wasm = (await getWasmInstance());
|
||||
await promiseToFuture(wasm.functions["change_log_level"]!
|
||||
.call(jsonEncode(logLevel.json, toEncodable: veilidApiToEncodable)));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> shutdownVeilidCore() {
|
||||
throw UnimplementedError();
|
||||
Future<void> shutdownVeilidCore() async {
|
||||
var wasm = (await getWasmInstance());
|
||||
await promiseToFuture(wasm.functions["shutdown_veilid_core"]!.call());
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> debug(String command) {
|
||||
throw UnimplementedError();
|
||||
Future<String> debug(String command) async {
|
||||
var wasm = (await getWasmInstance());
|
||||
return await promiseToFuture(wasm.functions["debug"]!.call(command));
|
||||
}
|
||||
|
||||
@override
|
||||
String veilidVersionString() {
|
||||
throw UnimplementedError();
|
||||
Future<String> veilidVersionString() async {
|
||||
var wasm = (await getWasmInstance());
|
||||
return await promiseToFuture(wasm.functions["debug"]!.call());
|
||||
}
|
||||
|
||||
@override
|
||||
VeilidVersion veilidVersion() {
|
||||
throw UnimplementedError();
|
||||
Future<VeilidVersion> veilidVersion() async {
|
||||
var wasm = (await getWasmInstance());
|
||||
var jsonVersion = jsonDecode(
|
||||
await promiseToFuture(wasm.functions["get_veilid_state"]!.call()));
|
||||
return VeilidVersion(
|
||||
jsonVersion["major"], jsonVersion["minor"], jsonVersion["patch"]);
|
||||
}
|
||||
}
|
||||
|
@ -1,41 +1,17 @@
|
||||
import 'dart:async';
|
||||
// In order to *not* need this ignore, consider extracting the "web" version
|
||||
// of your plugin as a separate package, instead of inlining it in the same
|
||||
// package as the core of your plugin.
|
||||
// ignore: avoid_web_libraries_in_flutter
|
||||
import 'dart:html' as html show window;
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
|
||||
|
||||
// xxx link in WASM version of veilid-flutter
|
||||
|
||||
/// A web implementation of the Veilid plugin.
|
||||
/// A stub web implementation of the Veilid plugin
|
||||
/// Because everything is done with FFI or WASM, we don't use this interface
|
||||
class VeilidPluginStubWeb {
|
||||
static void registerWith(Registrar registrar) {
|
||||
// final MethodChannel channel = MethodChannel(
|
||||
// 'veilid',
|
||||
// const StandardMethodCodec(),
|
||||
// registrar,
|
||||
// );
|
||||
static void registerWith(Registrar registrar) {}
|
||||
|
||||
// final pluginInstance = VeilidWeb();
|
||||
// channel.setMethodCallHandler(pluginInstance.handleMethodCall);
|
||||
}
|
||||
|
||||
/// Handles method calls over the MethodChannel of this plugin.
|
||||
/// Note: Check the "federated" architecture for a new way of doing this:
|
||||
/// https://flutter.dev/go/federated-plugins
|
||||
Future<dynamic> handleMethodCall(MethodCall call) async {
|
||||
// switch (call.method) {
|
||||
// case 'getPlatformVersion':
|
||||
// return getPlatformVersion();
|
||||
// default:
|
||||
throw PlatformException(
|
||||
code: 'Unimplemented',
|
||||
details: 'veilid for web doesn\'t implement \'${call.method}\'',
|
||||
);
|
||||
// }
|
||||
throw PlatformException(
|
||||
code: 'Unimplemented',
|
||||
details: 'Veilid for Web doesn\'t implement \'${call.method}\'',
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -17,6 +17,8 @@ dependencies:
|
||||
change_case: ^1.0.1
|
||||
path_provider: ^2.0.9
|
||||
path: ^1.8.0
|
||||
wasm_interop: ^2.0.1
|
||||
mutex: ^3.0.0
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
@ -100,12 +100,12 @@ where
|
||||
|
||||
// WASM Bindings
|
||||
|
||||
#[wasm_bindgen(js_namespace = veilid)]
|
||||
#[wasm_bindgen()]
|
||||
pub fn initialize_veilid_wasm() {
|
||||
console_error_panic_hook::set_once();
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_namespace = veilid)]
|
||||
#[wasm_bindgen()]
|
||||
pub fn startup_veilid_core(update_callback: Function, json_config: String) -> Promise {
|
||||
wrap_api_future(async move {
|
||||
let update_callback = Arc::new(move |update: VeilidUpdate| {
|
||||
@ -129,7 +129,7 @@ pub fn startup_veilid_core(update_callback: Function, json_config: String) -> Pr
|
||||
})
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_namespace = veilid)]
|
||||
#[wasm_bindgen()]
|
||||
pub fn get_veilid_state() -> Promise {
|
||||
wrap_api_future(async move {
|
||||
let veilid_api = get_veilid_api()?;
|
||||
@ -148,7 +148,7 @@ pub fn change_log_level(log_level: String) -> Promise {
|
||||
})
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_namespace = veilid)]
|
||||
#[wasm_bindgen()]
|
||||
pub fn shutdown_veilid_core() -> Promise {
|
||||
wrap_api_future(async move {
|
||||
let veilid_api = take_veilid_api()?;
|
||||
@ -157,7 +157,7 @@ pub fn shutdown_veilid_core() -> Promise {
|
||||
})
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_namespace = veilid)]
|
||||
#[wasm_bindgen()]
|
||||
pub fn debug(command: String) -> Promise {
|
||||
wrap_api_future(async move {
|
||||
let veilid_api = get_veilid_api()?;
|
||||
@ -166,7 +166,7 @@ pub fn debug(command: String) -> Promise {
|
||||
})
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_namespace = veilid)]
|
||||
#[wasm_bindgen()]
|
||||
pub fn veilid_version_string() -> String {
|
||||
veilid_core::veilid_version_string()
|
||||
}
|
||||
@ -178,7 +178,7 @@ pub struct VeilidVersion {
|
||||
pub patch: u32,
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_namespace = veilid)]
|
||||
#[wasm_bindgen()]
|
||||
pub fn veilid_version() -> JsValue {
|
||||
let (major, minor, patch) = veilid_core::veilid_version();
|
||||
let vv = VeilidVersion {
|
||||
|
Loading…
Reference in New Issue
Block a user