Skip to content

Instantly share code, notes, and snippets.

@themouette
Created November 27, 2018 15:49
Show Gist options
  • Save themouette/d86ebbf341f23ee9c9f32495bea88f0e to your computer and use it in GitHub Desktop.
Save themouette/d86ebbf341f23ee9c9f32495bea88f0e to your computer and use it in GitHub Desktop.
export const whyIsItRecalculating = (
funcName,
...fieldNames
) => decoratedFunc => {
let prevValues;
return (...values) => {
// eslint-disable-next-line no-console
console.group(`whyIsItRecalculating ${funcName}`);
if (prevValues) {
if (prevValues.length < values.length) {
// eslint-disable-next-line no-console
console.log(
'received more values than last time: %d vs %d',
prevValues.length,
values.length
);
} else if (prevValues.length > values.length) {
// eslint-disable-next-line no-console
console.log(
'received less values than last time: %d vs %d',
prevValues.length,
values.length
);
}
let i;
for (i = 0; i < prevValues.length; i += 1) {
if (prevValues[i] !== values[i]) {
// eslint-disable-next-line no-console
console.log(
`Argument ${i} ("${fieldNames[i] ||
'unknown'}") changed from`,
prevValues[i],
'to',
values[i]
);
}
}
}
// eslint-disable-next-line no-console
console.groupEnd();
prevValues = values;
return decoratedFunc(...values);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment