Skip to content

Commit 613f0a0

Browse files
committed
fix: filter undefined and null from all object refs
1 parent 5dbfbe7 commit 613f0a0

File tree

5 files changed

+18
-4
lines changed

5 files changed

+18
-4
lines changed

libs/angular-three-cannon/services/src/lib/body.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { NgtcStore, NgtcUtils } from 'angular-three-cannon';
2626
import { NGTC_DEBUG_API } from 'angular-three-cannon/debug';
2727
import { combineLatest, Observable, Subscription } from 'rxjs';
2828
import * as THREE from 'three';
29+
import { filterEmpty } from './utils';
2930

3031
export type NgtcAtomicApi<K extends AtomicName> = {
3132
set: (value: AtomicProps[K]) => void;
@@ -191,7 +192,7 @@ function injectBody<TBodyProps extends BodyProps, TObject extends THREE.Object3D
191192
});
192193

193194
// start the pipeline as soon as bodyRef has a truthy value
194-
subscription = combineLatest([physicsStore.select('worker'), bodyRef.$])
195+
subscription = combineLatest([physicsStore.select('worker'), bodyRef.$.pipe(filterEmpty())])
195196
.pipe(
196197
tapEffect(([worker, object]) => {
197198
const currentWorker = worker;

libs/angular-three-cannon/services/src/lib/constraint.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import { injectNgtDestroy, injectNgtRef, is, makeId, NgtInjectedRef, tapEffect } from 'angular-three';
1212
import { NgtcStore } from 'angular-three-cannon';
1313
import { combineLatest, takeUntil } from 'rxjs';
14+
import { filterEmpty } from './utils';
1415

1516
export interface NgtcConstraintApi {
1617
disable: () => void;
@@ -122,7 +123,7 @@ function injectConstraint<
122123
const bodyARef = !is.ref(bodyA) ? injectNgtRef(bodyA) : bodyA;
123124
const bodyBRef = !is.ref(bodyB) ? injectNgtRef(bodyB) : bodyB;
124125

125-
combineLatest([physicsStore.select('worker'), bodyARef.$, bodyBRef.$])
126+
combineLatest([physicsStore.select('worker'), bodyARef.$.pipe(filterEmpty()), bodyBRef.$.pipe(filterEmpty())])
126127
.pipe(
127128
tapEffect(([worker, bodyA, bodyB]) => {
128129
const opts = optsFn();

libs/angular-three-cannon/services/src/lib/raycast-vehicle.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { injectNgtDestroy, injectNgtRef, NgtInjectedRef, tapEffect } from 'angul
44
import { NgtcStore, NgtcUtils } from 'angular-three-cannon';
55
import { combineLatest, takeUntil } from 'rxjs';
66
import * as THREE from 'three';
7+
import { filterEmpty } from './utils';
78

89
export interface NgtcRaycastVehicleProps {
910
chassisBody: NgtInjectedRef<THREE.Object3D>;
@@ -45,7 +46,12 @@ export function injectRaycastVehicle<TObject extends THREE.Object3D = THREE.Obje
4546
queueMicrotask(() => {
4647
const { chassisBody, indexForwardAxis = 2, indexRightAxis = 0, indexUpAxis = 1, wheelInfos, wheels } = fn();
4748

48-
combineLatest([store.select('worker'), ref.$, chassisBody.$, ...wheels.map((wheel) => wheel.$)])
49+
combineLatest([
50+
store.select('worker'),
51+
ref.$.pipe(filterEmpty()),
52+
chassisBody.$.pipe(filterEmpty()),
53+
...wheels.map((wheel) => wheel.$.pipe(filterEmpty())),
54+
])
4955
.pipe(
5056
tapEffect(([worker, object]) => {
5157
const uuid = object.uuid;

libs/angular-three-cannon/services/src/lib/spring.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { SpringOptns } from '@pmndrs/cannon-worker-api';
33
import { injectNgtDestroy, makeId, NgtInjectedRef, tapEffect } from 'angular-three';
44
import { NgtcStore } from 'angular-three-cannon';
55
import { combineLatest, takeUntil } from 'rxjs';
6+
import { filterEmpty } from './utils';
67

78
export interface NgtcSpringApi {
89
setDamping: (value: number) => void;
@@ -32,7 +33,7 @@ export function injectSpring<
3233
const uuid = makeId();
3334
const { destroy$ } = injectNgtDestroy();
3435

35-
combineLatest([store.select('worker'), bodyA.$, bodyB.$])
36+
combineLatest([store.select('worker'), bodyA.$.pipe(filterEmpty()), bodyB.$.pipe(filterEmpty())])
3637
.pipe(
3738
tapEffect(([worker, bodyA, bodyB]) => {
3839
const opts = optsFn();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { filter, MonoTypeOperatorFunction } from 'rxjs';
2+
3+
export function filterEmpty<T>(): MonoTypeOperatorFunction<T> {
4+
return filter<T>((obj): obj is T => obj != null);
5+
}

0 commit comments

Comments
 (0)