|
| 1 | +import { lruMemoize, createSelectorCreator } from 'reselect'; |
| 2 | +import type { CreateSelectorFunction } from './createSelectorType'; |
| 3 | + |
| 4 | +export type { CreateSelectorFunction } from './createSelectorType'; |
| 5 | + |
| 6 | +/* eslint-disable no-underscore-dangle */ // __cacheKey__ |
| 7 | + |
| 8 | +const reselectCreateSelector = createSelectorCreator({ |
| 9 | + memoize: lruMemoize, |
| 10 | + memoizeOptions: { |
| 11 | + maxSize: 1, |
| 12 | + equalityCheck: Object.is, |
| 13 | + }, |
| 14 | +}); |
| 15 | + |
| 16 | +type SelectorWithArgs = ReturnType<typeof reselectCreateSelector> & { selectorArgs: any[3] }; |
| 17 | + |
| 18 | +/* eslint-disable id-denylist */ |
| 19 | +export const createSelector = (( |
| 20 | + a: Function, |
| 21 | + b: Function, |
| 22 | + c?: Function, |
| 23 | + d?: Function, |
| 24 | + e?: Function, |
| 25 | + f?: Function, |
| 26 | + ...other: any[] |
| 27 | +) => { |
| 28 | + if (other.length > 0) { |
| 29 | + throw new Error('Unsupported number of selectors'); |
| 30 | + } |
| 31 | + |
| 32 | + let selector: any; |
| 33 | + |
| 34 | + if (a && b && c && d && e && f) { |
| 35 | + selector = (state: any, a1: any, a2: any, a3: any) => { |
| 36 | + const va = a(state, a1, a2, a3); |
| 37 | + const vb = b(state, a1, a2, a3); |
| 38 | + const vc = c(state, a1, a2, a3); |
| 39 | + const vd = d(state, a1, a2, a3); |
| 40 | + const ve = e(state, a1, a2, a3); |
| 41 | + return f(va, vb, vc, vd, ve, a1, a2, a3); |
| 42 | + }; |
| 43 | + } else if (a && b && c && d && e) { |
| 44 | + selector = (state: any, a1: any, a2: any, a3: any) => { |
| 45 | + const va = a(state, a1, a2, a3); |
| 46 | + const vb = b(state, a1, a2, a3); |
| 47 | + const vc = c(state, a1, a2, a3); |
| 48 | + const vd = d(state, a1, a2, a3); |
| 49 | + return e(va, vb, vc, vd, a1, a2, a3); |
| 50 | + }; |
| 51 | + } else if (a && b && c && d) { |
| 52 | + selector = (state: any, a1: any, a2: any, a3: any) => { |
| 53 | + const va = a(state, a1, a2, a3); |
| 54 | + const vb = b(state, a1, a2, a3); |
| 55 | + const vc = c(state, a1, a2, a3); |
| 56 | + return d(va, vb, vc, a1, a2, a3); |
| 57 | + }; |
| 58 | + } else if (a && b && c) { |
| 59 | + selector = (state: any, a1: any, a2: any, a3: any) => { |
| 60 | + const va = a(state, a1, a2, a3); |
| 61 | + const vb = b(state, a1, a2, a3); |
| 62 | + return c(va, vb, a1, a2, a3); |
| 63 | + }; |
| 64 | + } else if (a && b) { |
| 65 | + selector = (state: any, a1: any, a2: any, a3: any) => { |
| 66 | + const va = a(state, a1, a2, a3); |
| 67 | + return b(va, a1, a2, a3); |
| 68 | + }; |
| 69 | + } else if (a) { |
| 70 | + selector = a; |
| 71 | + } else { |
| 72 | + throw new Error('Missing arguments'); |
| 73 | + } |
| 74 | + |
| 75 | + return selector; |
| 76 | +}) as unknown as CreateSelectorFunction; |
| 77 | +/* eslint-enable id-denylist */ |
| 78 | + |
| 79 | +export const createSelectorMemoized: CreateSelectorFunction = (...selectors: any[]) => { |
| 80 | + type CacheKey = { id: number }; |
| 81 | + |
| 82 | + const cache = new WeakMap<CacheKey, SelectorWithArgs>(); |
| 83 | + let nextCacheId = 1; |
| 84 | + |
| 85 | + const combiner = selectors[selectors.length - 1]; |
| 86 | + const nSelectors = selectors.length - 1 || 1; |
| 87 | + // (s1, s2, ..., sN, a1, a2, a3) => { ... } |
| 88 | + const argsLength = Math.max(combiner.length - nSelectors, 0); |
| 89 | + |
| 90 | + if (argsLength > 3) { |
| 91 | + throw new Error('Unsupported number of arguments'); |
| 92 | + } |
| 93 | + |
| 94 | + // prettier-ignore |
| 95 | + const selector = (state: any, a1: any, a2: any, a3: any) => { |
| 96 | + let cacheKey = state.__cacheKey__; |
| 97 | + if (!cacheKey) { |
| 98 | + cacheKey = { id: nextCacheId }; |
| 99 | + state.__cacheKey__ = cacheKey; |
| 100 | + nextCacheId += 1; |
| 101 | + } |
| 102 | + |
| 103 | + let fn = cache.get(cacheKey); |
| 104 | + if (!fn) { |
| 105 | + let reselectArgs = selectors; |
| 106 | + const selectorArgs = [undefined, undefined, undefined]; |
| 107 | + switch (argsLength) { |
| 108 | + case 0: |
| 109 | + break; |
| 110 | + case 1: { |
| 111 | + reselectArgs = [ |
| 112 | + ...selectors.slice(0, -1), |
| 113 | + () => selectorArgs[0], |
| 114 | + combiner |
| 115 | + ]; |
| 116 | + break; |
| 117 | + } |
| 118 | + case 2: { |
| 119 | + reselectArgs = [ |
| 120 | + ...selectors.slice(0, -1), |
| 121 | + () => selectorArgs[0], |
| 122 | + () => selectorArgs[1], |
| 123 | + combiner, |
| 124 | + ]; |
| 125 | + break; |
| 126 | + } |
| 127 | + case 3: { |
| 128 | + reselectArgs = [ |
| 129 | + ...selectors.slice(0, -1), |
| 130 | + () => selectorArgs[0], |
| 131 | + () => selectorArgs[1], |
| 132 | + () => selectorArgs[2], |
| 133 | + combiner, |
| 134 | + ]; |
| 135 | + break; |
| 136 | + } |
| 137 | + default: |
| 138 | + throw new Error('Unsupported number of arguments'); |
| 139 | + } |
| 140 | + |
| 141 | + fn = reselectCreateSelector(...(reselectArgs as any)) as unknown as SelectorWithArgs; |
| 142 | + fn.selectorArgs = selectorArgs; |
| 143 | + |
| 144 | + cache.set(cacheKey, fn); |
| 145 | + } |
| 146 | + |
| 147 | + /* eslint-disable no-fallthrough */ |
| 148 | + |
| 149 | + switch (argsLength) { |
| 150 | + case 3: fn.selectorArgs[2] = a3; |
| 151 | + case 2: fn.selectorArgs[1] = a2; |
| 152 | + case 1: fn.selectorArgs[0] = a1; |
| 153 | + case 0: |
| 154 | + default: |
| 155 | + } |
| 156 | + switch (argsLength) { |
| 157 | + case 0: return fn(state); |
| 158 | + case 1: return fn(state, a1); |
| 159 | + case 2: return fn(state, a1, a2); |
| 160 | + case 3: return fn(state, a1, a2, a3); |
| 161 | + default: |
| 162 | + throw new Error('unreachable'); |
| 163 | + } |
| 164 | + }; |
| 165 | + |
| 166 | + return selector as any; |
| 167 | +}; |
0 commit comments