diff --git a/packages/react-tao/src/helpers.js b/packages/react-tao/src/helpers.js index 37f669a..0a778ae 100644 --- a/packages/react-tao/src/helpers.js +++ b/packages/react-tao/src/helpers.js @@ -8,8 +8,12 @@ export function normalizeAC({ t, term, a, action, o, orient }) { }; } -export const cleanInput = ({ term, action, orient }) => { +export function cleanInput({ term, action, orient }) { const incoming = { term, action, orient }; Object.keys(incoming).forEach(k => incoming[k] == null && delete incoming[k]); return incoming; -}; +} + +export function normalizeClean({ t, term, a, action, o, orient }) { + return cleanInput(normalizeAC({ t, term, a, action, o, orient })); +} diff --git a/packages/react-tao/src/hooks.js b/packages/react-tao/src/hooks.js new file mode 100644 index 0000000..e5c78b3 --- /dev/null +++ b/packages/react-tao/src/hooks.js @@ -0,0 +1,81 @@ +import { useContext, useEffect, useLayoutEffect } from 'react'; +import { Context } from './Provider'; +import { normalizeClean } from './helpers'; + +function getPermutations({ t, term, a, action, o, orient }) { + const trigram = normalizeClean({ t, term, a, action, o, orient }); + return cartesian(trigram); +} + +function handlerForPermutations(taoHandle, permutations, handler) { + if (permutations.length) { + permutations.forEach(({ term, action, orient }) => + taoHandle({ term, action, orient }, handler) + ); + } +} + +function useTaoEffect( + handlerType, + { t, term, a, action, o, orient }, + handler, + dependencies +) { + const [addingHandler, removingHandler] = [ + `add${handlerType}Handler`, + `remove${handlerType}Handler` + ]; + const { TAO } = useContext(Context); + const permutations = getPermutations({ t, term, a, action, o, orient }); + useEffect(() => { + handlerForPermutations(TAO[addingHandler], permutations, handler); + return () => { + handlerForPermutations(TAO[removingHandler], permutations, handler); + }; + }, dependencies); +} + +export function useTaoInlineHandler( + { t, term, a, action, o, orient }, + handler, + dependencies +) { + useTaoEffect( + 'Inline', + { t, term, a, action, o, orient }, + handler, + dependencies + ); +} + +export function useTaoAsyncHandler( + { t, term, a, action, o, orient }, + handler, + dependencies +) { + useTaoEffect( + 'Async', + { t, term, a, action, o, orient }, + handler, + dependencies + ); +} + +export function useTaoInterceptHandler( + { t, term, a, action, o, orient }, + handler, + dependencies +) { + useTaoEffect( + 'Intercept', + { t, term, a, action, o, orient }, + handler, + dependencies + ); +} + +export function useTaoDataContext(name) { + const { getDataContext } = useContext(Context); + const dataContext = getDataContext(name); + return useContext(dataContext); +} diff --git a/packages/react-tao/src/index.js b/packages/react-tao/src/index.js index 91249a1..d6c8d6d 100644 --- a/packages/react-tao/src/index.js +++ b/packages/react-tao/src/index.js @@ -5,3 +5,4 @@ export { default as RenderHandler } from './RenderHandler'; export { default as SwitchHandler } from './SwitchHandler'; export { default as createContextHandler } from './createContextHandler'; export { default as withContext } from './withContext'; +export * from './hooks';