Skip to content

Commit 71d6822

Browse files
authored
Merge pull request #60 from demike/perf/use-on-push
perf: use on push
2 parents 5f30f08 + db1f551 commit 71d6822

14 files changed

+49
-19
lines changed

projects/ngx-three-demo/src/app/app.component.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { Component } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
22

33
@Component({
44
selector: 'app-root',
55
templateUrl: './app.component.html',
6-
styleUrls: ['./app.component.scss']
6+
styleUrls: ['./app.component.scss'],
7+
changeDetection: ChangeDetectionStrategy.OnPush,
78
})
89
export class AppComponent {
910
title = 'ngx-three-demo';

projects/ngx-three-demo/src/app/dynamic-loader-example/dynamic-loader-example.component.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ASSET_PATH } from '../assets';
55
@Component({
66
selector: 'app-dynamic-loader-example',
77
templateUrl: './dynamic-loader-example.component.html',
8-
changeDetection: ChangeDetectionStrategy.Default,
8+
changeDetection: ChangeDetectionStrategy.OnPush,
99
})
1010
export class DynamicLoaderExampleComponent implements OnDestroy {
1111
public modelUrl: string;

projects/ngx-three-demo/src/app/example-page/example-page.component.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { Component, Type } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, Type } from '@angular/core';
22
import { ActivatedRoute } from '@angular/router';
33
import { EditorService } from '../code/EditorService';
44

55
@Component({
66
selector: 'app-example-page',
77
templateUrl: './example-page.component.html',
88
styleUrls: ['./example-page.component.scss'],
9+
changeDetection: ChangeDetectionStrategy.OnPush,
910
})
1011
export class ExamplePageComponent {
1112
public exampleComponent: Type<any> | null = null;

projects/ngx-three-demo/src/app/outline-pass-events-example/outline-pass-events-example.component.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
22
import { RaycasterEmitEvent, ThMesh, ThObject3D, ThOutlinePass } from 'ngx-three';
33
import { Color, MeshPhongMaterial, Vector2 } from 'three';
44
import GUI from 'lil-gui';
55

66
@Component({
77
selector: 'app-outline-pass-events-example',
88
templateUrl: './outline-pass-events-example.component.html',
9-
styleUrls: ['./outline-pass-events-example.component.scss']
9+
styleUrls: ['./outline-pass-events-example.component.scss'],
10+
changeDetection: ChangeDetectionStrategy.OnPush,
1011
})
1112
export class OutlinePassEventsExampleComponent implements OnInit {
1213
protected outlinePassResolution = new Vector2(window.innerWidth, window.innerHeight);
@@ -53,7 +54,7 @@ export class OutlinePassEventsExampleComponent implements OnInit {
5354
x: Math.random() * 4 - 2,
5455
y: Math.random() * 4 - 2,
5556
z: Math.random() * 4 - 2,
56-
scale: Math.random() * 0.3 + 0.1
57+
scale: Math.random() * 0.3 + 0.1,
5758
});
5859
}
5960
}

projects/ngx-three-demo/src/main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ if (environment.production) {
99
}
1010

1111
platformBrowserDynamic()
12-
.bootstrapModule(AppModule)
12+
.bootstrapModule(AppModule, { ngZoneEventCoalescing: true, ngZoneRunCoalescing: true /*, ngZone: 'noop' */ })
1313
.catch((err) => console.error(err));

