Skip to content

Commit 7e2048b

Browse files
committed
docs: adjust setup-canvas
1 parent f33136c commit 7e2048b

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

libs/soba/src/setup-canvas.ts

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
ViewContainerRef,
1616
afterNextRender,
1717
createEnvironmentInjector,
18+
effect,
1819
inject,
1920
input,
2021
reflectComponentType,
@@ -31,7 +32,6 @@ import {
3132
injectBeforeRender,
3233
resolveRef,
3334
} from 'angular-three';
34-
import { injectAutoEffect } from 'ngxtension/auto-effect';
3535
import { mergeInputs } from 'ngxtension/inject-inputs';
3636
import * as THREE from 'three';
3737
import { Object3D } from 'three';
@@ -93,39 +93,45 @@ const STORY_OPTIONS = new InjectionToken<Signal<Record<string, unknown>>>('story
9393
host: { class: 'storybook-scene' },
9494
})
9595
export class StorybookScene {
96-
Math = Math;
96+
protected Math = Math;
9797

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);
103102

104-
anchor = viewChild.required('anchor', { read: ViewContainerRef });
103+
private anchor = viewChild.required('anchor', { read: ViewContainerRef });
104+
private ref?: ComponentRef<unknown>;
105+
private inputsMirror?: string[];
105106

106107
constructor() {
107-
let ref: ComponentRef<unknown>;
108-
109108
afterNextRender(() => {
110109
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();
122113
});
123114
});
124115

116+
effect(() => {
117+
this.setStoryOptions(this.storyOptions());
118+
});
119+
125120
inject(DestroyRef).onDestroy(() => {
126-
ref?.destroy();
121+
this.ref?.destroy();
127122
});
128123
}
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+
}
129135
}
130136

131137
@Component({
@@ -146,7 +152,7 @@ export class StorybookSetup {
146152
canvasOptions = input(defaultCanvasOptions, { transform: mergeInputs(defaultCanvasOptions) });
147153
withLoader = input(false);
148154

149-
anchor = viewChild.required('anchor', { read: ViewContainerRef });
155+
private anchor = viewChild.required('anchor', { read: ViewContainerRef });
150156

151157
constructor() {
152158
const envInjector = inject(EnvironmentInjector);
@@ -176,7 +182,6 @@ export class StorybookSetup {
176182
});
177183

178184
inject(DestroyRef).onDestroy(() => {
179-
console.log('storybook-setup destroy');
180185
ref?.destroy();
181186
refEnvInjector?.destroy();
182187
});

0 commit comments

Comments
 (0)