@taladb/react 0.8.4 → 0.9.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/dist/index.d.mts CHANGED
@@ -1,27 +1,52 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import { ReactNode } from 'react';
3
- import { TalaDB, Document, Collection, Filter } from 'taladb';
3
+ import { TalaDB, OpenDBOptions, Document, Collection, Filter } from 'taladb';
4
4
 
5
- interface TalaDBProviderProps {
6
- /** The TalaDB instance returned by `openDB()`. */
7
- db: TalaDB;
5
+ type TalaDBProviderProps = {
8
6
  children: ReactNode;
9
- }
7
+ } & ({
8
+ /** A TalaDB instance you opened yourself with `openDB()`. */
9
+ db: TalaDB;
10
+ name?: never;
11
+ options?: never;
12
+ fallback?: never;
13
+ } | {
14
+ /**
15
+ * Database name — the provider owns the `openDB(name)` lifecycle:
16
+ * it opens lazily on the client (never during SSR), provides the handle
17
+ * once ready, and closes it on unmount. The natural form for Next.js,
18
+ * where `openDB` cannot run during server rendering.
19
+ */
20
+ name: string;
21
+ /** Options forwarded to `openDB(name, options)` (e.g. inline sync config). */
22
+ options?: OpenDBOptions;
23
+ /**
24
+ * Rendered while the database is opening (and during SSR).
25
+ * Defaults to `null`. Children only render once the db is ready, so
26
+ * `useTalaDB()` never observes a missing instance.
27
+ */
28
+ fallback?: ReactNode;
29
+ db?: never;
30
+ });
10
31
  /**
11
32
  * Provides a TalaDB instance to all child hooks.
12
33
  *
13
- * @example
34
+ * Two forms:
35
+ *
36
+ * **Instance form** — you own the lifecycle (plain React, React Native):
37
+ * ```tsx
14
38
  * const db = await openDB('myapp.db')
39
+ * <TalaDBProvider db={db}>…</TalaDBProvider>
40
+ * ```
15
41
  *
16
- * function App() {
17
- * return (
18
- * <TalaDBProvider db={db}>
19
- * <MyComponent />
20
- * </TalaDBProvider>
21
- * )
22
- * }
42
+ * **Name form** — the provider owns the lifecycle (recommended for Next.js):
43
+ * ```tsx
44
+ * <TalaDBProvider name="myapp.db" fallback={<Splash />}>…</TalaDBProvider>
45
+ * ```
46
+ * The database opens client-side only; during SSR (and while opening) the
47
+ * `fallback` renders instead of children, so hooks always see a ready db.
23
48
  */
24
- declare function TalaDBProvider({ db, children }: TalaDBProviderProps): react_jsx_runtime.JSX.Element;
49
+ declare function TalaDBProvider(props: TalaDBProviderProps): react_jsx_runtime.JSX.Element;
25
50
  /**
26
51
  * Returns the TalaDB instance from the nearest `<TalaDBProvider>`.
27
52
  *
@@ -49,6 +74,8 @@ interface FindResult<T> {
49
74
  data: T[];
50
75
  /** True until the first snapshot has been delivered from the database. */
51
76
  loading: boolean;
77
+ /** Most recent subscription error, cleared by the next successful snapshot. */
78
+ error: unknown | null;
52
79
  }
53
80
  /**
54
81
  * Subscribe to a live query. Re-renders whenever the matching documents change.
@@ -72,6 +99,8 @@ interface FindOneResult<T> {
72
99
  data: T | null;
73
100
  /** True until the first snapshot has been delivered from the database. */
74
101
  loading: boolean;
102
+ /** Most recent subscription error, cleared by the next successful snapshot. */
103
+ error: unknown | null;
75
104
  }
