Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Gltf removal #631

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
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
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(),
const 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