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

Commit

Permalink
feat(core): change Ref to extends BehaviorSubject
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed Apr 22, 2022
1 parent c57e885 commit 29b14ef
Show file tree
Hide file tree
Showing 31 changed files with 122 additions and 127 deletions.
13 changes: 6 additions & 7 deletions libs/core/meshes/src/skinned-mesh/skinned-mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
NgtInstanceState,
NgtMatrix4,
NgtObject,
NgtRef,
NgtStore,
NGT_HOST_BONE_REF,
NGT_HOST_SKELETON_REF,
Expand All @@ -19,6 +18,7 @@ import {
provideCommonMeshRef,
provideInstanceRef,
provideObjectRef,
Ref,
tapEffect,
} from '@angular-three/core';
import {
Expand Down Expand Up @@ -143,7 +143,7 @@ export class NgtSkeleton extends NgtInstance<THREE.Skeleton, NgtSkeletonState> {
@Optional()
@SkipSelf()
@Inject(NGT_HOST_SKINNED_MESH_REF)
parentHostRef: AnyFunction<NgtRef<THREE.SkinnedMesh>>,
parentHostRef: AnyFunction<Ref<THREE.SkinnedMesh>>,
@Optional() private skinnedMesh: NgtSkinnedMesh
) {
if (parentHostRef && !parentHostRef().value.isSkinnedMesh) {
Expand Down Expand Up @@ -245,21 +245,20 @@ export class NgtBone extends NgtObject<THREE.Bone> {
@Optional()
@SkipSelf()
@Inject(NGT_HOST_BONE_REF)
private hostBoneRef: AnyFunction<NgtRef<THREE.Bone>>,
private hostBoneRef: AnyFunction<Ref>,
@Optional()
@SkipSelf()
@Inject(NGT_HOST_SKELETON_REF)
private hostSkeletonRef: AnyFunction<NgtRef<THREE.Skeleton>>,
private hostSkeletonRef: AnyFunction<Ref>,
@Optional()
@SkipSelf()
@Inject(NGT_HOST_SKINNED_MESH_REF)
private hostSkinnedMeshRef: AnyFunction<NgtRef<THREE.SkinnedMesh>>
private hostSkinnedMeshRef: AnyFunction<Ref>
) {
super(
zone,
store,
() =>
(parentBone?.instance || parentSkinnedMesh?.instance) as NgtRef,
() => (parentBone?.instance || parentSkinnedMesh?.instance) as Ref,
(hostBoneRef || hostSkinnedMeshRef) as AnyFunction
);
}
Expand Down
27 changes: 9 additions & 18 deletions libs/core/src/lib/abstracts/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
pairwise,
pipe,
startWith,
switchMap,
tap,
withLatestFrom,
} from 'rxjs';
Expand All @@ -35,7 +34,6 @@ import type {
AttachFunction,
BooleanInput,
NgtInstanceInternal,
NgtRef,
NgtUnknownInstance,
UnknownRecord,
} from '../types';
Expand All @@ -48,7 +46,7 @@ import { is } from '../utils/is';
import { mutate } from '../utils/mutate';

export interface NgtInstanceState<TInstance extends object = UnknownRecord> {
instance: NgtRef<TInstance>;
instance: Ref<TInstance>;
instanceArgs: unknown[];
attach: string[] | AttachFunction;
noAttach: boolean;
Expand Down Expand Up @@ -106,26 +104,19 @@ export abstract class NgtInstance<
return this.get((s) => s.attach);
}

readonly instance$ = this.select((s) => s.instance).pipe(
switchMap((ref) =>
ref.ref$.pipe(
filter(
(instance): instance is NgtUnknownInstance<TInstance> =>
instance != null
)
)
)
readonly instance$ = this.instance.pipe(
filter((instance): instance is TInstance => instance != null)
);

get instance(): NgtRef<TInstance> {
get instance(): Ref<TInstance> {
return this.get((s) => s.instance);
}

get __ngt__(): NgtInstanceInternal {
return this.instance.value.__ngt__;
return (this.instance.value as NgtUnknownInstance).__ngt__;
}

get parent(): NgtRef {
get parent(): Ref {
if (!this.skipParent) return this.parentRef?.();
return this.parentHostRef?.() || this.parentRef?.();
}
Expand All @@ -147,11 +138,11 @@ export abstract class NgtInstance<
@Optional()
@SkipSelf()
@Inject(NGT_INSTANCE_REF)
protected parentRef: AnyFunction<NgtRef>,
protected parentRef: AnyFunction<Ref>,
@Optional()
@SkipSelf()
@Inject(NGT_INSTANCE_HOST_REF)
protected parentHostRef: AnyFunction<NgtRef>
protected parentHostRef: AnyFunction<Ref>
) {
super();
this.set({
Expand Down Expand Up @@ -479,7 +470,7 @@ export abstract class NgtInstance<
)
);

private checkUpdate(value: NgtUnknownInstance): void {
private checkUpdate(value: unknown): void {
if (is.object3d(value)) {
value.updateMatrix();
} else if (is.camera(value)) {
Expand Down
15 changes: 8 additions & 7 deletions libs/core/src/lib/abstracts/material-geometry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Directive, Input } from '@angular/core';
import * as THREE from 'three';
import type { AnyConstructor, NgtRef, UnknownRecord } from '../types';
import { Ref } from '../ref';
import type { AnyConstructor, UnknownRecord } from '../types';
import { is } from '../utils/is';
import { NgtObject, NgtObjectInputsState } from './object';

Expand All @@ -10,9 +11,9 @@ export interface NgtMaterialGeometryState<
material:
| THREE.Material
| THREE.Material[]
| NgtRef<THREE.Material>
| NgtRef<THREE.Material>[];
geometry: THREE.BufferGeometry | NgtRef<THREE.BufferGeometry>;
| Ref<THREE.Material>
| Ref<THREE.Material>[];
geometry: THREE.BufferGeometry | Ref<THREE.BufferGeometry>;
morphTargetInfluences?: number[];
morphTargetDictionary?: Record<string, number>;
}
Expand All @@ -28,14 +29,14 @@ export abstract class NgtMaterialGeometry<
material:
| THREE.Material
| THREE.Material[]
| NgtRef<THREE.Material>
| NgtRef<THREE.Material>[]
| Ref<THREE.Material>
| Ref<THREE.Material>[]
) {
this.set({ material });
}

@Input() set geometry(
geometry: THREE.BufferGeometry | NgtRef<THREE.BufferGeometry>
geometry: THREE.BufferGeometry | Ref<THREE.BufferGeometry>
) {
this.set({ geometry });
}
Expand Down
5 changes: 2 additions & 3 deletions libs/core/src/lib/abstracts/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import type {
NgtEvent,
NgtEventHandlers,
NgtQuaternion,
NgtRef,
NgtRenderState,
NgtTriple,
NgtUnknownInstance,
Expand Down Expand Up @@ -257,11 +256,11 @@ export abstract class NgtObjectInputs<
@Optional()
@SkipSelf()
@Inject(NGT_OBJECT_REF)
protected parentObjectRef: AnyFunction<NgtRef<THREE.Object3D>>,
protected parentObjectRef: AnyFunction<Ref<THREE.Object3D>>,
@Optional()
@SkipSelf()
@Inject(NGT_OBJECT_HOST_REF)
protected parentObjectHostRef: AnyFunction<NgtRef<THREE.Object3D>>
protected parentObjectHostRef: AnyFunction<Ref<THREE.Object3D>>
) {
super(zone, store, parentObjectRef, parentObjectHostRef);
this.set({
Expand Down
3 changes: 1 addition & 2 deletions libs/core/src/lib/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ import { provideCanvasInstanceRef } from './di/object';
import { NgtResize } from './services/resize';
import { NgtComponentStore } from './stores/component-store';
import { NgtStore } from './stores/store';
import {
import type {
BooleanInput,
NgtCameraOptions,
NgtDpr,
NgtGLOptions,
NgtSceneOptions,
NgtSize,
NgtState,
} from './types';
import { coerceBooleanProperty } from './utils/coercion';
Expand Down
5 changes: 3 additions & 2 deletions libs/core/src/lib/di/attribute.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Provider } from '@angular/core';
import * as THREE from 'three';
import { Ref } from '../ref';
import { NgtCommonAttribute } from '../three/attribute';
import { NGT_COMMON_ATTRIBUTE_REF } from '../tokens';
import type { AnyConstructor, NgtRef } from '../types';
import type { AnyConstructor } from '../types';
import { provideInstanceRef } from './instance';

export function provideCommonAttributeRef<TType extends AnyConstructor<any>>(
subAttributeType: TType,
factory?: (
instance: InstanceType<TType>
) => NgtRef<THREE.BufferAttribute | THREE.InterleavedBufferAttribute>
) => Ref<THREE.BufferAttribute | THREE.InterleavedBufferAttribute>
): Provider {
return [
provideInstanceRef(subAttributeType, factory),
Expand Down
5 changes: 3 additions & 2 deletions libs/core/src/lib/di/audio.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Provider } from '@angular/core';
import * as THREE from 'three';
import { Ref } from '../ref';
import { NgtCommonAudio } from '../three/audio';
import { NGT_COMMON_AUDIO_REF } from '../tokens';
import type { AnyConstructor, NgtRef } from '../types';
import type { AnyConstructor } from '../types';
import { provideObjectRef } from './object';

export function provideCommonAudioRef<TType extends AnyConstructor<any>>(
subAudioType: TType,
factory?: (instance: InstanceType<TType>) => NgtRef<THREE.Object3D>
factory?: (instance: InstanceType<TType>) => Ref<THREE.Object3D>
): Provider {
return [
provideObjectRef(subAudioType, factory),
Expand Down
5 changes: 3 additions & 2 deletions libs/core/src/lib/di/camera.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Provider } from '@angular/core';
import { Ref } from '../ref';
import { NgtCommonCamera } from '../three/camera';
import { NGT_COMMON_CAMERA_REF } from '../tokens';
import type { AnyConstructor, NgtRef } from '../types';
import type { AnyConstructor } from '../types';
import { provideObjectRef } from './object';

export function provideCommonCameraRef<TType extends AnyConstructor<any>>(
subCameraType: TType,
factory?: (instance: InstanceType<TType>) => NgtRef
factory?: (instance: InstanceType<TType>) => Ref
): Provider {
return [
provideObjectRef(subCameraType, factory),
Expand Down
5 changes: 3 additions & 2 deletions libs/core/src/lib/di/curve.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Provider } from '@angular/core';
import { Ref } from '../ref';
import { NgtCommonCurve } from '../three/curve';
import { NGT_COMMON_CURVE_REF } from '../tokens';
import type { AnyConstructor, NgtRef } from '../types';
import type { AnyConstructor } from '../types';
import { provideInstanceRef } from './instance';

export function provideCommonCurveRef<TType extends AnyConstructor<any>>(
subCurveType: TType,
factory?: (instance: InstanceType<TType>) => NgtRef
factory?: (instance: InstanceType<TType>) => Ref
): Provider {
return [
provideInstanceRef(subCurveType, factory),
Expand Down
5 changes: 3 additions & 2 deletions libs/core/src/lib/di/geometry.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Provider } from '@angular/core';
import { Ref } from '../ref';
import { NgtCommonGeometry } from '../three/geometry';
import { NGT_COMMON_GEOMETRY_REF } from '../tokens';
import { AnyConstructor, NgtRef } from '../types';
import { AnyConstructor } from '../types';
import { provideInstanceRef } from './instance';

export function provideCommonGeometryRef<TType extends AnyConstructor<any>>(
subGeometryType: TType,
factory?: (instance: InstanceType<TType>) => NgtRef
factory?: (instance: InstanceType<TType>) => Ref
): Provider {
return [
provideInstanceRef(subGeometryType, factory),
Expand Down
5 changes: 3 additions & 2 deletions libs/core/src/lib/di/helper.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Provider } from '@angular/core';
import { Ref } from '../ref';
import { NgtCommonHelper } from '../three/helper';
import { NGT_COMMON_HELPER_REF } from '../tokens';
import type { AnyConstructor, NgtRef } from '../types';
import type { AnyConstructor } from '../types';
import { provideObjectRef } from './object';

export function provideCommonHelperRef<TType extends AnyConstructor<any>>(
subHelperType: TType,
factory?: (instance: InstanceType<TType>) => NgtRef
factory?: (instance: InstanceType<TType>) => Ref
): Provider {
return [
provideObjectRef(subHelperType, factory),
Expand Down
9 changes: 5 additions & 4 deletions libs/core/src/lib/di/instance.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Provider } from '@angular/core';
import { NgtInstance } from '../abstracts/instance';
import { Ref } from '../ref';
import { NGT_INSTANCE_HOST_REF, NGT_INSTANCE_REF } from '../tokens';
import type { AnyConstructor, AnyFunction, NgtRef } from '../types';
import type { AnyConstructor, AnyFunction } from '../types';

export function provideInstanceRef<TType extends AnyConstructor<any>>(
subType: TType,
factory?: (instance: InstanceType<TType>) => NgtRef
factory?: (instance: InstanceType<TType>) => Ref
): Provider {
return [
{ provide: NgtInstance, useExisting: subType },
Expand All @@ -21,8 +22,8 @@ export function provideInstanceRef<TType extends AnyConstructor<any>>(

export function provideInstanceHostRef<TType extends AnyConstructor<any>>(
subType: TType,
factory: (instance: InstanceType<TType>) => NgtRef,
hostFactory?: (instance: InstanceType<TType>) => AnyFunction<NgtRef>
factory: (instance: InstanceType<TType>) => Ref,
hostFactory?: (instance: InstanceType<TType>) => AnyFunction<Ref>
): Provider {
return [
{
Expand Down
5 changes: 3 additions & 2 deletions libs/core/src/lib/di/light.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Provider } from '@angular/core';
import { Ref } from '../ref';
import { NgtCommonLight } from '../three/light';
import { NGT_COMMON_LIGHT_REF } from '../tokens';
import type { AnyConstructor, NgtRef } from '../types';
import type { AnyConstructor } from '../types';
import { provideObjectRef } from './object';

export function provideCommonLightRef<TType extends AnyConstructor<any>>(
subLightType: TType,
factory?: (instance: InstanceType<TType>) => NgtRef
factory?: (instance: InstanceType<TType>) => Ref
): Provider {
return [
provideObjectRef(subLightType, factory),
Expand Down
5 changes: 3 additions & 2 deletions libs/core/src/lib/di/line.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Provider } from '@angular/core';
import { Ref } from '../ref';
import { NgtCommonLine } from '../three/line';
import { NGT_COMMON_LINE_REF } from '../tokens';
import type { AnyConstructor, NgtRef } from '../types';
import type { AnyConstructor } from '../types';
import { provideMaterialGeometryObjectRef } from './material-geometry';

export function provideCommonLineRef<TType extends AnyConstructor<any>>(
subLineType: TType,
factory?: (instance: InstanceType<TType>) => NgtRef
factory?: (instance: InstanceType<TType>) => Ref
): Provider {
return [
provideMaterialGeometryObjectRef(subLineType, factory),
Expand Down
5 changes: 3 additions & 2 deletions libs/core/src/lib/di/material-geometry.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Provider } from '@angular/core';
import { NgtMaterialGeometry } from '../abstracts/material-geometry';
import { Ref } from '../ref';
import { NGT_MATERIAL_GEOMETRY_OBJECT_FACTORY } from '../tokens';
import type { AnyConstructor, NgtRef } from '../types';
import type { AnyConstructor } from '../types';
import { provideObjectRef } from './object';

export function provideMaterialGeometryObjectRef<
TType extends AnyConstructor<any>
>(
subMaterialGeometryType: TType,
factory?: (instance: InstanceType<TType>) => NgtRef
factory?: (instance: InstanceType<TType>) => Ref
): Provider {
return [
provideObjectRef(subMaterialGeometryType, factory),
Expand Down
5 changes: 3 additions & 2 deletions libs/core/src/lib/di/material.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Provider } from '@angular/core';
import { Ref } from '../ref';
import { NgtCommonMaterial } from '../three/material';
import { NGT_COMMON_MATERIAL_REF } from '../tokens';
import type { AnyConstructor, NgtRef } from '../types';
import type { AnyConstructor } from '../types';
import { provideInstanceRef } from './instance';

export function provideCommonMaterialRef<TType extends AnyConstructor<any>>(
subMaterialType: TType,
factory?: (instance: InstanceType<TType>) => NgtRef
factory?: (instance: InstanceType<TType>) => Ref
): Provider {
return [
provideInstanceRef(subMaterialType, factory),
Expand Down
Loading

0 comments on commit 29b14ef

Please sign in to comment.