Skip to content

Commit

Permalink
[image-refresh] Prevent registering refresh handlers twice
Browse files Browse the repository at this point in the history
The global `refreshing_images` variable will track all image elements
which should be refreshed, in order to prevent registering refresh
handlers twice, for example when running multiple instances of this
component.
  • Loading branch information
amotl committed Jun 17, 2023
1 parent 113262d commit 45ac3a2
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion text-panel/image-refresh.html
Expand Up @@ -21,13 +21,19 @@

<script type="text/javascript">

// Track all image elements which should be refreshed, in order to prevent
// registering refresh handlers twice, for example when running multiple
// instances of this component.
window.refreshing_images = []

/**
* Apply image refreshing to all `img` elements which have a `data-refresh` attribute.
*/
function registerRefreshingImages() {
const images = document.getElementsByTagName("img")
for (const image of images) {
if ("data-refresh" in image.attributes) {
if (!window.refreshing_images.includes(image) && "data-refresh" in image.attributes) {
window.refreshing_images.push(image)
console.log(`Registering image refresh for: ${image.src}`)
refreshImage(image)
}
Expand Down

0 comments on commit 45ac3a2

Please sign in to comment.