Skip to content

Commit 537be01

Browse files
committed
fix(soba): unwrap afterNextRender in injectHelper
BREAKING CHANGE: this is considered a breaking change because of timing for unwrapping `afterNextRender` The consumers can migrate `injetHelper` with the following decision making guidances: - If the result of the `injectHelper` are used as bindings on the template, there's no need to change anything. This also applies to `computed` using these result and the computed(s) are used as bindings on the template. - If the result of the `injectHelper` are used in a side-effects, then the consumers need to make sure the parameters to these `injectHelper` have a chance to resolve (i.e: `input` and `afterNextRender`; this was the reason `afterNextRender` was used internally). In addition, the consumers also need to make sure the `injectHelper` are invoked in an Injection Context.
1 parent d57d1cc commit 537be01

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

libs/soba/abstractions/src/lib/helper.ts

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {
2-
afterNextRender,
32
ChangeDetectionStrategy,
43
Component,
54
computed,
65
CUSTOM_ELEMENTS_SCHEMA,
6+
effect,
77
ElementRef,
88
Injector,
99
input,
@@ -13,7 +13,6 @@ import {
1313
} from '@angular/core';
1414
import { extend, getLocalState, injectBeforeRender, injectStore, resolveRef } from 'angular-three';
1515
import { assertInjector } from 'ngxtension/assert-injector';
16-
import { injectAutoEffect } from 'ngxtension/auto-effect';
1716
import { Object3D } from 'three';
1817

1918
type HelperArgs<T> = T extends [infer _, ...infer R] ? R : never;
@@ -30,37 +29,34 @@ export function injectHelper<
3029
}: { injector?: Injector; args?: () => HelperArgs<ConstructorParameters<TConstructor>> } = {},
3130
) {
3231
return assertInjector(injectHelper, injector, () => {
33-
const autoEffect = injectAutoEffect();
3432
const store = injectStore();
3533
const scene = store.select('scene');
3634

3735
const helper = signal<THelperInstance | null>(null);
3836

39-
afterNextRender(() => {
40-
autoEffect(() => {
41-
let currentHelper: THelperInstance | undefined = undefined;
42-
const maybeObject3D = object();
43-
if (!maybeObject3D) return;
37+
effect((onCleanup) => {
38+
let currentHelper: THelperInstance | undefined = undefined;
39+
const maybeObject3D = object();
40+
if (!maybeObject3D) return;
4441

45-
const object3D = resolveRef(maybeObject3D);
46-
if (!object3D) return;
42+
const object3D = resolveRef(maybeObject3D);
43+
if (!object3D) return;
4744

48-
currentHelper = new (helperConstructor() as any)(object3D, ...args());
49-
if (!currentHelper) return;
45+
currentHelper = new (helperConstructor() as any)(object3D, ...args());
46+
if (!currentHelper) return;
5047

51-
untracked(() => {
52-
helper.set(currentHelper);
53-
});
48+
untracked(() => {
49+
helper.set(currentHelper);
50+
});
5451

55-
// Prevent the helpers from blocking rays
56-
currentHelper.traverse((child) => (child.raycast = () => null));
57-
scene().add(currentHelper);
52+
// Prevent the helpers from blocking rays
53+
currentHelper.traverse((child) => (child.raycast = () => null));
54+
scene().add(currentHelper);
5855

59-
return () => {
60-
helper.set(null);
61-
scene().remove(currentHelper);
62-
currentHelper.dispose?.();
63-
};
56+
onCleanup(() => {
57+
helper.set(null);
58+
scene().remove(currentHelper);
59+
currentHelper.dispose?.();
6460
});
6561
});
6662

0 commit comments

Comments
 (0)