( state: S | (() => S) )
| 34 | // useModel rules: |
| 35 | // DON'T USE useModel OUTSIDE createStore func |
| 36 | function useModel<S>( |
| 37 | state: S | (() => S) |
| 38 | ): [S, (state: Partial<S> | ((state: S) => S | void)) => void] { |
| 39 | const storeId = Global.currentStoreId |
| 40 | const index = Global.mutableState[storeId].count |
| 41 | Global.mutableState[storeId].count += 1 |
| 42 | if (!Global.mutableState[storeId].hasOwnProperty(index)) { |
| 43 | if (typeof state === 'function') { |
| 44 | // @ts-ignore |
| 45 | Global.mutableState[storeId][index] = state() |
| 46 | } else { |
| 47 | Global.mutableState[storeId][index] = state |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | const setter = async (state: Partial<S> | ((prevState: S) => S | void)) => { |
| 52 | if (typeof state === 'function') { |
| 53 | Global.mutableState[storeId][index] = produce( |
| 54 | Global.mutableState[storeId][index], |
| 55 | // @ts-ignore |
| 56 | state |
| 57 | ) |
| 58 | } else { |
| 59 | if ( |
| 60 | Global.mutableState[storeId][index] && |
| 61 | state && |
| 62 | Global.mutableState[storeId][index].constructor.name === 'Object' && |
| 63 | state.constructor.name === 'Object' |
| 64 | ) { |
| 65 | Global.mutableState[storeId][index] = { |
| 66 | ...Global.mutableState[storeId][index], |
| 67 | ...state |
| 68 | } |
| 69 | } else { |
| 70 | Global.mutableState[storeId][index] = state |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | const context: InnerContext = { |
| 75 | Global, |
| 76 | action: () => { |
| 77 | return typeof state === 'function' |
| 78 | ? // @ts-ignore |
| 79 | Global.mutableState[storeId][index] |
| 80 | : state |
| 81 | }, |
| 82 | actionName: 'setter', |
| 83 | consumerActions, |
| 84 | disableSelectorUpdate: true, |
| 85 | middlewareConfig: {}, |
| 86 | modelName: storeId, |
| 87 | newState: {}, |
| 88 | params: undefined, |
| 89 | type: 'u' |
| 90 | } |
| 91 | |
| 92 | // pass update event to other components subscribe the same store |
| 93 | return await applyMiddlewares(actionMiddlewares, context) |
no outgoing calls
searching dependent graphs…