-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CircDeps: Split out small screen event handling
Move small screen event handling into its own class, reducing circular dependencies from 41 to 12. Also adjust the handler so we only check the screen size once per update instead of N times, where N is the number of listeners.
- Loading branch information
Showing
7 changed files
with
53 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { BaseLog } from '../../Shared/ConsoleLog.js'; | ||
|
||
/** | ||
* Set of all registered event listeners. | ||
* @type {Set<(e: UIEvent) => void>}*/ | ||
const smallScreenListeners = new Set(); | ||
|
||
let smallScreenCached = false; | ||
|
||
/** | ||
* Initializes the global window resize event listener, which acts as a wrapper around individually registered listeners. */ | ||
export function SetupWindowResizeEventHandler() { | ||
window.addEventListener('resize', (e) => { | ||
if (smallScreenCached === isSmallScreen()) { | ||
return; | ||
} | ||
|
||
BaseLog.verbose(`Window changed from small=${smallScreenCached} to ${!smallScreenCached}, ` + | ||
`triggering ${smallScreenListeners.size} listeners`); | ||
smallScreenCached = !smallScreenCached; | ||
for (const listener of smallScreenListeners) { | ||
listener(e); | ||
} | ||
}); | ||
} | ||
|
||
|
||
/** @returns Whether the current window size is considered small */ | ||
export function isSmallScreen() { return document.body.clientWidth < 768; } | ||
|
||
/** | ||
* Adds an listener to the window resize event. | ||
* Ensures the event is only triggered when the small/large screen threshold is crossed. | ||
* @param {(e: Event) => void} callback */ | ||
export function addWindowResizedListener(callback) { | ||
smallScreenListeners.add(callback); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters