(
[configOrStore, separateFn]: [
configOrStore:
| {
store: Store<State>
keys: Keys
fn(state: State, keys: Keys): Result
updateFilter?: (update: Result, current: Result) => boolean
}
| Store<State>,
separateFn?: (state: State, keys: Keys) => Result,
],
scope?: Scope,
)
| 109 | } |
| 110 | |
| 111 | export function useStoreMapBase<State, Result, Keys extends ReadonlyArray<any>>( |
| 112 | [configOrStore, separateFn]: [ |
| 113 | configOrStore: |
| 114 | | { |
| 115 | store: Store<State> |
| 116 | keys: Keys |
| 117 | fn(state: State, keys: Keys): Result |
| 118 | updateFilter?: (update: Result, current: Result) => boolean |
| 119 | } |
| 120 | | Store<State>, |
| 121 | separateFn?: (state: State, keys: Keys) => Result, |
| 122 | ], |
| 123 | scope?: Scope, |
| 124 | ): Accessor<Result> { |
| 125 | let fn: (state: State, keys: Keys) => Result |
| 126 | let updateFilter: (update: Result, current: Result) => boolean = |
| 127 | basicUpdateFilter |
| 128 | let store: Store<State> |
| 129 | let keys: Keys |
| 130 | if (separateFn) { |
| 131 | fn = separateFn |
| 132 | store = configOrStore as Store<State> |
| 133 | keys = [] as unknown as Keys |
| 134 | } else { |
| 135 | fn = (configOrStore as any).fn |
| 136 | store = (configOrStore as any).store |
| 137 | keys = (configOrStore as any).keys |
| 138 | updateFilter = (configOrStore as any).updateFilter || basicUpdateFilter |
| 139 | } |
| 140 | if (!is.store(store)) throwError('useStoreMap expects a store') |
| 141 | if (!Array.isArray(keys)) throwError('useStoreMap expects an array as keys') |
| 142 | if (typeof fn !== 'function') throwError('useStoreMap expects a function') |
| 143 | const [result, setResult] = createSignal<{ |
| 144 | fn: (state: State, keys: Keys) => Result |
| 145 | upd: (update: Result, current: Result) => boolean |
| 146 | val: Result |
| 147 | init: boolean |
| 148 | store: Store<State> |
| 149 | active: boolean |
| 150 | }>({} as any, {equals: false}) |
| 151 | const refState = result() |
| 152 | refState.fn = fn |
| 153 | refState.upd = updateFilter |
| 154 | refState.init = refState.store === store |
| 155 | refState.store = store |
| 156 | refState.active = true |
| 157 | onCleanup(() => { |
| 158 | if (refState) { |
| 159 | refState.active = false |
| 160 | } |
| 161 | }) |
| 162 | const inc = () => setResult(refState) |
| 163 | |
| 164 | updateRef(stateReader(store, scope), keys, refState) |
| 165 | const stop = createWatch({ |
| 166 | unit: store, |
| 167 | fn: val => updateRef(val, keys, refState, inc), |
| 168 | scope: scope, |
no test coverage detected
searching dependent graphs…