veilidchat/lib/veilid_init.dart

79 lines
2.2 KiB
Dart
Raw Normal View History

2023-07-22 23:29:10 -04:00
import 'dart:async';
2023-07-26 14:20:29 -04:00
import 'package:flutter/foundation.dart';
2023-09-26 18:46:02 -04:00
import 'package:riverpod_annotation/riverpod_annotation.dart';
2023-07-26 14:20:29 -04:00
2023-01-09 22:50:34 -05:00
import 'processor.dart';
2023-09-26 18:46:02 -04:00
import 'veilid_support/veilid_support.dart';
part 'veilid_init.g.dart';
2023-01-09 22:50:34 -05:00
Future<String> getVeilidVersion() async {
String veilidVersion;
try {
veilidVersion = Veilid.instance.veilidVersionString();
} on Exception {
veilidVersion = 'Failed to get veilid version.';
}
return veilidVersion;
}
// Initialize Veilid
// Call only once.
2023-01-10 21:04:18 -05:00
void _initVeilid() {
2023-01-09 22:50:34 -05:00
if (kIsWeb) {
2023-07-26 14:20:29 -04:00
const platformConfig = VeilidWASMConfig(
2023-01-09 22:50:34 -05:00
logging: VeilidWASMConfigLogging(
performance: VeilidWASMConfigLoggingPerformance(
enabled: true,
level: VeilidConfigLogLevel.debug,
logsInTimings: true,
logsInConsole: false),
api: VeilidWASMConfigLoggingApi(
enabled: true, level: VeilidConfigLogLevel.info)));
2023-07-02 11:31:40 -04:00
Veilid.instance.initializeVeilidCore(platformConfig.toJson());
2023-01-09 22:50:34 -05:00
} else {
2023-07-26 14:20:29 -04:00
const platformConfig = VeilidFFIConfig(
2023-01-09 22:50:34 -05:00
logging: VeilidFFIConfigLogging(
terminal: VeilidFFIConfigLoggingTerminal(
enabled: false,
level: VeilidConfigLogLevel.debug,
),
otlp: VeilidFFIConfigLoggingOtlp(
enabled: false,
level: VeilidConfigLogLevel.trace,
2023-09-26 18:46:02 -04:00
grpcEndpoint: '127.0.0.1:4317',
2023-07-26 14:20:29 -04:00
serviceName: 'VeilidChat'),
2023-01-09 22:50:34 -05:00
api: VeilidFFIConfigLoggingApi(
enabled: true, level: VeilidConfigLogLevel.info)));
2023-07-02 11:31:40 -04:00
Veilid.instance.initializeVeilidCore(platformConfig.toJson());
2023-01-09 22:50:34 -05:00
}
}
2023-07-22 23:29:10 -04:00
Completer<Veilid> eventualVeilid = Completer<Veilid>();
2023-01-09 22:50:34 -05:00
Processor processor = Processor();
Future<void> initializeVeilid() async {
2023-07-22 23:29:10 -04:00
// Ensure this runs only once
if (eventualVeilid.isCompleted) {
2023-01-09 22:50:34 -05:00
return;
}
// Init Veilid
2023-01-10 21:04:18 -05:00
_initVeilid();
2023-01-09 22:50:34 -05:00
2023-01-11 19:17:04 -05:00
// Veilid logging
initVeilidLog();
2023-01-09 22:50:34 -05:00
// Startup Veilid
await processor.startup();
2023-07-22 23:29:10 -04:00
// Share the initialized veilid instance to the rest of the app
eventualVeilid.complete(Veilid.instance);
}
2023-09-26 18:46:02 -04:00
// Expose the Veilid instance as a FutureProvider
@riverpod
FutureOr<Veilid> veilidInstance(VeilidInstanceRef ref) async =>
await eventualVeilid.future;