Skip to content

Commit

Permalink
fix: Gltf removal
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoecheza committed Jun 7, 2023
1 parent 3d91501 commit c2db311
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export default withSdk<Props>(
[files]
)

const handleRemove = useCallback(() => {
sdk.operations.removeComponent(entity, GltfContainer.componentId)
sdk.operations.dispatch()
const handleRemove = useCallback(async () => {
sdk.operations.removeComponent(entity, GltfContainer)
await sdk.operations.dispatch()
}, [])
const handleDrop = useCallback(async (src: string) => {
const { operations } = sdk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default withSdk<Props>(
const { handleAction } = useContextMenu()

const handleRemove = useCallback(() => {
sdk.operations.removeComponent(entity, Transform.componentId)
sdk.operations.removeComponent(entity, Transform)
sdk.operations.dispatch()
}, [])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ export const putGltfContainerComponent: ComponentOperation = (entity, component)
const currentValue = entity.ecsComponentValues.gltfContainer
entity.ecsComponentValues.gltfContainer = newValue || undefined

// for simplicity of the example, we will remove the Gltf on every update.
if (newValue && currentValue?.src !== newValue?.src) {
removeGltf(entity)
loadGltf(entity, newValue.src)
}
const shouldLoadGltf = newValue && currentValue?.src !== newValue?.src
const shouldRemoveGltf = !newValue || shouldLoadGltf

if (shouldRemoveGltf) removeGltf(entity)
if (shouldLoadGltf) loadGltf(entity, newValue.src)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Entity, IEngine } from '@dcl/ecs'
import { Entity, IEngine, LastWriteWinElementSetComponentDefinition } from '@dcl/ecs'
import removeComponent from './remove-component'

describe('removeComponent', () => {
Expand All @@ -19,38 +19,22 @@ describe('removeComponent', () => {
})
describe('and then passing the entity and the component id to the removeComponent operation', () => {
let entity: Entity
let componentId: number
let removeComponentOperation: ReturnType<typeof removeComponent>
const deleteFromMock = jest.fn()
const componentMock = {
createOrReplace: jest.fn(),
let component = {
deleteFrom: deleteFromMock
} as unknown
} as unknown as LastWriteWinElementSetComponentDefinition<unknown>
beforeEach(() => {
entity = 0 as Entity
componentId = 0
removeComponentOperation = removeComponent(engine)
getComponentMock.mockReturnValue(componentMock)
})
afterEach(() => {
getComponentMock.mockReset()
deleteFromMock.mockReset()
})
it('should add the component to the entity', () => {
removeComponentOperation(entity, componentId)
it('should remove the component to the entity', () => {
removeComponentOperation(entity, component)
expect(deleteFromMock).toHaveBeenCalledWith(entity)
})
describe('and the component is not an LWW component', () => {
beforeEach(() => {
getComponentMock.mockReturnValue({})
})
afterEach(() => {
getComponentMock.mockReset()
})
it('should throw an error', () => {
expect(() => removeComponentOperation(entity, componentId)).toThrowError()
})
})
})
})
})
14 changes: 4 additions & 10 deletions packages/@dcl/inspector/src/lib/sdk/operations/remove-component.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { Entity, IEngine } from '@dcl/ecs'
import { isLastWriteWinComponent } from '../../../hooks/sdk/useComponentValue'
import { Entity, IEngine, LastWriteWinElementSetComponentDefinition } from '@dcl/ecs'

export function removeComponent(engine: IEngine) {
return function removeComponent(entity: Entity, componentId: number) {
const component = engine.getComponent(componentId)
if (isLastWriteWinComponent(component)) {
component.deleteFrom(entity)
} else {
throw new Error('Cannot add component: it must be an LWW component')
}
export function removeComponent(_engine: IEngine) {
return function removeComponent<T>(entity: Entity, component: LastWriteWinElementSetComponentDefinition<T>) {
component.deleteFrom(entity)
}
}

Expand Down

0 comments on commit c2db311

Please sign in to comment.