From 8ea058f098cd2c40c37e65b54695b3d496e878d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=A1vio=20Caetano?= Date: Mon, 25 Nov 2019 17:07:34 -0300 Subject: [PATCH 1/2] fix(useEffectUpdate): add overloads with generic types fix #14 --- src/index.tsx | 126 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 123 insertions(+), 3 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index d584f8f..e6aaa13 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -84,6 +84,121 @@ export const useWorker = ( return { callback, error, isLoading, setError, setIsLoading }; }; +/** + * Executes a effect and sends to its callback the previous state. + * + * @param effect The effect to be executed. Receives the dependencies' previous state as arguments. + * @param dependencies The effect's dependencies. + */ +export function useEffectUpdate( + effect: (oldState: [T1]) => void | (() => void), + dependencies: [T1], +): void; + +/** + * Executes a effect and sends to its callback the previous state. + * + * @param effect The effect to be executed. Receives the dependencies' previous state as arguments. + * @param dependencies The effect's dependencies. + */ +export function useEffectUpdate( + effect: (oldState: [T1, T2]) => void | (() => void), + dependencies: [T1, T2], +): void; + +/** + * Executes a effect and sends to its callback the previous state. + * + * @param effect The effect to be executed. Receives the dependencies' previous state as arguments. + * @param dependencies The effect's dependencies. + */ +export function useEffectUpdate( + effect: (oldState: [T1, T2, T3]) => void | (() => void), + dependencies: [T1, T2, T3], +): void; + +/** + * Executes a effect and sends to its callback the previous state. + * + * @param effect The effect to be executed. Receives the dependencies' previous state as arguments. + * @param dependencies The effect's dependencies. + * @category Effects + */ +export function useEffectUpdate( + effect: (oldState: [T1, T2, T3, T4]) => void | (() => void), + dependencies: [T1, T2, T3, T4], +): void; + +/** + * Executes a effect and sends to its callback the previous state. + * + * @param effect The effect to be executed. Receives the dependencies' previous state as arguments. + * @param dependencies The effect's dependencies. + */ +export function useEffectUpdate( + effect: (oldState: [T1, T2, T3, T4, T5]) => void | (() => void), + dependencies: [T1, T2, T3, T4, T5], +): void; + +/** + * Executes a effect and sends to its callback the previous state. + * + * @param effect The effect to be executed. Receives the dependencies' previous state as arguments. + * @param dependencies The effect's dependencies. + */ +export function useEffectUpdate( + effect: (oldState: [T1, T2, T3, T4, T5, T6]) => void | (() => void), + dependencies: [T1, T2, T3, T4, T5, T6], +): void; + +/** + * Executes a effect and sends to its callback the previous state. + * + * @param effect The effect to be executed. Receives the dependencies' previous state as arguments. + * @param dependencies The effect's dependencies. + */ +export function useEffectUpdate( + effect: (oldState: [T1, T2, T3, T4, T5, T6, T7]) => void | (() => void), + dependencies: [T1, T2, T3, T4, T5, T6, T7], +): void; + +/** + * Executes a effect and sends to its callback the previous state. + * + * @param effect The effect to be executed. Receives the dependencies' previous state as arguments. + * @param dependencies The effect's dependencies. + */ +export function useEffectUpdate( + effect: (oldState: [T1, T2, T3, T4, T5, T6, T7, T8]) => void | (() => void), + dependencies: [T1, T2, T3, T4, T5, T6, T7, T8], +): void; + +/** + * Executes a effect and sends to its callback the previous state. + * + * @param effect The effect to be executed. Receives the dependencies' previous state as arguments. + * @param dependencies The effect's dependencies. + */ +export function useEffectUpdate( + effect: ( + oldState: [T1, T2, T3, T4, T5, T6, T7, T8, T9], + ) => void | (() => void), + dependencies: [T1, T2, T3, T4, T5, T6, T7, T8, T9], +): void; + +/** + * Executes a effect and sends to its callback the previous state. + * + * @param effect The effect to be executed. Receives the dependencies' previous state as arguments. + * @param dependencies The effect's dependencies. + */ +export function useEffectUpdate( + effect: ( + oldState: [T1, T2, T3, T4, T5, T6, T7, T8, T9, T10], + ) => void | (() => void), + dependencies: [T1, T2, T3, T4, T5, T6, T7, T8, T9, T10], +): void; + /** * Executes a effect and sends to its callback the previous state. * @@ -93,10 +208,15 @@ export const useWorker = ( * the effect is executed. * @category Effects */ -export const useEffectUpdate = ( +export function useEffectUpdate( effect: (oldState: Dependencies) => void | (() => void), dependencies: Dependencies, -) => { +): void; + +export function useEffectUpdate( + effect: (oldState: Dependencies) => void | (() => void), + dependencies: Dependencies, +): void { const oldState = useRef([] as any); useEffect(() => { try { @@ -105,7 +225,7 @@ export const useEffectUpdate = ( oldState.current = dependencies; } }, dependencies); -}; +} /** * Conditionally executes an effect. From a6955ca64a79a0b1783648a4f22764cf40ffce02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=A1vio=20Caetano?= Date: Mon, 25 Nov 2019 17:08:18 -0300 Subject: [PATCH 2/2] fix(useConditionalEffect): add overloads with generic types fix #14 --- src/index.tsx | 153 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 150 insertions(+), 3 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index e6aaa13..51bb893 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -227,6 +227,148 @@ export function useEffectUpdate( }, dependencies); } +/** + * Conditionally executes an effect. + * + * @param evalCondition A function that receives the dependencies' previous state as argument and + * returns a boolean defining if the effect should be executed. + * @param effect The effect callback to be executed. + * @param dependencies The effect's dependencies. + */ +export function useConditionalEffect( + evalCondition: (oldState: [T1]) => boolean, + effect: () => (() => void) | void, + dependencies: [T1], +): void; + +/** + * Conditionally executes an effect. + * + * @param evalCondition A function that receives the dependencies' previous state as argument and + * returns a boolean defining if the effect should be executed. + * @param effect The effect callback to be executed. + * @param dependencies The effect's dependencies. + */ +export function useConditionalEffect( + evalCondition: (oldState: [T1, T2]) => boolean, + effect: () => (() => void) | void, + dependencies: [T1, T2], +): void; + +/** + * Conditionally executes an effect. + * + * @param evalCondition A function that receives the dependencies' previous state as argument and + * returns a boolean defining if the effect should be executed. + * @param effect The effect callback to be executed. + * @param dependencies The effect's dependencies. + */ +export function useConditionalEffect( + evalCondition: (oldState: [T1, T2, T3]) => boolean, + effect: () => (() => void) | void, + dependencies: [T1, T2, T3], +): void; + +/** + * Conditionally executes an effect. + * + * @param evalCondition A function that receives the dependencies' previous state as argument and + * returns a boolean defining if the effect should be executed. + * @param effect The effect callback to be executed. + * @param dependencies The effect's dependencies. + */ +export function useConditionalEffect( + evalCondition: (oldState: [T1, T2, T3, T4]) => boolean, + effect: () => (() => void) | void, + dependencies: [T1, T2, T3, T4], +): void; + +/** + * Conditionally executes an effect. + * + * @param evalCondition A function that receives the dependencies' previous state as argument and + * returns a boolean defining if the effect should be executed. + * @param effect The effect callback to be executed. + * @param dependencies The effect's dependencies. + */ +export function useConditionalEffect( + evalCondition: (oldState: [T1, T2, T3, T4, T5]) => boolean, + effect: () => (() => void) | void, + dependencies: [T1, T2, T3, T4, T5], +): void; + +/** + * Conditionally executes an effect. + * + * @param evalCondition A function that receives the dependencies' previous state as argument and + * returns a boolean defining if the effect should be executed. + * @param effect The effect callback to be executed. + * @param dependencies The effect's dependencies. + */ +export function useConditionalEffect( + evalCondition: (oldState: [T1, T2, T3, T4, T5, T6]) => boolean, + effect: () => (() => void) | void, + dependencies: [T1, T2, T3, T4, T5, T6], +): void; + +/** + * Conditionally executes an effect. + * + * @param evalCondition A function that receives the dependencies' previous state as argument and + * returns a boolean defining if the effect should be executed. + * @param effect The effect callback to be executed. + * @param dependencies The effect's dependencies. + */ +export function useConditionalEffect( + evalCondition: (oldState: [T1, T2, T3, T4, T5, T6, T7]) => boolean, + effect: () => (() => void) | void, + dependencies: [T1, T2, T3, T4, T5, T6, T7], +): void; + +/** + * Conditionally executes an effect. + * + * @param evalCondition A function that receives the dependencies' previous state as argument and + * returns a boolean defining if the effect should be executed. + * @param effect The effect callback to be executed. + * @param dependencies The effect's dependencies. + */ +export function useConditionalEffect( + evalCondition: (oldState: [T1, T2, T3, T4, T5, T6, T7, T8]) => boolean, + effect: () => (() => void) | void, + dependencies: [T1, T2, T3, T4, T5, T6, T7, T8], +): void; + +/** + * Conditionally executes an effect. + * + * @param evalCondition A function that receives the dependencies' previous state as argument and + * returns a boolean defining if the effect should be executed. + * @param effect The effect callback to be executed. + * @param dependencies The effect's dependencies. + */ +export function useConditionalEffect( + evalCondition: (oldState: [T1, T2, T3, T4, T5, T6, T7, T8, T9]) => boolean, + effect: () => (() => void) | void, + dependencies: [T1, T2, T3, T4, T5, T6, T7, T8, T9], +): void; + +/** + * Conditionally executes an effect. + * + * @param evalCondition A function that receives the dependencies' previous state as argument and + * returns a boolean defining if the effect should be executed. + * @param effect The effect callback to be executed. + * @param dependencies The effect's dependencies. + */ +export function useConditionalEffect( + evalCondition: ( + oldState: [T1, T2, T3, T4, T5, T6, T7, T8, T9, T10], + ) => boolean, + effect: () => (() => void) | void, + dependencies: [T1, T2, T3, T4, T5, T6, T7, T8, T9, T10], +): void; + /** * Conditionally executes an effect. * @@ -238,16 +380,21 @@ export function useEffectUpdate( * the effect is evaluating if it should be executed. * @category Effects */ -export const useConditionalEffect = ( +export function useConditionalEffect( evalCondition: (oldState: Dependencies) => boolean, effect: () => (() => void) | void, dependencies: Dependencies, -) => { +): void; +export function useConditionalEffect( + evalCondition: (oldState: Dependencies) => boolean, + effect: () => (() => void) | void, + dependencies: Dependencies, +): void { useEffectUpdate( oldState => (evalCondition(oldState) ? effect() : undefined), dependencies, ); -}; +} /** * Runs an effect when the component gets mounted.