Skip to content

Commit 57d7c88

Browse files
Merge pull request #126 from Juneezee/cleanup-clduploadwidget-cldvideoplayer
fix: Clean up CldVideoPlayer and CldUploadWidget on astro:before-swap
2 parents 2d7b31c + 5f5d689 commit 57d7c88

File tree

2 files changed

+90
-8
lines changed

2 files changed

+90
-8
lines changed

astro-cloudinary/src/components/CldUploadWidget.astro

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ interface Cloudinary {
5353
createUploadWidget: CloudinaryCreateUploadWidget;
5454
}
5555

56-
window.addEventListener('load', async () => {
56+
async function initCldUploadWidgets() {
5757
const widgets = document.querySelectorAll('.astro-cloudinary-clduploadwidget') as NodeListOf<HTMLSpanElement>;
5858

5959
if ( widgets.length === 0 ) return;
@@ -76,7 +76,7 @@ window.addEventListener('load', async () => {
7676
const cloudinary = window.cloudinary as Cloudinary;
7777

7878
widgets.forEach(widget => {
79-
let widgetInstance: CloudinaryUploadWidget;
79+
let widgetInstance: CloudinaryUploadWidget | undefined;
8080

8181
// Primary user interaction point for the upload widget, create
8282
// an instance if it doesnt already exist (someone triggering before idle)
@@ -98,6 +98,15 @@ window.addEventListener('load', async () => {
9898
widgetInstance.open();
9999
});
100100

101+
// Cleanup
102+
document.addEventListener('astro:before-swap', async () => {
103+
if (widgetInstance) {
104+
widgetInstance.destroy().finally(() => {
105+
widgetInstance = undefined;
106+
});
107+
}
108+
}, { once: true });
109+
101110
// Parse the upload options from the DOM and configure the
102111
// remaining options that couldn't be serialized
103112

@@ -155,5 +164,39 @@ window.addEventListener('load', async () => {
155164
}, resultsCallback);
156165
}
157166
});
158-
})
159-
</script>
167+
}
168+
169+
function useState() {
170+
let isInitialising = false;
171+
let isInitialised = false;
172+
173+
return {
174+
async initialise() {
175+
if (!isInitialising && !isInitialised) {
176+
isInitialising = true;
177+
await initCldUploadWidgets()
178+
.then(() => {
179+
isInitialised = true;
180+
})
181+
.finally(() => {
182+
isInitialising = false;
183+
});
184+
}
185+
},
186+
187+
reset() {
188+
isInitialising = false;
189+
isInitialised = false;
190+
},
191+
};
192+
}
193+
194+
const state = useState();
195+
196+
window.addEventListener('load', state.initialise);
197+
198+
// Lifecycle events, requires <ClientRouter />
199+
// https://docs.astro.build/en/guides/view-transitions/#lifecycle-events
200+
document.addEventListener('astro:after-swap', state.reset);
201+
document.addEventListener('astro:page-load', state.initialise);
202+
</script>

astro-cloudinary/src/components/CldVideoPlayer.astro

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ interface Cloudinary {
8181
videoPlayer: (video: HTMLVideoElement, options: {}) => CloudinaryVideoPlayer;
8282
}
8383

84-
window.addEventListener('load', async () => {
84+
async function initCldVideoPlayers() {
8585
const videos = document.querySelectorAll('.astro-cloudinary-cldvideoplayer') as NodeListOf<HTMLVideoElement>;
8686

8787
if ( videos.length === 0 ) return;
@@ -120,8 +120,13 @@ window.addEventListener('load', async () => {
120120

121121
if ( !player ) return;
122122

123-
// Loop through all avialable player events and create custom event callbacks
123+
// Cleanup
124+
document.addEventListener('astro:before-swap', async () => {
125+
player.dispose();
126+
player.videojs.cloudinary.dispose();
127+
}, { once: true });
124128

129+
// Loop through all avialable player events and create custom event callbacks
125130
const events = [
126131
'loadstart',
127132
'suspend',
@@ -169,5 +174,39 @@ window.addEventListener('load', async () => {
169174
});
170175
});
171176
})
172-
});
173-
</script>
177+
}
178+
179+
function useState() {
180+
let isInitialising = false;
181+
let isInitialised = false;
182+
183+
return {
184+
async initialise() {
185+
if (!isInitialising && !isInitialised) {
186+
isInitialising = true;
187+
await initCldVideoPlayers()
188+
.then(() => {
189+
isInitialised = true;
190+
})
191+
.finally(() => {
192+
isInitialising = false;
193+
});
194+
}
195+
},
196+
197+
reset() {
198+
isInitialising = false;
199+
isInitialised = false;
200+
},
201+
};
202+
}
203+
204+
const state = useState()
205+
206+
window.addEventListener('load', state.initialise);
207+
208+
// Lifecycle events, requires <ClientRouter />
209+
// https://docs.astro.build/en/guides/view-transitions/#lifecycle-events
210+
document.addEventListener('astro:after-swap', state.reset);
211+
document.addEventListener('astro:page-load', state.initialise);
212+
</script>

0 commit comments

Comments
 (0)