lumo

:lumo: Standalone ClojureScript environment. Currently at version 1.9.0
mattford 2017-06-11T01:36:17.646766Z

With these two macros

mattford 2017-06-11T01:36:36.647536Z

(ns guller.macros)

(defmacro then->>
  [x & forms]
  (loop [x x, forms forms]
    (if forms
      (let [form (first forms)
            threaded (if (seq? form)
                       `(.then ~x #(~(first form) ~@(next form) %))
                       `(.then ~x #(~form %)))]
        (recur threaded (next forms)))
      x)))

(defmacro then->
  [x & forms]
  (loop [x x, forms forms]
    (if forms
      (let [form (first forms)
            threaded (if (seq? form)
                       `(.then ~x #(~(first form) % ~@(next form)))
                       `(.then ~x #(~form %)))]
        (recur threaded (next forms)))
      x)))

mattford 2017-06-11T01:36:48.648057Z

I was able to write the code like this

mattford 2017-06-11T01:37:08.648829Z

(ns guller.core
  (:require-macros [guller.macros :refer [then-> then->>]]))

(require '[cljs.pprint :as pp :refer [pprint]])

(def github (js/require "github"))
(def githubapi (github.))

(defn flip
  "given a function, create a flipped 2-argument function"
  [f]
  (fn [a b] (f b a)))

(defn getOrgs
  "Get the organisations for an authenticated user"
  []
  (then-> (.. githubapi -users (getOrgs {}))
          (js->clj :keywordize-keys true)
          :data
          ((flip map) :login)))

(defn getReposForOrg
  "Get all the repos for an Org"
  [org]
  (then-> (.. githubapi -repos (getForOrg #js {:org org}))
          (js->clj :keywordize-keys true)
          :data
          ((flip map) :ssh_url)
          pprint))

(defn -main []
  (.. githubapi (authenticate #js {:type "netrc"}))
  (then->> (getOrgs)
           (mapv getReposForOrg)))

mattford 2017-06-11T01:38:21.651751Z

Seems to do a good job of hiding call-backs and promises.

mattford 2017-06-11T01:40:03.655673Z

The threading looks familiar.

mattford 2017-06-11T01:40:46.657626Z

Not sure if I prefer the reduce (see history) version or not as gives more control over the position of the response.

mattford 2017-06-11T09:13:31.764543Z

Actually it’s not that good; it works over synchronous call backs i.e., doesn’t actually chain promises. Hmmmmm.

pesterhazy 2017-06-11T13:15:48.437155Z

Interesting stuff