Skip to content

Commit 387f330

Browse files
committed
Add a oncePerRefresh function.
1 parent cb53903 commit 387f330

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/lib/once-per-refresh.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const hasRunSinceTheLastFullPageload = new Set<string>()
2+
3+
/**
4+
* This function helps you run a given function only once per full pageload.
5+
* So this will not run again due to turbolink navigation.
6+
*/
7+
export function oncePerRefresh(key: string, fun: () => void) {
8+
if (hasRunSinceTheLastFullPageload.has(key)) {
9+
return
10+
}
11+
fun()
12+
hasRunSinceTheLastFullPageload.add(key)
13+
}

0 commit comments

Comments
 (0)