adopt method

Future<void> adopt(
  1. EmbeddingModel model,
  2. ActiveEmbedderParams params
)

Records model, or closes it when the cache cannot take custody.

The shell that built model owns it until this returns: a third-party model may throw from addCloseListener, which is abstract on the interface, and dropping it then would leave a live worker nobody can reach. One place, not three — the shells each had a copy, and each copy let a throwing close() replace the error that explains the failure.

Implementation

Future<void> adopt(EmbeddingModel model, ActiveEmbedderParams params) async {
  try {
    record(model, params);
  } catch (error, stack) {
    try {
      await model.close();
    } catch (closeError, closeStack) {
      // `print`, as in [reuseOrInvalidate]: a leak is debugged in release.
      // ignore: avoid_print
      print(
        '[flutter_gemma] WARNING: an embedder the cache could not take '
        'also failed to close; its worker and native model may be leaked: '
        '$closeError\n$closeStack',
      );
    }
    Error.throwWithStackTrace(error, stack);
  }
}