Created
April 20, 2023 12:27
-
-
Save lekoala/7ec2732e2bca73363daeac2d86130b93 to your computer and use it in GitHub Desktop.
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
/** | |
* @callback ElementCallback | |
* @param {HTMLElement} el | |
* @returns {void} | |
*/ | |
const set = new WeakSet(); | |
/** | |
* Initialize an element exactly once | |
* @param {String} selector | |
* @param {ElementCallback} callback | |
*/ | |
export default function initialize(selector, callback) { | |
document.querySelectorAll(selector).forEach( | |
/** | |
* @param {HTMLElement} el | |
*/ | |
(el) => { | |
if (set.has(el)) { | |
return; | |
} | |
set.add(el); | |
callback(el); | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment