Skip to content

Commit 5d69491

Browse files
authored
fix: dispose and lazy setters (#201)
1 parent 49768e1 commit 5d69491

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/Autofocus.tsx renamed to src/effects/Autofocus.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import React, {
88
forwardRef,
99
useImperativeHandle,
1010
RefObject,
11+
useMemo,
1112
} from 'react'
1213
import { useThree, useFrame, createPortal } from '@react-three/fiber'
1314
import { CopyPass, DepthPickingPass, DepthOfFieldEffect } from 'postprocessing'
14-
import { DepthOfField, EffectComposerContext } from './index'
15+
import { DepthOfField, EffectComposerContext } from '..'
1516
import { easing } from 'maath'
1617

1718
export type AutofocusProps = typeof DepthOfField & {
@@ -42,8 +43,8 @@ export const Autofocus = forwardRef<AutofocusApi, AutofocusProps>(
4243
const { composer, camera } = useContext(EffectComposerContext)
4344

4445
// see: https://codesandbox.io/s/depthpickingpass-x130hg
45-
const [depthPickingPass] = useState(new DepthPickingPass())
46-
const [copyPass] = useState(new CopyPass())
46+
const [depthPickingPass] = useState(() => new DepthPickingPass())
47+
const [copyPass] = useState(() => new CopyPass())
4748
useEffect(() => {
4849
composer.addPass(depthPickingPass)
4950
composer.addPass(copyPass)
@@ -53,9 +54,16 @@ export const Autofocus = forwardRef<AutofocusApi, AutofocusProps>(
5354
}
5455
}, [composer, depthPickingPass, copyPass])
5556

56-
const [hitpoint] = useState(new THREE.Vector3(0, 0, 0))
57+
useEffect(() => {
58+
return () => {
59+
depthPickingPass.dispose()
60+
copyPass.dispose()
61+
}
62+
}, [depthPickingPass, copyPass])
63+
64+
const [hitpoint] = useState(() => new THREE.Vector3(0, 0, 0))
5765

58-
const [ndc] = useState(new THREE.Vector3(0, 0, 0))
66+
const [ndc] = useState(() => new THREE.Vector3(0, 0, 0))
5967
const getHit = useCallback(
6068
async (x: number, y: number) => {
6169
ndc.x = x
@@ -104,15 +112,15 @@ export const Autofocus = forwardRef<AutofocusApi, AutofocusProps>(
104112
})
105113

106114
// Ref API
107-
useImperativeHandle(
108-
fref,
115+
const api = useMemo<AutofocusApi>(
109116
() => ({
110117
dofRef,
111118
hitpoint,
112119
update,
113120
}),
114121
[hitpoint, update]
115122
)
123+
useImperativeHandle(fref, () => api, [api])
116124

117125
return (
118126
<>

src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './effects/Autofocus'
12
export * from './effects/Bloom'
23
export * from './effects/BrightnessContrast'
34
export * from './effects/ChromaticAberration'
@@ -30,6 +31,5 @@ export * from './effects/TiltShift2'
3031
export * from './effects/SSR'
3132

3233
export * from './Selection'
33-
export * from './Autofocus'
3434
export * from './EffectComposer'
3535
export * from './util'

0 commit comments

Comments
 (0)