safeAddScript static method

Future<bool>? safeAddScript(
  1. String name,
  2. String buffer, {
  3. String? contextCheck,
})

Implementation

static Future<bool>? safeAddScript(String name, String buffer,
    {String? contextCheck}) async {
  contextCheck ??= name;
  assert(buffer.isNotEmpty == true);

  if (hasContext(contextCheck)) return true;

  if (!_scriptFetched.containsKey(name)) {
    final c = Completer<bool>();

    print('AddingScript $name');
    _scriptFetched[name] = c.future;
    html.document.body!.children.add(script(buffer));

    Timer.periodic(Duration(milliseconds: 300), (t) {
      if (hasContext(contextCheck!)) {
        t.cancel();
        c.complete(true);
      }
    });
  }

  return _scriptFetched[name]!;
}