@evolu/react 10.4.0 → 11.0.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.
Files changed (62) hide show
  1. package/README.md +2 -2
  2. package/dist/src/Task.d.ts +43 -0
  3. package/dist/src/Task.d.ts.map +1 -0
  4. package/dist/src/Task.js +47 -0
  5. package/dist/src/index.d.ts +4 -0
  6. package/dist/src/index.d.ts.map +1 -0
  7. package/dist/src/index.js +3 -0
  8. package/dist/src/local-first/createEvoluBinding.d.ts +72 -0
  9. package/dist/src/local-first/createEvoluBinding.d.ts.map +1 -0
  10. package/dist/src/local-first/createEvoluBinding.js +82 -0
  11. package/dist/src/local-first/useIsSsr.d.ts.map +1 -0
  12. package/dist/{useIsSsr.js → src/local-first/useIsSsr.js} +3 -4
  13. package/package.json +21 -21
  14. package/src/Task.tsx +60 -0
  15. package/src/index.ts +3 -11
  16. package/src/local-first/createEvoluBinding.tsx +212 -0
  17. package/src/{useIsSsr.ts → local-first/useIsSsr.ts} +2 -3
  18. package/dist/EvoluContext.d.ts +0 -3
  19. package/dist/EvoluContext.d.ts.map +0 -1
  20. package/dist/EvoluContext.js +0 -2
  21. package/dist/EvoluProvider.d.ts +0 -7
  22. package/dist/EvoluProvider.d.ts.map +0 -1
  23. package/dist/EvoluProvider.js +0 -4
  24. package/dist/createUseEvolu.d.ts +0 -13
  25. package/dist/createUseEvolu.d.ts.map +0 -1
  26. package/dist/createUseEvolu.js +0 -14
  27. package/dist/index.d.ts +0 -11
  28. package/dist/index.d.ts.map +0 -1
  29. package/dist/index.js +0 -11
  30. package/dist/useEvolu.d.ts +0 -9
  31. package/dist/useEvolu.d.ts.map +0 -1
  32. package/dist/useEvolu.js +0 -15
  33. package/dist/useEvoluError.d.ts +0 -4
  34. package/dist/useEvoluError.d.ts.map +0 -1
  35. package/dist/useEvoluError.js +0 -8
  36. package/dist/useIsSsr.d.ts.map +0 -1
  37. package/dist/useOwner.d.ts +0 -9
  38. package/dist/useOwner.d.ts.map +0 -1
  39. package/dist/useOwner.js +0 -16
  40. package/dist/useQueries.d.ts +0 -13
  41. package/dist/useQueries.d.ts.map +0 -1
  42. package/dist/useQueries.js +0 -31
  43. package/dist/useQuery.d.ts +0 -37
  44. package/dist/useQuery.d.ts.map +0 -1
  45. package/dist/useQuery.js +0 -45
  46. package/dist/useQuerySubscription.d.ts +0 -10
  47. package/dist/useQuerySubscription.d.ts.map +0 -1
  48. package/dist/useQuerySubscription.js +0 -18
  49. package/dist/useSyncState.d.ts +0 -4
  50. package/dist/useSyncState.d.ts.map +0 -1
  51. package/dist/useSyncState.js +0 -11
  52. package/src/EvoluContext.ts +0 -4
  53. package/src/EvoluProvider.tsx +0 -13
  54. package/src/createUseEvolu.ts +0 -17
  55. package/src/useEvolu.ts +0 -20
  56. package/src/useEvoluError.ts +0 -9
  57. package/src/useOwner.ts +0 -18
  58. package/src/useQueries.ts +0 -52
  59. package/src/useQuery.ts +0 -57
  60. package/src/useQuerySubscription.ts +0 -38
  61. package/src/useSyncState.ts +0 -13
  62. /package/dist/{useIsSsr.d.ts → src/local-first/useIsSsr.d.ts} +0 -0
@@ -9,7 +9,6 @@ const emptySubscribe = () => () => {};
9
9
  *
10
10
  * @see https://kurtextrem.de/posts/react-uses-hydration
11
11
  */
