Last active
February 11, 2019 00:00
-
-
Save amanda-mitchell/a9f9586aff7dd8b63b02ec2a1e868bf7 to your computer and use it in GitHub Desktop.
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
function useBoundEffect<TArguments extends any[]>( | |
effect: (...params: TArguments) => ReturnType<React.EffectCallback>, | |
getArguments: () => TArguments | |
) { | |
const lastArguments = useRef<TArguments | undefined>(undefined); | |
const lastEffectCleanup = useRef<ReturnType<React.EffectCallback>>(undefined); | |
useEffect(() => { | |
const newArguments = getArguments(); | |
if (areEquivalent(lastArguments.current, newArguments)) { | |
return; | |
} | |
lastArguments.current = newArguments; | |
if (lastEffectCleanup.current) { | |
lastEffectCleanup.current(); | |
} | |
lastEffectCleanup.current = effect(...newArguments); | |
}); | |
useEffect(() => { | |
return () => { | |
if (lastEffectCleanup.current) { | |
lastEffectCleanup.current(); | |
} | |
}; | |
}, []); | |
} | |
function areEquivalent<TArguments extends any[]>( | |
left: TArguments | undefined, | |
right: TArguments | undefined | |
) { | |
if (!left) { | |
return !right; | |
} | |
if (!right) { | |
return false; | |
} | |
const { length } = left; | |
if (length !== right.length) { | |
return false; | |
} | |
for (let index = 0; index < length; ++index) { | |
if (left[index] !== right[index]) { | |
return false; | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment