@@ -15,6 +15,7 @@ import {
15
15
ViewContainerRef ,
16
16
afterNextRender ,
17
17
createEnvironmentInjector ,
18
+ effect ,
18
19
inject ,
19
20
input ,
20
21
reflectComponentType ,
@@ -31,7 +32,6 @@ import {
31
32
injectBeforeRender ,
32
33
resolveRef ,
33
34
} from 'angular-three' ;
34
- import { injectAutoEffect } from 'ngxtension/auto-effect' ;
35
35
import { mergeInputs } from 'ngxtension/inject-inputs' ;
36
36
import * as THREE from 'three' ;
37
37
import { Object3D } from 'three' ;
@@ -93,39 +93,45 @@ const STORY_OPTIONS = new InjectionToken<Signal<Record<string, unknown>>>('story
93
93
host : { class : 'storybook-scene' } ,
94
94
} )
95
95
export class StorybookScene {
96
- Math = Math ;
96
+ protected Math = Math ;
97
97
98
- autoEffect = injectAutoEffect ( ) ;
99
- canvasOptions = inject ( CANVAS_OPTIONS ) ;
100
- story = inject ( STORY_COMPONENT ) ;
101
- storyMirror = inject ( STORY_COMPONENT_MIRROR ) ;
102
- storyOptions = inject ( STORY_OPTIONS ) ;
98
+ protected canvasOptions = inject ( CANVAS_OPTIONS ) ;
99
+ private story = inject ( STORY_COMPONENT ) ;
100
+ private storyMirror = inject ( STORY_COMPONENT_MIRROR ) ;
101
+ private storyOptions = inject ( STORY_OPTIONS ) ;
103
102
104
- anchor = viewChild . required ( 'anchor' , { read : ViewContainerRef } ) ;
103
+ private anchor = viewChild . required ( 'anchor' , { read : ViewContainerRef } ) ;
104
+ private ref ?: ComponentRef < unknown > ;
105
+ private inputsMirror ?: string [ ] ;
105
106
106
107
constructor ( ) {
107
- let ref : ComponentRef < unknown > ;
108
-
109
108
afterNextRender ( ( ) => {
110
109
untracked ( ( ) => {
111
- ref = this . anchor ( ) . createComponent ( this . story ) ;
112
-
113
- const componentInputs = this . storyMirror . inputs . map ( ( input ) => input . propName ) ;
114
- this . autoEffect ( ( ) => {
115
- const storyOptions = this . storyOptions ( ) ;
116
- for ( const key of componentInputs ) {
117
- ref . setInput ( key , storyOptions [ key ] ) ;
118
- }
119
- } ) ;
120
-
121
- ref . changeDetectorRef . detectChanges ( ) ;
110
+ this . ref = this . anchor ( ) . createComponent ( this . story ) ;
111
+ this . setStoryOptions ( this . storyOptions ( ) ) ;
112
+ this . ref . changeDetectorRef . detectChanges ( ) ;
122
113
} ) ;
123
114
} ) ;
124
115
116
+ effect ( ( ) => {
117
+ this . setStoryOptions ( this . storyOptions ( ) ) ;
118
+ } ) ;
119
+
125
120
inject ( DestroyRef ) . onDestroy ( ( ) => {
126
- ref ?. destroy ( ) ;
121
+ this . ref ?. destroy ( ) ;
127
122
} ) ;
128
123
}
124
+
125
+ private setStoryOptions ( options : NgtAnyRecord ) {
126
+ if ( ! this . ref ) return ;
127
+ if ( ! this . inputsMirror ) {
128
+ this . inputsMirror = this . storyMirror . inputs . map ( ( input ) => input . propName ) ;
129
+ }
130
+
131
+ for ( const key of this . inputsMirror ) {
132
+ this . ref . setInput ( key , options [ key ] ) ;
133
+ }
134
+ }
129
135
}
130
136
131
137
@Component ( {
@@ -146,7 +152,7 @@ export class StorybookSetup {
146
152
canvasOptions = input ( defaultCanvasOptions , { transform : mergeInputs ( defaultCanvasOptions ) } ) ;
147
153
withLoader = input ( false ) ;
148
154
149
- anchor = viewChild . required ( 'anchor' , { read : ViewContainerRef } ) ;
155
+ private anchor = viewChild . required ( 'anchor' , { read : ViewContainerRef } ) ;
150
156
151
157
constructor ( ) {
152
158
const envInjector = inject ( EnvironmentInjector ) ;
@@ -176,7 +182,6 @@ export class StorybookSetup {
176
182
} ) ;
177
183
178
184
inject ( DestroyRef ) . onDestroy ( ( ) => {
179
- console . log ( 'storybook-setup destroy' ) ;
180
185
ref ?. destroy ( ) ;
181
186
refEnvInjector ?. destroy ( ) ;
182
187
} ) ;
0 commit comments