Skip to content

Commit

Permalink
⚡ dispose of AlgoliaSearch object after search completed
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse committed Dec 17, 2023
1 parent abd7cf7 commit 9618f5c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 44 deletions.
92 changes: 49 additions & 43 deletions bin/main_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,52 +29,58 @@ void _showPlaceholder() {
}

Future<void> _performSearch(String query, {String? version}) async {
final SearchResponse searchResponse = await AlgoliaSearch.query(
query,
version: version,
);
try {
final SearchResponse searchResponse = await AlgoliaSearch.query(
query,
version: version,
);

if (searchResponse.nbHits > 0) {
final AlfredItems items = AlfredItems(
searchResponse.hits
.map((Hit hit) => SearchResult.fromJson(
<String, dynamic>{...hit, 'objectID': hit.objectID}))
.map(
(SearchResult result) => AlfredItem(
uid: result.objectID,
title: result.prettyTitle,
subtitle: result.content.isNotEmpty
? result.content.truncate(75)
: result.id,
arg: result.permalink,
text: AlfredItemText(
largeType: result.id,
copy: result.id,
if (searchResponse.nbHits > 0) {
final AlfredItems items = AlfredItems(
searchResponse.hits
.map((Hit hit) =>
SearchResult.fromJson(
<String, dynamic>{...hit, 'objectID': hit.objectID}))
.map(
(SearchResult result) =>
AlfredItem(
uid: result.objectID,
title: result.prettyTitle,
subtitle: result.content.isNotEmpty
? result.content.truncate(75)
: result.id,
arg: result.permalink,
text: AlfredItemText(
largeType: result.id,
copy: result.id,
),
quickLookUrl: result.permalink,
icon: AlfredItemIcon(path: 'icon.png'),
valid: true,
),
quickLookUrl: result.permalink,
icon: AlfredItemIcon(path: 'icon.png'),
valid: true,
),
)
.toList(),
);
_workflow.addItems(items.items);
} else {
final Uri url =
Uri.https('www.google.com', '/search', {'q': 'Django $query'});
)
.toList(),
);
_workflow.addItems(items.items);
} else {
final Uri url =
Uri.https('www.google.com', '/search', {'q': 'Django $query'});

_workflow.addItem(
AlfredItem(
title: 'No matching answers found',
subtitle: 'Shall I try and search Google?',
arg: url.toString(),
text: AlfredItemText(
copy: url.toString(),
_workflow.addItem(
AlfredItem(
title: 'No matching answers found',
subtitle: 'Shall I try and search Google?',
arg: url.toString(),
text: AlfredItemText(
copy: url.toString(),
),
quickLookUrl: url.toString(),
icon: AlfredItemIcon(path: 'google.png'),
valid: true,
),
quickLookUrl: url.toString(),
icon: AlfredItemIcon(path: 'google.png'),
valid: true,
),
);
);
}
} finally {
AlgoliaSearch.dispose();
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: Search the Django documentation using Alfred
# Prevent accidental publishing to pub.dev.
publish_to: 'none'

version: 2.1.10
version: 2.1.11

environment:
sdk: '>=3.2.0 <4.0.0'
Expand Down

0 comments on commit 9618f5c

Please sign in to comment.