From 6d51d7d08c4a4072ed1c14e28483d919f8182378 Mon Sep 17 00:00:00 2001 From: geotre Date: Sat, 15 May 2021 20:22:33 +0100 Subject: [PATCH] Fix issue #192 (#197) --- karax/karax.nim | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/karax/karax.nim b/karax/karax.nim index bae82d2..3b81262 100644 --- a/karax/karax.nim +++ b/karax/karax.nim @@ -37,6 +37,7 @@ type toFocus: Node toFocusV: VNode renderId: int + rendering: bool patches: seq[Patch] # we reuse this to save allocations patchLen: int patchesV: seq[PatchV] @@ -629,8 +630,21 @@ proc avoidDomDiffing*(kxi: KaraxInstance = kxi) = ## This is an experimental API. kxi.currentTree = nil +proc reqFrame(callback: proc()): int {.importc: "window.requestAnimationFrame".} +when false: + proc cancelFrame(id: int) {.importc: "window.cancelAnimationFrame".} + proc dodraw(kxi: KaraxInstance) = if kxi.renderer.isNil: return + kxi.renderId = 0 + + if kxi.rendering: + # there is a render already running, delay 1 frame + kxi.renderId = reqFrame(proc () = kxi.dodraw) + return + + kxi.rendering = true + let rdata = RouterData(hashPart: hashPart) let newtree = kxi.renderer(rdata) inc kxi.runCount @@ -661,16 +675,12 @@ proc dodraw(kxi: KaraxInstance) = # now that it's part of the DOM, give it the focus: if kxi.toFocus != nil: kxi.toFocus.focus() - kxi.renderId = 0 + kxi.rendering = false when defined(stats): kxi.recursion = 0 var total = 0 echo "depth ", depth(kxi.currentTree, total), " total ", total -proc reqFrame(callback: proc()): int {.importc: "window.requestAnimationFrame".} -when false: - proc cancelFrame(id: int) {.importc: "window.cancelAnimationFrame".} - proc redraw*(kxi: KaraxInstance = kxi) = # we buffer redraw requests: when false: