-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
46 lines (38 loc) · 1.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import {
createContext,
useContext,
onCleanup,
createComponent
} from 'solid-js'
import { createStore, reconcile } from 'solid-js/store'
const StoreContext = createContext()
export function StoreonProvider (props) {
if (process.env.NODE_ENV !== 'production' && !props.store) {
throw new Error(
'Could not find store in props. ' +
'Please ensure that you pass store to the provider ' +
'<StoreonProvider store={store}>'
)
}
let [state, setState] = createStore(props.store.get())
let unbind = props.store.on('@changed', (_, changed) => {
Object.keys(changed).forEach(key => {
setState(key, reconcile(changed[key]))
})
})
onCleanup(() => unbind())
return createComponent(StoreContext.Provider, {
value: [state, props.store.dispatch],
children: () => props.children
})
}
export function useStoreon () {
let store = useContext(StoreContext)
if (process.env.NODE_ENV !== 'production' && !store) {
throw new Error(
'Could not find storeon context value. ' +
'Please ensure the component is wrapped in a <StoreonProvider>'
)
}
return store
}