 
 (defn get-scratch-entid
   "Entity ID of the scratch area with SCRATCH-KEY as the value of its
-  :scratch/key attribute."
+  :scratch/key attribute. Assumes that this scratch area already exists."
   [conn [_ scratch-key]]
-  (tsky/bind '[:find ?e .
-               :in $ ?key
-               :where [?e :scratch/key ?key]]
-             conn scratch-key))
+  (let [r (tsky/bind '[:find ?e .
+                       :in $ ?key
+                       :where [?e :scratch/key ?key]]
+                     conn scratch-key)]
+    (assert @r)
+    r))
 (th-utils/register-sub* get-scratch-entid)
 
-(defn get-scratch-val
-  "Scratch area with entity ID SCRATCH-ENTID."
+(defn get-scratch-contents
+  "Contents of scratch area with entity ID SCRATCH-ENTID."
   [conn [_ scratch-entid]]
-  (reaction
-    (safe-get (d/pull @conn [:scratch/val] scratch-entid) :scratch/val)))
-(th-utils/register-sub* get-scratch-val)
+  (reaction (d/pull @conn '[*] scratch-entid)))
+(th-utils/register-sub* get-scratch-contents)
 
 (defn search-result
   "Result of the material search."

(defn search-submitted
  "Send XHR searching for materials."
  [db [scratch-entid]]
  (let [search-string (get-in (d/pull db [:scratch/val] scratch-entid)
                              [:scratch/val "searchInput"])
        url (str "/gq/" (th-utils/url-encode search-string))]
    (when search-string
      (ajax/GET url

;; and then a random number. Or just the provided scratch-key, but we'd have to
;; do check for collisions, which could be ugly.
(defn get-scratch
  "Adds a scratch entity to the app-db and returns a pair [eid ratom].

  eid is the ID of the scratch entity. It has at least one attribute,
  :scratch/key, a unique key which identifies it. Being a scratch space, you can
  attach arbitrary other attributes to it. Their values should probably be
  scalar, but you might try collections. I'm not sure about this. See also
  https://github.com/tonsky/datascript/issues/69.

  ratom is a reactive atom holding all the attributes of the scratch entity."
  []
  (let [scratch-key (uuid/make-random-squuid)]
    (rf/dispatch-sync [:new-scratch scratch-key])
    (let [scratch-entid @(rf/subscribe [:get-scratch-entid scratch-key])

          scratch-contents-ra
          (rf/subscribe [:get-scratch-contents scratch-entid])]
      [scratch-entid scratch-contents-ra])))

(defn dispatch-scratch
  "Convenience fn around rf/dispatch. Suppose s is what (get-scratch) returned.
