karax icon indicating copy to clipboard operation
karax copied to clipboard

[low priority] give more informative error msg when reusing dthunk

Open timotheecour opened this issue 4 years ago • 1 comments

D20190929T173159 reduced example:

import karax / [kbase, vdom, kdom, karax, karaxdsl]
import js/jsffi

var elem = document.createTextNode("hello world")

proc foo(): VNode =
  result = buildHtml(tdiv):
    dthunk(elem)

proc createDom(): VNode =
  result = buildHtml(tdiv):
    foo()
    foo()

when isMainModule:
  setRenderer createDom, "ROOT"

gives: Uncaught Error: Error: unhandled exception: /Users/timothee/git_clone/nim/karax/karax/karax.nim(649, 12) same(kxi.currentTree, document.getElementById(kxi.rootId)) [AssertionError]

(same error as https://github.com/pragmagic/karax/issues/86 but seems unrelated)

timotheecour avatar Sep 30 '19 17:09 timotheecour

EDIT: cloneNode is needed to insert same DOM Node twice, see https://stackoverflow.com/questions/20203143/how-to-insert-the-same-html-element-twice

  # workaround
  import karax / [kbase, vdom, kdom, karax, karaxdsl]
  import js/jsffi

  var elem = document.createTextNode("hello world")
  let elem2 = elem.cloneNode(false)

  proc createDom(): VNode =
    result = buildHtml(tdiv):
      dthunk(elem)
      # dthunk(elem) # would give error
      dthunk(elem2) # works

  when isMainModule:
    setRenderer createDom, "ROOT"

but maybe karax could provide a better error msg in this case, pointing to the 2 usages of the same Node; so leaving this issue open as low priority

timotheecour avatar Sep 30 '19 18:09 timotheecour