Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.

Commit

Permalink
fix(core): ensure to only listen for specific prop on canvas internal
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed Nov 25, 2021
1 parent 6cbbba1 commit db52245
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 32 deletions.
5 changes: 3 additions & 2 deletions packages/controls/src/lib/controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
AnimationStore,
CanvasStore,
DestroyedService,
distinctKeyMap,
NgtAnimationParticipant,
NgtCamera,
UnknownRecord,
Expand Down Expand Up @@ -39,8 +40,8 @@ export abstract class NgtControls<TControls = unknown>
ngOnInit() {
this.ngZone.runOutsideAngular(() => {
this.canvasStore.selectors.internal$
.pipe(takeUntil(this.destroyed))
.subscribe(({ active }) => {
.pipe(distinctKeyMap('active'), takeUntil(this.destroyed))
.subscribe((active) => {
this.ngZone.runOutsideAngular(() => {
const { camera, renderer, scene } =
this.canvasStore.getImperativeState();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { CanvasStore, DestroyedService } from '@angular-three/core';
import {
CanvasStore,
DestroyedService,
distinctKeyMap,
} from '@angular-three/core';
import {
Directive,
EventEmitter,
Expand Down Expand Up @@ -44,8 +48,8 @@ export class NgtAudioListener implements OnInit {
}

this.canvasStore.selectors.internal$
.pipe(takeUntil(this.destroyed))
.subscribe(({ active }) => {
.pipe(distinctKeyMap('active'), takeUntil(this.destroyed))
.subscribe((active) => {
this.ngZone.runOutsideAngular(() => {
const { camera } = this.canvasStore.getImperativeState();
if (active && camera) {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export * from './lib/three/texture';

export * from './lib/utils/apply-props.util';
export * from './lib/utils/build-graph.util';
export * from './lib/utils/distinct-key-map.operator';
export * from './lib/utils/controller';

export * from './lib/services/loader.service';
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/lib/canvas.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { AnimationStore } from './stores/animation.store';
import { CanvasStore } from './stores/canvas.store';
import { EventsStore } from './stores/events.store';
import { InstancesStore } from './stores/instances.store';
import { distinctKeyMap } from './utils/distinct-key-map.operator';

@Component({
selector: 'ngt-canvas',
Expand Down Expand Up @@ -140,8 +141,12 @@ export class NgtCanvasComponent implements OnInit {

private initActiveListener() {
this.canvasStore.selectors.internal$
.pipe(takeUntil(this.destroyed), observeOn(asapScheduler))
.subscribe(({ active }) => {
.pipe(
distinctKeyMap('active'),
takeUntil(this.destroyed),
observeOn(asapScheduler)
)
.subscribe((active) => {
this.ngZone.runOutsideAngular(() => {
if (active) {
const { renderer, camera, scene } =
Expand Down
16 changes: 16 additions & 0 deletions packages/core/src/lib/utils/distinct-key-map.operator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {
distinctUntilKeyChanged,
map,
Observable,
OperatorFunction,
pipe,
} from 'rxjs';

export function distinctKeyMap<T extends object, TKey extends keyof T>(
key: TKey
): OperatorFunction<T, T[TKey]> {
return pipe<Observable<T>, Observable<T>, Observable<T[TKey]>>(
distinctUntilKeyChanged(key),
map((data) => data[key])
);
}
19 changes: 4 additions & 15 deletions packages/postprocessing/src/lib/effect-composer.directive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CanvasStore } from '@angular-three/core';
import { CanvasStore, distinctKeyMap } from '@angular-three/core';
import {
Directive,
EventEmitter,
Expand All @@ -9,14 +9,7 @@ import {
SkipSelf,
} from '@angular/core';
import { ComponentStore } from '@ngrx/component-store';
import {
distinctUntilKeyChanged,
EMPTY,
map,
switchMap,
tap,
withLatestFrom,
} from 'rxjs';
import { EMPTY, switchMap, tap, withLatestFrom } from 'rxjs';
import * as THREE from 'three';
import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer';

Expand Down Expand Up @@ -45,10 +38,7 @@ export class NgtEffectComposer extends ComponentStore<{}> implements OnInit {
ngOnInit() {
this.ngZone.runOutsideAngular(() => {
this.initComposer(
this.canvasStore.selectors.internal$.pipe(
distinctUntilKeyChanged('active'),
map((internal) => internal.active)
)
this.canvasStore.selectors.internal$.pipe(distinctKeyMap('active'))
);
});
}
Expand All @@ -63,8 +53,7 @@ export class NgtEffectComposer extends ComponentStore<{}> implements OnInit {

if (this.watchSizeChanged) {
return this.canvasStore.selectors.internal$.pipe(
distinctUntilKeyChanged('size'),
map((internal) => internal.size),
distinctKeyMap('size'),
tap(({ width, height }) => {
this.ngZone.runOutsideAngular(() => {
this._composer.setSize(width, height);
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import {
CanvasStore,
distinctKeyMap,
EnhancedComponentStore,
NGT_OBJECT_3D_WATCHED_CONTROLLER,
NgtObject3dController,
NgtSize,
} from '@angular-three/core';
import { Inject, Injectable, NgZone, SimpleChanges } from '@angular/core';
import {
distinctUntilKeyChanged,
map,
Observable,
of,
tap,
withLatestFrom,
} from 'rxjs';
import { Observable, of, tap, withLatestFrom } from 'rxjs';
import * as THREE from 'three';

export interface SobaOrthographicCameraState {
Expand Down Expand Up @@ -44,8 +38,7 @@ export const initialSobaOrthographicCameraState: SobaOrthographicCameraState =
@Injectable()
export class SobaOrthographicCameraStore extends EnhancedComponentStore<SobaOrthographicCameraState> {
readonly size$ = this.canvasStore.selectors.internal$.pipe(
distinctUntilKeyChanged('size'),
map((internal) => internal.size)
distinctKeyMap('size')
);

readonly projectMatrixParams$ = this.select(
Expand Down
7 changes: 7 additions & 0 deletions packages/storybook/.storybook/preview-body.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<style>
#root {
width: 100%;
height: 100%;
}
</style>

0 comments on commit db52245

Please sign in to comment.