veilid/veilid-flutter/lib/veilid_js.dart

86 lines
2.5 KiB
Dart
Raw Normal View History

2022-02-06 20:18:42 -06:00
import 'veilid.dart';
2022-03-17 09:31:10 -05:00
import 'dart:html' as html;
import 'dart:js' as js;
import 'dart:js_util' as js_util;
2022-02-06 20:18:42 -06:00
import 'dart:async';
2022-03-15 22:02:24 -05:00
import 'dart:convert';
2022-02-06 20:18:42 -06:00
//////////////////////////////////////////////////////////
Veilid getVeilid() => VeilidJS();
2022-03-17 09:31:10 -05:00
Object wasm = js_util.getProperty(html.window, "veilid_wasm");
2022-03-15 22:02:24 -05:00
2022-03-17 09:31:10 -05:00
Future<T> _wrapApiPromise<T>(Object p) {
return js_util.promiseToFuture(p).then((value) => value as T).catchError(
(error) => Future<T>.error(
VeilidAPIException.fromJson(jsonDecode(error as String))));
2022-03-15 22:02:24 -05:00
}
2022-02-13 20:09:43 -06:00
class VeilidJS implements Veilid {
2022-06-15 20:51:38 -05:00
@override
void configureVeilidPlatform(Map<String, dynamic> platformConfigJson) {
var platformConfigJsonString =
jsonEncode(platformConfigJson, toEncodable: veilidApiToEncodable);
js_util.callMethod(
wasm, "configure_veilid_platform", [platformConfigJsonString]);
}
2022-03-03 19:45:39 -06:00
@override
2022-03-15 22:02:24 -05:00
Stream<VeilidUpdate> startupVeilidCore(VeilidConfig config) async* {
var streamController = StreamController<VeilidUpdate>();
2022-03-17 09:31:10 -05:00
updateCallback(String update) {
var updateJson = jsonDecode(update);
if (updateJson["kind"] == "Shutdown") {
streamController.close();
} else {
var update = VeilidUpdate.fromJson(updateJson);
streamController.add(update);
}
}
await _wrapApiPromise(js_util.callMethod(wasm, "startup_veilid_core", [
js.allowInterop(updateCallback),
jsonEncode(config.json, toEncodable: veilidApiToEncodable)
]));
2022-03-15 22:02:24 -05:00
yield* streamController.stream;
2022-02-06 20:18:42 -06:00
}
2022-03-03 19:45:39 -06:00
@override
2022-03-15 22:02:24 -05:00
Future<VeilidState> getVeilidState() async {
2022-03-17 09:31:10 -05:00
return VeilidState.fromJson(jsonDecode(await _wrapApiPromise(
js_util.callMethod(wasm, "get_veilid_state", []))));
2022-02-06 20:18:42 -06:00
}
2022-03-03 19:45:39 -06:00
@override
2022-06-07 20:31:05 -05:00
Future<void> changeApiLogLevel(VeilidConfigLogLevel logLevel) {
return _wrapApiPromise(js_util.callMethod(wasm, "change_api_log_level",
2022-03-17 09:31:10 -05:00
[jsonEncode(logLevel.json, toEncodable: veilidApiToEncodable)]));
2022-02-06 20:18:42 -06:00
}
2022-02-09 08:47:36 -06:00
2022-03-03 19:45:39 -06:00
@override
2022-03-17 09:31:10 -05:00
Future<void> shutdownVeilidCore() {
return _wrapApiPromise(
js_util.callMethod(wasm, "shutdown_veilid_core", []));
2022-02-06 20:18:42 -06:00
}
2022-03-03 19:45:39 -06:00
@override
2022-03-17 09:31:10 -05:00
Future<String> debug(String command) {
return _wrapApiPromise(js_util.callMethod(wasm, "debug", [command]));
2022-03-03 19:45:39 -06:00
}
@override
2022-03-17 09:31:10 -05:00
String veilidVersionString() {
return js_util.callMethod(wasm, "veilid_version_string", []);
2022-02-06 20:18:42 -06:00
}
2022-03-03 19:45:39 -06:00
@override
2022-03-17 09:31:10 -05:00
VeilidVersion veilidVersion() {
var jsonVersion =
jsonDecode(js_util.callMethod(wasm, "veilid_version", []));
2022-03-15 22:02:24 -05:00
return VeilidVersion(
jsonVersion["major"], jsonVersion["minor"], jsonVersion["patch"]);
2022-02-06 20:18:42 -06:00
}
}