Skip to content

Commit

Permalink
replace nextAnimationFrame with nextRepaint to render stream element …
Browse files Browse the repository at this point in the history
…updates (#1042)

* replace nextAnimationFrame by nextMicroTask

* fix linter warnings

* use nextEventLoopTick

* use nextMicroTask in stream element

* event loop tick

* useNextAnimationFrame setting on session

* revert to initial

* conditioned nexTAnimationFrame

* use nextEventTick on stream_message_renderer only

* update stream element tests

* nextRepaint util function with browsert tab awareness

* integrate nextRepaint into stream element test

* await on nextRepaint on stream element

* restore session cache & nextRepaint returns sync

* restore nextAnimationFrame on strea element test
  • Loading branch information
michelson authored Nov 1, 2023
1 parent 0613595 commit 3d48c86
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/elements/stream_element.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StreamActions } from "../core/streams/stream_actions"
import { nextAnimationFrame } from "../util"
import { nextRepaint } from "../util"

// <turbo-stream action=replace target=id><template>...

Expand Down Expand Up @@ -43,7 +43,7 @@ export class StreamElement extends HTMLElement {
const event = this.beforeRenderEvent

if (this.dispatchEvent(event)) {
await nextAnimationFrame()
await nextRepaint()
await event.detail.render(this)
}
})())
Expand Down
8 changes: 8 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ export function dispatch(eventName, { target, cancelable, detail } = {}) {
return event
}

export function nextRepaint() {
if (document.visibilityState === "hidden") {
return nextEventLoopTick()
} else {
return nextAnimationFrame()
}
}

export function nextAnimationFrame() {
return new Promise((resolve) => requestAnimationFrame(() => resolve()))
}
Expand Down

0 comments on commit 3d48c86

Please sign in to comment.