Skip to content

Commit

Permalink
chore: Don't restart the animator loop if already running
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Jan 15, 2025
1 parent 246884e commit d44a1eb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ internal partial class RenderingLoopAnimator

internal static IDisposable RegisterFrameEvent(Action action)
{
var requiresEnable = _frameHandlers.IsEmpty;

var disposable = WeakEventHelper.RegisterEvent(
_frameHandlers,
action,
(h, s, a) => (h as Action)?.Invoke());

NativeMethods.SetEnabled(true);
if (requiresEnable)
{
NativeMethods.SetEnabled(true);
}

return Disposable.Create(() =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@
}

private static scheduleAnimationFrame() {
RenderingLoopAnimator._frameRequestId = window.requestAnimationFrame(RenderingLoopAnimator.onAnimationFrame);
if (RenderingLoopAnimator._frameRequestId == null) {
RenderingLoopAnimator._frameRequestId = window.requestAnimationFrame(RenderingLoopAnimator.onAnimationFrame);
}
}

private static onAnimationFrame() {
RenderingLoopAnimator.dispatchFrame();

RenderingLoopAnimator._frameRequestId = null;

if (RenderingLoopAnimator._isEnabled) {
RenderingLoopAnimator.scheduleAnimationFrame();
} else {
RenderingLoopAnimator._frameRequestId = null;
}
}

Expand Down

0 comments on commit d44a1eb

Please sign in to comment.