Skip to content

Instantly share code, notes, and snippets.

@olivergeorge
Last active July 14, 2021 06:54
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 olivergeorge/a149956af8d137372a954603da15d3e2 to your computer and use it in GitHub Desktop.
Save olivergeorge/a149956af8d137372a954603da15d3e2 to your computer and use it in GitHub Desktop.

It would be nice if reg-fx handlers could take more than one arg. e.g.

(reg-event-fx ::big-red-button-clicked
  (fn [_ _] 
    {:fx [[::change-world 1 2 3]]}))
    
(reg-fx ::change-world
  (fn [a b c]
    (xxx a b c)))

Seems like a modest change to reg-fx :fx would allow this...

Before...

(reg-fx
  :fx
  (fn [seq-of-effects]
    (if-not (sequential? seq-of-effects)
      (console :error "re-frame: \":fx\" effect expects a seq, but was given " (type seq-of-effects))
      (doseq [[effect-key effect-value] (remove nil? seq-of-effects)]
        (when (= :db effect-key)
          (console :warn "re-frame: \":fx\" effect should not contain a :db effect"))
        (if-let [effect-fn (get-handler kind effect-key false)]
          (effect-fn effect-value)
          (console :warn "re-frame: in \":fx\" effect found " effect-key " which has no associated handler. Ignoring."))))))

After...

(reg-fx
  :fx
  (fn [seq-of-effects]
    (if-not (sequential? seq-of-effects)
      (console :error "re-frame: \":fx\" effect expects a seq, but was given " (type seq-of-effects))
;                         NEW
      (doseq [[effect-key & effect-values] (remove nil? seq-of-effects)]
        (when (= :db effect-key)
          (console :warn "re-frame: \":fx\" effect should not contain a :db effect"))
        (if-let [effect-fn (get-handler kind effect-key false)]
;          NEW
          (apply effect-fn effect-values) 
          (console :warn "re-frame: in \":fx\" effect found " effect-key " which has no associated handler. Ignoring."))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment