@@ -33,6 +33,19 @@ import { injectAutoEffect } from 'ngxtension/auto-effect';
3333import { mergeInputs } from 'ngxtension/inject-inputs' ;
3434import { InstancedMesh , Matrix4 , Object3D , Quaternion , QuaternionTuple , Vector3 } from 'three' ;
3535
36+ export interface NgtcCannonWorkerEvents {
37+ collide : WorkerCollideEvent ;
38+ collideBegin : WorkerCollideBeginEvent ;
39+ collideEnd : WorkerCollideEndEvent ;
40+ frame : WorkerFrameMessage ;
41+ rayhit : WorkerRayhitEvent ;
42+ }
43+
44+ export interface NgtcCannonWorker extends CannonWorkerAPI {
45+ on : < K extends keyof NgtcCannonWorkerEvents > ( event : K , cb : ( data : NgtcCannonWorkerEvents [ K ] [ 'data' ] ) => void ) => void ;
46+ removeAllListeners : ( ) => void ;
47+ }
48+
3649const v = new Vector3 ( ) ;
3750const s = new Vector3 ( 1 , 1 , 1 ) ;
3851const q = new Quaternion ( ) ;
@@ -122,7 +135,8 @@ export class NgtcPhysics {
122135 options = input ( defaultOptions , { transform : mergeInputs ( defaultOptions ) } ) ;
123136
124137 private invalidate = this . store . select ( 'invalidate' ) ;
125- private worker = signal < CannonWorkerAPI > ( null ! ) ;
138+ // @ts -expect-error - worker is not nullable, and we don't want to use ! operator
139+ private worker = signal < CannonWorkerAPI > ( null ) ;
126140
127141 api : NgtcPhysicsApi = {
128142 bodies : { } ,
@@ -155,11 +169,8 @@ export class NgtcPhysics {
155169 }
156170
157171 private connectWorker ( ) {
158- this . autoEffect ( ( injector ) => {
159- const worker = this . worker ( ) as CannonWorkerAPI & {
160- on : ( event : string , cb : ( ...args : any [ ] ) => void ) => void ;
161- removeAllListeners : ( ) => void ;
162- } ;
172+ this . autoEffect ( ( ) => {
173+ const worker = this . worker ( ) as NgtcCannonWorker ;
163174 if ( ! worker ) return ;
164175
165176 worker . connect ( ) ;
@@ -183,7 +194,7 @@ export class NgtcPhysics {
183194
184195 this . autoEffect ( ( ) => {
185196 const [ worker , value ] = [ untracked ( this . worker ) , computedValue ( ) ] ;
186- // @ts -expect-error
197+ // @ts -expect-error - we know key is a valid key of CannonWorkerAPI
187198 worker [ key ] = value ;
188199 } ) ;
189200 }
@@ -193,9 +204,9 @@ export class NgtcPhysics {
193204 injectBeforeRender (
194205 ( { delta } ) => {
195206 const [ { isPaused, maxSubSteps, stepSize } , worker ] = [ this . options ( ) , this . worker ( ) ] ;
196- if ( isPaused || ! worker ) return ;
207+ if ( isPaused || ! worker || stepSize == null ) return ;
197208 timeSinceLastCalled += delta ;
198- worker . step ( { maxSubSteps, stepSize : stepSize ! , timeSinceLastCalled } ) ;
209+ worker . step ( { maxSubSteps, stepSize, timeSinceLastCalled } ) ;
199210 timeSinceLastCalled = 0 ;
200211 } ,
201212 { injector : this . injector } ,
0 commit comments