Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/engine/src/ecs/functions/ComponentFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,16 +276,20 @@ export const getOrAddComponent = <C extends Component>(entity: Entity, component
return hasComponent(entity, component) ? getComponent(entity, component) : addComponent(entity, component, args)
}

export const removeComponent = <C extends Component>(entity: Entity, component: C) => {
export const removeComponent = async <C extends Component>(entity: Entity, component: C) => {
if (!hasComponent(entity, component)) return
component.existenceMap[entity].set(false)
component.onRemove(entity, component.stateMap[entity]!)
bitECS.removeComponent(Engine.instance, component, entity, false)
component.stateMap[entity]?.set(none)
delete component.valueMap[entity]
const root = component.reactorMap.get(entity)
if (root?.isRunning) root?.stop()
component.reactorMap.delete(entity)
// we need to wait for the reactor to stop before removing the state, otherwise
// we can trigger errors in useEffect cleanup functions
if (root?.isRunning) await root?.stop()
// NOTE: we may need to perform cleanup after a timeout here in case there
// are other reactors also referencing this state in their cleanup functions
if (!hasComponent(entity, component)) component.stateMap[entity]?.set(none)
}

export const getAllComponents = (entity: Entity): Component[] => {
Expand Down