(requireAllKeys = true)
| 2 | * Helper utility that updates the specified callback whenever any of the specified indices have changed. |
| 3 | */ |
| 4 | export default function createCallbackMemoizer(requireAllKeys = true) { |
| 5 | let cachedIndices = {}; |
| 6 | |
| 7 | return ({callback, indices}) => { |
| 8 | const keys = Object.keys(indices); |
| 9 | const allInitialized = |
| 10 | !requireAllKeys || |
| 11 | keys.every(key => { |
| 12 | const value = indices[key]; |
| 13 | return Array.isArray(value) ? value.length > 0 : value >= 0; |
| 14 | }); |
| 15 | const indexChanged = |
| 16 | keys.length !== Object.keys(cachedIndices).length || |
| 17 | keys.some(key => { |
| 18 | const cachedValue = cachedIndices[key]; |
| 19 | const value = indices[key]; |
| 20 | |
| 21 | return Array.isArray(value) |
| 22 | ? cachedValue.join(',') !== value.join(',') |
| 23 | : cachedValue !== value; |
| 24 | }); |
| 25 | |
| 26 | cachedIndices = indices; |
| 27 | |
| 28 | if (allInitialized && indexChanged) { |
| 29 | callback(indices); |
| 30 | } |
| 31 | }; |
| 32 | } |
no test coverage detected
searching dependent graphs…