Skip to content

Commit

Permalink
feat(react-hooks): implemented hooks for react 16.8+ to use with tao.js
Browse files Browse the repository at this point in the history
affects: @tao.js/react

useTaoXxxHandler and useTaoDataContext provide conveniences
for functional components

ISSUES CLOSED: #26
  • Loading branch information
eudaimos committed Feb 24, 2021
1 parent 92674d9 commit f505060
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/react-tao/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }));
}
81 changes: 81 additions & 0 deletions packages/react-tao/src/hooks.js
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 1 addition & 0 deletions packages/react-tao/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit f505060

Please sign in to comment.