The following bit of code will work fine as is when the list is unsorted. You 
can add and remove persons as expected.

The problem crops up when we want the list to be sorted. Just adding in sorting 
commands seems to change something that causes the wrong name to be associated 
with the remove action. Any ideas how to fix this problem?

To see the bug, do the following:

  1. Uncomment the call to `sorted` in line **A**
  2. Uncomment the `sort` calls in lines **B** and **C**
  3. Run the app
  4. Add a person named otto
  5. Click the `(x)` next to otto to delete him



Expected:

  * otto is deleted and the list renders the other names in order



Actual:

  * otto is not deleted
  * The person alphabetically after otto is deleted
  * This process is repeated each time you try to delete otto


    
    
    include karax/prelude
    import algorithm, sequtils
    
    var
      people: seq[string] = @["wendy", "sam", "nancy"] #.sorted # line A
      newPersonName = ""
    
    proc mkRemoveAction(person: string): proc() =
      result = proc() =
        echo "trying to remove " & person
        people = filter(people,
                        proc(pA: string): bool =
                          pA != person)
        # sort(people)                # line B
        echo "people = " & $people
    
    proc createDom(): VNode =
      result = buildHtml(tdiv):
        h1: text "Example Bug"
        tdiv:
          input(`type` = "input"):
            proc onchange(ev: Event, n: VNode) =
              echo "value = " & n.value
              newPersonName = $n.value
          button(`type` = "button"):
            text "Add Person"
            proc onclick(ev: Event, n: VNode) =
              echo "trying to add person: " & newPersonName
              people.add(newPersonName)
              # sort(people)          # line C
              echo "People = " & $people
        ul:
          for x in people:
            li:
              text x
              span:
                button(`type` = "button", onclick = mkRemoveAction(x)):
                  text "(X)"
    
    setRenderer createDom
    
    
    Run

Reply via email to