|
1 | 1 | import { useEffect, useState, useContext } from 'react'; |
2 | | -// @ts-ignore |
3 | 2 | import SyncStateReactContext from '../components/Context'; |
4 | | -import { produceWithPatches, enablePatches } from 'immer'; |
5 | 3 | import { ComputeCallback, DocStore } from '@syncstate/core'; |
6 | | -enablePatches(); |
7 | | - |
8 | | -//create your forceUpdate hook |
9 | | -function useForceUpdate() { |
10 | | - const [value, setValue] = useState(0); // integer state |
11 | | - return () => setValue(value => ++value); // update the state to force render |
12 | | -} |
13 | 4 |
|
14 | 5 | export function useComputed(subtree: string, computeCallback: ComputeCallback) { |
15 | | - // const forceUpdate = useForceUpdate(); |
16 | 6 | const store: DocStore = useContext(SyncStateReactContext); |
17 | 7 |
|
18 | 8 | const [computedValue, setComputedValue] = useState(undefined); |
19 | 9 |
|
20 | 10 | useEffect(() => { |
21 | | - const dispose = store.compute(subtree, (getValue, change) => { |
| 11 | + const dispose = store.compute(subtree, (getValue: any, change: any) => { |
22 | 12 | console.log('$$$$compute observer hook'); |
23 | 13 | const computed = computeCallback(getValue, change); |
24 | 14 |
|
25 | 15 | setComputedValue(computed); |
26 | 16 | }); |
| 17 | + |
| 18 | + return dispose; |
27 | 19 | }, []); |
28 | 20 |
|
29 | 21 | console.log('computed value through hook', computedValue); |
30 | 22 |
|
31 | 23 | return [computedValue]; |
32 | 24 | } |
33 | 25 |
|
34 | | -// export function useDoc(path: string = '', depth: number = 1) { |
35 | | -// return useSyncState('doc', path, depth); |
36 | | -// } |
| 26 | +export function useComputedDoc(computeCallback: ComputeCallback) { |
| 27 | + return useComputed('doc', computeCallback); |
| 28 | +} |
0 commit comments