Skip to content

Commit

Permalink
In-house isRxQuery implementation (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
cvara authored Feb 27, 2023
1 parent 72a483a commit 15e282a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { RxQuery } from 'rxdb';

/**
* Second variable in array is a function that can be invoked to cancel the promise.
*/
Expand Down Expand Up @@ -37,3 +39,19 @@ export const getCancelablePromise = <T>(
},
];
};

export const isObject = (val: unknown): val is Record<string, unknown> => {
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'
);
};
5 changes: 3 additions & 2 deletions src/useRxQuery.ts
Original file line number Diff line number Diff line change
@@ -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<T> = Map<string, RxDocument<T, any>>;
export type AnyQueryResult<T> = DeepReadonly<T>[] | RxDocument<T>[];
Expand Down

0 comments on commit 15e282a

Please sign in to comment.