projects/ngx-three/src/lib/ThControlBase.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, inject, OnDestroy } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, inject, OnDestroy } from '@angular/core';
22
import { Subscription } from 'rxjs';
33
import { EventDispatcher } from 'three';
44
import { ThObject3D } from './generated/ThObject3D';
@@ -8,6 +8,7 @@ import { ThWrapperBase } from './ThWrapperBase';
88
@Component({
99
selector: 'th-abs-control',
1010
template: '',
11+
changeDetection: ChangeDetectionStrategy.OnPush,
1112
})
1213
// eslint-disable-next-line @angular-eslint/component-class-suffix
1314
export class ThControlBase<T, ARGS> extends ThWrapperBase<T, ARGS> implements OnDestroy {

projects/ngx-three/src/lib/ThGeometryBase.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { Component } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
22
import { BufferGeometry, NormalOrGLBufferAttributes } from 'three';
33
import { ThObject3D } from './generated/ThObject3D';
44
import { ThWrapperBase } from './ThWrapperBase';
55
@Component({
66
selector: 'th-abs-geometry',
7-
template: ''
7+
template: '',
8+
changeDetection: ChangeDetectionStrategy.OnPush,
89
})
910
// eslint-disable-next-line @angular-eslint/component-class-suffix
1011
export class ThGeometryBase<T extends BufferGeometry<NormalOrGLBufferAttributes>, ARGS> extends ThWrapperBase<T, ARGS> {

projects/ngx-three/src/lib/ThMaterialBase.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { Component } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
22
import { Material, Mesh } from 'three';
33
import { ThObject3D } from './generated/ThObject3D';
44
import { ThWrapperBase } from './ThWrapperBase';
55
@Component({
66
selector: 'th-abs-material',
7-
template: ''
7+
template: '',
8+
changeDetection: ChangeDetectionStrategy.OnPush,
89
})
910
// eslint-disable-next-line @angular-eslint/component-class-suffix
1011
export class ThMaterialBase<T extends Material, ARGS> extends ThWrapperBase<T, ARGS> {

projects/ngx-three/src/lib/ThObjectBase.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { Component, Input, OnInit } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, Input, OnInit } from '@angular/core';
22
import { Object3D, Vector3 } from 'three';
33
import { ThWrapperBase } from './ThWrapperBase';
44
@Component({
55
selector: 'th-abs-object',
6-
template: '<ng-content/>'
6+
template: '<ng-content/>',
7+
changeDetection: ChangeDetectionStrategy.OnPush,
78
})
89
// eslint-disable-next-line @angular-eslint/component-class-suffix
910
export class ThObjectBase<T extends Object3D, ARGS = unknown> extends ThWrapperBase<T, ARGS> implements OnInit {

projects/ngx-three/src/lib/ThPassBase.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { Component, OnInit, Optional } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, OnInit, Optional } from '@angular/core';
22
import { ThEffectComposer } from './generated/overrides/ThEffectComposer';
33
import { Pass } from 'three/examples/jsm/postprocessing/Pass.js';
44
import { ThWrapperBase } from './ThWrapperBase';
55
@Component({
66
selector: 'th-abs-control',
77
template: '',
8+
changeDetection: ChangeDetectionStrategy.OnPush,
89
})
910
// eslint-disable-next-line @angular-eslint/component-class-suffix
1011
export class ThPassBase<T extends Pass = Pass, ARGS = unknown> extends ThWrapperBase<T, ARGS> implements OnInit {
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { Component } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
22
import { Texture } from 'three';
33
import { ThWrapperBase } from './ThWrapperBase';
44
@Component({
55
selector: 'th-abs-texture',
6-
template: ''
6+
template: '',
7+
changeDetection: ChangeDetectionStrategy.OnPush,
78
})
89
// eslint-disable-next-line @angular-eslint/component-class-suffix
910
export class ThTextureBase<T extends Texture, ARGS> extends ThWrapperBase<T, ARGS> {}

projects/ngx-three/src/lib/ThView.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
ChangeDetectionStrategy,
23
Component,
34
ContentChild,
45
ElementRef,
@@ -28,6 +29,7 @@ export const HOST_ELEMENT = new InjectionToken<ElementRef<HTMLElement>>('HOST_EL
2829
{ provide: RAYCASTER, useValue: new Raycaster() },
2930
RaycasterService,
3031
],
32+
changeDetection: ChangeDetectionStrategy.OnPush,
3133
})
3234
// eslint-disable-next-line @angular-eslint/component-class-suffix
3335
export class ThView implements OnInit {

projects/ngx-three/src/lib/ThWrapperBase.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
22
import { ComponentFixture, TestBed } from '@angular/core/testing';
33
import { EventDispatcher, Object3DEventMap } from 'three';
44
import { ThWrapperBase } from './ThWrapperBase';
@@ -17,6 +17,7 @@ class ExampleObj extends EventDispatcher<Object3DEventMap & ExampleEventMap> {
1717
@Component({
1818
selector: 'th-wrapper-impl',
1919
template: '',
20+
changeDetection: ChangeDetectionStrategy.OnPush,
2021
})
2122
class ThWrapperImplComponent extends ThWrapperBase<ExampleObj, any> {
2223
getType() {

projects/ngx-three/src/lib/ThWrapperBase.ts

+18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
ChangeDetectionStrategy,
23
Component,
34
EventEmitter,
45
Input,
@@ -18,11 +19,13 @@ import { isDisposable } from './util';
1819
export interface ThWrapperEventMap<T = Object3D> extends Object3DEventMap {
1920
changes: { changes: SimpleChanges };
2021
loaded: { object: T };
22+
removed: { object: T };
2123
}
2224

2325
@Component({
2426
selector: 'th-abs-wrapper',
2527
template: '',
28+
changeDetection: ChangeDetectionStrategy.OnPush,
2629
})
2730
// eslint-disable-next-line @angular-eslint/component-class-suffix
2831
export class ThWrapperBase<T, ARGS = unknown> implements ThWrapperLifeCycle, OnChanges, OnInit, OnDestroy {
@@ -149,6 +152,7 @@ export class ThWrapperBase<T, ARGS = unknown> implements ThWrapperLifeCycle, OnC
149152
if (this.autoDispose) {
150153
this.disposeObjRef();
151154
}
155+
this.emitRemoveEvent();
152156
}
153157

154158
protected applyObjRef(objRef: T | undefined) {
@@ -176,6 +180,20 @@ export class ThWrapperBase<T, ARGS = unknown> implements ThWrapperLifeCycle, OnC
176180
}
177181
}
178182

183+
protected emitRemoveEvent() {
184+
// only emit change if _objRef is no proxy,
185+
// and trigger emit over objRef event emitter
186+
if (this._objRef && !isLazyObject3dProxy(this._objRef as any)) {
187+
(this._objRef as unknown as Object3D<ThWrapperEventMap<T>>).dispatchEvent?.({
188+
type: 'removed',
189+
object: this._objRef,
190+
});
191+
if (this._objRef$) {
192+
this._objRef$.next(this._objRef);
193+
}
194+
}
195+
}
196+
179197
protected emitPropertyChanges(changes: SimpleChanges) {
180198
if (this._objRef) {
181199
(this._objRef as unknown as EventDispatcher<ThWrapperEventMap<T>>).dispatchEvent?.({ type: 'changes', changes });

0 commit comments

Comments
 (0)