Fork me on GitHub
#hoplon
<
2017-09-09
>
thedavidmeister01:09:00

@dm3 for stuff like this i'd usually wire up a function that takes a cell and returns a new one that behaves the way i want (probably with do-watch)

thedavidmeister01:09:41

(defn passive-cell
 [active v]
 (j/with-let [c (j/cell @v)]
  (h/do-watch
   active
   (fn [_ n] (reset! c @v)))))

thedavidmeister01:09:21

it seems strange though

thedavidmeister01:09:19

seems like the main reason you want this is because you have the function that has side effects inside the cell= but you want to decide whether the side effects happen at all outside the cell. personally i would just build the contents of the possible alert unconditionally without side effects, then decide whether to actually trigger the alert based on that as step 2.

thedavidmeister01:09:43

(defc= alert-string (when bad? (str "BAD: " (muted indicator))))

thedavidmeister01:09:35

(do-watch alert-string (fn [_ n] (when n (println n))))

thedavidmeister01:09:19

(cell= (when alert-string (println alert-string)))

thedavidmeister01:09:05

the latter can be bundled up into a fn pretty easy if you end up with a lot of boilerplate

thedavidmeister01:09:45

(defn alert-cell
 [c]
 (j/cell= (when c (println c))))

thedavidmeister01:09:17

(alert-cell (cell= (when bad? (str "BAD: " (muted indicator)))))

thedavidmeister01:09:30

you could probably even go a step further with macros to get

thedavidmeister01:09:37

(alert-cell= (when bad? (str "BAD: " (muted indicator))))