ensureReady method

  1. @override
Future<LiteRtLmWebRuntimeState> ensureReady()
override

Waits for the page's loader (window.litertLmReady) when there is one, then reports whether window.EmbeddingEngine exists. Throws when the loader itself failed (the module could not be imported).

Implementation

@override
Future<LiteRtLmWebRuntimeState> ensureReady() async {
  // Said before the load: a runtime of another version than the one this
  // release is measured on behaves otherwise, without a missing member.
  noteOtherWebRuntimeVersion();
  if (_hasEmbeddingEngine) return _versionState;
  final ready = globalContext['litertLmReady'];
  if (ready == null || ready.isUndefinedOrNull) {
    return LiteRtLmWebRuntimeState.loaderMissing;
  }
  // Module scripts are deferred: the shim may still be importing the
  // module. A rejection (the CDN import failed) propagates.
  if (ready.typeofEquals('object') && (ready as JSObject).has('then')) {
    await (ready as JSPromise<JSAny?>).toDart;
    // The module the shim imported is fetched by now.
    noteOtherWebRuntimeVersion();
  }
  return _hasEmbeddingEngine
      ? _versionState
      : LiteRtLmWebRuntimeState.embeddingEngineMissing;
}