@toapi/react 1.2.5 → 1.3.0

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.
package/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  React-Binding for [Toapi](https://www.npmjs.com/package/@toapi/client)
4
4
 
5
+ 📚 [Docs](https://toapi-js.github.io/toapi/react/)
6
+
5
7
  ## Installation
6
8
  ```bash
7
9
  npm install @toapi/react
@@ -0,0 +1,3 @@
1
+ import type { Observable } from "@toapi/common";
2
+ export type ObservablePromise<T> = Promise<T> & Observable<T>;
3
+ //# sourceMappingURL=observable-promise.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"observable-promise.d.ts","sourceRoot":"","sources":["../src/observable-promise.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAEhD,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC"}
File without changes
@@ -0,0 +1,10 @@
1
+ import React from "react";
2
+ import type { ObservablePromise } from "../observable-promise";
3
+ interface Options {
4
+ startTransition?: typeof React.startTransition;
5
+ }
6
+ export declare function useCursorQuery<T extends {
7
+ cursor: string;
8
+ }>(query: (cursor: string) => ObservablePromise<T[]>, { startTransition }?: Options): T[][];
9
+ export {};
10
+ //# sourceMappingURL=use-cursor-query.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-cursor-query.d.ts","sourceRoot":"","sources":["../../src/unstable/use-cursor-query.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAE/D,UAAU,OAAO;IACf,eAAe,CAAC,EAAE,OAAO,KAAK,CAAC,eAAe,CAAC;CAChD;AAID,wBAAgB,cAAc,CAAC,CAAC,SAAS;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,EACzD,KAAK,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,iBAAiB,CAAC,CAAC,EAAE,CAAC,EACjD,EAAE,eAAuC,EAAE,GAAE,OAAY,SAK1D"}
@@ -0,0 +1,5 @@
1
+ import React from "react";
2
+ export function useCursorQuery(query, { startTransition = React.startTransition } = []) {
3
+ const [pages, setPages] = React.useState([]);
4
+ return React.useMemo(() => [pages.flatMap((page) => page.items)], [pages]);
5
+ }
@@ -0,0 +1,11 @@
1
+ import type { Observable } from "@farbenmeer/tapi/client";
2
+ import * as React from "react";
3
+ type ObservablePromise<T> = Promise<T> & Observable<T>;
4
+ interface Options {
5
+ startTransition?: typeof React.startTransition;
6
+ onError?: (error: Error) => void;
7
+ swr?: boolean;
8
+ }
9
+ export declare function useLacy<T>(query: ObservablePromise<T> | (() => ObservablePromise<T>), options?: Options): import("@farbenmeer/lacy").LacyPromise<T, never>;
10
+ export {};
11
+ //# sourceMappingURL=use-lacy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-lacy.d.ts","sourceRoot":"","sources":["../src/use-lacy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAG/B,KAAK,iBAAiB,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;AAEvD,UAAU,OAAO;IACf,eAAe,CAAC,EAAE,OAAO,KAAK,CAAC,eAAe,CAAC;IAC/C,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IACjC,GAAG,CAAC,EAAE,OAAO,CAAC;CACf;AAED,wBAAgB,OAAO,CAAC,CAAC,EACvB,KAAK,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,iBAAiB,CAAC,CAAC,CAAC,CAAC,EAC1D,OAAO,GAAE,OAAY,oDAuCtB"}
@@ -0,0 +1,35 @@
1
+ import * as React from "react";
2
+ import { lacy } from "@farbenmeer/lacy";
3
+ export function useLacy(query, options = {}) {
4
+ const optionsRef = React.useRef(options);
5
+ optionsRef.current = options;
6
+ const observable = React.useMemo(typeof query === "function" ? query : () => query, [query]);
7
+ const [data, setData] = React.useState(observable);
8
+ React.useEffect(() => {
9
+ const unsubscribe = observable.subscribe((next) => {
10
+ const { startTransition = React.startTransition, onError, swr, } = optionsRef.current;
11
+ startTransition(async () => {
12
+ if (swr) {
13
+ try {
14
+ await next;
15
+ setData(next);
16
+ }
17
+ catch (error) {
18
+ onError?.(error);
19
+ }
20
+ }
21
+ else {
22
+ setData(next);
23
+ try {
24
+ await next;
25
+ }
26
+ catch (error) {
27
+ onError?.(error);
28
+ }
29
+ }
30
+ });
31
+ });
32
+ return unsubscribe;
33
+ }, [observable]);
34
+ return React.useMemo(() => lacy(data), [data]);
35
+ }
@@ -1,4 +1,4 @@
1
- import type { Observable } from "@toapi/common";
1
+ import { type Observable } from "@toapi/common";
2
2
  import * as React from "react";
3
3
  type ObservablePromise<T> = Promise<T> & Observable<T>;
4
4
  interface Options {
@@ -1 +1 @@
1
- {"version":3,"file":"use-query.d.ts","sourceRoot":"","sources":["../src/use-query.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,KAAK,iBAAiB,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;AAEvD,UAAU,OAAO;IACf,eAAe,CAAC,EAAE,OAAO,KAAK,CAAC,eAAe,CAAC;CAChD;AAED,wBAAgB,QAAQ,CAAC,CAAC,EACxB,KAAK,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,iBAAiB,CAAC,CAAC,CAAC,CAAC,EAC1D,EAAE,eAAuC,EAAE,GAAE,OAAY,KAmC1D"}
1
+ {"version":3,"file":"use-query.d.ts","sourceRoot":"","sources":["../src/use-query.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,KAAK,iBAAiB,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;AAEvD,UAAU,OAAO;IACf,eAAe,CAAC,EAAE,OAAO,KAAK,CAAC,eAAe,CAAC;CAChD;AAED,wBAAgB,QAAQ,CAAC,CAAC,EAExB,KAAK,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,iBAAiB,CAAC,CAAC,CAAC,CAAC,EAC1D,EAAE,eAAuC,EAAE,GAAE,OAAY,KA0C1D"}
package/dist/use-query.js CHANGED
@@ -1,29 +1,38 @@
1
+ import {} from "@toapi/common";
1
2
  import * as React from "react";
2
- export function useQuery(query, { startTransition = React.startTransition } = {}) {
3
- const observable = React.useMemo(typeof query === "function" ? query : () => query, [query]);
4
- // The client keeps queryKey stable for the lifetime of a cached query,
5
- // while each refresh returns a new promise. Comparing promises would mistake
6
- // an inline factory's next render for a query switch and suspend on refresh.
7
- // Custom observables without a key retain their existing identity semantics.
8
- const source = observable.queryKey ?? observable;
3
+ export function useQuery(
4
+ // TODO remove the useless function-form in next major release
5
+ query, { startTransition = React.startTransition } = {}) {
6
+ const observable = typeof query === "function" ? query() : query;
7
+ const queryKey = observable.queryKey ?? null;
9
8
  const [state, setState] = React.useState(null);
10
9
  React.useEffect(() => {
11
10
  let active = true;
12
11
  const unsubscribe = observable.subscribe((next) => {
13
12
  startTransition(async () => {
14
- const value = await next;
15
- // A late update from a subscription we have already left behind must
16
- // not overwrite the current one.
17
- if (active)
18
- setState({ source, value });
13
+ try {
14
+ const value = await next;
15
+ // A late update from a subscription we have already left behind must
16
+ // not overwrite the current one.
17
+ if (active)
18
+ setState({ promise: next, queryKey, value });
19
+ }
20
+ catch { }
19
21
  });
20
22
  });
21
23
  return () => {
22
24
  active = false;
23
25
  unsubscribe();
24
26
  };
25
- }, [source, startTransition]);
26
- return state !== null && state.source === source
27
- ? state.value
28
- : React.use(observable);
27
+ }, [queryKey, startTransition]);
28
+ if (!state) {
29
+ return React.use(observable);
30
+ }
31
+ if (state.queryKey !== observable.queryKey) {
32
+ return React.use(observable);
33
+ }
34
+ if (state.promise === observable) {
35
+ return React.use(observable);
36
+ }
37
+ return state.value;
29
38
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toapi/react",
3
- "version": "1.2.5",
3
+ "version": "1.3.0",
4
4
  "author": {
5
5
  "name": "Michel Smola",
6
6
  "email": "michel.smola@farbenmeer.de"
@@ -12,24 +12,26 @@
12
12
  "license": "MIT",
13
13
  "repository": {
14
14
  "type": "git",
15
- "url": "git+https://github.com/farbenmeer/tapi.git"
15
+ "url": "git+https://github.com/toapi-js/toapi.git"
16
16
  },
17
17
  "files": [
18
18
  "dist"
19
19
  ],
20
20
  "dependencies": {
21
- "@toapi/common": "^1.2.5"
21
+ "@toapi/common": "^1.3.0"
22
22
  },
23
23
  "devDependencies": {
24
- "@toapi/client": "^1.2.5",
25
- "@toapi/server": "^1.2.5",
26
24
  "@happy-dom/global-registrator": "^20.0.0",
27
25
  "@testing-library/dom": "^10.4.1",
28
26
  "@testing-library/jest-dom": "^7.0.0",
29
27
  "@testing-library/react": "^16.3.0",
28
+ "@toapi/client": "^1.3.0",
29
+ "@toapi/server": "^1.3.0",
30
30
  "@types/node": "^25.0.3",
31
31
  "@types/react": "^19.2.7",
32
32
  "react": "^19.1.1",
33
+ "react-dom": "^19.3.0",
34
+ "react-error-boundary": "^6.1.5",
33
35
  "vitest": "^5.0.0",
34
36
  "zod": "^4.1.5"
35
37
  },
@@ -40,6 +42,6 @@
40
42
  "scripts": {
41
43
  "build": "tsc -p tsconfig.build.json",
42
44
  "release": "pnpm run build && pnpm publish --no-git-checks",
43
- "test": "vitest"
45
+ "test": "vitest run"
44
46
  }
45
47
  }