Last active
May 22, 2019 22:00
-
-
Save kbrownlees/93e57729d6ad95f38381 to your computer and use it in GitHub Desktop.
Logging createSelector.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createSelector, createSelectorCreator, defaultMemoize } from 'reselect' | |
function changeMemoize(func, changeCallback) { | |
const defaultMemoizeInstance = defaultMemoize(func); | |
if (changeCallback === undefined) { | |
return defaultMemoizeInstance | |
} | |
let lastArgs = undefined; | |
let lastResult = undefined; | |
return (...args) => { | |
const result = defaultMemoizeInstance(...args); | |
if (lastResult === undefined || result !== lastResult) { | |
changeCallback(lastArgs, lastResult, args, result); | |
lastResult = result; | |
lastArgs = args; | |
} | |
return result | |
} | |
} | |
function createLogChange(name) { | |
return (lastArgs, lastResult, newArgs, newResult) => { | |
console.log(name, lastArgs, lastResult, newArgs, newResult) | |
} | |
} | |
export function createSelectorCustom(...args) { | |
let name; | |
if (typeof(args[0]) === 'string') { | |
name = args.shift() | |
} | |
//return createSelector(..args) | |
return createSelectorCreator(changeMemoize, createLogChange(name))(...args) | |
} | |
export default createSelectorCustom |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment