Skip to content

Commit

Permalink
[Typescript] Add types for world messages (like setGravity)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornstar committed Sep 5, 2021
1 parent 8b35cb5 commit 540acb8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
10 changes: 6 additions & 4 deletions src/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import CannonWorker from '../src/worker'
import { useUpdateWorldPropsEffect } from './useUpdateWorldPropsEffect'

import type { Buffers, Refs, Events, Subscriptions, ProviderContext } from './setup'
import type { AtomicProps } from './hooks'
import type { AtomicProps, Triplet } from './hooks'

export type Broadphase = 'Naive' | 'SAP'

export type ProviderProps = {
children: React.ReactNode
Expand All @@ -23,8 +25,8 @@ export type ProviderProps = {
iterations?: number

allowSleep?: boolean
broadphase?: 'Naive' | 'SAP'
gravity?: number[]
broadphase?: Broadphase
gravity?: Triplet
quatNormalizeFast?: boolean
quatNormalizeSkip?: number
solver?: 'GS' | 'Split'
Expand Down Expand Up @@ -303,7 +305,7 @@ export default function Provider({
return () => worker.terminate()
}, [])

useUpdateWorldPropsEffect({ worker, axisIndex, broadphase, gravity, iterations, step, tolerance })
useUpdateWorldPropsEffect({ axisIndex, broadphase, gravity, iterations, step, tolerance, worker })

const api = useMemo(
() => ({ worker, bodies, refs, buffers, events, subscriptions }),
Expand Down
9 changes: 7 additions & 2 deletions src/setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { RayOptions } from 'cannon-es'
import type { Object3D } from 'three'
import type { WorkerCollideEvent, WorkerRayhitEvent } from './Provider'
import type { ProviderProps, WorkerCollideEvent, WorkerRayhitEvent } from './Provider'
import type {
AtomicProps,
BodyProps,
Expand Down Expand Up @@ -71,7 +71,7 @@ export const vectorNames = [
export type VectorName = typeof vectorNames[number]
export type CannonVectorName = Exclude<VectorName, 'rotation'> | 'quaternion'

export type SetOpName<T extends AtomicName | CannonVectorName> = `set${Capitalize<T>}`
export type SetOpName<T extends AtomicName | CannonVectorName | WorldPropName> = `set${Capitalize<T>}`
export type SubscriptionName = AtomicName | CannonVectorName | 'sliding'

type Operation<T extends string, P> = { op: T } & (P extends void ? {} : { props: P })
Expand Down Expand Up @@ -206,6 +206,10 @@ type UnsubscribeMessage = Operation<'unsubscribe', number>

type SubscriptionMessage = SubscribeMessage | UnsubscribeMessage

export type WorldPropName = 'axisIndex' | 'broadphase' | 'gravity' | 'iterations' | 'step' | 'tolerance'

type WorldMessage<T extends WorldPropName> = Operation<SetOpName<T>, Required<ProviderProps[T]>>

type CannonMessage =
| ApplyMessage
| AtomicMessage
Expand All @@ -219,6 +223,7 @@ type CannonMessage =
| SubscriptionMessage
| VectorMessage
| WakeUpMessage
| WorldMessage<WorldPropName>

export interface CannonWorker extends Worker {
postMessage: (message: CannonMessage) => void
Expand Down
26 changes: 12 additions & 14 deletions src/useUpdateWorldPropsEffect.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import { useEffect } from 'react'
import type { ProviderProps } from './Provider'
import type { CannonWorker, WorldPropName } from './setup'

type useUpdateWorldPropsEffect = Pick<
ProviderProps,
'gravity' | 'tolerance' | 'step' | 'iterations' | 'broadphase' | 'axisIndex'
> & { worker: Worker }
type Props = Pick<Required<ProviderProps>, WorldPropName> & { worker: CannonWorker }

export function useUpdateWorldPropsEffect({
worker,
axisIndex,
broadphase,
gravity,
tolerance,
step,
iterations,
broadphase,
axisIndex,
}: useUpdateWorldPropsEffect) {
step,
tolerance,
worker,
}: Props) {
useEffect(() => void worker.postMessage({ op: 'setAxisIndex', props: axisIndex }), [axisIndex])
useEffect(() => void worker.postMessage({ op: 'setBroadphase', props: broadphase }), [broadphase])
useEffect(() => void worker.postMessage({ op: 'setGravity', props: gravity }), [gravity])
useEffect(() => void worker.postMessage({ op: 'setTolerance', props: tolerance }), [tolerance])
useEffect(() => void worker.postMessage({ op: 'setStep', props: step }), [step])
useEffect(() => void worker.postMessage({ op: 'setIterations', props: iterations }), [iterations])
useEffect(() => void worker.postMessage({ op: 'setBroadphase', props: broadphase }), [broadphase])
useEffect(() => void worker.postMessage({ op: 'setAxisIndex', props: axisIndex }), [axisIndex])
useEffect(() => void worker.postMessage({ op: 'setStep', props: step }), [step])
useEffect(() => void worker.postMessage({ op: 'setTolerance', props: tolerance }), [tolerance])
}

1 comment on commit 540acb8

@vercel
Copy link

@vercel vercel bot commented on 540acb8 Sep 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.