Skip to content

Instantly share code, notes, and snippets.

@jaen

jaen/test.clj Secret

Created January 11, 2016 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaen/c59a3e0abcf0c6f6ade2 to your computer and use it in GitHub Desktop.
Save jaen/c59a3e0abcf0c6f6ade2 to your computer and use it in GitHub Desktop.
(deftype Wrapper [deref-reaction deref-fn reset-fn]
ratom/IReactiveAtom
IDeref
(-deref [this]
(-deref deref-reaction))
IHash
(-hash [_]
(hash deref-fn))
IReset
(-reset! [this newval]
(reset-fn newval))
ISwap
(-swap! [this f] (-reset! this (f @deref-reaction)))
(-swap! [this f x] (-reset! this (f @deref-reaction x)))
(-swap! [this f x y] (-reset! this (f @deref-reaction x y)))
(-swap! [this f x y more] (-reset! this (apply f @deref-reaction x y more)))
IPrintWithWriter
(-pr-writer [a w opts]
(ratom/pr-atom deref-reaction w opts "Wrapper:"))
IWatchable
(-notify-watches [this old new]
(#'ratom/notify-w deref-reaction old new))
(-add-watch [this key f]
(#'ratom/add-w deref-reaction key f))
(-remove-watch [this key]
(#'ratom/remove-w deref-reaction key))
ratom/IDisposable
(dispose! [this]
(ratom/dispose! deref-reaction)))
(defn wrap [deref-fn reset-fn]
(let [deref-reaction (ratom/make-reaction deref-fn :auto-run true)]
(Wrapper. deref-reaction deref-fn reset-fn)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment