From f4de690712ab14bb0d0df3f45da15f21594b6a1c Mon Sep 17 00:00:00 2001 From: squidfunk Date: Thu, 13 Feb 2020 17:48:53 +0100 Subject: [PATCH] Improved subscription architecture --- .../javascripts/components2/search/index.ts | 3 ++- .../components2/search/query/index.ts | 2 +- .../components2/search/reset/index.ts | 16 +++++++++------- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/assets/javascripts/components2/search/index.ts b/src/assets/javascripts/components2/search/index.ts index 40c7de5f9..5b9ef47d1 100644 --- a/src/assets/javascripts/components2/search/index.ts +++ b/src/assets/javascripts/components2/search/index.ts @@ -80,7 +80,8 @@ export function mountSearch( /* Mount search query */ const query$ = useComponent("search-query") .pipe( - mountSearchQuery(handler) + mountSearchQuery(handler), + shareReplay(1) ) /* Mount search reset */ diff --git a/src/assets/javascripts/components2/search/query/index.ts b/src/assets/javascripts/components2/search/query/index.ts index ee8a17317..2302ef16d 100644 --- a/src/assets/javascripts/components2/search/query/index.ts +++ b/src/assets/javascripts/components2/search/query/index.ts @@ -69,7 +69,7 @@ export function mountSearchQuery( data: value })) ) - .subscribe(tx$) + .subscribe(tx$.next.bind(tx$)) /* Toggle search on focus */ query$ diff --git a/src/assets/javascripts/components2/search/reset/index.ts b/src/assets/javascripts/components2/search/reset/index.ts index 22525d334..9f027b6cf 100644 --- a/src/assets/javascripts/components2/search/reset/index.ts +++ b/src/assets/javascripts/components2/search/reset/index.ts @@ -20,12 +20,13 @@ * IN THE SOFTWARE. */ -import { NEVER, OperatorFunction, pipe } from "rxjs" +import { OperatorFunction, pipe } from "rxjs" import { + mapTo, + startWith, switchMap, switchMapTo, - tap, - withLatestFrom + tap } from "rxjs/operators" import { watchSearchReset } from "observables" @@ -41,12 +42,13 @@ import { useComponent } from "../../_" * * @return Operator function */ -export function mountSearchReset(): OperatorFunction { +export function mountSearchReset(): OperatorFunction { const query$ = useComponent("search-query") return pipe( switchMap(watchSearchReset), - withLatestFrom(query$), - tap(([, el]) => el.focus()), - switchMapTo(NEVER) + switchMapTo(query$), + tap(el => el.focus()), + mapTo(undefined), + startWith(undefined) ) }