import { ObservableInput , Subject , from , map , share } from "rxjs" ; import { configuration , feature , translation } from "~/_" ; import { WorkerHandler , watchWorker } from "~/browser" ; import { SearchIndex } from "../../_" ; import { SearchOptions , SearchPipeline } from "../../options" ; import { SearchMessage , SearchMessageType , SearchSetupMessage , isSearchResultMessage } from "../message" ; /**
 * Search worker
 */ export type SearchWorker = WorkerHandler < SearchMessage > ; param data returns function setupSearchIndex ( { config , docs } ) { if ( config . lang . length === 1 && config . lang [ 0 ] === "en" ) config . lang = [ translation ( "search.config.lang" ) ] ; if ( config . separator === "[\\s\\-]+" ) config . separator = translation ( "search.config.separator" ) ; const pipeline = translation ( "search.config.pipeline" ) . split ( /\s*,\s*/ ) . filter ( Boolean ) as SearchPipeline ; const options = { pipeline , suggestions : feature ( "search.suggest" ) } ; return { config , docs , options } ; } param url param index returns export function setupSearchWorker ( url , index ) { const config = configuration ( ) ; const worker = new Worker ( url ) ; const tx$ = new Subject < SearchMessage > ( ) ; const rx$ = watchWorker ( worker , { tx$ } ) . pipe ( map ( message => { if ( isSearchResultMessage ( message ) ) { for ( const result of message . data . items ) for ( const document of result ) document . location = `${ new URL ( document . location , config . base ) }` ; } return message ; } ) , share ( ) ) ; from ( index ) . pipe ( map ( data => ( { type : SearchMessageType . SETUP , data : setupSearchIndex ( data ) } as SearchSetupMessage ) ) ) . subscribe ( tx$ . next . bind ( tx$ ) ) ; return { tx$ , rx$ } ; }