/
handle-sessions.es
52 lines (45 loc) · 1.41 KB
/
handle-sessions.es
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*!
* Copyright (c) 2014-present Cliqz GmbH. All rights reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { tap, switchMap, takeUntil } from 'rxjs/operators';
import Enricher from '../operators/enricher';
import logger from '../logger';
import mixResults from '../mixers/mix-results';
import { getResultOrder } from '../operators/results/utils';
import finalize from '../operators/streams/finalize';
/*
* Takes a query stream, creates a (mixed) result stream for each query.
* Stop updating if user highlighted a result.
*/
export default function handleSessions(query$, highlight$, providers, config) {
const enricher = new Enricher();
// TODO: create session ID here
const highlightWithLogger$ = highlight$.pipe(
tap(() => logger.log('Highlight'))
);
let lastResult = {};
const results$ = query$.pipe(
tap(({ query }) => logger.log(`Query '${query}'`)),
switchMap(
({ query, ...params }) => mixResults(
{
query,
...params,
resultOrder: getResultOrder(lastResult),
},
providers,
enricher,
config,
).pipe(takeUntil(highlightWithLogger$)),
),
finalize(config),
tap((result) => {
lastResult = result;
}),
);
return results$;
}