Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: RangeError Maximum call stack size exceeded #1065

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Bug fix for #1064.
  • Loading branch information
davidjerleke committed Nov 20, 2024
commit 169a7bdc9d72277ec95fea67378148641e0ad240
41 changes: 12 additions & 29 deletions packages/embla-carousel/src/components/Animations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,26 @@ import { EventStore } from './EventStore'
import { WindowType } from './utils'

export type AnimationsUpdateType = (engine: EngineType) => void
export type AnimationsRenderType = (
engine: EngineType,
lagOffset: number
) => void
export type AnimationsRenderType = (engine: EngineType, alpha: number) => void

export type AnimationsType = {
init: () => void
destroy: () => void
start: () => void
stop: () => void
update: () => void
render: (lagOffset: number) => void
render: (alpha: number) => void
}

export function Animations(
ownerDocument: Document,
ownerWindow: WindowType,
update: () => void,
render: (lagOffset: number) => void
render: (alpha: number) => void
): AnimationsType {
const documentVisibleHandler = EventStore()
const fixedTimeStep = 1000 / 60

let lastTimeStamp: number | null = null
let accumulatedTime = 0
let animationId = 0
Expand All @@ -40,36 +38,21 @@ export function Animations(
documentVisibleHandler.clear()
}

function shouldUpdate(): boolean {
return accumulatedTime >= fixedTimeStep
}

function updateAndRemoveAccumulatedTime(): void {
update()
accumulatedTime -= fixedTimeStep
if (shouldUpdate()) updateAndRemoveAccumulatedTime()
}

function renderWithAlpha(): void {
const alpha = accumulatedTime / fixedTimeStep
render(alpha)
}

function animate(timeStamp: DOMHighResTimeStamp): void {
if (!animationId) return

if (!lastTimeStamp) {
lastTimeStamp = timeStamp
update()
renderWithAlpha()
}
if (!lastTimeStamp) lastTimeStamp = timeStamp

const timeElapsed = timeStamp - lastTimeStamp
lastTimeStamp = timeStamp
accumulatedTime += timeElapsed

if (shouldUpdate()) updateAndRemoveAccumulatedTime()
renderWithAlpha()
while (accumulatedTime >= fixedTimeStep) {
update()
accumulatedTime -= fixedTimeStep
}

const alpha = accumulatedTime / fixedTimeStep
render(alpha)

if (animationId) {
animationId = ownerWindow.requestAnimationFrame(animate)
Expand Down
Loading