76
105
  /**
77
106
  * Subscribe to a single document live query. Re-renders when the matching
package/dist/index.d.ts CHANGED
@@ -1,27 +1,52 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import { ReactNode } from 'react';
3
- import { TalaDB, Document, Collection, Filter } from 'taladb';
3
+ import { TalaDB, OpenDBOptions, Document, Collection, Filter } from 'taladb';
4
4
 
5
- interface TalaDBProviderProps {
6
- /** The TalaDB instance returned by `openDB()`. */
7
- db: TalaDB;
5
+ type TalaDBProviderProps = {
8
6
  children: ReactNode;
9
- }
7
+ } & ({
8
+ /** A TalaDB instance you opened yourself with `openDB()`. */
9
+ db: TalaDB;
10
+ name?: never;
11
+ options?: never;
12
+ fallback?: never;
13
+ } | {
14
+ /**
15
+ * Database name — the provider owns the `openDB(name)` lifecycle:
16
+ * it opens lazily on the client (never during SSR), provides the handle
17
+ * once ready, and closes it on unmount. The natural form for Next.js,
18
+ * where `openDB` cannot run during server rendering.
19
+ */
20
+ name: string;
21
+ /** Options forwarded to `openDB(name, options)` (e.g. inline sync config). */
22
+ options?: OpenDBOptions;
23
+ /**
24
+ * Rendered while the database is opening (and during SSR).
25
+ * Defaults to `null`. Children only render once the db is ready, so
26
+ * `useTalaDB()` never observes a missing instance.
27
+ */
28
+ fallback?: ReactNode;
29
+ db?: never;
30
+ });
10
31
  /**
11
32
  * Provides a TalaDB instance to all child hooks.
12
33
  *
13
- * @example
34
+ * Two forms:
35
+ *
36
+ * **Instance form** — you own the lifecycle (plain React, React Native):
37
+ * ```tsx
14
38
  * const db = await openDB('myapp.db')
39
+ * <TalaDBProvider db={db}>…</TalaDBProvider>
40
+ * ```
15
41
  *
16
- * function App() {
17
- * return (
18
- * <TalaDBProvider db={db}>
19
- * <MyComponent />
20
- * </TalaDBProvider>
21
- * )
22
- * }
42
+ * **Name form** — the provider owns the lifecycle (recommended for Next.js):
43
+ * ```tsx
44
+ * <TalaDBProvider name="myapp.db" fallback={<Splash />}>…</TalaDBProvider>
45
+ * ```
46
+ * The database opens client-side only; during SSR (and while opening) the
47
+ * `fallback` renders instead of children, so hooks always see a ready db.
23
48
  */
24
- declare function TalaDBProvider({ db, children }: TalaDBProviderProps): react_jsx_runtime.JSX.Element;
49
+ declare function TalaDBProvider(props: TalaDBProviderProps): react_jsx_runtime.JSX.Element;
25
50
  /**
26
51
  * Returns the TalaDB instance from the nearest `<TalaDBProvider>`.
27
52
  *
@@ -49,6 +74,8 @@ interface FindResult<T> {
49
74
  data: T[];
50
75
  /** True until the first snapshot has been delivered from the database. */
51
76
  loading: boolean;
77
+ /** Most recent subscription error, cleared by the next successful snapshot. */
78
+ error: unknown | null;
52
79
  }
53
80
  /**
54
81
  * Subscribe to a live query. Re-renders whenever the matching documents change.
@@ -72,6 +99,8 @@ interface FindOneResult<T> {
72
99
  data: T | null;
73
100
  /** True until the first snapshot has been delivered from the database. */
74
101
  loading: boolean;
102
+ /** Most recent subscription error, cleared by the next successful snapshot. */
103
+ error: unknown | null;
75
104
  }
