MCPcopy Create free account
hub / github.com/effector/effector / useStoreMapBase

Function useStoreMapBase

src/react/apiBase.ts:178–257  ·  view source on GitHub ↗
(
  [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,
)

Source from the content-addressed store, hash-verified

176}
177
178export 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)

Callers 3

useStoreMapFunction · 0.90
useStoreMapFunction · 0.90
useListBaseFunction · 0.70

Calls 7

throwErrorFunction · 0.90
createWatchFunction · 0.90
keysEqualFunction · 0.85
storeMethod · 0.80
stateReaderFunction · 0.70
fnFunction · 0.50
updateFilterFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…