@cero-base/react 0.0.2 → 0.0.5
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 +1 -1
- package/package.json +3 -3
- package/src/hooks/use-query.js +10 -19
- package/src/ui/cero.jsx +6 -7
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cero-base/react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
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.
|
|
22
|
-
"@cero-base/rpc": "^0.0.
|
|
21
|
+
"@cero-base/core": "^0.0.5",
|
|
22
|
+
"@cero-base/rpc": "^0.0.5"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@testing-library/react": "^16.0.0",
|
package/src/hooks/use-query.js
CHANGED
|
@@ -3,46 +3,37 @@ import { store } from '@simplestack/store'
|
|
|
3
3
|
import { useStoreValue } from '@simplestack/store/react'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* useQuery —
|
|
7
|
-
*
|
|
8
|
-
* All named hooks compose from this. Exported as escape hatch for custom subscriptions.
|
|
9
|
-
*
|
|
10
|
-
* Uses @simplestack/store as the reactive layer — signal-based, fine-grained updates.
|
|
11
|
-
* The store persists between mounts (stale-while-revalidate for free).
|
|
12
|
-
* Redundant updates are skipped via Object.is inside store.select().set().
|
|
6
|
+
* useQuery — subscribes to a Readable stream, returns reactive data.
|
|
13
7
|
*
|
|
14
8
|
* @param {Function} streamFn - Returns a Readable stream (from .sub())
|
|
15
9
|
* @param {Array} deps - Dependency array (re-subscribes when changed)
|
|
16
10
|
* @returns {{ data: any, busy: boolean, error: Error|null }}
|
|
17
11
|
*/
|
|
18
12
|
export function useQuery(streamFn, deps) {
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
s.current = store({ data: null, busy: true, error: null })
|
|
13
|
+
const state = useRef(null)
|
|
14
|
+
if (state.current === null) {
|
|
15
|
+
state.current = store({ data: null, busy: true, error: null })
|
|
23
16
|
}
|
|
24
17
|
|
|
25
18
|
useEffect(() => {
|
|
26
|
-
const st =
|
|
19
|
+
const st = state.current
|
|
27
20
|
st.select('busy').set(true)
|
|
28
21
|
|
|
29
22
|
const stream = streamFn()
|
|
30
23
|
|
|
31
|
-
stream.on('data', (
|
|
32
|
-
|
|
33
|
-
ref.current = d
|
|
34
|
-
st.select('data').set(d)
|
|
24
|
+
stream.on('data', (data) => {
|
|
25
|
+
st.select('data').set(data)
|
|
35
26
|
st.select('busy').set(false)
|
|
36
27
|
st.select('error').set(null)
|
|
37
28
|
})
|
|
38
29
|
|
|
39
|
-
stream.on('error', (
|
|
40
|
-
st.select('error').set(
|
|
30
|
+
stream.on('error', (error) => {
|
|
31
|
+
st.select('error').set(error)
|
|
41
32
|
st.select('busy').set(false)
|
|
42
33
|
})
|
|
43
34
|
|
|
44
35
|
return () => stream.destroy()
|
|
45
36
|
}, deps) // eslint-disable-line react-hooks/exhaustive-deps
|
|
46
37
|
|
|
47
|
-
return useStoreValue(
|
|
38
|
+
return useStoreValue(state.current)
|
|
48
39
|
}
|
package/src/ui/cero.jsx
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
import { useState, useEffect } from 'react'
|
|
2
2
|
import { CeroContext } from './context.js'
|
|
3
3
|
|
|
4
|
-
export function Cero({
|
|
5
|
-
const [ready, setReady] = useState(
|
|
4
|
+
export function Cero({ db, children }) {
|
|
5
|
+
const [ready, setReady] = useState(db.opened)
|
|
6
6
|
|
|
7
7
|
useEffect(() => {
|
|
8
|
-
if (!
|
|
9
|
-
|
|
10
|
-
.ready()
|
|
8
|
+
if (!db.opened)
|
|
9
|
+
db.ready()
|
|
11
10
|
.then(() => setReady(true))
|
|
12
11
|
.catch(console.error)
|
|
13
12
|
else setReady(true)
|
|
14
|
-
}, [
|
|
13
|
+
}, [db])
|
|
15
14
|
|
|
16
15
|
if (!ready) return null
|
|
17
16
|
|
|
18
|
-
return <CeroContext.Provider value={
|
|
17
|
+
return <CeroContext.Provider value={db}>{children}</CeroContext.Provider>
|
|
19
18
|
}
|