2018-03-04 21:37:55 +00:00
|
|
|
// Avoid re-renders by caching the most recent value of an array
|
|
|
|
// or an object, using an approach similar to https://github.com/reactjs/reselect.
|
|
|
|
// This avoids the issue where Svelte may keep re-rendering because it doesn't
|
|
|
|
// know if an object/array has changed or not.
|
|
|
|
|
2018-04-06 00:57:36 +00:00
|
|
|
import isEqual from 'lodash-es/isEqual'
|
2018-03-04 21:37:55 +00:00
|
|
|
|
|
|
|
if (process.browser && process.env.NODE_ENV !== 'production') {
|
|
|
|
window.reselectStats = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function reselect (store, outputKey, inputKey) {
|
|
|
|
let prevValue
|
|
|
|
let nextValue
|
|
|
|
let count = 0
|
|
|
|
let countKey = `${outputKey}_reselectCount`
|
|
|
|
|
|
|
|
store.compute(countKey, [inputKey], input => {
|
|
|
|
if (process.browser && process.env.NODE_ENV !== 'production') {
|
|
|
|
window.reselectStats[inputKey] = window.reselectStats[inputKey] || {numInputChanges: 0, numOutputChanges: 0}
|
|
|
|
window.reselectStats[inputKey].numInputChanges++
|
|
|
|
}
|
|
|
|
if (!isEqual(prevValue, input)) {
|
|
|
|
nextValue = input
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
})
|
|
|
|
|
|
|
|
store.compute(outputKey, [countKey], () => {
|
|
|
|
if (process.browser && process.env.NODE_ENV !== 'production') {
|
|
|
|
window.reselectStats[inputKey].numOutputChanges++
|
|
|
|
}
|
|
|
|
prevValue = nextValue
|
|
|
|
nextValue = null
|
|
|
|
return prevValue
|
|
|
|
})
|
|
|
|
}
|