Skip to content

Commit 3bf38c4

Browse files
committed
feat: clean up wait$ mechanism
1 parent 9cee002 commit 3bf38c4

File tree

1 file changed

+5
-13
lines changed
  • libs/angular-three-cannon/services/src/lib

1 file changed

+5
-13
lines changed

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,39 +167,31 @@ function injectBody<TBodyProps extends BodyProps, TObject extends THREE.Object3D
167167
): NgtcBodyReturn<TObject> {
168168
let subscription: Subscription | undefined = undefined;
169169

170-
// a ReplaySubject that would emit once in queueMicrotask call
171-
const microQueue$ = new ReplaySubject<void>(1);
172170
// a ReplaySubject that would emit whenever our props emits. This is done so that the consumers can pass in
173171
// Observable to injectBody if they have reactive props (eg: Input)
174172
const propsSubjectList = [] as ReplaySubject<TBodyProps>[];
175173

176-
// an array of streams we want to wait to emit until we decide to give the bodyRef an empty Object3D
177-
const waits$ = [microQueue$] as Observable<unknown>[];
178-
179174
const debugApi = inject(NGTC_DEBUG_API, { skipSelf: true, optional: true });
180175
const physicsStore = inject(NgtcStore, { skipSelf: true });
181176

182177
// clean up our streams on destroy
183178
injectNgtDestroy(() => {
184-
microQueue$.complete();
185179
propsSubjectList.forEach((sub) => sub.complete());
186180
subscription?.unsubscribe();
187181
});
188182

189183
// give our bodyRef an NgtInjectedRef
190184
let bodyRef = injectNgtRef<TObject>();
191185

192-
// add waitFor if the consumers pass it in
193-
if (waitFor) waits$.push(waitFor);
194186
// re-assign bodyRef if the consumers pass a ref in
195187
if (ref) bodyRef = ref;
196188

197189
// fire microQueue$
198-
queueMicrotask(() => microQueue$.next());
199-
200-
// when all waits$ emit, we give bodyRef an empty Object3D if it doesn't have one. Physic Object can still be affected
201-
// by Physics without a visual representation
202-
combineLatest(waits$).subscribe(() => (bodyRef.nativeElement ||= new THREE.Object3D() as TObject));
190+
queueMicrotask(() => {
191+
// waitFor assumes the consumer will be using the bodyRef on the template
192+
// with the model (waitFor) as a THREE instance
193+
if (!waitFor) bodyRef.nativeElement ||= new THREE.Object3D() as TObject;
194+
});
203195

204196
// start the pipeline as soon as bodyRef has a truthy value
205197
subscription = combineLatest([physicsStore.select('worker'), bodyRef.$])

0 commit comments

Comments
 (0)