Skip to content

Commit 7836a17

Browse files
committed
Move method outside of the SearchBackend.
1 parent 26e2bce commit 7836a17

File tree

5 files changed

+37
-40
lines changed

5 files changed

+37
-40
lines changed

app/lib/search/backend.dart

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -427,38 +427,6 @@ class SearchBackend {
427427
}
428428
}
429429

430-
/// Downloads the remote SDK content from [uri] and creates a cached file in the
431-
/// `.dart_tool/pub-search-data/` directory.
432-
///
433-
/// When a local file in `app/.dart_tool/pub-search-data/<reduced-uri>` exists,
434-
/// its content will be loaded instead. URL reduction replaces slashes and other
435-
/// non-characters with a single dash `-`, like:
436-
/// - `https-api.dart.dev-stable-latest-index.json`
437-
Future<String> loadOrFetchSdkIndexJsonAsString(
438-
Uri uri, {
439-
@visibleForTesting Duration? ttl,
440-
}) async {
441-
final fileName = uri.toString().replaceAll(RegExp(r'[^a-z0-9\.]+'), '-');
442-
final file = File(p.join('.dart_tool', 'pub-search-data', fileName));
443-
if (await file.exists()) {
444-
var canUseCached = true;
445-
if (ttl != null) {
446-
final age = clock.now().difference(await file.lastModified());
447-
if (age > ttl) {
448-
canUseCached = false;
449-
}
450-
}
451-
if (canUseCached) {
452-
return await file.readAsString();
453-
}
454-
}
455-
456-
final content = await httpGetWithRetry(uri, responseFn: (rs) => rs.body);
457-
await file.parent.create(recursive: true);
458-
await file.writeAsString(content);
459-
return content;
460-
}
461-
462430
Future<List<PackageDocument>?> fetchSnapshotDocuments() async {
463431
try {
464432
final map = await _snapshotStorage.getContentAsJsonMap();
@@ -609,6 +577,38 @@ List<ApiDocPage> apiDocPagesFromPubData(PubDartdocData pubData) {
609577
return results;
610578
}
611579

580+
/// Downloads the remote SDK content from [uri] and creates a cached file in the
581+
/// `.dart_tool/pub-search-data/` directory.
582+
///
583+
/// When a local file in `app/.dart_tool/pub-search-data/<reduced-uri>` exists,
584+
/// its content will be loaded instead. URL reduction replaces slashes and other
585+
/// non-characters with a single dash `-`, like:
586+
/// - `https-api.dart.dev-stable-latest-index.json`
587+
Future<String> loadOrFetchSdkIndexJsonAsString(
588+
Uri uri, {
589+
@visibleForTesting Duration? ttl,
590+
}) async {
591+
final fileName = uri.toString().replaceAll(RegExp(r'[^a-z0-9\.]+'), '-');
592+
final file = File(p.join('.dart_tool', 'pub-search-data', fileName));
593+
if (await file.exists()) {
594+
var canUseCached = true;
595+
if (ttl != null) {
596+
final age = clock.now().difference(await file.lastModified());
597+
if (age > ttl) {
598+
canUseCached = false;
599+
}
600+
}
601+
if (canUseCached) {
602+
return await file.readAsString();
603+
}
604+
}
605+
606+
final content = await httpGetWithRetry(uri, responseFn: (rs) => rs.body);
607+
await file.parent.create(recursive: true);
608+
await file.writeAsString(content);
609+
return content;
610+
}
611+
612612
class _CombinedSearchIndex implements SearchIndex {
613613
const _CombinedSearchIndex();
614614

app/lib/search/dart_sdk_mem_index.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ SdkMemIndex? get dartSdkMemIndex =>
3838
Future<SdkMemIndex?> createDartSdkMemIndex() async {
3939
try {
4040
final index = await SdkMemIndex.dart();
41-
final content =
42-
await searchBackend.loadOrFetchSdkIndexJsonAsString(index.indexJsonUri);
41+
final content = await loadOrFetchSdkIndexJsonAsString(index.indexJsonUri);
4342
await index.addDartdocIndex(DartdocIndex.parseJsonText(content));
4443
index.updateWeights(
4544
libraryWeights: dartSdkLibraryWeights,

app/lib/search/flutter_sdk_mem_index.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ SdkMemIndex? get flutterSdkMemIndex =>
5555
Future<SdkMemIndex?> createFlutterSdkMemIndex() async {
5656
try {
5757
final index = SdkMemIndex.flutter();
58-
final content =
59-
await searchBackend.loadOrFetchSdkIndexJsonAsString(index.indexJsonUri);
58+
final content = await loadOrFetchSdkIndexJsonAsString(index.indexJsonUri);
6059
await index.addDartdocIndex(DartdocIndex.parseJsonText(content),
6160
allowedLibraries: _allowedLibraries);
6261
index.updateWeights(

app/test/search/backend_test.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ void main() {
1414
group('search backend', () {
1515
testWithProfile('fetch SDK library description', fn: () async {
1616
final index = await SdkMemIndex.dart();
17-
final content = await searchBackend
18-
.loadOrFetchSdkIndexJsonAsString(index.indexJsonUri);
17+
final content = await loadOrFetchSdkIndexJsonAsString(index.indexJsonUri);
1918
await index.addDartdocIndex(DartdocIndex.parseJsonText(content));
2019
expect(
2120
index.getLibraryDescription('dart:async'),

app/test/search/dartdoc_index_parsing_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import '../shared/test_services.dart';
1313
void main() {
1414
group('dartdoc index.json parsing', () {
1515
testWithProfile('parse Dart SDK index.json', fn: () async {
16-
final textContent = await searchBackend.loadOrFetchSdkIndexJsonAsString(
16+
final textContent = await loadOrFetchSdkIndexJsonAsString(
1717
Uri.parse('https://api.dart.dev/stable/latest/index.json'),
1818
ttl: Duration(days: 1),
1919
);
@@ -35,7 +35,7 @@ void main() {
3535
});
3636

3737
testWithProfile('parse Flutter SDK index.json', fn: () async {
38-
final textContent = await searchBackend.loadOrFetchSdkIndexJsonAsString(
38+
final textContent = await loadOrFetchSdkIndexJsonAsString(
3939
Uri.parse('https://api.flutter.dev/flutter/index.json'),
4040
ttl: Duration(days: 1),
4141
);

0 commit comments

Comments
 (0)