injectJSLibraries method

Future<void> injectJSLibraries()

Implementation

Future<void> injectJSLibraries() async {
  if (kIsWeb) {
    final List<Future<void>> loading = <Future<void>>[];
    final List<html.HtmlElement> tags = <html.HtmlElement>[];
    final enxJsUrl = assetManager
        .getAssetUrl('packages/enx_flutter_plugin/assets/EnxRtc.js');
    final html.ScriptElement enxScript = html.ScriptElement()
      ..async = true
      ..defer = true
      ..src = enxJsUrl;
    loading.add(enxScript.onLoad.first);
    tags.add(enxScript);
    html.querySelector('head')!.children.addAll(tags);

    // ignore: undefined_prefixed_name
    final jsUrl1 = assetManager.getAssetUrl(
        'packages/enx_flutter_plugin/assets/enx_flutter_plugin.js');
    final html.ScriptElement script = html.ScriptElement()
      ..async = true
      ..defer = true
      ..src = jsUrl1;
    loading.add(script.onLoad.first);
    tags.add(script);

    html.querySelector('head')!.children.addAll(tags);
    await Future.wait(loading);

    final completer = Completer<void>();
    script.addEventListener('load', (event) => {completer.complete()});
    return completer.future;
  }
}