Skip to content

Commit 5e45354

Browse files
committed
fix(core): adjust effect usage in core canvas
1 parent aeb341f commit 5e45354

File tree

5 files changed

+58
-50
lines changed

5 files changed

+58
-50
lines changed

libs/core/src/lib/canvas.ts

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -105,65 +105,72 @@ export class NgtCanvas {
105105

106106
// NOTE: this signal is updated outside of Zone
107107
protected resizeResult = signal<ResizeResult>({} as ResizeResult, { equal: Object.is });
108+
private configurator = signal<NgtCanvasConfigurator | null>(null);
108109

109-
private configurator?: NgtCanvasConfigurator;
110110
private glEnvironmentInjector?: EnvironmentInjector;
111111
private glRef?: ComponentRef<unknown>;
112112

113113
constructor() {
114+
// NOTE: this means that everything in NgtCanvas will be in afterNextRender.
115+
// this allows the content of NgtCanvas to use effect instead of afterNextRender
114116
afterNextRender(() => {
117+
const canvasElement = this.glCanvas().nativeElement;
115118
this.zone.runOutsideAngular(() => {
116-
this.configurator = this.initRoot(this.glCanvas().nativeElement);
117-
effect(
118-
() => {
119-
this.noZoneResizeEffect();
120-
},
121-
{ injector: this.injector },
122-
);
119+
this.configurator.set(this.initRoot(canvasElement));
123120
});
121+
122+
effect(
123+
() => {
124+
const resizeResult = this.resizeResult();
125+
if (!resizeResult.width || resizeResult.width <= 0 || !resizeResult.height || resizeResult.height <= 0)
126+
return;
127+
128+
const configurator = this.configurator();
129+
if (!configurator) return;
130+
131+
const canvasOptions = {
132+
gl: this.gl(),
133+
shadows: this.shadows(),
134+
legacy: this.legacy(),
135+
linear: this.linear(),
136+
flat: this.flat(),
137+
orthographic: this.orthographic(),
138+
frameloop: this.frameloop(),
139+
performance: this.performance(),
140+
dpr: this.dpr(),
141+
raycaster: this.raycaster(),
142+
scene: this.scene(),
143+
camera: this.camera(),
144+
events: this.events(),
145+
eventSource: this.eventSource(),
146+
eventPrefix: this.eventPrefix(),
147+
lookAt: this.lookAt(),
148+
size: resizeResult,
149+
};
150+
151+
this.zone.runOutsideAngular(() => {
152+
configurator.configure(canvasOptions);
153+
154+
untracked(() => {
155+
if (this.glRef) {
156+
this.glRef.changeDetectorRef.detectChanges();
157+
} else {
158+
this.noZoneRender();
159+
}
160+
});
161+
});
162+
},
163+
{ injector: this.injector },
164+
);
124165
});
125166

126167
inject(DestroyRef).onDestroy(() => {
127168
this.glRef?.destroy();
128169
this.glEnvironmentInjector?.destroy();
129-
this.configurator?.destroy();
170+
this.configurator()?.destroy();
130171
});
131172
}
132173

133-
private noZoneResizeEffect() {
134-
const resizeResult = this.resizeResult();
135-
if (resizeResult.width > 0 && resizeResult.height > 0) {
136-
if (!this.configurator) this.configurator = this.initRoot(this.glCanvas().nativeElement);
137-
this.configurator.configure({
138-
gl: this.gl(),
139-
shadows: this.shadows(),
140-
legacy: this.legacy(),
141-
linear: this.linear(),
142-
flat: this.flat(),
143-
orthographic: this.orthographic(),
144-
frameloop: this.frameloop(),
145-
performance: this.performance(),
146-
dpr: this.dpr(),
147-
raycaster: this.raycaster(),
148-
scene: this.scene(),
149-
camera: this.camera(),
150-
events: this.events(),
151-
eventSource: this.eventSource(),
152-
eventPrefix: this.eventPrefix(),
153-
lookAt: this.lookAt(),
154-
size: resizeResult,
155-
});
156-
157-
untracked(() => {
158-
if (this.glRef) {
159-
this.glRef.changeDetectorRef.detectChanges();
160-
} else {
161-
this.noZoneRender();
162-
}
163-
});
164-
}
165-
}
166-
167174
private noZoneRender() {
168175
// NOTE: destroy previous instances if existed
169176
this.glEnvironmentInjector?.destroy();

libs/core/src/lib/instance.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export function invalidateInstance<TInstance extends object>(instance: TInstance
1515
while (store.snapshot.previousRoot) {
1616
store = store.snapshot.previousRoot;
1717
}
18+
1819
if (store.snapshot.internal.frames === 0) {
1920
store.snapshot.invalidate();
2021
}

libs/core/src/lib/loader.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,16 @@ function _injectLoader<
133133
NgtBranchingReturn<TReturn, NgtGLTFLike, NgtGLTFLike & NgtObjectMap>
134134
> | null>(null);
135135

136-
const effector = load(loaderConstructorFactory, inputs, {
136+
const cachedResultPromisesEffect = load(loaderConstructorFactory, inputs, {
137137
extensions,
138138
onProgress,
139139
onLoad: onLoad as (data: unknown) => void,
140140
});
141141

142142
effect(() => {
143143
const originalUrls = inputs();
144-
const cachedEffect = effector();
145-
Promise.all(cachedEffect).then((results) => {
144+
const cachedResultPromises = cachedResultPromisesEffect();
145+
Promise.all(cachedResultPromises).then((results) => {
146146
response.update(() => {
147147
if (Array.isArray(originalUrls)) return results;
148148
if (typeof originalUrls === 'string') return results[0];

libs/core/src/lib/portal.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export type NgtPortalInjectableState = Partial<
130130
template: `
131131
<ng-container #anchor />
132132
133-
@if (renderAutoBeforeRender()) {
133+
@if (shouldAutoRender()) {
134134
<ngt-portal-before-render
135135
[renderPriority]="autoRenderPriority()"
136136
[parentScene]="parentScene()"
@@ -166,7 +166,7 @@ export class NgtPortal {
166166
private pointer = new Vector2();
167167
private portalRendered = signal(false);
168168

169-
protected renderAutoBeforeRender = computed(() => this.portalRendered() && this.autoRender());
169+
protected shouldAutoRender = computed(() => this.portalRendered() && this.autoRender());
170170

171171
private portalView?: EmbeddedViewRef<unknown>;
172172

libs/core/src/lib/roots.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ export function injectCanvasRootInitializer(injector?: Injector) {
260260
if (gl.setClearAlpha) {
261261
gl.setClearAlpha(0);
262262
}
263-
gl.setPixelRatio(makeDpr(state.viewport.dpr));
264-
gl.setSize(state.size.width, state.size.height);
263+
gl.setPixelRatio(makeDpr(state.viewport.dpr ?? dpr));
264+
gl.setSize(sizeOptions?.width ?? state.size.width, sizeOptions?.height ?? state.size.height);
265265

266266
if (
267267
is.obj(glOptions) &&

0 commit comments

Comments
 (0)