Added observable for web worker communication

This commit is contained in:
squidfunk
2019-12-17 15:59:13 +01:00
parent 862982a69d
commit 4e4e086af7
13 changed files with 208 additions and 61 deletions

View File

@@ -29,6 +29,7 @@
*/
export interface Config {
base: string /* Base URL */
search: string /* Web worker URL */
}
/* ----------------------------------------------------------------------------

View File

@@ -0,0 +1,96 @@
/*
* Copyright (c) 2016-2019 Martin Donath <martin.donath@squidfunk.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
import { Observable, fromEvent } from "rxjs"
import {
pluck,
shareReplay,
switchMap,
take,
tap,
throttle
} from "rxjs/operators"
/* ----------------------------------------------------------------------------
* Types
* ------------------------------------------------------------------------- */
/**
* Worker message
*/
export interface WorkerMessage {
type: unknown /* Message type */
data: unknown /* Message data */
}
/* ----------------------------------------------------------------------------
* Function types
* ------------------------------------------------------------------------- */
/**
* Options
*
* @template T - Worker message type
*/
interface Options<T extends WorkerMessage> {
message$: Observable<T> /* Message observable */
}
/* ----------------------------------------------------------------------------
* Functions
* ------------------------------------------------------------------------- */
/**
* Watch a web worker
*
* This function returns an observable that will send all values emitted by the
* message observable to the web worker. Web worker communication is expected
* to be bidirectional (request-response) and synchronous. Messages that are
* emitted during a pending request are throttled, the last one is emitted.
*
* @param worker - Web worker
*
* @return Worker message observable
*/
export function watchWorker<T extends WorkerMessage>(
worker: Worker, { message$ }: Options<T>
): Observable<T> {
/* Receive messages from web worker */
const worker$ = fromEvent(worker, "message")
.pipe(
pluck<Event, T>("data")
)
/* Send request and wait for response */
return message$
.pipe(
throttle(() => worker$, { leading: true, trailing: true }),
tap(message => worker.postMessage(message)),
switchMap(() => worker$
.pipe(
take(1)
)
),
shareReplay(1)
)
}

View File

@@ -395,7 +395,7 @@
<!-- Theme-related JavaScript -->
{% block scripts %}
<script src="{{ 'assets/javascripts/app.js' | url }}"></script>
<script src="{{ 'assets/javascripts/bundle.js' | url }}"></script>
<!-- Load additional languages for search -->
{% if lang.t("search.language") != "en" %}
@@ -442,7 +442,8 @@
<!-- Application initialization -->
<script>
app = initialize({
base: "{{ base_url }}"
base: "{{ base_url }}",
search: "{{ 'assets/javascripts/search.js' | url }}"
});
</script>