@toapi/react 1.3.4 → 1.4.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.
@@ -3,7 +3,8 @@ import * as React from "react";
3
3
  type ObservablePromise<T> = Promise<T> & Observable<T>;
4
4
  interface Options {
5
5
  startTransition?: typeof React.startTransition;
6
+ onStatusChange?(status: "loading" | "idle"): void;
6
7
  }
7
- export declare function useQuery<T>(query: ObservablePromise<T> | (() => ObservablePromise<T>), { startTransition }?: Options): T;
8
+ export declare function useQuery<T>(query: ObservablePromise<T> | (() => ObservablePromise<T>), { startTransition, onStatusChange }?: Options): T;
8
9
  export {};
9
10
  //# sourceMappingURL=use-query.d.ts.map
@@ -1 +1 @@
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,KA2C1D"}
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;IAC/C,cAAc,CAAC,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,GAAG,IAAI,CAAC;CACnD;AAED,wBAAgB,QAAQ,CAAC,CAAC,EAExB,KAAK,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,iBAAiB,CAAC,CAAC,CAAC,CAAC,EAC1D,EAAE,eAAuC,EAAE,cAAc,EAAE,GAAE,OAAY,KAmD1E"}
package/dist/use-query.js CHANGED
@@ -2,24 +2,33 @@ import {} from "@toapi/common";
2
2
  import * as React from "react";
3
3
  export function useQuery(
4
4
  // TODO remove the useless function-form in next major release
5
- query, { startTransition = React.startTransition } = {}) {
5
+ query, { startTransition = React.startTransition, onStatusChange } = {}) {
6
6
  const observable = typeof query === "function" ? query() : query;
7
7
  const queryKey = observable.queryKey;
8
8
  const initialObservable = React.useMemo(() => observable, [queryKey]);
9
9
  const [state, setState] = React.useState(null);
10
10
  React.useEffect(() => {
11
11
  let active = true;
12
- const unsubscribe = observable.subscribe((next) => {
13
- startTransition(async () => {
14
- try {
15
- const value = await next;
16
- // A late update from a subscription we have already left behind must
17
- // not overwrite the current one.
18
- if (active)
19
- setState({ promise: next, queryKey, value });
20
- }
21
- catch { }
22
- });
12
+ let emitted = 0;
13
+ let applied = 0;
14
+ const unsubscribe = observable.subscribe(async (next) => {
15
+ const seq = ++emitted;
16
+ onStatusChange?.("loading");
17
+ try {
18
+ const value = await next;
19
+ // A late update from a subscription we have already left behind must
20
+ // not overwrite the current one.
21
+ if (!active || seq < applied)
22
+ return;
23
+ applied = seq;
24
+ startTransition(() => setState({ promise: next, queryKey, value }));
25
+ }
26
+ finally {
27
+ if (!active || seq < applied)
28
+ return;
29
+ applied = seq;
30
+ onStatusChange?.("idle");
31
+ }
23
32
  });
24
33
  return () => {
25
34
  active = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toapi/react",
3
- "version": "1.3.4",
3
+ "version": "1.4.0",
4
4
  "author": {
5
5
  "name": "Michel Smola",
6
6
  "email": "michel.smola@farbenmeer.de"
@@ -18,15 +18,15 @@
18
18
  "dist"
19
19
  ],
20
20
  "dependencies": {
21
- "@toapi/common": "^1.3.4"
21
+ "@toapi/common": "^1.4.0"
22
22
  },
23
23
  "devDependencies": {
24
24
  "@happy-dom/global-registrator": "^20.0.0",
25
25
  "@testing-library/dom": "^10.4.1",
26
26
  "@testing-library/jest-dom": "^7.0.0",
27
27
  "@testing-library/react": "^16.3.0",
28
- "@toapi/client": "^1.3.4",
29
- "@toapi/server": "^1.3.4",
28
+ "@toapi/client": "^1.4.0",
29
+ "@toapi/server": "^1.4.0",
30
30
  "@types/node": "^25.0.3",
31
31
  "@types/react": "^19.2.7",
32
32
  "react": "^19.1.1",