fix: traits

This commit is contained in:
cha0s 2021-04-07 03:19:29 -05:00
parent d7218e38e5
commit 0899fab01c
8 changed files with 48 additions and 38 deletions

View File

@ -1,3 +1,4 @@
import {decorateWithLatus} from '@latus/core';
import {gatherComponents} from '@latus/react';
import EntityController from './controllers/entity';
@ -11,6 +12,9 @@ export default {
'@avocado/resource/persea.controllers': () => [
EntityController,
],
'@avocado/resource/resources.decorate': decorateWithLatus(
require.context('./resources/decorators', false, /\.js$/),
),
'@avocado/traits/components': gatherComponents(
require.context('./traits', false, /\.jsx$/),
),

View File

@ -0,0 +1,22 @@
import {Context} from '@avocado/behavior';
export default (Entity, latus) => class ContextedEntity extends Entity {
createContext(variables = {}) {
const context = this.context
? this.context.clone()
: new Context(
{
entity: this,
},
latus,
);
const entries = Object.entries(variables);
for (let i = 0; i < entries.length; i++) {
const [key, value] = entries[i];
context.add(key, value);
}
return context;
}
};

View File

@ -2,14 +2,12 @@ import './alive.scss';
import {join} from 'path';
import {Number} from '@avocado/persea';
import {Context} from '@avocado/behavior';
import {Condition, Expressions} from '@avocado/behavior/persea';
import {
hot,
PropTypes,
React,
useEffect,
useLatus,
useState,
} from '@latus/react';
import {useJsonPatcher} from '@avocado/resource/persea';
@ -19,19 +17,10 @@ const Alive = ({
json,
path,
}) => {
const latus = useLatus();
const patch = useJsonPatcher();
const createContextWithEntity = () => (
new Context(
{
entity,
},
latus,
)
);
const [context, setContext] = useState(createContextWithEntity(entity));
const [context, setContext] = useState(entity.createContext());
useEffect(() => {
setContext(createContextWithEntity(entity));
setContext(entity.createContext());
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [entity]);
return (
@ -55,7 +44,6 @@ const Alive = ({
path={join(path, 'params/deathActions')}
/>
</div>
</div>
</div>
<label>
@ -80,7 +68,7 @@ Alive.displayName = 'Alive';
Alive.propTypes = {
entity: PropTypes.shape({
context: PropTypes.shape({}),
createContext: PropTypes.func,
}).isRequired,
json: PropTypes.shape({
params: PropTypes.shape({

View File

@ -1,6 +1,6 @@
import './index.scss';
import {Number} from '@avocado/persea';
import {Number as NumberComponent} from '@avocado/persea';
import {
PropTypes,
React,
@ -15,7 +15,7 @@ const Range = ({
const [isSingle, setIsSingle] = useState('undefined' === typeof range.min);
return (
<div className="range">
<Number
<NumberComponent
integer={integer}
onChange={(event, value) => {
onChange(
@ -51,7 +51,7 @@ const Range = ({
/>
</label>
{!isSingle && (
<Number
<NumberComponent
integer={integer}
onChange={(event, value) => {
onChange(
@ -74,7 +74,7 @@ const createRangePropType = (isRequired) => (props, propName, componentName) =>
`Invalid prop ${propName} suppied to ${componentName}. ${why}.`,
);
const range = props[propName];
if (!range && isRequired) {
if ((null === range || undefined === range) && isRequired) {
return fail('Is required but missing');
}
if (

View File

@ -49,7 +49,8 @@ JsonComponent.displayName = 'JsonComponent';
JsonComponent.propTypes = {
onChange: PropTypes.func.isRequired,
json: PropTypes.shape({}).isRequired,
// eslint-disable-next-line react/forbid-prop-types
json: PropTypes.any.isRequired,
};
export default JsonComponent;

View File

@ -14,6 +14,8 @@
"files": [
"index.js",
"index.js.map",
"persea.js",
"persea.js.map",
"test.js",
"test.js.map"
],

View File

@ -1,9 +1,8 @@
import {join} from 'path';
import {Context} from '@avocado/behavior';
import {Expressions} from '@avocado/behavior/persea';
import fullEntity from '@avocado/entity';
import {JsonComponent} from '@avocado/persea';
import {fullEntity} from '@avocado/entity';
import {Json} from '@avocado/persea';
import {useJsonPatcher} from '@avocado/resource/persea';
import {
PropTypes,
@ -20,18 +19,13 @@ const Collider = ({
}) => {
const latus = useLatus();
const patch = useJsonPatcher();
const [context, setContext] = useState();
const [context, setContext] = useState(entity.createContext());
useEffect(() => {
const createContext = async () => {
const context = new Context(
{
entity,
incident: [0, 0],
other: await fullEntity(latus),
},
latus,
);
setContext(context);
setContext(entity.createContext({
incident: [0, 0],
other: await fullEntity(latus),
}));
};
createContext();
// eslint-disable-next-line react-hooks/exhaustive-deps
@ -40,7 +34,7 @@ const Collider = ({
<div className="collider">
<div className="label">
Collides with groups
<JsonComponent
<Json
onChange={patch.onChange(join(path, 'params/collidesWithGroups'))}
json={json.params.collidesWithGroups}
/>
@ -119,7 +113,7 @@ Collider.displayName = 'Collider';
Collider.propTypes = {
entity: PropTypes.shape({
context: PropTypes.shape({}),
createContext: PropTypes.func,
}).isRequired,
json: PropTypes.shape({
params: PropTypes.shape({

View File

@ -12,13 +12,12 @@ import {
} from '@avocado/math/persea';
import {
hot,
memo,
PropTypes,
React,
useState,
} from '@latus/react';
const Emitted = memo(({json, path}) => {
const Emitted = ({json, path}) => {
const patch = useJsonPatcher();
const [hasSelfPosition, setHasSelfPosition] = useState(null !== json.params.position);
return (
@ -145,7 +144,7 @@ const Emitted = memo(({json, path}) => {
</label>
</div>
);
});
};
Emitted.displayName = 'Emitted';