Skip to content

Instantly share code, notes, and snippets.

@grav
Last active August 29, 2015 14:20
Show Gist options
  • Save grav/5d36a83c6c32c05ed1d1 to your computer and use it in GitHub Desktop.
Save grav/5d36a83c6c32c05ed1d1 to your computer and use it in GitHub Desktop.
promises and clojurescript
(defn setup-midi!
[]
(.then (.requestMIDIAccess js/navigator)
(fn [midi-access]
(.forEach (.-outputs midi-access)
(fn [o]
(let [name (.-name o)]
(when (= "IAC"
(apply str (take 3 name)))
(.then (.open o)
(fn [o]
(println "setting output to" (.-name o))
(swap! app-state
(fn [s] (update-in s [:output] (constantly o)))))))))))))
@uris77
Copy link

uris77 commented May 6, 2015

Naive refactor.

(defn set-output [output]
   (swap! app-state (fn [s] (update-in s [:output] (constantly output))))

(defn reduce-output [output]
    (let [name (.-name output)]
       (when (= "IAC" (apply str (take 3 name)))
       (.then (.open o) set-output))

(defn handle-promise [midi-access]
    (reduce reduce-output (.-outputs midi-access)))

(defn setup-midi! []
   (.then (.requestMIDIAccess js/navigator) handle-promise))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment