Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
SourceComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelEstes committed Aug 15, 2024
1 parent fdd8950 commit f1db8ac
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
4 changes: 2 additions & 2 deletions packages/ecs/src/ComponentFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export interface ComponentPartial<
* `@todo` Explain what reactive is in this context
* `@todo` Explain this function
*/
reactor?: any // previously <React.FC> breaks types
reactor?: React.FC<any>
/**
* @todo Explain ComponentPartial.errors[]
*/
Expand Down Expand Up @@ -161,7 +161,7 @@ export interface Component<
toJSON: (entity: Entity, component: State<ComponentType>) => JSON
onSet: (entity: Entity, component: State<ComponentType>, json?: SetJSON) => void
onRemove: (entity: Entity, component: State<ComponentType>) => void
reactor?: any
reactor?: React.FC<any>
reactorMap: Map<Entity, ReactorRoot>
stateMap: Record<Entity, State<ComponentType> | undefined>
errors: ErrorTypes[]
Expand Down
28 changes: 19 additions & 9 deletions packages/engine/src/scene/components/SourceComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ All portions of the code written by the Ethereal Engine team are Copyright © 20
Ethereal Engine. All Rights Reserved.
*/

import { defineComponent } from '@etherealengine/ecs/src/ComponentFunctions'
import { useEntityContext } from '@etherealengine/ecs'
import { defineComponent, useComponent } from '@etherealengine/ecs/src/ComponentFunctions'
import { Entity } from '@etherealengine/ecs/src/Entity'
import { hookstate, none } from '@etherealengine/hyperflux'
import { useLayoutEffect } from 'react'

const entitiesBySource = {} as Record<string, Entity[]>

Expand All @@ -49,15 +51,23 @@ export const SourceComponent = defineComponent({
}
},

onRemove: (entity, component) => {
const src = component.value
reactor: () => {
const entity = useEntityContext()
const sourceComponent = useComponent(entity, SourceComponent)

const entities = SourceComponent.entitiesBySource[src].filter((currentEntity) => currentEntity !== entity)
if (entities.length === 0) {
SourceComponent.entitiesBySourceState[src].set(none)
} else {
SourceComponent.entitiesBySourceState[src].set(entities)
}
useLayoutEffect(() => {
const source = sourceComponent.value
return () => {
const entities = SourceComponent.entitiesBySource[source].filter((currentEntity) => currentEntity !== entity)
if (entities.length === 0) {
SourceComponent.entitiesBySourceState[source].set(none)
} else {
SourceComponent.entitiesBySourceState[source].set(entities)
}
}
}, [])

return null
},

entitiesBySourceState: hookstate(entitiesBySource),
Expand Down
4 changes: 4 additions & 0 deletions packages/spatial/src/xr/XRComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ export const XRAnchorComponent = defineComponent({
anchor?.delete()
}
}, [xrAnchorComponent.anchor])

return null
}
})

Expand Down Expand Up @@ -340,5 +342,7 @@ export const XRSpaceComponent = defineComponent({
setComponent(entity, EntityTreeComponent, { parentEntity })
setComponent(entity, TransformComponent)
}, [])

return null
}
})
2 changes: 2 additions & 0 deletions packages/spatial/src/xrui/components/XRUIComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,7 @@ export const XRUIComponent = defineComponent({
xrui.destroy()
}
}, [])

return null
}
})

0 comments on commit f1db8ac

Please sign in to comment.