@@ -13,11 +13,16 @@ export const packageSearch = defineComponent(() => ({
13
13
query : "" ,
14
14
cursor : 0 ,
15
15
results : [ ] as NpmPackage [ ] ,
16
+ controller : new AbortController ( ) ,
16
17
init ( ) {
17
18
this . dialog = findDialog ( ) ;
18
19
this . resultsList = findResultsList ( ) ;
19
20
this . $watch ( "query" , async ( ) => {
20
- this . results = await searchPackages ( this . query ) ;
21
+ this . controller . abort ( ) ;
22
+ this . controller = new AbortController ( ) ;
23
+ try {
24
+ this . results = await searchPackages ( this . query , this . controller . signal ) ;
25
+ } catch { }
21
26
this . cursor = 0 ;
22
27
} ) ;
23
28
this . $watch ( "cursor" , ( ) => {
@@ -52,8 +57,11 @@ const findResultsList = (): HTMLUListElement | undefined => {
52
57
return document . querySelector < HTMLUListElement > ( "#package-search-results" ) ?? undefined ;
53
58
} ;
54
59
55
- const searchPackages = async ( text : string ) : Promise < NpmPackage [ ] > => {
56
- const res = await fetch ( `https://registry.npmjs.org/-/v1/search?text=${ text } ` ) ;
60
+ const searchPackages = async ( query : string , signal : AbortSignal ) : Promise < NpmPackage [ ] > => {
61
+ if ( query . length < 2 ) {
62
+ return [ ] ;
63
+ }
64
+ const res = await fetch ( `https://registry.npmjs.org/-/v1/search?text=${ query } ` , { signal } ) ;
57
65
const json = ( await res . json ( ) ) as {
58
66
objects : { package : { name : string ; description ?: string } } [ ] ;
59
67
} ;
0 commit comments