MCPcopy Create free account
hub / github.com/byte-fe/react-model / useStore

Function useStore

src/index.tsx:355–390  ·  view source on GitHub ↗
(modelName: string, selector?: Function)

Source from the content-addressed store, hash-verified

353}
354
355const useStore = (modelName: string, selector?: Function) => {
356 const setState = useState({})[1]
357 const hash = useRef<string>('')
358 // createStore('xxx', () => {}) has the top priority
359
360 const mutableState = get([modelName])(Global.mutableState)
361 const isFromCreateStore = !!mutableState
362 const usedSelector = isFromCreateStore ? mutableState.selector : selector
363 const usedState = isFromCreateStore ? mutableState : getState(modelName)
364
365 useStoreEffect(() => {
366 Global.uid += 1
367 const local_hash = '' + Global.uid
368 hash.current = local_hash
369 if (!Global.Setter.functionSetter[modelName]) {
370 Global.Setter.functionSetter[modelName] = {}
371 }
372 Global.Setter.functionSetter[modelName][local_hash] = {
373 setState,
374 selector: usedSelector
375 }
376 return function cleanup() {
377 delete Global.Setter.functionSetter[modelName][local_hash]
378 }
379 }, [])
380
381 if (isFromCreateStore) {
382 return usedSelector(usedState)
383 } else {
384 const updaters = getActions(modelName, {
385 __hash: hash.current,
386 type: 'f'
387 })
388 return [usedSelector ? usedSelector(usedState) : usedState, updaters]
389 }
390}
391
392// Class API
393class Provider extends PureComponent<{}, Global['State']> {

Callers 15

createStoreFunction · 0.85
ModelFunction · 0.85
dubugger.spec.tsFile · 0.85
getActions.spec.tsFile · 0.85
unmount.spec.tsFile · 0.85
useStore.spec.tsFile · 0.85
actions.spec.tsFile · 0.85
initial.spec.tsFile · 0.85
actions.spec.tsFile · 0.85
return.spec.tsFile · 0.85

Calls 3

getFunction · 0.90
getStateFunction · 0.85
getActionsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…