1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | 4x 3x 2x 3x 2x 2x 3x 1x 2x 1x | import { useContext, useEffect, useLayoutEffect } from 'react'; import { Context } from './Provider'; import { getPermutations } from './helpers'; export function useTaoContext() { const { TAO } = useContext(Context); return TAO; } function useTaoEffect( handlerType, { t, term, a, action, o, orient }, handler, dependencies ) { const [addingHandler, removingHandler] = [ `add${handlerType}Handler`, `remove${handlerType}Handler` ]; const TAO = useTaoContext(); const permutations = getPermutations({ t, term, a, action, o, orient }); useEffect(() => { permutations.forEach(trigram => TAO[addingHandler](trigram, handler)); return () => { permutations.forEach(trigram => TAO[removingHandler](trigram, 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); if (!dataContext) { return; } return useContext(dataContext); } |