Skip to content

Commit

Permalink
Use React.DependencyList instead of any[] for deps
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornstar committed Aug 31, 2021
1 parent 97caf10 commit 17c9731
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 42 deletions.
66 changes: 47 additions & 19 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,87 +144,115 @@ function Physics({

function Debug({ children, color = 'black', scale = 1 }: DebugProps): JSX.Element

function usePlane(fn: GetByIndex<PlaneProps>, fwdRef?: React.Ref<THREE.Object3D>, deps?: any[]): Api
function usePlane(
fn: GetByIndex<PlaneProps>,
fwdRef?: React.Ref<THREE.Object3D>,
deps?: React.DependencyList,
): Api

function useBox(fn: GetByIndex<BoxProps>, fwdRef?: React.Ref<THREE.Object3D>, deps?: any[]): Api
function useBox(
fn: GetByIndex<BoxProps>,
fwdRef?: React.Ref<THREE.Object3D>,
deps?: React.DependencyList,
): Api

function useCylinder(fn: GetByIndex<CylinderProps>, fwdRef?: React.Ref<THREE.Object3D>, deps?: any[]): Api
function useCylinder(
fn: GetByIndex<CylinderProps>,
fwdRef?: React.Ref<THREE.Object3D>,
deps?: React.DependencyList,
): Api

function useHeightfield(
fn: GetByIndex<HeightfieldProps>,
fwdRef?: React.Ref<THREE.Object3D>,
deps?: any[],
deps?: React.DependencyList,
): Api

function useParticle(fn: GetByIndex<ParticleProps>, fwdRef?: React.Ref<THREE.Object3D>, deps?: any[]): Api
function useParticle(
fn: GetByIndex<ParticleProps>,
fwdRef?: React.Ref<THREE.Object3D>,
deps?: React.DependencyList,
): Api

function useSphere(fn: GetByIndex<SphereProps>, fwdRef?: React.Ref<THREE.Object3D>, deps?: any[]): Api
function useSphere(
fn: GetByIndex<SphereProps>,
fwdRef?: React.Ref<THREE.Object3D>,
deps?: React.DependencyList,
): Api

function useTrimesh(fn: GetByIndex<TrimeshProps>, fwdRef?: React.Ref<THREE.Object3D>, deps?: any[]): Api
function useTrimesh(
fn: GetByIndex<TrimeshProps>,
fwdRef?: React.Ref<THREE.Object3D>,
deps?: React.DependencyList,
): Api

function useConvexPolyhedron(
fn: GetByIndex<ConvexPolyhedronProps>,
fwdRef?: React.Ref<THREE.Object3D>,
deps?: any[],
deps?: React.DependencyList,
): Api

function useCompoundBody(
fn: GetByIndex<CompoundBodyProps>,
fwdRef?: React.Ref<THREE.Object3D>,
deps?: any[],
deps?: React.DependencyList,
): Api

function useRaycastVehicle(
fn: () => RaycastVehicleProps,
fwdRef?: React.Ref<THREE.Object3D>,
deps: any[] = [],
deps: React.DependencyList[] = [],
): [React.RefObject<THREE.Object3D>, RaycastVehiclePublicApi]

function usePointToPointConstraint(
bodyA: React.Ref<THREE.Object3D>,
bodyB: React.Ref<THREE.Object3D>,
optns: PointToPointConstraintOpts,
deps: any[] = [],
deps: React.DependencyList = [],
): ConstraintApi

function useConeTwistConstraint(
bodyA: React.Ref<THREE.Object3D>,
bodyB: React.Ref<THREE.Object3D>,
optns: ConeTwistConstraintOpts,
deps: any[] = [],
deps: React.DependencyList = [],
): ConstraintApi

function useDistanceConstraint(
bodyA: React.Ref<THREE.Object3D>,
bodyB: React.Ref<THREE.Object3D>,
optns: DistanceConstraintOpts,
deps: any[] = [],
deps: React.DependencyList = [],
): ConstraintApi

function useHingeConstraint(
bodyA: React.Ref<THREE.Object3D>,
bodyB: React.Ref<THREE.Object3D>,
optns: HingeConstraintOpts,
deps: any[] = [],
deps: React.DependencyList = [],
): ConstraintApi

function useLockConstraint(
bodyA: React.Ref<THREE.Object3D>,
bodyB: React.Ref<THREE.Object3D>,
optns: LockConstraintOpts,
deps: any[] = [],
deps: React.DependencyList = [],
): ConstraintApi

function useSpring(
bodyA: React.Ref<THREE.Object3D>,
bodyB: React.Ref<THREE.Object3D>,
optns: SpringOptns,
deps: any[] = [],
deps: React.DependencyList = [],
): void

function useRaycastClosest(options: RayOptns, callback: (e: Event) => void, deps: any[] = []): void
function useRaycastAny(options: RayOptns, callback: (e: Event) => void, deps: any[] = []): void
function useRaycastAll(options: RayOptns, callback: (e: Event) => void, deps: any[] = []): void
function useRaycastClosest(
options: RayOptns,
callback: (e: Event) => void,
deps: React.DependencyList = [],
): void
function useRaycastAny(options: RayOptns, callback: (e: Event) => void, deps: React.DependencyList = []): void
function useRaycastAll(options: RayOptns, callback: (e: Event) => void, deps: React.DependencyList = []): void
```

### Returned api
Expand Down
66 changes: 43 additions & 23 deletions src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { MaterialOptions } from 'cannon-es'
import type { MutableRefObject, Ref, RefObject } from 'react'
import type { DependencyList, MutableRefObject, Ref, RefObject } from 'react'
import type { Euler } from 'three'
import type {
AddRayMessage,
Expand Down Expand Up @@ -244,7 +244,7 @@ function useBody<B extends BodyProps<unknown>>(
fn: GetByIndex<B>,
argsFn: ArgFn<B['args']>,
fwdRef: Ref<Object3D>,
deps: any[] = [],
deps: DependencyList = [],
): Api {
const ref = useForwardedRef(fwdRef)
const { worker, refs, events, subscriptions } = useContext(context)
Expand Down Expand Up @@ -403,33 +403,49 @@ function makeTriplet(v: Vector3 | Triplet): Triplet {
return v instanceof Vector3 ? [v.x, v.y, v.z] : v
}

export function usePlane(fn: GetByIndex<PlaneProps>, fwdRef: Ref<Object3D> = null, deps?: any[]) {
export function usePlane(fn: GetByIndex<PlaneProps>, fwdRef: Ref<Object3D> = null, deps?: DependencyList) {
return useBody('Plane', fn, () => [], fwdRef, deps)
}
export function useBox(fn: GetByIndex<BoxProps>, fwdRef: Ref<Object3D> = null, deps?: any[]) {
export function useBox(fn: GetByIndex<BoxProps>, fwdRef: Ref<Object3D> = null, deps?: DependencyList) {
const defaultBoxArgs: Triplet = [1, 1, 1]
return useBody('Box', fn, (args = defaultBoxArgs): Triplet => args, fwdRef, deps)
}
export function useCylinder(fn: GetByIndex<CylinderProps>, fwdRef: Ref<Object3D> = null, deps?: any[]) {
export function useCylinder(
fn: GetByIndex<CylinderProps>,
fwdRef: Ref<Object3D> = null,
deps?: DependencyList,
) {
return useBody('Cylinder', fn, (args = [] as []) => args, fwdRef, deps)
}
export function useHeightfield(fn: GetByIndex<HeightfieldProps>, fwdRef: Ref<Object3D> = null, deps?: any[]) {
export function useHeightfield(
fn: GetByIndex<HeightfieldProps>,
fwdRef: Ref<Object3D> = null,
deps?: DependencyList,
) {
return useBody('Heightfield', fn, (args) => args, fwdRef, deps)
}
export function useParticle(fn: GetByIndex<ParticleProps>, fwdRef: Ref<Object3D> = null, deps?: any[]) {
export function useParticle(
fn: GetByIndex<ParticleProps>,
fwdRef: Ref<Object3D> = null,
deps?: DependencyList,
) {
return useBody('Particle', fn, () => [], fwdRef, deps)
}
export function useSphere(fn: GetByIndex<SphereProps>, fwdRef: Ref<Object3D> = null, deps?: any[]) {
export function useSphere(fn: GetByIndex<SphereProps>, fwdRef: Ref<Object3D> = null, deps?: DependencyList) {
return useBody('Sphere', fn, (radius = 1): [number] => [radius], fwdRef, deps)
}
export function useTrimesh(fn: GetByIndex<TrimeshProps>, fwdRef: Ref<Object3D> = null, deps?: any[]) {
export function useTrimesh(
fn: GetByIndex<TrimeshProps>,
fwdRef: Ref<Object3D> = null,
deps?: DependencyList,
) {
return useBody<TrimeshProps>('Trimesh', fn, (args) => args, fwdRef, deps)
}

export function useConvexPolyhedron(
fn: GetByIndex<ConvexPolyhedronProps>,
fwdRef: Ref<Object3D> = null,
deps?: any[],
deps?: DependencyList,
) {
return useBody<ConvexPolyhedronProps>(
'ConvexPolyhedron',
Expand All @@ -448,7 +464,7 @@ export function useConvexPolyhedron(
export function useCompoundBody(
fn: GetByIndex<CompoundBodyProps>,
fwdRef: Ref<Object3D> = null,
deps?: any[],
deps?: DependencyList,
) {
return useBody('Compound', fn, (args) => args as unknown[], fwdRef, deps)
}
Expand Down Expand Up @@ -494,7 +510,7 @@ function useConstraint<T extends 'Hinge' | ConstraintTypes>(
bodyA: Ref<Object3D>,
bodyB: Ref<Object3D>,
optns: any = {},
deps: any[] = [],
deps: DependencyList = [],
): ConstraintORHingeApi<T> {
const { worker } = useContext(context)
const uuid = MathUtils.generateUUID()
Expand Down Expand Up @@ -542,39 +558,39 @@ export function usePointToPointConstraint(
bodyA: Ref<Object3D> = null,
bodyB: Ref<Object3D> = null,
optns: PointToPointConstraintOpts,
deps: any[] = [],
deps: DependencyList = [],
) {
return useConstraint('PointToPoint', bodyA, bodyB, optns, deps)
}
export function useConeTwistConstraint(
bodyA: Ref<Object3D> = null,
bodyB: Ref<Object3D> = null,
optns: ConeTwistConstraintOpts,
deps: any[] = [],
deps: DependencyList = [],
) {
return useConstraint('ConeTwist', bodyA, bodyB, optns, deps)
}
export function useDistanceConstraint(
bodyA: Ref<Object3D> = null,
bodyB: Ref<Object3D> = null,
optns: DistanceConstraintOpts,
deps: any[] = [],
deps: DependencyList = [],
) {
return useConstraint('Distance', bodyA, bodyB, optns, deps)
}
export function useHingeConstraint(
bodyA: Ref<Object3D> = null,
bodyB: Ref<Object3D> = null,
optns: HingeConstraintOpts,
deps: any[] = [],
deps: DependencyList = [],
) {
return useConstraint('Hinge', bodyA, bodyB, optns, deps)
}
export function useLockConstraint(
bodyA: Ref<Object3D> = null,
bodyB: Ref<Object3D> = null,
optns: LockConstraintOpts,
deps: any[] = [],
deps: DependencyList = [],
) {
return useConstraint('Lock', bodyA, bodyB, optns, deps)
}
Expand All @@ -583,7 +599,7 @@ export function useSpring(
bodyA: Ref<Object3D> = null,
bodyB: Ref<Object3D> = null,
optns: SpringOptns,
deps: any[] = [],
deps: DependencyList = [],
): SpringApi {
const { worker } = useContext(context)
const [uuid] = useState(() => MathUtils.generateUUID())
Expand Down Expand Up @@ -618,7 +634,7 @@ export function useSpring(

type RayOptions = Omit<AddRayMessage['props'], 'mode'>

function useRay(mode: RayMode, options: RayOptions, callback: (e: Event) => void, deps: any[] = []) {
function useRay(mode: RayMode, options: RayOptions, callback: (e: Event) => void, deps: DependencyList = []) {
const { worker, events } = useContext(context)
const [uuid] = useState(() => MathUtils.generateUUID())
useEffect(() => {
Expand All @@ -631,15 +647,19 @@ function useRay(mode: RayMode, options: RayOptions, callback: (e: Event) => void
}, deps)
}

export function useRaycastClosest(options: RayOptions, callback: (e: Event) => void, deps: any[] = []) {
export function useRaycastClosest(
options: RayOptions,
callback: (e: Event) => void,
deps: DependencyList = [],
) {
useRay('Closest', options, callback, deps)
}

export function useRaycastAny(options: RayOptions, callback: (e: Event) => void, deps: any[] = []) {
export function useRaycastAny(options: RayOptions, callback: (e: Event) => void, deps: DependencyList = []) {
useRay('Any', options, callback, deps)
}

export function useRaycastAll(options: RayOptions, callback: (e: Event) => void, deps: any[] = []) {
export function useRaycastAll(options: RayOptions, callback: (e: Event) => void, deps: DependencyList = []) {
useRay('All', options, callback, deps)
}

Expand Down Expand Up @@ -687,7 +707,7 @@ function isString(v: unknown): v is string {
export function useRaycastVehicle(
fn: () => RaycastVehicleProps,
fwdRef: Ref<Object3D> = null,
deps: any[] = [],
deps: DependencyList = [],
): [RefObject<Object3D>, RaycastVehiclePublicApi] {
const ref = useForwardedRef(fwdRef)
const { worker, subscriptions } = useContext(context)
Expand Down

0 comments on commit 17c9731

Please sign in to comment.