From 15e282afac42912c6b511c92e5de3b70cec6e408 Mon Sep 17 00:00:00 2001 From: Christoforos Varakliotis Date: Mon, 27 Feb 2023 18:02:39 +0200 Subject: [PATCH] In-house isRxQuery implementation (#73) --- src/helpers.ts | 18 ++++++++++++++++++ src/useRxQuery.ts | 5 +++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/helpers.ts b/src/helpers.ts index afae164..49ddff3 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -1,3 +1,5 @@ +import { RxQuery } from 'rxdb'; + /** * Second variable in array is a function that can be invoked to cancel the promise. */ @@ -37,3 +39,19 @@ export const getCancelablePromise = ( }, ]; }; + +export const isObject = (val: unknown): val is Record => { + return typeof val === 'object' && val !== null; +}; + +export const isRxQuery = (val: unknown): val is RxQuery => { + return ( + isObject(val) && + 'skip' in val && + 'limit' in val && + '$' in val && + isObject(val.$) && + 'subscribe' in val.$ && + typeof val.$.subscribe === 'function' + ); +}; diff --git a/src/useRxQuery.ts b/src/useRxQuery.ts index 9361f3a..488f28c 100644 --- a/src/useRxQuery.ts +++ b/src/useRxQuery.ts @@ -1,7 +1,8 @@ import { useEffect, useCallback, useReducer, Reducer } from 'react'; -import { RxQuery, RxDocument, isRxQuery } from 'rxdb'; -import { Override } from './type.helpers'; +import { RxQuery, RxDocument } from 'rxdb'; import { DeepReadonly } from 'rxdb/dist/types/types'; +import { Override } from './type.helpers'; +import { isRxQuery } from './helpers'; export type ResultMap = Map>; export type AnyQueryResult = DeepReadonly[] | RxDocument[];