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

[Typescript] Add types for world messages (like setGravity) #282

Merged
merged 1 commit into from
Sep 5, 2021
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
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])
}