-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-hooks): implemented hooks for react 16.8+ to use with tao.js
affects: @tao.js/react useTaoXxxHandler and useTaoDataContext provide conveniences for functional components ISSUES CLOSED: #26
- Loading branch information
Showing
3 changed files
with
88 additions
and
2 deletions.
There are no files selected for viewing
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
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
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); | ||
} |
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