@triplit/react 0.0.38-beta.0 → 0.0.38-beta.1
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/use-entity.d.ts +7 -0
- package/dist/use-entity.js +10 -0
- package/dist/use-query.d.ts +7 -0
- package/dist/use-query.js +33 -0
- package/package.json +3 -3
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
import { TriplitClient, Models, CollectionNameFromModels, SubscriptionOptions } from '@triplit/client';
|
2
|
+
export declare function useEntity<M extends Models<any, any> | undefined, CN extends CollectionNameFromModels<M>>(client: TriplitClient<M>, collectionName: CN, id: string, options?: SubscriptionOptions): {
|
3
|
+
fetching: boolean;
|
4
|
+
fetchingRemote: boolean;
|
5
|
+
results: any;
|
6
|
+
error: any;
|
7
|
+
};
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { useQuery } from './use-query.js';
|
2
|
+
export function useEntity(client, collectionName, id, options) {
|
3
|
+
const { fetching, fetchingRemote, results, error } = useQuery(client, client.query(collectionName).entityId(id), options);
|
4
|
+
return {
|
5
|
+
fetching,
|
6
|
+
fetchingRemote,
|
7
|
+
results: results ? results.get(id) : undefined,
|
8
|
+
error,
|
9
|
+
};
|
10
|
+
}
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import { TriplitClient, ClientQuery, ClientQueryBuilder, SubscriptionOptions, CollectionNameFromModels, Models } from '@triplit/client';
|
2
|
+
export declare function useQuery<M extends Models<any, any> | undefined, CN extends CollectionNameFromModels<M>>(client: TriplitClient<any>, query: ClientQueryBuilder<ClientQuery<M, CN>>, options?: SubscriptionOptions): {
|
3
|
+
fetching: boolean;
|
4
|
+
fetchingRemote: boolean;
|
5
|
+
results: (M extends Models<any, any> ? Map<string, import("packages/db/src/schema.js").ResultTypeFromModel<import("packages/db/src/db.js").ModelFromModels<M, CN>>> : M extends undefined ? Map<string, any> : never) | undefined;
|
6
|
+
error: any;
|
7
|
+
};
|
@@ -0,0 +1,33 @@
|
|
1
|
+
import { useEffect, useState } from 'react';
|
2
|
+
export function useQuery(client, query, options) {
|
3
|
+
const [results, setResults] = useState(undefined);
|
4
|
+
const [fetching, setFetching] = useState(true);
|
5
|
+
const [fetchingRemote, setFetchingRemote] = useState(true);
|
6
|
+
const [error, setError] = useState(undefined);
|
7
|
+
const builtQuery = query && query.build();
|
8
|
+
const stringifiedQuery = builtQuery && JSON.stringify(builtQuery);
|
9
|
+
useEffect(() => {
|
10
|
+
if (!client)
|
11
|
+
return;
|
12
|
+
setResults(undefined);
|
13
|
+
setFetching(true);
|
14
|
+
const unsubscribe = client.subscribe(builtQuery, (localResults, { hasRemoteFulfilled }) => {
|
15
|
+
setFetching(false);
|
16
|
+
setError(undefined);
|
17
|
+
setFetchingRemote(!hasRemoteFulfilled);
|
18
|
+
setResults(new Map(localResults));
|
19
|
+
}, (error) => {
|
20
|
+
setFetching(false);
|
21
|
+
setError(error);
|
22
|
+
}, options);
|
23
|
+
return () => {
|
24
|
+
unsubscribe();
|
25
|
+
};
|
26
|
+
}, [stringifiedQuery, client]);
|
27
|
+
return {
|
28
|
+
fetching,
|
29
|
+
fetchingRemote,
|
30
|
+
results,
|
31
|
+
error,
|
32
|
+
};
|
33
|
+
}
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@triplit/react",
|
3
3
|
"packageManager": "yarn@3.4.1",
|
4
|
-
"version": "0.0.38-beta.
|
4
|
+
"version": "0.0.38-beta.1",
|
5
5
|
"source": "./src/index.ts",
|
6
6
|
"main": "./dist/index.js",
|
7
7
|
"module": "./dist/index.js",
|
@@ -16,7 +16,7 @@
|
|
16
16
|
}
|
17
17
|
},
|
18
18
|
"scripts": {
|
19
|
-
"build": "tsc
|
19
|
+
"build": "tsc --build --force",
|
20
20
|
"lint:build": "npx publint",
|
21
21
|
"lint": "tsc --noEmit",
|
22
22
|
"publish-pkg": "node ../../scripts/npm-check-version-and-publish.js"
|
@@ -25,7 +25,7 @@
|
|
25
25
|
"/dist"
|
26
26
|
],
|
27
27
|
"dependencies": {
|
28
|
-
"@triplit/client": "^0.0.38-beta.
|
28
|
+
"@triplit/client": "^0.0.38-beta.1"
|
29
29
|
},
|
30
30
|
"devDependencies": {
|
31
31
|
"@parcel/config-default": "^2.9.3",
|