Skip to content

Commit

Permalink
Debounce page refreshes triggered via Turbo streams
Browse files Browse the repository at this point in the history
Fix Turbo 8 refresh debounce in frontend hotwired#1093
  • Loading branch information
brunoprietog committed Dec 4, 2023
1 parent 5c48398 commit ce3fab6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/core/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ScrollObserver } from "../observers/scroll_observer"
import { StreamMessage } from "./streams/stream_message"
import { StreamMessageRenderer } from "./streams/stream_message_renderer"
import { StreamObserver } from "../observers/stream_observer"
import { clearBusyState, dispatch, findClosestRecursively, getVisitAction, markAsBusy } from "../util"
import { clearBusyState, dispatch, findClosestRecursively, getVisitAction, markAsBusy, debounce } from "../util"
import { PageView } from "./drive/page_view"
import { FrameElement } from "../elements/frame_element"
import { Preloader } from "./drive/preloader"
Expand Down Expand Up @@ -44,6 +44,7 @@ export class Session {

constructor(recentRequests) {
this.recentRequests = recentRequests
this.refresh = debounce(this.refresh.bind(this), 150)
}

start() {
Expand Down
17 changes: 16 additions & 1 deletion src/tests/functional/page_refresh_stream_action_tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from "@playwright/test"
import { assert } from "chai"
import { nextBeat } from "../helpers/page"
import { getFromLocalStorage, nextBeat } from "../helpers/page"

test.beforeEach(async ({ page }) => {
await page.goto("/src/tests/fixtures/page_refresh_stream_action.html")
Expand Down Expand Up @@ -42,6 +42,21 @@ test("fetch injects a Turbo-Request-Id with a UID generated automatically", asyn
}
})

test("debounce stream page refreshes", async ({ page }) => {
page.evaluate(() => {
localStorage.setItem("visitsCount", 0)
addEventListener("turbo:visit", () => {
localStorage.setItem("visitsCount", localStorage.getItem("visitsCount") + 1)
})
})

await page.click("#refresh button")
await page.click("#refresh button")
await nextBeat()

assert.equal(await getFromLocalStorage(page, "visitsCount"), 1)
})

async function textContent(page) {
const messages = await page.locator("#content")
return await messages.textContent()
Expand Down
2 changes: 1 addition & 1 deletion src/tests/helpers/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export async function isScrolledToSelector(page, selector) {
}

export function nextBeat() {
return sleep(100)
return sleep(250)
}

export function nextBody(_page, timeout = 500) {
Expand Down
10 changes: 10 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,13 @@ export async function around(callback, reader) {

return [before, after]
}

export function debounce(fn, delay) {
let timeoutId = null

return (...args) => {
const callback = () => fn.apply(this, args)
clearTimeout(timeoutId)
timeoutId = setTimeout(callback, delay)
}
}

0 comments on commit ce3fab6

Please sign in to comment.