karaxnim / karax

Karax. Single page applications for Nim.
MIT License
1.08k stars 91 forks source link

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

Open timotheecour opened 5 years ago

timotheecour commented 5 years ago

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 commented 5 years ago

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