76
105
  /**
77
106
  * Subscribe to a single document live query. Re-renders when the matching
package/dist/index.js CHANGED
@@ -1,7 +1,10 @@
1
+ 'use client';
1
2
  "use strict";
3
+ var __create = Object.create;
2
4
  var __defProp = Object.defineProperty;
3
5
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
5
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
9
  var __export = (target, all) => {
7
10
  for (var name in all)
@@ -15,6 +18,14 @@ var __copyProps = (to, from, except, desc) => {
15
18
  }
16
19
  return to;
17
20
  };
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
18
29
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
30
 
20
31
  // src/index.ts
@@ -32,13 +43,49 @@ module.exports = __toCommonJS(index_exports);
32
43
  var import_react = require("react");
33
44
  var import_jsx_runtime = require("react/jsx-runtime");
34
45
  var TalaDBContext = (0, import_react.createContext)(null);
35
- function TalaDBProvider({ db, children }) {
46
+ function TalaDBProvider(props) {
47
+ if ("db" in props && props.db) {
48
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TalaDBContext.Provider, { value: props.db, children: props.children });
49
+ }
50
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(NamedProvider, { ...props });
51
+ }
52
+ function NamedProvider({
53
+ name,
54
+ options,
55
+ fallback = null,
56
+ children
57
+ }) {
58
+ const [db, setDb] = (0, import_react.useState)(null);
59
+ const [error, setError] = (0, import_react.useState)(null);
60
+ const optionsKey = JSON.stringify(options ?? null);
61
+ (0, import_react.useEffect)(() => {
62
+ setError(null);
63
+ let cancelled = false;
64
+ let opened = null;
65
+ import("taladb").then(({ openDB }) => openDB(name, options)).then((instance) => {
66
+ if (cancelled) {
67
+ void instance.close();
68
+ return;
69
+ }
70
+ opened = instance;
71
+ setDb(instance);
72
+ }).catch((e) => {
73
+ if (!cancelled) setError(e);
74
+ });
75
+ return () => {
76
+ cancelled = true;
77
+ if (opened) void opened.close();
78
+ setDb(null);
79
+ };
80
+ }, [name, optionsKey]);
81
+ if (error !== null) throw error;
82
+ if (db === null) return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: fallback });
36
83
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TalaDBContext.Provider, { value: db, children });
37
84
  }
38
85
  function useTalaDB() {
39
86
  const db = (0, import_react.useContext)(TalaDBContext);
40
87
  if (db === null) {
41
- throw new Error("useTalaDB must be used inside <TalaDBProvider db={...}>");
88
+ throw new Error('useTalaDB must be used inside <TalaDBProvider db={...}> or <TalaDBProvider name="...">');
42
89
  }
43
90
  return db;
44
91
  }
@@ -53,13 +100,16 @@ function useCollection(name) {
53
100
  // src/useFind.ts
54
101
  var import_react3 = require("react");
55
102
  function useFind(collection, filter) {
56
- const snapshotRef = (0, import_react3.useRef)({ data: [], loading: true });
103
+ const snapshotRef = (0, import_react3.useRef)({ data: [], loading: true, error: null });
57
104
  const filterKey = JSON.stringify(filter ?? null);
58
105
  const subscribe = (0, import_react3.useCallback)(
59
106
  (notify) => {
60
- snapshotRef.current = { data: snapshotRef.current.data, loading: true };
107
+ snapshotRef.current = { data: snapshotRef.current.data, loading: true, error: null };
61
108
  return collection.subscribe(filter ?? {}, (docs) => {
62
- snapshotRef.current = { data: docs, loading: false };
109
+ snapshotRef.current = { data: docs, loading: false, error: null };
110
+ notify();
111
+ }, (error) => {
112
+ snapshotRef.current = { ...snapshotRef.current, loading: false, error };
63
113
  notify();
64
114
  });
65
115
  },
@@ -74,13 +124,16 @@ function useFind(collection, filter) {
74
124
  // src/useFindOne.ts
75
125
  var import_react4 = require("react");
76
126
  function useFindOne(collection, filter) {
77
- const snapshotRef = (0, import_react4.useRef)({ data: null, loading: true });
127
+ const snapshotRef = (0, import_react4.useRef)({ data: null, loading: true, error: null });
78
128
  const filterKey = JSON.stringify(filter);
79
129
  const subscribe = (0, import_react4.useCallback)(
80
130
  (notify) => {
81
- snapshotRef.current = { data: snapshotRef.current.data, loading: true };
131
+ snapshotRef.current = { data: snapshotRef.current.data, loading: true, error: null };
82
132
  return collection.subscribe(filter, (docs) => {
83
- snapshotRef.current = { data: docs[0] ?? null, loading: false };
133
+ snapshotRef.current = { data: docs[0] ?? null, loading: false, error: null };
134
+ notify();
135
+ }, (error) => {
136
+ snapshotRef.current = { ...snapshotRef.current, loading: false, error };
84
137
  notify();
85
138
  });
86
139
  },
package/dist/index.mjs CHANGED
@@ -1,14 +1,52 @@
1
+ 'use client';
2
+
1
3
  // src/context.tsx
2
- import { createContext, useContext } from "react";
3
- import { jsx } from "react/jsx-runtime";
4
+ import { createContext, useContext, useEffect, useState } from "react";
5
+ import { Fragment, jsx } from "react/jsx-runtime";
4
6
  var TalaDBContext = createContext(null);
5
- function TalaDBProvider({ db, children }) {
7
+ function TalaDBProvider(props) {
8
+ if ("db" in props && props.db) {
9
+ return /* @__PURE__ */ jsx(TalaDBContext.Provider, { value: props.db, children: props.children });
10
+ }
11
+ return /* @__PURE__ */ jsx(NamedProvider, { ...props });
12
+ }
13
+ function NamedProvider({
14
+ name,
15
+ options,
16
+ fallback = null,
17
+ children
18
+ }) {
19
+ const [db, setDb] = useState(null);
20
+ const [error, setError] = useState(null);
21
+ const optionsKey = JSON.stringify(options ?? null);
22
+ useEffect(() => {
23
+ setError(null);
24
+ let cancelled = false;
25
+ let opened = null;
26
+ import("taladb").then(({ openDB }) => openDB(name, options)).then((instance) => {
27
+ if (cancelled) {
28
+ void instance.close();
29
+ return;
30
+ }
31
+ opened = instance;
32
+ setDb(instance);
33
+ }).catch((e) => {
34
+ if (!cancelled) setError(e);
35
+ });
36
+ return () => {
37
+ cancelled = true;
38
+ if (opened) void opened.close();
39
+ setDb(null);
40
+ };
41
+ }, [name, optionsKey]);
42
+ if (error !== null) throw error;
43
+ if (db === null) return /* @__PURE__ */ jsx(Fragment, { children: fallback });
6
44
  return /* @__PURE__ */ jsx(TalaDBContext.Provider, { value: db, children });
7
45
  }
