11import { setContext , getContext } from "svelte" ;
22
3- import SearchWorker from "$lib/workers/search.worker?worker" ;
3+ import { SearchClient } from "$lib/workers/client" ;
4+ import { MetricsStore } from "$lib/stores/metrics.svelte" ;
5+ import { getNextSortState } from "$lib/utilities/sort" ;
46
57import type { Store } from "$lib/types/store" ;
68import type { CatalogType } from "$lib/types/catalog" ;
79import type { Status } from "$lib/types/status" ;
810import { SortStateSchema , type SortState } from "$lib/types/table" ;
9- import type { WorkerToMainMessage } from "$lib/types/worker" ;
1011
1112export class CatalogStore {
1213 status = $state < Status > ( "pending" ) ;
@@ -15,45 +16,40 @@ export class CatalogStore {
1516 results = $state < Store [ ] > ( [ ] ) ;
1617 offset = $state ( 0 ) ;
1718
19+ metrics = new MetricsStore ( ) ;
20+
1821 _query = $state ( "" ) ;
1922 _type = $state < CatalogType > ( "stores" ) ;
2023 _sort = $state < SortState > ( SortStateSchema . parse ( { } ) ) ;
2124
22- worker : Worker | undefined ;
23-
24- private requestId = 0 ;
25- private ready = false ;
25+ client : SearchClient | undefined ;
2626
2727 constructor ( ) {
2828 if ( typeof window !== "undefined" ) {
29- this . worker = new SearchWorker ( ) ;
30-
31- this . worker . onmessage = ( e : MessageEvent < WorkerToMainMessage > ) => {
32- const message = e . data ;
33-
34- if ( message . type === "ready" ) {
35- this . ready = true ;
36- this . count = message . count ;
37- this . total = message . count ;
29+ this . client = new SearchClient ( {
30+ onReady : ( count ) => {
31+ this . count = count ;
32+ this . total = count ;
3833 this . search ( this . query , 0 , 40 ) ;
39- } else if ( message . type === "results" ) {
40- if ( message . id === this . requestId && message . id !== undefined ) {
41- this . results = message . results ;
42- this . count = message . total ;
43- this . offset = message . offset ;
44- this . status = "success" ;
34+ } ,
35+ onResults : ( results , total , offset ) => {
36+ this . results = results ;
37+ this . count = total ;
38+ this . offset = offset ;
39+ this . status = "success" ;
40+ if ( this . offset === 0 ) {
41+ this . metrics . stop ( ) ;
4542 }
46- } else if ( message . type === "error" ) {
43+ } ,
44+ onError : ( error ) => {
4745 this . status = "error" ;
48- console . error ( "Catalog error:" , message . error ) ;
46+ console . error ( "Catalog error:" , error ) ;
4947 }
50- } ;
51-
52- this . worker . onerror = ( ) => {
53- this . status = "error" ;
54- } ;
48+ } ) ;
5549
56- this . load ( ) ;
50+ this . metrics . start ( ) ;
51+
52+ this . init ( ) ;
5753 }
5854 }
5955
@@ -63,6 +59,7 @@ export class CatalogStore {
6359
6460 set query ( value : string ) {
6561 this . _query = value ;
62+ this . metrics . start ( ) ;
6663 this . search ( value , 0 , 40 ) ;
6764 }
6865
@@ -72,60 +69,48 @@ export class CatalogStore {
7269
7370 set type ( value : CatalogType ) {
7471 this . _type = value ;
75- this . status = "pending" ;
76- this . results = [ ] ;
77- this . count = 0 ;
78- this . total = 0 ;
79- this . _query = "" ;
80- this . _sort = SortStateSchema . parse ( { } ) ;
81- this . ready = false ;
82- this . load ( ) ;
72+
73+ this . reset ( ) ;
74+
75+ this . metrics . restart ( ) ;
76+
77+ this . init ( ) ;
8378 }
8479
8580 get sort ( ) {
8681 return this . _sort ;
8782 }
8883
8984 toggleSort ( key : string ) {
90- if ( this . _sort . key === key ) {
91- if ( this . _sort . direction === "asc" ) {
92- this . _sort = { key, direction : "desc" } ;
93- } else {
94- this . _sort = SortStateSchema . parse ( { } ) ;
95- }
96- } else {
97- this . _sort = { key, direction : "asc" } ;
98- }
99-
85+ this . _sort = getNextSortState ( this . _sort , key ) ;
86+ this . metrics . start ( ) ;
10087 this . search ( this . query , 0 , 40 ) ;
10188 }
10289
103- load ( ) {
104- if ( this . worker ) {
105- this . worker . postMessage ( { type : "load" , payload : `/${ this . type } .json` } ) ;
106- }
90+ init ( ) {
91+ this . client ?. load ( `/${ this . type } .json` ) ;
10792 }
10893
10994 search ( q : string , offset : number = 0 , limit : number = 20 ) {
110- if ( this . ready && this . worker ) {
111- this . requestId ++ ;
112- this . worker . postMessage ( {
113- type : "search" ,
114- payload : q ,
115- offset,
116- limit,
117- sort : this . _sort . key ? { ...this . _sort } : undefined ,
118- id : this . requestId
119- } ) ;
120- }
95+ this . client ?. search ( q , offset , limit , this . _sort ) ;
12196 }
12297
123- loadRange ( offset : number , limit : number ) {
98+ load ( offset : number , limit : number ) {
12499 this . search ( this . query , offset , limit ) ;
125100 }
126101
102+ reset ( ) {
103+ this . status = "pending" ;
104+ this . results = [ ] ;
105+ this . count = 0 ;
106+ this . total = 0 ;
107+
108+ this . _query = "" ;
109+ this . _sort = SortStateSchema . parse ( { } ) ;
110+ }
111+
127112 destroy ( ) {
128- this . worker ?. terminate ( ) ;
113+ this . client ?. terminate ( ) ;
129114 }
130115}
131116
0 commit comments