Search code examples
clojurescriptom

Having trouble correctly manipulating state in Om app


I'm having trouble manipulating state in an Om app. Specifically, I can't figure out how to remove items from a list.

Here's a demonstration of my folly. It's a simplified app that's broken. https://gist.github.com/rerb/29d10959e71ba1e31e8e

Two buttons are displayed. When pressed, they should remove themselves.

After deleting the first item, I get this error when trying to delete the second:

   Uncaught Error: No protocol method IDeref.-deref defined for type cljs.core/PersistentArrayMap: {:id 2}

If I delete the second one first, I get this error when trying to delete the first:

   Uncaught Error: Assert failed: Can't put nil in on a channel

What simple thing am I missing? I'm Einstellung.


Solution

  • The function remove returns a sequence, not a vector. Your state went from {:buttons [{:id 1} {:id 2}]} to {:buttons ({:id 1})}. By using into after remove you solve your problems:

    (fn [buttons]
      (into [] (remove #(= button-id (:id %)) buttons))))
    

    Note: I tried this with Chestnut and it seems to work. Also, if you are beginning with Om avoid using core.async in the beginning. It is a great complement to Om but using both while learning was too much for me to handle.