@@ -19,7 +19,8 @@ import { rxChangeEventToEventReduceChangeEvent } from './rx-change-event';
1919import {
2020 arrayFilterNotEmpty ,
2121 clone ,
22- ensureNotFalsy
22+ ensureNotFalsy ,
23+ getFromMapOrCreate
2324} from './plugins/utils' ;
2425import { getQueryMatcher , getSortComparator , normalizeMangoQuery } from './rx-query-helper' ;
2526
@@ -51,63 +52,64 @@ export const RXQUERY_QUERY_PARAMS_CACHE: WeakMap<RxQuery, QueryParams<any>> = ne
5152export function getQueryParams < RxDocType > (
5253 rxQuery : RxQuery < RxDocType >
5354) : QueryParams < RxDocType > {
54- if ( ! RXQUERY_QUERY_PARAMS_CACHE . has ( rxQuery ) ) {
55- const collection = rxQuery . collection ;
56- const normalizedMangoQuery = normalizeMangoQuery (
57- collection . storageInstance . schema ,
58- clone ( rxQuery . mangoQuery )
59- ) ;
60- const primaryKey = collection . schema . primaryPath ;
55+ return getFromMapOrCreate (
56+ RXQUERY_QUERY_PARAMS_CACHE ,
57+ rxQuery ,
58+ ( ) => {
59+ const collection = rxQuery . collection ;
60+ const normalizedMangoQuery = normalizeMangoQuery (
61+ collection . storageInstance . schema ,
62+ clone ( rxQuery . mangoQuery )
63+ ) ;
64+ const primaryKey = collection . schema . primaryPath ;
6165
62- /**
63- * Create a custom sort comparator
64- * that uses the hooks to ensure
65- * we send for example compressed documents to be sorted by compressed queries.
66- */
67- const sortComparator = getSortComparator (
68- collection . schema . jsonSchema ,
69- normalizedMangoQuery
70- ) ;
66+ /**
67+ * Create a custom sort comparator
68+ * that uses the hooks to ensure
69+ * we send for example compressed documents to be sorted by compressed queries.
70+ */
71+ const sortComparator = getSortComparator (
72+ collection . schema . jsonSchema ,
73+ normalizedMangoQuery
74+ ) ;
7175
72- const useSortComparator : DeterministicSortComparator < RxDocType > = ( docA : RxDocType , docB : RxDocType ) => {
73- const sortComparatorData = {
74- docA,
75- docB,
76- rxQuery
76+ const useSortComparator : DeterministicSortComparator < RxDocType > = ( docA : RxDocType , docB : RxDocType ) => {
77+ const sortComparatorData = {
78+ docA,
79+ docB,
80+ rxQuery
81+ } ;
82+ return sortComparator ( sortComparatorData . docA , sortComparatorData . docB ) ;
7783 } ;
78- return sortComparator ( sortComparatorData . docA , sortComparatorData . docB ) ;
79- } ;
8084
81- /**
82- * Create a custom query matcher
83- * that uses the hooks to ensure
84- * we send for example compressed documents to match compressed queries.
85- */
86- const queryMatcher = getQueryMatcher (
87- collection . schema . jsonSchema ,
88- normalizedMangoQuery
89- ) ;
90- const useQueryMatcher : QueryMatcher < RxDocumentData < RxDocType > > = ( doc : RxDocumentData < RxDocType > ) => {
91- const queryMatcherData = {
92- doc,
93- rxQuery
85+ /**
86+ * Create a custom query matcher
87+ * that uses the hooks to ensure
88+ * we send for example compressed documents to match compressed queries.
89+ */
90+ const queryMatcher = getQueryMatcher (
91+ collection . schema . jsonSchema ,
92+ normalizedMangoQuery
93+ ) ;
94+ const useQueryMatcher : QueryMatcher < RxDocumentData < RxDocType > > = ( doc : RxDocumentData < RxDocType > ) => {
95+ const queryMatcherData = {
96+ doc,
97+ rxQuery
98+ } ;
99+ return queryMatcher ( queryMatcherData . doc ) ;
94100 } ;
95- return queryMatcher ( queryMatcherData . doc ) ;
96- } ;
97101
98- const ret : QueryParams < any > = {
99- primaryKey : rxQuery . collection . schema . primaryPath as any ,
100- skip : normalizedMangoQuery . skip ,
101- limit : normalizedMangoQuery . limit ,
102- sortFields : getSortFieldsOfQuery ( primaryKey , normalizedMangoQuery ) as string [ ] ,
103- sortComparator : useSortComparator ,
104- queryMatcher : useQueryMatcher
105- } ;
106- RXQUERY_QUERY_PARAMS_CACHE . set ( rxQuery , ret ) ;
107- return ret ;
108- } else {
109- return RXQUERY_QUERY_PARAMS_CACHE . get ( rxQuery ) as QueryParams < RxDocType > ;
110- }
102+ const ret : QueryParams < any > = {
103+ primaryKey : rxQuery . collection . schema . primaryPath as any ,
104+ skip : normalizedMangoQuery . skip ,
105+ limit : normalizedMangoQuery . limit ,
106+ sortFields : getSortFieldsOfQuery ( primaryKey , normalizedMangoQuery ) as string [ ] ,
107+ sortComparator : useSortComparator ,
108+ queryMatcher : useQueryMatcher
109+ } ;
110+ return ret ;
111+ }
112+ ) ;
111113}
112114
113115
0 commit comments