mangayomi-mirror/lib/eval/lib.dart
NBA2K1 8b1f7fc581 Add service disposal and lifecycle cleanup
This commit improves memory management, reduces redundant interpreter
instantiation, and standardizes service usage patterns.

- Add `dispose()` to `ExtensionService` interface and implement it across
  Dart, JS, LNReader, and Mihon services.
- Replace repeated interpreter creation in `DartExtensionService` with a
  persistent `_interpreter` instance initialized once in the constructor.
- Add disposal logic for JS DOM selector and Cheerio instances to prevent
  memory leaks.
- Introduce `withExtensionService()` helper to ensure services are always
  disposed after use.
- Update call sites across the codebase to use `withExtensionService()`
  or manual try/finally disposal.
- Improve isolate service message handling by extracting `responsePort`
  earlier.
- Ensure safer defaults (e.g., returning empty lists, const lists) when
  service calls fail.
2026-01-13 01:11:19 +01:00

29 lines
917 B
Dart

import 'package:mangayomi/eval/interface.dart';
import 'package:mangayomi/models/source.dart';
import 'dart/service.dart';
import 'javascript/service.dart';
import 'mihon/service.dart';
import 'lnreader/service.dart';
ExtensionService getExtensionService(Source source, String androidProxyServer) {
return switch (source.sourceCodeLanguage) {
SourceCodeLanguage.dart => DartExtensionService(source),
SourceCodeLanguage.javascript => JsExtensionService(source),
SourceCodeLanguage.mihon => MihonExtensionService(source, androidProxyServer),
SourceCodeLanguage.lnreader => LNReaderExtensionService(source),
};
}
Future<T> withExtensionService<T>(
Source source,
String proxyServer,
Future<T> Function(ExtensionService service) action,
) async {
final service = getExtensionService(source, proxyServer);
try {
return await action(service);
} finally {
service.dispose();
}
}