diff --git a/packages/entity/src/persea/index.js b/packages/entity/src/persea/index.js index ab470b3..0783e54 100644 --- a/packages/entity/src/persea/index.js +++ b/packages/entity/src/persea/index.js @@ -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$/), ), diff --git a/packages/entity/src/persea/resources/decorators/entity.js b/packages/entity/src/persea/resources/decorators/entity.js new file mode 100644 index 0000000..3ba4f1b --- /dev/null +++ b/packages/entity/src/persea/resources/decorators/entity.js @@ -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; + } + +}; diff --git a/packages/entity/src/persea/traits/alive.jsx b/packages/entity/src/persea/traits/alive.jsx index 5b49096..7fdc99b 100644 --- a/packages/entity/src/persea/traits/alive.jsx +++ b/packages/entity/src/persea/traits/alive.jsx @@ -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')} /> -