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 patch = useJsonPatcher();
const types = latus.get('%behavior-types');
let i = 0;
const {ops, value: expressionValue} = value;
const Renderables = [];
@ -175,10 +176,18 @@ const Expression = ({
}
if (isKey(op)) {
const current = compile({type: 'expression', ops: ops.slice(0, i + 1)}, latus)(context);
description = {
...context.constructor.descriptionFor(current),
...description?.children?.[op.key] || {},
};
description = description.children?.[op.key] || {type: 'undefined'};
const {args, type} = description;
if ('void' !== type) {
description = {
...context.constructor.descriptionFor(
type,
current,
latus,
),
...(args ? {args} : {}),
};
}
if (description.args && (!nextOp || !isInvocation(nextOp))) {
const R = Renderables.pop();
Renderables.push(
@ -199,38 +208,9 @@ const Expression = ({
type: 'invoke',
args: description.vararg
? []
: description.args.map((arg) => {
if ('any' === arg.type) {
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.args.map(
(arg) => (types[arg.type] || types.undefined).create(undefined),
),
},
});
})(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);
if (
description.children

View File

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

View File

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