(n: any, u?: any)
| 98 | function createStore<S>(useHook: CustomModelHook<S>): LaneAPI<S> |
| 99 | function createStore<S>(name: string, useHook: CustomModelHook<S>): LaneAPI<S> |
| 100 | function createStore<S>(n: any, u?: any): LaneAPI<S> { |
| 101 | const hasName = typeof n === 'string' |
| 102 | Global.storeId += hasName ? 0 : 1 |
| 103 | const storeId = hasName ? n : Global.storeId.toString() |
| 104 | if (!Global.Actions[storeId]) { |
| 105 | Global.Actions[storeId] = {} |
| 106 | } |
| 107 | if (!Global.mutableState[storeId]) { |
| 108 | Global.mutableState[storeId] = { count: 0 } |
| 109 | } |
| 110 | // Global.currentStoreId = storeId |
| 111 | // const state = useHook() |
| 112 | // Global.State = produce(Global.State, (s) => { |
| 113 | // s[hash] = state |
| 114 | // }) |
| 115 | const selector = () => { |
| 116 | Global.mutableState[storeId].count = 0 |
| 117 | Global.currentStoreId = storeId |
| 118 | const res = u ? u() : n() |
| 119 | return res |
| 120 | } |
| 121 | Global.mutableState[storeId].selector = selector |
| 122 | return { |
| 123 | // TODO: support selector |
| 124 | useStore: () => useStore(storeId, selector), |
| 125 | getState: () => selector(), |
| 126 | subscribe: (callback: () => void) => { |
| 127 | if (!Global.subscriptions[storeId]) { |
| 128 | Global.subscriptions[storeId] = [] |
| 129 | } |
| 130 | Global.subscriptions[storeId].push(callback) |
| 131 | }, |
| 132 | unsubscribe: (callback?: () => void) => { |
| 133 | if (Global.subscriptions[storeId]) { |
| 134 | if (callback) { |
| 135 | const idx = Global.subscriptions[storeId].indexOf(callback) |
| 136 | if (idx >= 0) Global.subscriptions[storeId].splice(idx, 1) |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | function Model<E, Ctx extends {}, MT extends ModelType<any, any, {}>>( |
| 144 | models: MT, |
no test coverage detected
searching dependent graphs…