Skip to content

Commit 0514ac9

Browse files
committed
refactor: reduce duplicated code
1 parent 5eba7d8 commit 0514ac9

File tree

1 file changed

+32
-38
lines changed

1 file changed

+32
-38
lines changed

src/app/components/game/game-component.component.ts

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ interface HTMLElementExtended extends HTMLElement {
3737
mozRequestFullScreen(): void;
3838
}
3939

40+
function callFullscreenMethod(
41+
target: Record<string, unknown>,
42+
methods: ReadonlyArray<string>,
43+
action: string
44+
): void {
45+
for (const method of methods) {
46+
if (typeof target[method] === 'function') {
47+
try {
48+
const result = (target[method] as () => unknown)();
49+
if (result instanceof Promise) {
50+
result.catch(error => {
51+
log.warn(`Failed to ${action}:`, error);
52+
});
53+
}
54+
} catch (error) {
55+
log.warn(`Failed to ${action}:`, error);
56+
}
57+
return;
58+
}
59+
}
60+
}
61+
4062
@Component({
4163
selector: 'app-game-component',
4264
templateUrl: './game-component.component.html',
@@ -221,47 +243,19 @@ export class GameComponent {
221243
}
222244

223245
exitFullscreen(): void {
224-
const doc = window.document as DocumentExtended;
225-
if (doc.exitFullscreen) {
226-
doc.exitFullscreen()
227-
.catch(error => {
228-
log.warn('Failed to exit fullscreen:', error);
229-
});
230-
} else if (doc.webkitExitFullscreen) {
231-
try {
232-
doc.webkitExitFullscreen();
233-
} catch (error) {
234-
log.warn('Failed to exit fullscreen (webkit):', error);
235-
}
236-
} else if (doc.mozCancelFullScreen) {
237-
try {
238-
doc.mozCancelFullScreen();
239-
} catch (error) {
240-
log.warn('Failed to exit fullscreen (moz):', error);
241-
}
242-
}
246+
callFullscreenMethod(
247+
window.document as unknown as Record<string, unknown>,
248+
['exitFullscreen', 'webkitExitFullscreen', 'mozCancelFullScreen'],
249+
'exit fullscreen'
250+
);
243251
}
244252

245253
requestFullscreen(): void {
246-
const element = document.body as HTMLElementExtended;
247-
if (element.requestFullscreen) {
248-
element.requestFullscreen()
249-
.catch(error => {
250-
log.warn('Failed to enter fullscreen:', error);
251-
});
252-
} else if (element.webkitRequestFullscreen) {
253-
try {
254-
element.webkitRequestFullscreen();
255-
} catch (error) {
256-
log.warn('Failed to enter fullscreen (webkit):', error);
257-
}
258-
} else if (element.mozRequestFullScreen) {
259-
try {
260-
element.mozRequestFullScreen();
261-
} catch (error) {
262-
log.warn('Failed to enter fullscreen (moz):', error);
263-
}
264-
}
254+
callFullscreenMethod(
255+
document.body as unknown as Record<string, unknown>,
256+
['requestFullscreen', 'webkitRequestFullscreen', 'mozRequestFullScreen'],
257+
'enter fullscreen'
258+
);
265259
}
266260

267261
enterFullScreen(): void {

0 commit comments

Comments
 (0)