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 Script cleanup in useScript hook #4

Merged
merged 1 commit into from
Oct 31, 2024
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
2 changes: 1 addition & 1 deletion packages/examples/src/utils/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const useAsset = (src, type, props) => {
const app = useApp();

return useQuery({
queryKey: [src],
queryKey: [app.root?.getGuid(), src, type, props],
queryFn: () => app && fetchAsset(app, src, type, props)
})
}
Expand Down
7 changes: 4 additions & 3 deletions packages/lib/src/Container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const Container: FC<ContainerProps> = ({ asset, ...props }) => {
const app = useApp();

useLayoutEffect(() => {
if (asset && entityRef.current) {
if (app && asset?.resource && entityRef.current) {
const assetEntity = asset.resource.instantiateRenderEntity();
entityRef.current.addChild(assetEntity);
assetEntityRef.current = assetEntity;
Expand All @@ -26,13 +26,14 @@ export const Container: FC<ContainerProps> = ({ asset, ...props }) => {

entityRef.current.removeChild(assetEntityRef.current);
assetEntityRef.current.destroy();
asset.resource?.destroy();

entityRef.current = null;
assetEntityRef.current = null;

};
}, [app, parent, asset]);
}, [app, parent, asset, asset.resource]);

if (!asset) return null;

return <Entity ref={entityRef} {...props}/>;
};
8 changes: 6 additions & 2 deletions packages/lib/src/hooks/use-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ export const useComponent = (ctype: string, props: ComponentProps): void => {
}

return () => {
if (componentRef.current) {
const comp = componentRef.current
componentRef.current = null;

if(!app || !app.root) return;

if (comp) {
type SystemKeys = keyof typeof app.systems;
if (app.systems[ctype as SystemKeys]) parent.removeComponent(ctype);
componentRef.current = null;
}
};
}, [app, parent, ctype]);
Expand Down
13 changes: 6 additions & 7 deletions packages/lib/src/hooks/use-script.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,16 @@ export const useScript = (ScriptConstructor: typeof Script, props: Props) : void

// Cleanup function to remove the script when the component is unmounted
return () => {
if(!app) return;
const scriptComponent = scriptComponentRef.current;
const script = scriptRef.current;
if (script && scriptComponent) {
scriptComponent.destroy(scriptRef.current);
} else {
console.log('Script not found on parent entity', parent?.script, scriptRef.current);
script.fire('destroy');
}
scriptRef.current = null;
scriptComponentRef.current = null;

if (app && app.root && script && scriptComponent) {
scriptComponent.destroy(script);
} else if (script) {
script.fire('destroy');
}
};
}, [app, parent, ScriptConstructor]);

Expand Down
16 changes: 8 additions & 8 deletions packages/lib/src/scripts/orbit-controls/orbit-camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,10 @@ export class OrbitCameraInputMouse extends Script {

// Remove the listeners so if this entity is destroyed
this.on('destroy', function () {
this.app.mouse.off(EVENT_MOUSEDOWN, this.onMouseDown, this);
this.app.mouse.off(EVENT_MOUSEUP, this.onMouseUp, this);
this.app.mouse.off(EVENT_MOUSEMOVE, this.onMouseMove, this);
this.app.mouse.off(EVENT_MOUSEWHEEL, this.onMouseWheel, this);
this.app.mouse?.off(EVENT_MOUSEDOWN, this.onMouseDown, this);
this.app.mouse?.off(EVENT_MOUSEUP, this.onMouseUp, this);
this.app.mouse?.off(EVENT_MOUSEMOVE, this.onMouseMove, this);
this.app.mouse?.off(EVENT_MOUSEWHEEL, this.onMouseWheel, this);

window.removeEventListener('mouseout', onMouseOut, false);
});
Expand Down Expand Up @@ -619,10 +619,10 @@ export class OrbitCameraInputTouch extends Script {
this.app.touch.on(EVENT_TOUCHMOVE, this.onTouchMove, this);

this.on('destroy', function () {
this.app.touch.off(EVENT_TOUCHSTART, this.onTouchStartEndCancel, this);
this.app.touch.off(EVENT_TOUCHEND, this.onTouchStartEndCancel, this);
this.app.touch.off(EVENT_TOUCHCANCEL, this.onTouchStartEndCancel, this);
this.app.touch.off(EVENT_TOUCHMOVE, this.onTouchMove, this);
this.app.touch?.off(EVENT_TOUCHSTART, this.onTouchStartEndCancel, this);
this.app.touch?.off(EVENT_TOUCHEND, this.onTouchStartEndCancel, this);
this.app.touch?.off(EVENT_TOUCHCANCEL, this.onTouchStartEndCancel, this);
this.app.touch?.off(EVENT_TOUCHMOVE, this.onTouchMove, this);
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/src/scripts/post/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ class CameraFrame extends Script {
this.cameraComponent.renderPasses?.forEach((renderPass) => {
renderPass.destroy();
});
this.cameraComponent.renderPasses = [];
if (this.app.system) this.cameraComponent.renderPasses = [];
this.cameraComponent.rendering = null;

this.cameraComponent.jitter = 0;
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/src/scripts/shadow-catcher/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class ShadowCatcher extends Script {
this.material.destroy();
layers.remove(this.layer);

const camera = this.app.root.findOne(node => node?.camera?.enabled)?.camera;
const camera = this.app.root?.findOne(node => node?.camera?.enabled)?.camera;
if (!camera) return;

camera.layers = camera.layers.filter(layer => layer !== this.layer.id);
Expand Down
1 change: 1 addition & 0 deletions packages/lib/src/utils/picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export const usePicker = (app: AppBase | null, el: HTMLElement | null) => {

el.addEventListener('pointerup', onInteractionEvent);
el.addEventListener('pointerdown', onInteractionEvent);
el.addEventListener('mouseup', onInteractionEvent);
el.addEventListener('click', onInteractionEvent);
el.addEventListener('pointermove', onPointerMove);
app.on('update', onFrameUpdate);
Expand Down