@cero-base/react 0.0.1 → 0.0.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.
package/README.md CHANGED
@@ -11,9 +11,12 @@ npm install @cero-base/react
11
11
  ## Setup
12
12
 
13
13
  ```jsx
14
- import { Cero, Room } from '@cero-base/react'
14
+ import { Cero, Room, useClient } from '@cero-base/react'
15
15
 
16
16
  function App() {
17
+ const { db, ready, error } = useClient(client)
18
+
19
+ if (!ready) return null
17
20
  return (
18
21
  <Cero value={db}>
19
22
  <Room id={roomId}>
@@ -26,9 +29,17 @@ function App() {
26
29
 
27
30
  ## Hooks
28
31
 
32
+ ### useClient(client)
33
+
34
+ Manages client lifecycle (ready/error). Pass a `Client` instance.
35
+
36
+ ```jsx
37
+ const { db, ready, error } = useClient(client)
38
+ ```
39
+
29
40
  ### useCero()
30
41
 
31
- Access the db instance from context.
42
+ Access the db/client instance from context.
32
43
 
33
44
  ```jsx
34
45
  const db = useCero()
@@ -42,12 +53,9 @@ Access the current Room from context.
42
53
  const room = useRoom()
43
54
  ```
44
55
 
45
- ### useCollection(name, query?)
46
-
47
- Subscribe to a collection. Returns live data + CRUD methods.
56
+ ### useCollection(name, query?, opts?)
48
57
 
49
- - Local/private collections: uses `useCero()` context
50
- - Shared collections: requires `<Room>` provider
58
+ Subscribe to a collection with CRUD methods.
51
59
 
52
60
  ```jsx
53
61
  const { data, put, del, get, sub } = useCollection('messages')
@@ -55,7 +63,7 @@ const { data, put, del, get, sub } = useCollection('messages')
55
63
 
56
64
  ### useRooms()
57
65
 
58
- List all rooms. Re-fetches on update events.
66
+ Subscribe to room list.
59
67
 
60
68
  ```jsx
61
69
  const { data: rooms } = useRooms()
@@ -71,7 +79,7 @@ const { data: members } = useMembers()
71
79
 
72
80
  ### useProfile()
73
81
 
74
- Subscribe to identity profile. Returns profile data + setter.
82
+ Subscribe to identity profile with setter.
75
83
 
76
84
  ```jsx
77
85
  const { data: profile, set } = useProfile()
@@ -84,6 +92,7 @@ await set({ name: 'Alice' })
84
92
  | --------------------- | ------------------------------------ |
85
93
  | `Cero` | Context provider — wraps db instance |
86
94
  | `Room` | Context provider — wraps room by id |
95
+ | `useClient(client)` | Manage client lifecycle |
87
96
  | `useCero()` | Access db from context |
88
97
  | `useRoom()` | Access room from context |
89
98
  | `useProfile()` | Subscribe to profile |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cero-base/react",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "React hooks and providers for cero-base",
5
5
  "files": [
6
6
  "src",
@@ -18,8 +18,8 @@
18
18
  ".": "./src/index.js"
19
19
  },
20
20
  "dependencies": {
21
- "@cero-base/core": "^0.0.1",
22
- "@cero-base/rpc": "^0.0.1"
21
+ "@cero-base/core": "^0.0.2",
22
+ "@cero-base/rpc": "^0.0.2"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@testing-library/react": "^16.0.0",
@@ -1,39 +1,21 @@
1
- import { useState, useEffect, useRef } from 'react'
2
- import { Client, getRpc } from '@cero-base/rpc'
1
+ import { useState, useEffect } from 'react'
3
2
 
4
- export function useClient(setup, schema, spec) {
3
+ export function useClient(client) {
5
4
  const [db, setDb] = useState(null)
6
5
  const [ready, setReady] = useState(false)
7
6
  const [error, setError] = useState(null)
8
- const ref = useRef(null)
9
7
 
10
8
  useEffect(() => {
11
- let client = null
9
+ client
10
+ .ready()
11
+ .then(() => {
12
+ setDb(client)
13
+ setReady(true)
14
+ })
15
+ .catch((err) => setError(err.message || String(err)))
12
16
 
13
- try {
14
- const ctx = setup()
15
- ref.current = ctx
16
- const rpc = getRpc(ctx.pipe, spec)
17
- client = new Client(rpc, schema, spec)
18
-
19
- client
20
- .ready()
21
- .then(() => {
22
- setDb(client)
23
- setReady(true)
24
- })
25
- .catch((err) => setError('RPC ready failed: ' + (err.message || String(err))))
26
- } catch (err) {
27
- setError('Client setup failed: ' + (err.message || String(err)))
28
- }
29
-
30
- return () => {
31
- try {
32
- if (client) client.close()
33
- if (ref.current?.destroy) ref.current.destroy()
34
- } catch {}
35
- }
36
- }, [])
17
+ return () => client.close()
18
+ }, [client])
37
19
 
38
20
  return { db, ready, error }
39
21
  }