12
- export const useIsSsr = (): boolean => {
12
+ export const useIsSsr = (): boolean =>
13
13
  // TODO: Consider useDeferredValue(isSSRSync);
14
- return useSyncExternalStore(emptySubscribe, constFalse, constTrue);
15
- };
14
+ useSyncExternalStore(emptySubscribe, constFalse, constTrue);
@@ -1,3 +0,0 @@
1
- import { Evolu } from "@evolu/common/local-first";
2
- export declare const EvoluContext: import("react").Context<Evolu<any> | null>;
3
- //# sourceMappingURL=EvoluContext.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"EvoluContext.d.ts","sourceRoot":"","sources":["../src/EvoluContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,2BAA2B,CAAC;AAGlD,eAAO,MAAM,YAAY,4CAAyC,CAAC"}
@@ -1,2 +0,0 @@
1
- import { createContext } from "react";
2
- export const EvoluContext = createContext(null);
@@ -1,7 +0,0 @@
1
- import { Evolu } from "@evolu/common/local-first";
2
- import { ReactNode } from "react";
3
- export declare const EvoluProvider: ({ children, value, }: {
4
- readonly children?: ReactNode | undefined;
5
- readonly value: Evolu<any>;
6
- }) => React.ReactElement;
7
- //# sourceMappingURL=EvoluProvider.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"EvoluProvider.d.ts","sourceRoot":"","sources":["../src/EvoluProvider.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAE,MAAM,2BAA2B,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAGlC,eAAO,MAAM,aAAa,GAAI,sBAG3B;IACD,QAAQ,CAAC,QAAQ,CAAC,EAAE,SAAS,GAAG,SAAS,CAAC;IAC1C,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;CAC5B,KAAG,KAAK,CAAC,YAAqE,CAAC"}
@@ -1,4 +0,0 @@
1
- "use client";
2
- import { jsx as _jsx } from "react/jsx-runtime";
3
- import { EvoluContext } from "./EvoluContext.js";
4
- export const EvoluProvider = ({ children, value, }) => _jsx(EvoluContext, { value: value, children: children });
@@ -1,13 +0,0 @@
1
- import { Evolu, EvoluSchema } from "@evolu/common";
2
- /**
3
- * Creates a typed React Hook returning an instance of {@link Evolu}.
4
- *
5
- * ### Example
6
- *
7
- * ```ts
8
- * const useEvolu = createUseEvolu(evolu);
9
- * const { insert, update } = useEvolu();
10
- * ```
11
- */
12
- export declare const createUseEvolu: <S extends EvoluSchema>(evolu: Evolu<S>) => (() => Evolu<S>);
13
- //# sourceMappingURL=createUseEvolu.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createUseEvolu.d.ts","sourceRoot":"","sources":["../src/createUseEvolu.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAGnD;;;;;;;;;GASG;AACH,eAAO,MAAM,cAAc,GAAI,CAAC,SAAS,WAAW,EAElD,OAAO,KAAK,CAAC,CAAC,CAAC,KACd,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAA+B,CAAC"}
@@ -1,14 +0,0 @@
1
- import { useEvolu } from "./useEvolu.js";
2
- /**
3
- * Creates a typed React Hook returning an instance of {@link Evolu}.
4
- *
5
- * ### Example
6
- *
7
- * ```ts
8
- * const useEvolu = createUseEvolu(evolu);
9
- * const { insert, update } = useEvolu();
10
- * ```
11
- */
12
- export const createUseEvolu = (
13
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
14
- evolu) => useEvolu;
package/dist/index.d.ts DELETED
@@ -1,11 +0,0 @@
1
- export * from "./createUseEvolu.js";
2
- export * from "./EvoluContext.js";
3
- export * from "./EvoluProvider.js";
4
- export * from "./useEvolu.js";
5
- export * from "./useEvoluError.js";
6
- export * from "./useOwner.js";
7
- export * from "./useQueries.js";
8
- export * from "./useQuery.js";
9
- export * from "./useQuerySubscription.js";
10
- export * from "./useIsSsr.js";
11
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AACnC,cAAc,eAAe,CAAC;AAC9B,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,2BAA2B,CAAC;AAE1C,cAAc,eAAe,CAAC"}
package/dist/index.js DELETED
@@ -1,11 +0,0 @@
1
- export * from "./createUseEvolu.js";
2
- export * from "./EvoluContext.js";
3
- export * from "./EvoluProvider.js";
4
- export * from "./useEvolu.js";
5
- export * from "./useEvoluError.js";
6
- export * from "./useOwner.js";
7
- export * from "./useQueries.js";
8
- export * from "./useQuery.js";
9
- export * from "./useQuerySubscription.js";
10
- // export * from "./useSyncState.js"; TODO: Update it for the owner-api
11
- export * from "./useIsSsr.js";
@@ -1,9 +0,0 @@
1
- import { Evolu } from "@evolu/common/local-first";
2
- /**
3
- * React Hook returning a generic instance of {@link Evolu}.
4
- *
5
- * This is intended for internal usage. Applications should use
6
- * {@link createUseEvolu}, which provides a correctly typed instance.
7
- */
8
- export declare const useEvolu: () => Evolu;
9
- //# sourceMappingURL=useEvolu.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useEvolu.d.ts","sourceRoot":"","sources":["../src/useEvolu.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,2BAA2B,CAAC;AAKlD;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,QAAO,KAQ3B,CAAC"}
package/dist/useEvolu.js DELETED
@@ -1,15 +0,0 @@
1
- import { useContext } from "react";
2
- import { EvoluContext } from "./EvoluContext.js";
3
- /**
4
- * React Hook returning a generic instance of {@link Evolu}.
5
- *
6
- * This is intended for internal usage. Applications should use
7
- * {@link createUseEvolu}, which provides a correctly typed instance.
8
- */
9
- export const useEvolu = () => {
10
- const evolu = useContext(EvoluContext);
11
- if (evolu == null) {
12
- throw new Error("Could not find Evolu context value. Ensure the component is wrapped in an <EvoluProvider>.");
13
- }
14
- return evolu;
15
- };
@@ -1,4 +0,0 @@
1
- import { EvoluError } from "@evolu/common";
2
- /** Subscribe to {@link EvoluError} changes. */
3
- export declare const useEvoluError: () => EvoluError | null;
4
- //# sourceMappingURL=useEvoluError.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useEvoluError.d.ts","sourceRoot":"","sources":["../src/useEvoluError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,UAAU,EAAE,MAAM,eAAe,CAAC;AAItD,+CAA+C;AAC/C,eAAO,MAAM,aAAa,QAAO,UAAU,GAAG,IAG7C,CAAC"}
@@ -1,8 +0,0 @@
1
- import { constNull } from "@evolu/common";
2
- import { useSyncExternalStore } from "react";
3
- import { useEvolu } from "./useEvolu.js";
4
- /** Subscribe to {@link EvoluError} changes. */
5
- export const useEvoluError = () => {
6
- const evolu = useEvolu();
7
- return useSyncExternalStore(evolu.subscribeError, evolu.getError, constNull);
8
- };
@@ -1 +0,0 @@
1
- {"version":3,"file":"useIsSsr.d.ts","sourceRoot":"","sources":["../src/useIsSsr.ts"],"names":[],"mappings":"AAMA;;;;GAIG;AACH,eAAO,MAAM,QAAQ,QAAO,OAG3B,CAAC"}
@@ -1,9 +0,0 @@
1
- import { SyncOwner } from "@evolu/common";
2
- /**
3
- * React Hook for Evolu `useOwner` method.
4
- *
5
- * Using an owner means syncing it with its transports, or the transports
6
- * defined in Evolu config if the owner has no transports defined.
7
- */
8
- export declare const useOwner: (owner: SyncOwner | null) => void;
9
- //# sourceMappingURL=useOwner.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useOwner.d.ts","sourceRoot":"","sources":["../src/useOwner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAI1C;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,GAAI,OAAO,SAAS,GAAG,IAAI,KAAG,IAOlD,CAAC"}
package/dist/useOwner.js DELETED
@@ -1,16 +0,0 @@
1
- import { useEffect } from "react";
2
- import { useEvolu } from "./useEvolu.js";
3
- /**
4
- * React Hook for Evolu `useOwner` method.
5
- *
6
- * Using an owner means syncing it with its transports, or the transports
7
- * defined in Evolu config if the owner has no transports defined.
8
- */
9
- export const useOwner = (owner) => {
10
- const evolu = useEvolu();
11
- useEffect(() => {
12
- if (owner == null)
13
- return;
14
- return evolu.useOwner(owner);
15
- }, [evolu, owner]);
16
- };
@@ -1,13 +0,0 @@
1
- import { Queries, QueriesToQueryRows, QueriesToQueryRowsPromises, Row } from "@evolu/common/local-first";
2
- /**
3
- * The same as {@link useQuery}, but for many queries.
4
- *
5
- * The number of queries must remain stable across renders.
6
- */
7
- export declare const useQueries: <R extends Row, Q extends Queries<R>, OQ extends Queries<R>>(queries: [...Q], options?: Partial<{
8
- /** Queries that should be only loaded, not subscribed to. */
9
- readonly once: [...OQ];
10
- /** Reuse existing promises instead of loading so query will not suspense. */
11
- readonly promises: [...QueriesToQueryRowsPromises<Q>, ...QueriesToQueryRowsPromises<OQ>];
12
- }>) => [...QueriesToQueryRows<Q>, ...QueriesToQueryRows<OQ>];
13
- //# sourceMappingURL=useQueries.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useQueries.d.ts","sourceRoot":"","sources":["../src/useQueries.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,kBAAkB,EAClB,0BAA0B,EAC1B,GAAG,EACJ,MAAM,2BAA2B,CAAC;AAOnC;;;;GAIG;AACH,eAAO,MAAM,UAAU,GACrB,CAAC,SAAS,GAAG,EACb,CAAC,SAAS,OAAO,CAAC,CAAC,CAAC,EACpB,EAAE,SAAS,OAAO,CAAC,CAAC,CAAC,EAErB,SAAS,CAAC,GAAG,CAAC,CAAC,EACf,UAAS,OAAO,CAAC;IACf,6DAA6D;IAC7D,QAAQ,CAAC,IAAI,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC;IAEvB,6EAA6E;IAC7E,QAAQ,CAAC,QAAQ,EAAE,CACjB,GAAG,0BAA0B,CAAC,CAAC,CAAC,EAChC,GAAG,0BAA0B,CAAC,EAAE,CAAC,CAClC,CAAC;CACH,CAAM,KACN,CAAC,GAAG,kBAAkB,CAAC,CAAC,CAAC,EAAE,GAAG,kBAAkB,CAAC,EAAE,CAAC,CAkBtD,CAAC"}
@@ -1,31 +0,0 @@
1
- import { use, useRef } from "react";
2
- import { useEvolu } from "./useEvolu.js";
3
- import { useQuerySubscription } from "./useQuerySubscription.js";
4
- import { useIsSsr } from "./useIsSsr.js";
5
- /**
6
- * The same as {@link useQuery}, but for many queries.
7
- *
8
- * The number of queries must remain stable across renders.
9
- */
10
- export const useQueries = (queries, options = {}) => {
11
- const evolu = useEvolu();
12
- const once = useRef(options).current.once;
13
- const allQueries = once ? queries.concat(once) : queries;
14
- const wasSSR = useIsSsr();
15
- if (wasSSR) {
16
- if (!options.promises)
17
- void evolu.loadQueries(allQueries);
18
- }
19
- else {
20
- if (options.promises)
21
- options.promises.map(use);
22
- // No waterfall: loadQuery batches all queries in the same microtask,
23
- // so React suspends once and all promises resolve together.
24
- else
25
- evolu.loadQueries(allQueries).map(use);
26
- }
27
- return allQueries.map((query, i) =>
28
- // Safe until the number of queries is stable.
29
- // eslint-disable-next-line react-hooks/rules-of-hooks
30
- useQuerySubscription(query, { once: i > queries.length - 1 }));
31
- };
@@ -1,37 +0,0 @@
1
- import { Query, QueryRows, Row } from "@evolu/common/local-first";
2
- /**
3
- * Load and subscribe to the Query, and return an object with `rows` and `row`
4
- * properties that are automatically updated when data changes.
5
- *
6
- * Note that {@link useQuery} uses React Suspense. It means every usage of
7
- * {@link useQuery} blocks rendering until loading is completed. To avoid loading
8
- * waterfall with more queries, use {@link useQueries}.
9
- *
10
- * The `promise` option allows preloading queries before rendering, which can be
11
- * useful for complex queries that might take noticeable time even with local
12
- * data. However, this is rarely needed as local queries are typically fast.
13
- *
14
- * ### Example
15
- *
16
- * ```ts
17
- * // Get all rows.
18
- * const rows = useQuery(allTodos);
19
- *
20
- * // Get rows for a specific todo (the first row can be null).
21
- * const rows = useQuery(todoById(1));
22
- *
23
- * // Get all rows, but without subscribing to changes.
24
- * const rows = useQuery(allTodos, { once: true });
25
- *
26
- * // Preload a query (rarely needed).
27
- * const allTodosPromise = evolu.loadQuery(allTodos);
28
- * const rows = useQuery(allTodos, { promise: allTodosPromise });
29
- * ```
30
- */
31
- export declare const useQuery: <R extends Row>(query: Query<R>, options?: Partial<{
32
- /** Without subscribing to changes. */
33
- readonly once: boolean;
34
- /** Reuse existing promise instead of loading so query will not suspense. */
35
- readonly promise: Promise<QueryRows<R>>;
36
- }>) => QueryRows<R>;
37
- //# sourceMappingURL=useQuery.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useQuery.d.ts","sourceRoot":"","sources":["../src/useQuery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,2BAA2B,CAAC;AAOlE;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,eAAO,MAAM,QAAQ,GAAI,CAAC,SAAS,GAAG,EACpC,OAAO,KAAK,CAAC,CAAC,CAAC,EACf,UAAS,OAAO,CAAC;IACf,sCAAsC;IACtC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IAEvB,4EAA4E;IAC5E,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;CACzC,CAAM,KACN,SAAS,CAAC,CAAC,CAWb,CAAC"}
package/dist/useQuery.js DELETED
@@ -1,45 +0,0 @@
1
- import { use } from "react";
2
- import { useEvolu } from "./useEvolu.js";
3
- import { useQuerySubscription } from "./useQuerySubscription.js";
4
- import { useIsSsr } from "./useIsSsr.js";
5
- /**
6
- * Load and subscribe to the Query, and return an object with `rows` and `row`
7
- * properties that are automatically updated when data changes.
8
- *
9
- * Note that {@link useQuery} uses React Suspense. It means every usage of
10
- * {@link useQuery} blocks rendering until loading is completed. To avoid loading
11
- * waterfall with more queries, use {@link useQueries}.
12
- *
13
- * The `promise` option allows preloading queries before rendering, which can be
14
- * useful for complex queries that might take noticeable time even with local
15
- * data. However, this is rarely needed as local queries are typically fast.
16
- *
17
- * ### Example
18
- *
19
- * ```ts
20
- * // Get all rows.
21
- * const rows = useQuery(allTodos);
22
- *
23
- * // Get rows for a specific todo (the first row can be null).
24
- * const rows = useQuery(todoById(1));
25
- *
26
- * // Get all rows, but without subscribing to changes.
27
- * const rows = useQuery(allTodos, { once: true });
28
- *
29
- * // Preload a query (rarely needed).
30
- * const allTodosPromise = evolu.loadQuery(allTodos);
31
- * const rows = useQuery(allTodos, { promise: allTodosPromise });
32
- * ```
33
- */
34
- export const useQuery = (query, options = {}) => {
35
- const evolu = useEvolu();
36
- const isSSR = useIsSsr();
37
- if (isSSR) {
38
- if (!options.promise)
39
- void evolu.loadQuery(query);
40
- }
41
- else {
42
- use(options.promise ?? evolu.loadQuery(query));
43
- }
44
- return useQuerySubscription(query, options);
45
- };
@@ -1,10 +0,0 @@
1
- import { Query, QueryRows, Row } from "@evolu/common/local-first";
2
- /** Subscribe to {@link Query} {@link QueryRows} changes. */
3
- export declare const useQuerySubscription: <R extends Row>(query: Query<R>, options?: Partial<{
4
- /**
5
- * Only subscribe and get the current value once. Subscribed query will not
6
- * invoke React Suspense after a mutation.
7
- */
8
- readonly once: boolean;
9
- }>) => QueryRows<R>;
10
- //# sourceMappingURL=useQuerySubscription.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useQuerySubscription.d.ts","sourceRoot":"","sources":["../src/useQuerySubscription.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,EAAa,MAAM,2BAA2B,CAAC;AAI7E,4DAA4D;AAC5D,eAAO,MAAM,oBAAoB,GAAI,CAAC,SAAS,GAAG,EAChD,OAAO,KAAK,CAAC,CAAC,CAAC,EACf,UAAS,OAAO,CAAC;IACf;;;OAGG;IACH,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;CACxB,CAAM,KACN,SAAS,CAAC,CAAC,CAsBb,CAAC"}
@@ -1,18 +0,0 @@
1
- import { constVoid } from "@evolu/common";
2
- import { emptyRows } from "@evolu/common/local-first";
3
- import { useEffect, useMemo, useRef, useSyncExternalStore } from "react";
4
- import { useEvolu } from "./useEvolu.js";
5
- /** Subscribe to {@link Query} {@link QueryRows} changes. */
6
- export const useQuerySubscription = (query, options = {}) => {
7
- const evolu = useEvolu();
8
- // useRef to not break "rules-of-hooks"
9
- const { once } = useRef(options).current;
10
- if (once) {
11
- /* eslint-disable react-hooks/rules-of-hooks */
12
- useEffect(
13
- // No useSyncExternalStore, no unnecessary updates.
14
- () => evolu.subscribeQuery(query)(constVoid), [evolu, query]);
15
- return evolu.getQueryRows(query);
16
- }
17
- return useSyncExternalStore(useMemo(() => evolu.subscribeQuery(query), [evolu, query]), useMemo(() => () => evolu.getQueryRows(query), [evolu, query]), () => emptyRows);
18
- };
@@ -1,4 +0,0 @@
1
- import { SyncState } from "@evolu/common/local-first";
2
- /** Subscribe to {@link SyncState} changes. */
3
- export declare const useSyncState: () => SyncState;
4
- //# sourceMappingURL=useSyncState.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useSyncState.d.ts","sourceRoot":"","sources":["../src/useSyncState.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAGtD,8CAA8C;AAC9C,eAAO,MAAM,YAAY,QAAO,SAQ/B,CAAC"}
@@ -1,11 +0,0 @@
1
- import { useEvolu } from "./useEvolu.js";
2
- /** Subscribe to {@link SyncState} changes. */
3
- export const useSyncState = () => {
4
- const _evolu = useEvolu();
5
- // return useSyncExternalStore(
6
- // evolu.subscribeSyncState,
7
- // evolu.getSyncState,
8
- // () => initialSyncState,
9
- // );
10
- throw new Error("TODO");
11
- };
@@ -1,4 +0,0 @@
1
- import { Evolu } from "@evolu/common/local-first";
2
- import { createContext } from "react";
3
-
4
- export const EvoluContext = createContext<Evolu<any> | null>(null);
@@ -1,13 +0,0 @@
1
- "use client";
2
-
3
- import { Evolu } from "@evolu/common/local-first";
4
- import { ReactNode } from "react";
5
- import { EvoluContext } from "./EvoluContext.js";
6
-
7
- export const EvoluProvider = ({
8
- children,
9
- value,
10
- }: {
11
- readonly children?: ReactNode | undefined;
12
- readonly value: Evolu<any>;
13
- }): React.ReactElement => <EvoluContext value={value}>{children}</EvoluContext>;
@@ -1,17 +0,0 @@
1
- import { Evolu, EvoluSchema } from "@evolu/common";
2
- import { useEvolu } from "./useEvolu.js";
3
-
4
- /**
5
- * Creates a typed React Hook returning an instance of {@link Evolu}.
6
- *
7
- * ### Example
8
- *
9
- * ```ts
10
- * const useEvolu = createUseEvolu(evolu);
11
- * const { insert, update } = useEvolu();
12
- * ```
13
- */
14
- export const createUseEvolu = <S extends EvoluSchema>(
15
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
16
- evolu: Evolu<S>,
17
- ): (() => Evolu<S>) => useEvolu as () => Evolu<S>;
package/src/useEvolu.ts DELETED
@@ -1,20 +0,0 @@
1
- import { Evolu } from "@evolu/common/local-first";
2
- import { useContext } from "react";
3
- import { EvoluContext } from "./EvoluContext.js";
4
- import type { createUseEvolu } from "./createUseEvolu.js";
5
-
6
- /**
7
- * React Hook returning a generic instance of {@link Evolu}.
8
- *
9
- * This is intended for internal usage. Applications should use
10
- * {@link createUseEvolu}, which provides a correctly typed instance.
11
- */
12
- export const useEvolu = (): Evolu => {
13
- const evolu = useContext(EvoluContext);
14
- if (evolu == null) {
15
- throw new Error(
16
- "Could not find Evolu context value. Ensure the component is wrapped in an <EvoluProvider>.",
17
- );
18
- }
19
- return evolu as Evolu;
20
- };
@@ -1,9 +0,0 @@
1
- import { constNull, EvoluError } from "@evolu/common";
2
- import { useSyncExternalStore } from "react";
3
- import { useEvolu } from "./useEvolu.js";
4
-
5
- /** Subscribe to {@link EvoluError} changes. */
6
- export const useEvoluError = (): EvoluError | null => {
7
- const evolu = useEvolu();
8
- return useSyncExternalStore(evolu.subscribeError, evolu.getError, constNull);
9
- };
package/src/useOwner.ts DELETED
@@ -1,18 +0,0 @@
1
- import { SyncOwner } from "@evolu/common";
2
- import { useEffect } from "react";
3
- import { useEvolu } from "./useEvolu.js";
4
-
5
- /**
6
- * React Hook for Evolu `useOwner` method.
7
- *
8
- * Using an owner means syncing it with its transports, or the transports
9
- * defined in Evolu config if the owner has no transports defined.
10
- */
11
- export const useOwner = (owner: SyncOwner | null): void => {
12
- const evolu = useEvolu();
13
-
14
- useEffect(() => {
15
- if (owner == null) return;
16
- return evolu.useOwner(owner);
17
- }, [evolu, owner]);
18
- };
package/src/useQueries.ts DELETED
@@ -1,52 +0,0 @@
1
- import {
2
- Queries,
3
- QueriesToQueryRows,
4
- QueriesToQueryRowsPromises,
5
- Row,
6
- } from "@evolu/common/local-first";
7
- import { use, useRef } from "react";
8
- import { useEvolu } from "./useEvolu.js";
9
- import type { useQuery } from "./useQuery.js";
10
- import { useQuerySubscription } from "./useQuerySubscription.js";
11
- import { useIsSsr } from "./useIsSsr.js";
12
-
13
- /**
14
- * The same as {@link useQuery}, but for many queries.
15
- *
16
- * The number of queries must remain stable across renders.
17
- */
18
- export const useQueries = <
19
- R extends Row,
20
- Q extends Queries<R>,
21
- OQ extends Queries<R>,
22
- >(
23
- queries: [...Q],
24
- options: Partial<{
25
- /** Queries that should be only loaded, not subscribed to. */
26
- readonly once: [...OQ];
27
-
28
- /** Reuse existing promises instead of loading so query will not suspense. */
29
- readonly promises: [
30
- ...QueriesToQueryRowsPromises<Q>,
31
- ...QueriesToQueryRowsPromises<OQ>,
32
- ];
33
- }> = {},
34
- ): [...QueriesToQueryRows<Q>, ...QueriesToQueryRows<OQ>] => {
35
- const evolu = useEvolu();
36
- const once = useRef(options).current.once;
37
- const allQueries = once ? queries.concat(once) : queries;
38
- const wasSSR = useIsSsr();
39
- if (wasSSR) {
40
- if (!options.promises) void evolu.loadQueries(allQueries);
41
- } else {
42
- if (options.promises) options.promises.map(use);
43
- // No waterfall: loadQuery batches all queries in the same microtask,
44
- // so React suspends once and all promises resolve together.
45
- else evolu.loadQueries(allQueries).map(use);
46
- }
47
- return allQueries.map((query, i) =>
48
- // Safe until the number of queries is stable.
49
- // eslint-disable-next-line react-hooks/rules-of-hooks
50
- useQuerySubscription(query, { once: i > queries.length - 1 }),
51
- ) as never;
52
- };
package/src/useQuery.ts DELETED
@@ -1,57 +0,0 @@
1
- import { Query, QueryRows, Row } from "@evolu/common/local-first";
2
- import { use } from "react";
3
- import { useEvolu } from "./useEvolu.js";
4
- import type { useQueries } from "./useQueries.js";
5
- import { useQuerySubscription } from "./useQuerySubscription.js";
6
- import { useIsSsr } from "./useIsSsr.js";
7
-
8
- /**
9
- * Load and subscribe to the Query, and return an object with `rows` and `row`
10
- * properties that are automatically updated when data changes.
11
- *
12
- * Note that {@link useQuery} uses React Suspense. It means every usage of
13
- * {@link useQuery} blocks rendering until loading is completed. To avoid loading
14
- * waterfall with more queries, use {@link useQueries}.
15
- *
16
- * The `promise` option allows preloading queries before rendering, which can be
17
- * useful for complex queries that might take noticeable time even with local
18
- * data. However, this is rarely needed as local queries are typically fast.
19
- *
20
- * ### Example
21
- *
22
- * ```ts
23
- * // Get all rows.
24
- * const rows = useQuery(allTodos);
25
- *
26
- * // Get rows for a specific todo (the first row can be null).
27
- * const rows = useQuery(todoById(1));
28
- *
29
- * // Get all rows, but without subscribing to changes.
30
- * const rows = useQuery(allTodos, { once: true });
31
- *
32
- * // Preload a query (rarely needed).
33
- * const allTodosPromise = evolu.loadQuery(allTodos);
34
- * const rows = useQuery(allTodos, { promise: allTodosPromise });
35
- * ```
36
- */
37
- export const useQuery = <R extends Row>(
38
- query: Query<R>,
39
- options: Partial<{
40
- /** Without subscribing to changes. */
41
- readonly once: boolean;
42
-
43
- /** Reuse existing promise instead of loading so query will not suspense. */
44
- readonly promise: Promise<QueryRows<R>>;
45
- }> = {},
46
- ): QueryRows<R> => {
47
- const evolu = useEvolu();
48
- const isSSR = useIsSsr();
49
-
50
- if (isSSR) {
51
- if (!options.promise) void evolu.loadQuery(query);
52
- } else {
53
- use(options.promise ?? evolu.loadQuery(query));
54
- }
55
-
56
- return useQuerySubscription(query, options);
57
- };
@@ -1,38 +0,0 @@
1
- import { constVoid } from "@evolu/common";
2
- import { Query, QueryRows, Row, emptyRows } from "@evolu/common/local-first";
3
- import { useEffect, useMemo, useRef, useSyncExternalStore } from "react";
4
- import { useEvolu } from "./useEvolu.js";
5
-
6
- /** Subscribe to {@link Query} {@link QueryRows} changes. */
7
- export const useQuerySubscription = <R extends Row>(
8
- query: Query<R>,
9
- options: Partial<{
10
- /**
11
- * Only subscribe and get the current value once. Subscribed query will not
12
- * invoke React Suspense after a mutation.
13
- */
14
- readonly once: boolean;
15
- }> = {},
16
- ): QueryRows<R> => {
17
- const evolu = useEvolu();
18
-
19
- // useRef to not break "rules-of-hooks"
20
- const { once } = useRef(options).current;
21
-
22
- if (once) {
23
- /* eslint-disable react-hooks/rules-of-hooks */
24
- useEffect(
25
- // No useSyncExternalStore, no unnecessary updates.
26
- () => evolu.subscribeQuery(query)(constVoid),
27
- [evolu, query],
28
- );
29
- return evolu.getQueryRows(query);
30
- }
31
-
32
- return useSyncExternalStore(
33
- useMemo(() => evolu.subscribeQuery(query), [evolu, query]),
34
- useMemo(() => () => evolu.getQueryRows(query), [evolu, query]),
35
- () => emptyRows as QueryRows<R>,
36
- /* eslint-enable react-hooks/rules-of-hooks */
37
- );
38
- };
@@ -1,13 +0,0 @@
1
- import { SyncState } from "@evolu/common/local-first";
2
- import { useEvolu } from "./useEvolu.js";
3
-
4
- /** Subscribe to {@link SyncState} changes. */
5
- export const useSyncState = (): SyncState => {
6
- const _evolu = useEvolu();
7
- // return useSyncExternalStore(
8
- // evolu.subscribeSyncState,
9
- // evolu.getSyncState,
10
- // () => initialSyncState,
11
- // );
12
- throw new Error("TODO");
13
- };