Skip to content

Commit a88606b

Browse files
committed
feat(soba): controls
1 parent 6b61706 commit a88606b

File tree

4 files changed

+31
-38
lines changed

4 files changed

+31
-38
lines changed

libs/core/src/lib/html.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { NgtAnyRecord } from './types';
1717
const NGT_HTML_DOM_ELEMENT = new InjectionToken<'gl' | HTMLElement>('NGT_HTML_DOM_ELEMENT');
1818

1919
export function provideHTMLDomElement(): Provider;
20+
export function provideHTMLDomElement(factory: () => HTMLElement): Provider;
2021
export function provideHTMLDomElement<
2122
TDeps extends Array<ProviderToken<any>>,
2223
TValues extends {
@@ -28,6 +29,10 @@ export function provideHTMLDomElement(...args: any[]) {
2829
return { provide: NGT_HTML_DOM_ELEMENT, useFactory: () => 'gl' };
2930
}
3031

32+
if (args.length === 1) {
33+
return { provide: NGT_HTML_DOM_ELEMENT, useFactory: args[0] };
34+
}
35+
3136
return { provide: NGT_HTML_DOM_ELEMENT, useFactory: args.pop(), deps: args };
3237
}
3338

libs/soba/controls/src/lib/camera-controls.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,10 @@ export class NgtsCameraControls {
116116
});
117117

118118
effect((onCleanup) => {
119-
const [controls, regress, performanceRegress, invalidate] = [
120-
this.controls(),
121-
this.regress(),
122-
this.performanceRegress(),
123-
this.invalidate(),
124-
];
119+
const controls = this.controls();
120+
if (!controls) return;
121+
122+
const [regress, performanceRegress, invalidate] = [this.regress(), this.performanceRegress(), this.invalidate()];
125123

126124
const callback = (e: any) => {
127125
invalidate();
@@ -150,13 +148,13 @@ export class NgtsCameraControls {
150148
});
151149

152150
effect((onCleanup) => {
153-
const [controls, makeDefault] = [this.controls(), this.makeDefault()];
151+
const makeDefault = this.makeDefault();
152+
if (!makeDefault) return;
154153

155-
if (makeDefault) {
156-
const oldControls = this.store.snapshot.controls;
157-
this.store.update({ controls: controls as unknown as EventDispatcher });
158-
onCleanup(() => void this.store.update({ controls: oldControls }));
159-
}
154+
const controls = this.controls();
155+
const oldControls = this.store.snapshot.controls;
156+
this.store.update({ controls: controls as unknown as EventDispatcher });
157+
onCleanup(() => void this.store.update({ controls: oldControls }));
160158
});
161159
}
162160
}

libs/soba/controls/src/lib/orbit-controls.ts

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,7 @@ export class NgtsOrbitControls {
7070
},
7171
{ priority: -1 },
7272
);
73-
this.connectElement();
74-
this.makeControlsDefault();
75-
this.setEvents();
76-
}
7773

78-
private connectElement() {
7974
effect((onCleanup) => {
8075
const [keyEvents, domElement, controls] = [
8176
this.keyEvents(),
@@ -92,29 +87,25 @@ export class NgtsOrbitControls {
9287
}
9388
onCleanup(() => void controls.dispose());
9489
});
95-
}
9690

97-
private makeControlsDefault() {
9891
effect((onCleanup) => {
99-
const [controls, makeDefault] = [this.controls(), this.makeDefault()];
92+
const makeDefault = this.makeDefault();
93+
if (!makeDefault) return;
94+
95+
const controls = this.controls();
10096
if (!controls) return;
101-
if (makeDefault) {
102-
const oldControls = this.store.get('controls');
103-
this.store.update({ controls });
104-
onCleanup(() => void this.store.update({ controls: oldControls }));
105-
}
97+
98+
const oldControls = this.store.get('controls');
99+
this.store.update({ controls });
100+
onCleanup(() => void this.store.update({ controls: oldControls }));
106101
});
107-
}
108102

109-
private setEvents() {
110103
effect((onCleanup) => {
111-
const [controls, invalidate, performanceRegress, regress] = [
112-
this.controls(),
113-
this.invalidate(),
114-
this.performanceRegress(),
115-
this.regress(),
116-
];
104+
const controls = this.controls();
117105
if (!controls) return;
106+
107+
const [invalidate, performanceRegress, regress] = [this.invalidate(), this.performanceRegress(), this.regress()];
108+
118109
const changeCallback: (e: Event) => void = (e) => {
119110
invalidate();
120111
if (regress) performanceRegress();

libs/soba/controls/src/lib/scroll-controls.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,17 @@ export class NgtsScrollControls {
166166
});
167167

168168
effect((onCleanup) => {
169-
const [el, events, size, infinite, invalidate, horizontal, enabled] = [
170-
this.el,
171-
this.events(),
169+
const [el, events] = [this.el, this.events()];
170+
if (!events.connected || events.connected !== el) return;
171+
172+
const [size, infinite, invalidate, horizontal, enabled] = [
172173
this.size(),
173174
this.infinite(),
174175
this.invalidate(),
175176
this.horizontal(),
176177
this.enabled(),
177178
];
178179

179-
if (events.connected !== el) return;
180-
181180
const containerLength = size[horizontal ? 'width' : 'height'];
182181
const scrollLength = el[horizontal ? 'scrollWidth' : 'scrollHeight'];
183182
const scrollThreshold = scrollLength - containerLength;

0 commit comments

Comments
 (0)