Skip to content

Commit 7ffbd31

Browse files
committed
Proper dispose for reading indicator
1 parent 2de105a commit 7ffbd31

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/LinkDotNet.Blog.Web/Features/ShowBlogPost/Components/ReadingIndicator.razor

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@inject IJSRuntime JSRuntime
2+
@implements IAsyncDisposable
23

34
<div class="progress-container" @ref="progressContainer">
45
<svg class="progress-circle" viewBox="0 0 36 36">
@@ -21,4 +22,9 @@
2122
await JSRuntime.InvokeVoidAsync("initCircularReadingProgress", ContainerCssSelector, progressContainer);
2223
}
2324
}
25+
26+
public async ValueTask DisposeAsync()
27+
{
28+
await JSRuntime.InvokeVoidAsync("destroyCircularReadingProgress");
29+
}
2430
}

src/LinkDotNet.Blog.Web/Features/ShowBlogPost/Components/ReadingIndicator.razor.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ function hideProgressIndicator(progressContainer) {
2222
}, 500);
2323
}
2424

25+
function onScroll(onScroll) {
26+
if (!rafId) {
27+
rafId = requestAnimationFrame(onScroll);
28+
}
29+
}
30+
2531
window.initCircularReadingProgress = (parentContainer, progressContainer) => {
2632
const progressBar = document.getElementById('progressBar');
2733

@@ -44,9 +50,11 @@ window.initCircularReadingProgress = (parentContainer, progressContainer) => {
4450
rafId = null;
4551
};
4652

47-
window.addEventListener('scroll', () => {
48-
if (!rafId) {
49-
rafId = requestAnimationFrame(onScroll);
50-
}
51-
});
53+
window.addEventListener('scroll', onScroll) ;
5254
};
55+
56+
window.destroyCircularReadingProgress = () => {
57+
window.removeEventListener('scroll', onScroll);
58+
clearTimeout(progressTimeout);
59+
cancelAnimationFrame(rafId);
60+
}

0 commit comments

Comments
 (0)