From 0c8ec0b1d220d5c777c57d8d1156c8a15b522d20 Mon Sep 17 00:00:00 2001 From: cha0s Date: Sun, 21 Jun 2020 00:50:33 -0500 Subject: [PATCH] refactor: type rendering --- src/client/components/persea.jsx | 21 +++- src/client/components/traits.jsx | 35 ++---- .../types/actions.type-renderer.jsx | 11 +- .../components/types/condition.raw.scss | 2 +- .../types/condition.type-renderer.jsx | 9 +- src/client/components/types/literal.raw.scss | 9 ++ .../types/literal.type-renderer.jsx | 58 ++++++++++ .../components/types/property-prop-types.js | 2 + .../types/routines.type-renderer.jsx | 2 +- src/client/components/types/steps.jsx | 105 ------------------ .../{steps.raw.scss => traversal.raw.scss} | 2 +- .../types/traversal.type-renderer.jsx | 96 ++++++++++++++++ src/client/components/types/typing.js | 58 ++++++---- src/client/components/types/value.raw.scss | 9 -- .../components/types/value.type-renderer.jsx | 80 ++++--------- src/client/context/typeRenderers.js | 3 + src/client/hooks/useTypeRenderers.js | 5 + .../types => }/type-renderers.scwp.js | 0 18 files changed, 264 insertions(+), 243 deletions(-) create mode 100644 src/client/components/types/literal.raw.scss create mode 100644 src/client/components/types/literal.type-renderer.jsx delete mode 100644 src/client/components/types/steps.jsx rename src/client/components/types/{steps.raw.scss => traversal.raw.scss} (97%) create mode 100644 src/client/components/types/traversal.type-renderer.jsx create mode 100644 src/client/context/typeRenderers.js create mode 100644 src/client/hooks/useTypeRenderers.js rename src/client/{components/types => }/type-renderers.scwp.js (100%) diff --git a/src/client/components/persea.jsx b/src/client/components/persea.jsx index f260613..4a1d6d8 100644 --- a/src/client/components/persea.jsx +++ b/src/client/components/persea.jsx @@ -1,10 +1,23 @@ import {hot} from 'react-hot-loader'; -import React from 'react'; +import React, {useState} from 'react'; + +import TypeRenderersContext from '~/client/context/typeRenderers'; +import {all as allTypeRenderers} from '~/client/type-renderers.scwp'; import Entity from './entity'; -const Persea = () => ( - -); +const typeRenderMap = () => Object.values(allTypeRenderers()).reduce((r, M) => { + const {default: {type, Component}} = M; + return {...r, [type]: Component}; +}, {}); + +const Persea = () => { + const [typeRenderers] = useState(typeRenderMap()); + return ( + + + + ); +}; export default hot(module)(Persea); diff --git a/src/client/components/traits.jsx b/src/client/components/traits.jsx index d908dd4..57d452f 100644 --- a/src/client/components/traits.jsx +++ b/src/client/components/traits.jsx @@ -15,7 +15,7 @@ import { import {createSelector} from '@reduxjs/toolkit'; import {deregisterHooks, registerHooks} from 'scwp'; -import {all as allTypeRenderers} from './types/type-renderers.scwp'; +import Value from './types/value.type-renderer'; const SCROLL_MAG = 80; @@ -43,20 +43,10 @@ const ensureTraitComponents = () => { }, {}); } }; -let TypeRenderers; -const ensureTypeRenderers = () => { - if (!TypeRenderers) { - TypeRenderers = Object.values(allTypeRenderers()).reduce((r, M) => { - const {default: {type, Component}} = M; - return {...r, [type]: Component}; - }, {}); - } -}; const makeTabSelector = (context, type) => createSelector( (_) => _, (trait) => { - ensureTypeRenderers(); ensureTraitComponents(); const {params: paramsRaw, state: stateRaw} = trait; const {[type]: TraitComponent} = TraitComponents; @@ -68,27 +58,19 @@ const makeTabSelector = (context, type) => createSelector( const renderComponents = (description, values) => ( // eslint-disable-next-line no-shadow Object.values(mapObject(description, (description, key) => { - const {label, options, type: componentType} = description; - const Component = TypeRenderers[componentType]; + const {label, options} = description; return ( -