(
[configOrStore, separateFn]: [
configOrStore:
| {
store: Store<State>
keys: Keys
fn(state: State, keys: Keys): Result
updateFilter?: (update: Result, current: Result) => boolean
defaultValue?: Result
}
| Store<State>,
separateFn?: (state: State, keys: Keys) => Result,
],
scope?: Scope,
)
| 176 | } |
| 177 | |
| 178 | export function useStoreMapBase<State, Result, Keys extends ReadonlyArray<any>>( |
| 179 | [configOrStore, separateFn]: [ |
| 180 | configOrStore: |
| 181 | | { |
| 182 | store: Store<State> |
| 183 | keys: Keys |
| 184 | fn(state: State, keys: Keys): Result |
| 185 | updateFilter?: (update: Result, current: Result) => boolean |
| 186 | defaultValue?: Result |
| 187 | } |
| 188 | | Store<State>, |
| 189 | separateFn?: (state: State, keys: Keys) => Result, |
| 190 | ], |
| 191 | scope?: Scope, |
| 192 | ): Result { |
| 193 | let fn: (state: State, keys: Keys) => Result |
| 194 | let updateFilter: (update: Result, current: Result) => boolean = |
| 195 | basicUpdateFilter |
| 196 | let defaultValue: Result | undefined |
| 197 | let store: Store<State> |
| 198 | let keys: Keys |
| 199 | if (separateFn) { |
| 200 | fn = separateFn |
| 201 | store = configOrStore as Store<State> |
| 202 | keys = [] as unknown as Keys |
| 203 | } else { |
| 204 | ;({ |
| 205 | fn, |
| 206 | store, |
| 207 | keys, |
| 208 | defaultValue, |
| 209 | updateFilter = basicUpdateFilter, |
| 210 | } = configOrStore as any) |
| 211 | } |
| 212 | if (!is.store(store)) throwError('useStoreMap expects a store') |
| 213 | if (!Array.isArray(keys)) throwError('useStoreMap expects an array as keys') |
| 214 | if (typeof fn !== 'function') throwError('useStoreMap expects a function') |
| 215 | |
| 216 | const subscribe = React.useCallback( |
| 217 | (fn: () => void) => createWatch({unit: store, fn, scope}), |
| 218 | [store, scope], |
| 219 | ) |
| 220 | const read = React.useCallback( |
| 221 | () => stateReader(store, scope), |
| 222 | [store, scope], |
| 223 | ) |
| 224 | |
| 225 | const stateRef = React.useRef<State>() |
| 226 | const valueRef = React.useRef<Result>() |
| 227 | const keysRef = React.useRef(keys) |
| 228 | |
| 229 | const value = useSyncExternalStoreWithSelector( |
| 230 | subscribe, |
| 231 | read, |
| 232 | read, |
| 233 | state => { |
| 234 | if (stateRef.current !== state || !keysEqual(keysRef.current, keys)) { |
| 235 | let result = fn(state, keys) |
no test coverage detected
searching dependent graphs…