Skip to content

Commit 6ad06e1

Browse files
committed
fix(core): clean up portal; remove autoEffect
1 parent 94834a6 commit 6ad06e1

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

libs/core/src/lib/portal.ts

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
CUSTOM_ELEMENTS_SCHEMA,
88
DestroyRef,
99
Directive,
10+
effect,
1011
EmbeddedViewRef,
1112
inject,
1213
Injector,
@@ -17,13 +18,11 @@ import {
1718
viewChild,
1819
ViewContainerRef,
1920
} from '@angular/core';
20-
import { injectAutoEffect } from 'ngxtension/auto-effect';
2121
import { Camera, Object3D, Raycaster, Scene, Vector2, Vector3 } from 'three';
2222
import { getLocalState, prepare } from './instance';
2323
import { SPECIAL_INTERNAL_ADD_COMMENT } from './renderer/constants';
2424
import { injectStore, provideStore } from './store';
2525
import { NgtComputeFunction, NgtSize, NgtState } from './types';
26-
import { injectBeforeRender } from './utils/before-render';
2726
import { is } from './utils/is';
2827
import { signalStore } from './utils/signal-store';
2928
import { updateCamera } from './utils/update';
@@ -45,30 +44,34 @@ export class NgtPortalBeforeRender {
4544
parentCamera = input.required<Camera>();
4645

4746
constructor() {
48-
injectAutoEffect()((injector) => {
47+
effect((onCleanup) => {
4948
// track state
50-
this.portalStore.state();
51-
const priority = this.renderPriority();
49+
const [renderPriority, { internal }] = [this.renderPriority(), this.portalStore.state()];
5250

53-
let oldClear: boolean;
54-
return injectBeforeRender(
55-
() => {
56-
const { gl, scene, camera } = this.portalStore.snapshot;
57-
oldClear = gl.autoClear;
58-
if (this.renderPriority() === 1) {
51+
let oldClean: boolean;
52+
53+
const cleanup = internal.subscribe(
54+
({ gl, scene, camera }) => {
55+
const [parentScene, parentCamera] = [untracked(this.parentScene), untracked(this.parentCamera)];
56+
oldClean = gl.autoClear;
57+
if (renderPriority === 1) {
5958
// clear scene and render with default
6059
gl.autoClear = true;
61-
gl.render(this.parentScene(), this.parentCamera());
60+
gl.render(parentScene, parentCamera);
6261
}
62+
6363
// disable cleaning
6464
gl.autoClear = false;
6565
gl.clearDepth();
6666
gl.render(scene, camera);
6767
// restore
68-
gl.autoClear = oldClear;
68+
gl.autoClear = oldClean;
6969
},
70-
{ priority, injector },
70+
renderPriority,
71+
this.portalStore,
7172
);
73+
74+
onCleanup(() => cleanup());
7275
});
7376
}
7477

@@ -168,10 +171,9 @@ export class NgtPortal {
168171
private portalView?: EmbeddedViewRef<unknown>;
169172

170173
constructor() {
171-
const autoEffect = injectAutoEffect();
172-
173174
const parentState = this.parentStore.select();
174175

176+
// NOTE: we run this in afterNextRender for inputs to resolve
175177
afterNextRender(() => {
176178
const previousState = this.parentStore.snapshot;
177179

@@ -200,25 +202,28 @@ export class NgtPortal {
200202
this.portalStore.update((state) => ({ ...state, events: { ...state.events, ...events } })),
201203
});
202204

203-
autoEffect(() => {
204-
const state = this.state();
205-
const _parentState = parentState();
206-
this.portalStore.update((prev) => this.inject(_parentState, prev, state, untracked(this.container)));
207-
untracked(() => {
208-
if (this.portalView) {
205+
effect(
206+
() => {
207+
const state = this.state();
208+
const _parentState = parentState();
209+
this.portalStore.update((prev) => this.inject(_parentState, prev, state, untracked(this.container)));
210+
untracked(() => {
211+
if (this.portalView) {
212+
this.portalView.detectChanges();
213+
return;
214+
}
215+
216+
this.portalView = this.portalAnchor().createEmbeddedView(
217+
this.portalContent(),
218+
{ container: this.container(), injector: this.injector },
219+
{ injector: this.injector },
220+
);
209221
this.portalView.detectChanges();
210-
return;
211-
}
212-
213-
this.portalView = this.portalAnchor().createEmbeddedView(
214-
this.portalContent(),
215-
{ container: this.container(), injector: this.injector },
216-
{ injector: this.injector },
217-
);
218-
this.portalView.detectChanges();
219-
this.portalRendered.set(true);
220-
});
221-
});
222+
this.portalRendered.set(true);
223+
});
224+
},
225+
{ injector: this.injector },
226+
);
222227
});
223228

224229
inject(DestroyRef).onDestroy(() => {

0 commit comments

Comments
 (0)