2022-02-07 02:18:42 +00:00
|
|
|
import 'veilid.dart';
|
|
|
|
|
|
|
|
import 'dart:js';
|
2022-03-16 03:02:24 +00:00
|
|
|
import 'dart:js_util';
|
2022-02-07 02:18:42 +00:00
|
|
|
import 'dart:async';
|
2022-03-16 03:02:24 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'package:flutter/services.dart' show NetworkAssetBundle;
|
|
|
|
import 'package:wasm_interop/wasm_interop.dart';
|
|
|
|
import 'package:mutex/mutex.dart';
|
2022-02-07 02:18:42 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
Veilid getVeilid() => VeilidJS();
|
|
|
|
|
2022-03-16 03:02:24 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-02-14 02:09:43 +00:00
|
|
|
class VeilidJS implements Veilid {
|
2022-03-04 01:45:39 +00:00
|
|
|
@override
|
2022-03-16 03:02:24 +00:00
|
|
|
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;
|
2022-02-07 02:18:42 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 01:45:39 +00:00
|
|
|
@override
|
2022-03-16 03:02:24 +00:00
|
|
|
Future<VeilidState> getVeilidState() async {
|
|
|
|
var wasm = (await getWasmInstance());
|
|
|
|
return VeilidState.fromJson(jsonDecode(
|
|
|
|
await promiseToFuture(wasm.functions["get_veilid_state"]!.call())));
|
2022-02-07 02:18:42 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 01:45:39 +00:00
|
|
|
@override
|
2022-03-16 03:02:24 +00:00
|
|
|
Future<void> changeLogLevel(VeilidConfigLogLevel logLevel) async {
|
|
|
|
var wasm = (await getWasmInstance());
|
|
|
|
await promiseToFuture(wasm.functions["change_log_level"]!
|
|
|
|
.call(jsonEncode(logLevel.json, toEncodable: veilidApiToEncodable)));
|
2022-02-07 02:18:42 +00:00
|
|
|
}
|
2022-02-09 14:47:36 +00:00
|
|
|
|
2022-03-04 01:45:39 +00:00
|
|
|
@override
|
2022-03-16 03:02:24 +00:00
|
|
|
Future<void> shutdownVeilidCore() async {
|
|
|
|
var wasm = (await getWasmInstance());
|
|
|
|
await promiseToFuture(wasm.functions["shutdown_veilid_core"]!.call());
|
2022-02-07 02:18:42 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 01:45:39 +00:00
|
|
|
@override
|
2022-03-16 03:02:24 +00:00
|
|
|
Future<String> debug(String command) async {
|
|
|
|
var wasm = (await getWasmInstance());
|
|
|
|
return await promiseToFuture(wasm.functions["debug"]!.call(command));
|
2022-03-04 01:45:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2022-03-16 03:02:24 +00:00
|
|
|
Future<String> veilidVersionString() async {
|
|
|
|
var wasm = (await getWasmInstance());
|
|
|
|
return await promiseToFuture(wasm.functions["debug"]!.call());
|
2022-02-07 02:18:42 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 01:45:39 +00:00
|
|
|
@override
|
2022-03-16 03:02:24 +00:00
|
|
|
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"]);
|
2022-02-07 02:18:42 +00:00
|
|
|
}
|
|
|
|
}
|