Skip to content

Commit

Permalink
add observable-hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Dec 19, 2023
1 parent aff4a01 commit df78147
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 10 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Query and Replication for WooCommerce POS",
"author": "kilbot <[email protected]>",
"license": "MIT",
"main": "src/index.tsx",
"main": "src/index.ts",
"files": [
"src/*"
],
Expand All @@ -20,6 +20,7 @@
"dependencies": {
"@shelf/fast-natural-order-by": "^2.0.0",
"lodash": "^4.17.21",
"observable-hooks": "^4.2.3",
"rxjs": "^7.8.1",
"typescript": "^5.3.3"
},
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Query } from './query-state';

export { QueryProvider, useQueryManager } from './provider';
export { useQuery } from './use-query';
export type { Query };
16 changes: 8 additions & 8 deletions src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ export class Manager<TDatabase extends RxDatabase> {
}

registerQuery({
queryKey,
queryKeys,
collectionName,
initialParams,
hooks = {},
locale,
}: {
queryKey: (string | number | object)[];
queryKeys: (string | number | object)[];
collectionName: string;
initialParams?: QueryParams;
hooks?: QueryHooks;
locale?: string;
}) {
const key = this.serializeQueryKey(queryKey);
const key = this.serializeQueryKey(queryKeys);
if (key && !this.queries.has(key)) {
const collection = this.getCollection(collectionName);
if (collection) {
Expand Down Expand Up @@ -107,7 +107,7 @@ export class Manager<TDatabase extends RxDatabase> {
this.queries.set(key, query);
}
}
return this.getQuery(queryKey);
return this.getQuery(queryKeys);
}

getCollection(collectionName: string) {
Expand All @@ -117,8 +117,8 @@ export class Manager<TDatabase extends RxDatabase> {
return this.localDB[collectionName];
}

getQuery(queryKey: (string | number | object)[]) {
const key = this.serializeQueryKey(queryKey);
getQuery(queryKeys: (string | number | object)[]) {
const key = this.serializeQueryKey(queryKeys);
const query = this.queries.get(key);

if (!query) {
Expand All @@ -128,8 +128,8 @@ export class Manager<TDatabase extends RxDatabase> {
return query;
}

deregisterQuery(queryKey: (string | number | object)[]): void {
const key = this.serializeQueryKey(queryKey);
deregisterQuery(queryKeys: (string | number | object)[]): void {
const key = this.serializeQueryKey(queryKeys);
// cancel the query
const query = this.queries.get(key);
if (query) {
Expand Down
10 changes: 10 additions & 0 deletions src/query-state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { orderBy } from '@shelf/fast-natural-order-by';
import isEqual from 'lodash/isEqual';
import { ObservableResource } from 'observable-hooks';
import { BehaviorSubject, Observable, Subscription, Subject } from 'rxjs';
import { map, switchMap, distinctUntilChanged } from 'rxjs/operators';

Expand Down Expand Up @@ -57,6 +58,8 @@ export class Query<T extends RxCollection> {
readonly params$: Observable<QueryParams | undefined> = this.subjects.params.asObservable();
readonly result$: Observable<DocumentType<T>[]> = this.subjects.result.asObservable();
readonly error$: Observable<Error> = this.subjects.error.asObservable();
readonly resource = new ObservableResource(this.result$);
readonly paginatedResource = new ObservableResource(this.result$);

/**
*
Expand Down Expand Up @@ -168,6 +171,8 @@ export class Query<T extends RxCollection> {
return this;
}

search(query: string | Record<string, any> = '') {}

private updateParams(additionalParams: Partial<QueryParams> = {}): void {
// Construct the selector from where clauses
const selector = this.whereClauses.reduce((acc, clause) => {
Expand All @@ -190,6 +195,11 @@ export class Query<T extends RxCollection> {
return this.subjects.params.getValue();
}

/**
* Pagination
*/
nextPage() {}

/**
* Cancel
*
Expand Down
2 changes: 1 addition & 1 deletion src/use-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useQueryManager } from './provider';
import type { QueryParams, QueryHooks } from './query-state';

interface QueryOptions {
queryKey: (string | number | object)[];
queryKeys: (string | number | object)[];
collectionName: string;
initialParams?: QueryParams;
hooks?: QueryHooks;
Expand Down
Empty file added tests/replication-state.test.ts
Empty file.

0 comments on commit df78147

Please sign in to comment.