8
46
  function useTalaDB() {
9
47
  const db = useContext(TalaDBContext);
10
48
  if (db === null) {
11
- throw new Error("useTalaDB must be used inside <TalaDBProvider db={...}>");
49
+ throw new Error('useTalaDB must be used inside <TalaDBProvider db={...}> or <TalaDBProvider name="...">');
12
50
  }
13
51
  return db;
14
52
  }
@@ -23,13 +61,16 @@ function useCollection(name) {
23
61
  // src/useFind.ts
24
62
  import { useCallback, useRef, useSyncExternalStore } from "react";
25
63
  function useFind(collection, filter) {
26
- const snapshotRef = useRef({ data: [], loading: true });
64
+ const snapshotRef = useRef({ data: [], loading: true, error: null });
27
65
  const filterKey = JSON.stringify(filter ?? null);
28
66
  const subscribe = useCallback(
29
67
  (notify) => {
30
- snapshotRef.current = { data: snapshotRef.current.data, loading: true };
68
+ snapshotRef.current = { data: snapshotRef.current.data, loading: true, error: null };
31
69
  return collection.subscribe(filter ?? {}, (docs) => {
32
- snapshotRef.current = { data: docs, loading: false };
70
+ snapshotRef.current = { data: docs, loading: false, error: null };
71
+ notify();
72
+ }, (error) => {
73
+ snapshotRef.current = { ...snapshotRef.current, loading: false, error };
33
74
  notify();
34
75
  });
35
76
  },
@@ -44,13 +85,16 @@ function useFind(collection, filter) {
44
85
  // src/useFindOne.ts
45
86
  import { useCallback as useCallback2, useRef as useRef2, useSyncExternalStore as useSyncExternalStore2 } from "react";
46
87
  function useFindOne(collection, filter) {
47
- const snapshotRef = useRef2({ data: null, loading: true });
88
+ const snapshotRef = useRef2({ data: null, loading: true, error: null });
48
89
  const filterKey = JSON.stringify(filter);
49
90
  const subscribe = useCallback2(
50
91
  (notify) => {
51
- snapshotRef.current = { data: snapshotRef.current.data, loading: true };
92
+ snapshotRef.current = { data: snapshotRef.current.data, loading: true, error: null };
52
93
  return collection.subscribe(filter, (docs) => {
53
- snapshotRef.current = { data: docs[0] ?? null, loading: false };
94
+ snapshotRef.current = { data: docs[0] ?? null, loading: false, error: null };
95
+ notify();
96
+ }, (error) => {
97
+ snapshotRef.current = { ...snapshotRef.current, loading: false, error };
54
98
  notify();
55
99
  });
56
100
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taladb/react",
3
- "version": "0.8.4",
3
+ "version": "0.9.0",
4
4
  "description": "React hooks for TalaDB — useFind, useFindOne, and useCollection for React and React Native",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "peerDependencies": {
43
43
  "react": ">=18.0.0",
44
- "taladb": "^0.8.4"
44
+ "taladb": "^0.9.0"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@testing-library/react": "^16.0.0",
@@ -53,7 +53,7 @@
53
53
  "tsup": "^8.0.0",
54
54
  "typescript": "^5.9.3",
55
55
  "vitest": "^3.2.0",
56
- "taladb": "0.8.4"
56
+ "taladb": "0.9.0"
57
57
  },
58
58
  "scripts": {
59
59
  "build": "tsup",