From faea819945f79a4f9ccf01863c490a0924168a70 Mon Sep 17 00:00:00 2001 From: cha0s Date: Fri, 19 Jun 2020 17:59:37 -0500 Subject: [PATCH] feat: context types --- src/client/components/entity.jsx | 2 +- .../components/types/condition.raw.scss | 2 +- src/client/components/types/steps-lists.js | 31 ++++++++------- src/client/components/types/steps.jsx | 10 +++-- src/client/components/types/typing.js | 38 ++++++------------- .../components/types/value.type-renderer.jsx | 3 +- 6 files changed, 40 insertions(+), 46 deletions(-) diff --git a/src/client/components/entity.jsx b/src/client/components/entity.jsx index a09d1d8..2169468 100644 --- a/src/client/components/entity.jsx +++ b/src/client/components/entity.jsx @@ -14,7 +14,7 @@ const json = require('~/../fixtures/kitty.entity.json'); const EntityComponent = (props) => { const {context} = props; const entity = new Entity(json); - context.add('entity', entity); + context.add('entity', entity, 'entity'); const {traits} = json; return (
diff --git a/src/client/components/types/condition.raw.scss b/src/client/components/types/condition.raw.scss index 89d442b..1ea66cf 100644 --- a/src/client/components/types/condition.raw.scss +++ b/src/client/components/types/condition.raw.scss @@ -6,7 +6,7 @@ } .operator { - margin: 0.25em 0; + margin: 0; text-align: center; text-align-last: center; } diff --git a/src/client/components/types/steps-lists.js b/src/client/components/types/steps-lists.js index 68cd48d..d9ef3e6 100644 --- a/src/client/components/types/steps-lists.js +++ b/src/client/components/types/steps-lists.js @@ -1,35 +1,40 @@ import {flatten} from '@avocado/core'; +import {Context} from '@avocado/behavior' -import {typeFits, typeFromV} from './typing'; +import {typeFits} from './typing'; -export const variableStepsList = (key, variable, description, type) => { +export const variableStepsList = (key, variable, variableType, type) => { + const description = Context.typeDescription(variableType, variable); const steps = []; - if (typeFits(type, description.type)) { + if (typeFits(type, variableType)) { steps.push({type: 'key', key}); if ('function' === typeof variable) { steps.push({type: 'invoke', args: []}); } } - if (!variable || !variable.contextDescription) { - return steps; + if (!variable) { + return [steps]; } - const {children} = variable.contextDescription(); - const sublists = Object.entries(children) + const sublists = Object.entries(description) .map(([key, description]) => ( - flatten(variableStepsList(key, variable[key], description, type)) + variableStepsList(key, variable[key], description.type, type) )) .map((childLists) => ( - childLists.length > 0 ? [{type: 'key', key}].concat(childLists) : [] + childLists.map((stepsList) => ( + stepsList.length > 0 + ? [{type: 'key', key}].concat(stepsList) + : [] + )) )) - .filter((lists) => lists.length > 0) - return sublists.length > 0 ? sublists : [steps]; + return sublists.length > 0 ? flatten(sublists) : [steps]; }; export const contextStepsList = (context, type) => ( flatten( Object.entries(context.all()) - .map(([key, variable]) => ( - variableStepsList(key, variable, {type: typeFromV(variable)}, type) + .map(([key, [variable, variableType]]) => ( + variableStepsList(key, variable, variableType, type) )) ) + .filter((stepsList) => stepsList.length > 0) ); diff --git a/src/client/components/types/steps.jsx b/src/client/components/types/steps.jsx index 87c78b7..bb1c20d 100644 --- a/src/client/components/types/steps.jsx +++ b/src/client/components/types/steps.jsx @@ -3,7 +3,7 @@ import contempo from 'contempo'; import React, {useState} from 'react'; import {contextStepsList} from './steps-lists'; -import {typeFits, typeFromSteps, typeFromV} from './typing'; +import {typeFits, typeFromSteps, typeFromLiteral} from './typing'; import Value from './value.type-renderer'; const decorate = compose( @@ -19,7 +19,7 @@ const Steps = (props) => { else { stepsList = contextStepsList( context, - value.steps ? typeFromSteps(context, value.steps) : typeFromV(value.value), + value.steps ? typeFromSteps(context, value.steps) : typeFromLiteral(value.value), ); } return ( @@ -64,7 +64,11 @@ const Steps = (props) => {
diff --git a/src/client/components/types/typing.js b/src/client/components/types/typing.js index d44ebdd..b8dec3e 100644 --- a/src/client/components/types/typing.js +++ b/src/client/components/types/typing.js @@ -1,4 +1,6 @@ -export function typeFromV(v) { +import {Context} from '@avocado/behavior' + +export function typeFromLiteral(v) { if ('undefined' === typeof v) { return 'undefined'; } @@ -17,43 +19,27 @@ export function typeFromV(v) { if (v.length && 2 === v.length && v instanceof Array) { return 'vector'; } - if (!v.contextDescription) { - return 'object'; - } - const {type} = v.contextDescription(); - return type; + return 'object'; } export function typeFromSteps(context, steps) { if (!steps || 0 === steps.length) { return 'undefined'; } - const first = context.map.get(steps[0].key); - const [, type] = steps.slice(1).reduce( + const [, finalType] = steps.slice(1).reduce( ([v, type], step, i) => { - if (i === steps.length - 2) { - if (v.contextDescription) { - const {children} = v.contextDescription(); - return [undefined, step.key ? children[step.key].type : type]; - } - else { - return [undefined, type]; - } + const {key} = step; + if (key) { + const description = Context.typeDescription(type, v); + return [v[key], description[key] ? description[key].type : type]; } else { - if (step.key) { - const {children} = v.contextDescription(); - return [v[step.key], children[step.key].type]; - } - else { - return [v, type]; - } + return [v, type]; } - step }, - [first, typeFromV(first)], + context.get(steps[0].key), ); - return type; + return finalType; } export function typeFits(reference, candidate) { diff --git a/src/client/components/types/value.type-renderer.jsx b/src/client/components/types/value.type-renderer.jsx index b828974..ab44226 100644 --- a/src/client/components/types/value.type-renderer.jsx +++ b/src/client/components/types/value.type-renderer.jsx @@ -8,7 +8,7 @@ import Bool from './bool.type-renderer'; import propertyPropTypes from './property-prop-types'; import Steps from './steps'; import {contextStepsList} from './steps-lists'; -import {typeFits, typeFromSteps, typeFromV} from './typing'; +import {typeFits, typeFromSteps} from './typing'; const renderValue = (context, type, value) => { switch (value.type) { @@ -16,7 +16,6 @@ const renderValue = (context, type, value) => { return ; } case 'literal': - // const type = typeFromV(value.value); const stepsList = contextStepsList(context, type); const tierOptions = Object.keys(stepsList.reduce((r, optionSteps) => { if (!optionSteps[0] || !optionSteps[0].key) {