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 {gatherComponents} from '@latus/react';
import EntityController from './controllers/entity'; import EntityController from './controllers/entity';
@ -11,6 +12,9 @@ export default {
'@avocado/resource/persea.controllers': () => [ '@avocado/resource/persea.controllers': () => [
EntityController, EntityController,
], ],
'@avocado/resource/resources.decorate': decorateWithLatus(
require.context('./resources/decorators', false, /\.js$/),
),
'@avocado/traits/components': gatherComponents( '@avocado/traits/components': gatherComponents(
require.context('./traits', false, /\.jsx$/), 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 {join} from 'path';
import {Number} from '@avocado/persea'; import {Number} from '@avocado/persea';
import {Context} from '@avocado/behavior';
import {Condition, Expressions} from '@avocado/behavior/persea'; import {Condition, Expressions} from '@avocado/behavior/persea';
import { import {
hot, hot,
PropTypes, PropTypes,
React, React,
useEffect, useEffect,
useLatus,
useState, useState,
} from '@latus/react'; } from '@latus/react';
import {useJsonPatcher} from '@avocado/resource/persea'; import {useJsonPatcher} from '@avocado/resource/persea';
@ -19,19 +17,10 @@ const Alive = ({
json, json,
path, path,
}) => { }) => {
const latus = useLatus();
const patch = useJsonPatcher(); const patch = useJsonPatcher();
const createContextWithEntity = () => ( const [context, setContext] = useState(entity.createContext());
new Context(
{
entity,
},
latus,
)
);
const [context, setContext] = useState(createContextWithEntity(entity));
useEffect(() => { useEffect(() => {
setContext(createContextWithEntity(entity)); setContext(entity.createContext());
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [entity]); }, [entity]);
return ( return (
@ -55,7 +44,6 @@ const Alive = ({
path={join(path, 'params/deathActions')} path={join(path, 'params/deathActions')}
/> />
</div> </div>
</div> </div>
</div> </div>
<label> <label>
@ -80,7 +68,7 @@ Alive.displayName = 'Alive';
Alive.propTypes = { Alive.propTypes = {
entity: PropTypes.shape({ entity: PropTypes.shape({
context: PropTypes.shape({}), createContext: PropTypes.func,
}).isRequired, }).isRequired,
json: PropTypes.shape({ json: PropTypes.shape({
params: PropTypes.shape({ params: PropTypes.shape({

View File

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

View File

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

View File

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

View File

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

View File

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