Created
October 21, 2015 18:02
-
-
Save ossbase/b593400fee121f57ea05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro def-action | |
[name & {:keys [props]}] | |
(let [defaults {:disabled false}] | |
`(def ~(symbol name) ~(merge defaults props {:type (keyword name)})))) | |
(def-action call) | |
(def-action hangup) | |
(def-action mute) | |
(def-action unmute) | |
(def-action hold) | |
(def-action unhold) | |
(defn deactivate | |
"Action will be deactivation" | |
[action] | |
(merge action {:disabled true})) | |
(defn actions | |
"Select and execute flow for get action" | |
[member] | |
(cond | |
:else (-> [] | |
(call-member-actions member) | |
(call-member-visual-state member)))) | |
(defn call-member-actions | |
"" | |
[actions {{:keys [type] :or {type :idle} :as status} :status}] | |
(cond | |
(= type :calling) (if (:mute status) | |
(conj actions hangup hold unmute) | |
(conj actions hangup hold mute)) | |
(= type :holding) (conj actions hangup unhold) | |
(= type :idle) (conj actions call) | |
:else actions)) | |
(defn call-member-visual-state | |
"" | |
[actions {{:keys [type] :or {type :idle} :as status} :status}] | |
(cond | |
(= type :holding) (if (:mute status) | |
(conj actions (deactivate unmute)) | |
(conj actions (deactivate mute))) | |
(= type :idle) (do (println (deactivate mute)) (conj actions | |
(deactivate hold) | |
(deactivate mute))) | |
:else actions)) | |
;; test get actions | |
(flow-actions {:status {:type :calling}}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi!
I wouldn't start with macros. Some suggestions:
""
.(if (:mute status) ...)
as(if (:mute status) unmute mute)
cond
s can be written ascase
if you want fast and local dispatch or multi-methods if you want slower but extensible dispatch.cond
inactions
makes no sense. It's like callingif false { ingnore } else { doStuff }
in JavaScript.call-member-visual-state
does both data transformations and side effects. Try to extract thedo
block and print statement into another function that you can mark as side-effecting and preserve the other pure functionality.