chooseLocalOrCachedLibraryPath function

  1. @visibleForTesting
String? chooseLocalOrCachedLibraryPath({
  1. required String? localPath,
  2. required String? cachedPath,
  3. bool preferLocal = false,
  4. DateTime modifiedAt(
    1. String path
    )?,
})

Picks between local and cached paths using the same policy as lib/src/native_assets/native_library_resolver.dart chooseLocalOrCached.

Implementation

@visibleForTesting
String? chooseLocalOrCachedLibraryPath({
  required String? localPath,
  required String? cachedPath,
  bool preferLocal = false,
  DateTime Function(String path)? modifiedAt,
}) {
  DateTime mtime(String path) {
    if (modifiedAt != null) {
      return modifiedAt(path);
    }
    return File(path).lastModifiedSync();
  }

  if (preferLocal && localPath != null) {
    return localPath;
  }

  if (localPath != null && cachedPath != null) {
    final localTime = mtime(localPath);
    final cachedTime = mtime(cachedPath);
    if (!localTime.isBefore(cachedTime)) {
      return localPath;
    }
    return cachedPath;
  }

  return cachedPath ?? localPath;
}