(modelName: string, selector?: Function)
| 353 | } |
| 354 | |
| 355 | const 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 |
| 393 | class Provider extends PureComponent<{}, Global['State']> { |
no test coverage detected
searching dependent graphs…