@triplit/react 0.0.38-beta.0 → 0.0.38-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ export * from './use-query.js';
2
+ export * from './use-entity.js';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './use-query.js';
2
+ export * from './use-entity.js';
@@ -0,0 +1,7 @@
1
+ import { TriplitClient, Models, CollectionNameFromModels, SubscriptionOptions, ResultTypeFromModel, ModelFromModels } 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: ResultTypeFromModel<ModelFromModels<M, CN>> | undefined;
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 { ClientFetchResult, 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<M, CN>, options?: SubscriptionOptions): {
3
+ fetching: boolean;
4
+ fetchingRemote: boolean;
5
+ results: ClientFetchResult<ClientQuery<M, CN>> | 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.0",
4
+ "version": "0.0.38-beta.2",
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 -b",
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.0"
28
+ "@triplit/client": "^0.0.38-beta.2"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@parcel/config-default": "^2.9.3",