Improved search implementation to allow for alternative index sources

This commit is contained in:
squidfunk
2020-02-17 11:36:29 +01:00
parent 95c66b99e3
commit fdffefd536
2 changed files with 29 additions and 14 deletions

View File

@@ -20,7 +20,7 @@
* IN THE SOFTWARE. * IN THE SOFTWARE.
*/ */
import { Observable, Subject, fromEvent } from "rxjs" import { Observable, Subject, fromEvent, fromEventPattern } from "rxjs"
import { import {
pluck, pluck,
share, share,
@@ -87,8 +87,10 @@ export function watchWorker<T extends WorkerMessage>(
worker: Worker, { tx$ }: WatchOptions<T> worker: Worker, { tx$ }: WatchOptions<T>
): Observable<T> { ): Observable<T> {
/* Intercept messages from web worker */ /* Intercept messages from worker-like objects */
const rx$ = fromEvent(worker, "message") const rx$ = fromEventPattern<Event>(next =>
worker.addEventListener("message", next)
)
.pipe( .pipe(
pluck<Event, T>("data") pluck<Event, T>("data")
) )

View File

@@ -20,7 +20,7 @@
* IN THE SOFTWARE. * IN THE SOFTWARE.
*/ */
import { Subject } from "rxjs" import { Subject, from } from "rxjs"
import { ajax } from "rxjs/ajax" import { ajax } from "rxjs/ajax"
import { map, pluck } from "rxjs/operators" import { map, pluck } from "rxjs/operators"
@@ -43,6 +43,7 @@ import {
*/ */
interface SetupOptions { interface SetupOptions {
base: string /* Base url */ base: string /* Base url */
index?: Promise<SearchIndexOptions> /* Promise resolving with index */
} }
/* ---------------------------------------------------------------------------- /* ----------------------------------------------------------------------------
@@ -68,13 +69,18 @@ function resolve(base: URL | string, ...paths: string[]) {
/** /**
* Setup search web worker * Setup search web worker
* *
* This function will create a web worker to setup and query the search index
* which is done using `lunr`. The index can be passed explicitly in order to
* enable hacks like _localsearch_ via search index embedding as JSON. If no
* index is given, this function will load it from the default location.
*
* @param url - Worker url * @param url - Worker url
* @param options - Options * @param options - Options
* *
* @return Worker handler * @return Worker handler
*/ */
export function setupSearchWorker( export function setupSearchWorker(
url: string, { base }: SetupOptions url: string, { base, index }: SetupOptions
): WorkerHandler<SearchMessage> { ): WorkerHandler<SearchMessage> {
const worker = new Worker(url) const worker = new Worker(url)
const prefix = new URL(base, location.href) const prefix = new URL(base, location.href)
@@ -95,15 +101,22 @@ export function setupSearchWorker(
}) })
) )
/* Fetch index and setup search worker */ /* Fetch index if it wasn't passed explicitly */
ajax({ const index$ = typeof index !== "undefined"
url: resolve(prefix, "search/search_index.json"), ? from(index)
responseType: "json", : ajax({
withCredentials: true url: resolve(prefix, "search/search_index.json"),
}) responseType: "json",
.pipe( withCredentials: true
pluck("response"), })
map<SearchIndexOptions, SearchSetupMessage>(data => ({ .pipe<SearchIndexOptions>(
pluck("response")
)
/* Send index to search worker */
index$
.pipe<SearchSetupMessage>(
map(data => ({
type: SearchMessageType.SETUP, type: SearchMessageType.SETUP,
data data
})) }))