refactor: context type

This commit is contained in:
cha0s 2021-02-09 16:05:03 -06:00
parent b90873badf
commit 98ff1595da
3 changed files with 50 additions and 56 deletions

View File

@ -22,6 +22,7 @@ const Expression = ({
}) => { }) => {
const latus = useLatus(); const latus = useLatus();
const patch = useJsonPatcher(); const patch = useJsonPatcher();
const types = latus.get('%behavior-types');
let i = 0; let i = 0;
const {ops, value: expressionValue} = value; const {ops, value: expressionValue} = value;
const Renderables = []; const Renderables = [];
@ -175,10 +176,18 @@ const Expression = ({
} }
if (isKey(op)) { if (isKey(op)) {
const current = compile({type: 'expression', ops: ops.slice(0, i + 1)}, latus)(context); const current = compile({type: 'expression', ops: ops.slice(0, i + 1)}, latus)(context);
description = { description = description.children?.[op.key] || {type: 'undefined'};
...context.constructor.descriptionFor(current), const {args, type} = description;
...description?.children?.[op.key] || {}, if ('void' !== type) {
}; description = {
...context.constructor.descriptionFor(
type,
current,
latus,
),
...(args ? {args} : {}),
};
}
if (description.args && (!nextOp || !isInvocation(nextOp))) { if (description.args && (!nextOp || !isInvocation(nextOp))) {
const R = Renderables.pop(); const R = Renderables.pop();
Renderables.push( Renderables.push(
@ -199,38 +208,9 @@ const Expression = ({
type: 'invoke', type: 'invoke',
args: description.vararg args: description.vararg
? [] ? []
: description.args.map((arg) => { : description.args.map(
if ('any' === arg.type) { (arg) => (types[arg.type] || types.undefined).create(undefined),
return {type: 'undefined'}; ),
}
if ('condition' === arg.type) {
return {
...(false === arg.compile ? {compile: false} : {}),
type: 'condition',
operator: 'and',
operands: [
{
type: 'literal',
value: true,
},
{
type: 'literal',
value: true,
},
],
};
}
if ('expressions' === arg.type) {
return {
type: 'expressions',
expressions: [],
};
}
return {
type: 'literal',
value: null,
};
}),
}, },
}); });
})(description, opPath)} })(description, opPath)}
@ -242,6 +222,15 @@ const Expression = ({
); );
} }
} }
if (isInvocation(op)) {
if ('void' !== description.type) {
description = context.constructor.descriptionFor(
description.type,
undefined,
latus,
);
}
}
} while (i++ < ops.length - 1); } while (i++ < ops.length - 1);
if ( if (
description.children description.children

View File

@ -4,6 +4,7 @@ import {Context} from '@avocado/behavior';
import { import {
PropTypes, PropTypes,
React, React,
useLatus,
useState, useState,
} from '@latus/react'; } from '@latus/react';
import { import {
@ -26,26 +27,22 @@ const Literal = ({
value, value,
vararg, vararg,
}) => { }) => {
const latus = useLatus();
const patch = useJsonPatcher(); const patch = useJsonPatcher();
const [selectingType, setSelectingType] = useState(false); const [selectingType, setSelectingType] = useState(false);
const types = latus.get('%behavior-types');
const setLiteralType = (type) => { const setLiteralType = (type) => {
let value = null; const value = (types[type] || types.undefined).create();
switch (type) { if (null !== value) {
case 'number': value = 0; break; patch({
case 'string': value = ''; break; op: 'replace',
case 'array': value = []; break; path,
case 'object': value = {}; break; value: {
case 'vector': value = [0, 0]; break; type: 'literal',
default: value,
},
});
} }
patch({
op: 'replace',
path,
value: {
type: 'literal',
value,
},
});
setSelectingType(false); setSelectingType(false);
}; };
// eslint-disable-next-line react/destructuring-assignment // eslint-disable-next-line react/destructuring-assignment
@ -125,7 +122,13 @@ const Literal = ({
if ('undefined' !== typeof value) { if ('undefined' !== typeof value) {
elements.push( elements.push(
<div className="component" key={join(path, 'component')}> <div className="component" key={join(path, 'component')}>
{valueComponent(path, Context.inferTypeOf(value), {value})} {
valueComponent(
path,
Context.descriptionFor(undefined, value, latus).type,
{value},
)
}
</div>, </div>,
); );
} }
@ -133,7 +136,7 @@ const Literal = ({
} }
} }
}; };
const inferred = Context.inferTypeOf(value.value); const {type: inferred} = Context.descriptionFor(undefined, value.value, latus);
const typeKey = `[literal ${inferred}]`; const typeKey = `[literal ${inferred}]`;
return ( return (
<> <>

View File

@ -29,12 +29,14 @@ const EntityComponent = ({
}) => { }) => {
const latus = useLatus(); const latus = useLatus();
const uri = useUri(); const uri = useUri();
const {Entity} = latus.get('%resources'); const {Entity, EntityList} = latus.get('%resources');
const [entity, setEntity] = useState(); const [entity, setEntity] = useState();
useEffect(() => { useEffect(() => {
setEntity(); setEntity();
const loadEntity = async () => { const loadEntity = async () => {
setEntity(await Entity.load(resource)); const entity = await Entity.load(resource);
entity.list = new EntityList();
setEntity(entity);
}; };
loadEntity(); loadEntity();
return () => { return () => {