Skip to content

Instantly share code, notes, and snippets.

@bostonou
Created January 11, 2019 20:08
Show Gist options
  • Save bostonou/1fd59e620984e54e3d98fb150e425081 to your computer and use it in GitHub Desktop.
Save bostonou/1fd59e620984e54e3d98fb150e425081 to your computer and use it in GitHub Desktop.
clojurescriptmadeeasy.com/blog/real-world-core-async.html
(ns cljsmadeeasy.core
(:require [cljs.core.async :as a])
(:require-macros [cljs.core.async.macros :refer [go]]))
(defn fill-load-chan [number-of-pages]
(let [partitions (partition-all NUM-PAGES-PER-LOAD
(range 1 (inc number-of-pages)))
c (a/chan)]
(go
(doseq [page-numbers partitions]
(a/>! c page-numbers))
(a/close! c))
c))
(defn load-from-chan [c]
(go
(loop []
(when-let [page-numbers (a/<! c)]
(doseq [page-number page-numbers]
(load-page! page-number))
(a/<! (a/timeout WAIT-BETWEEN-LOAD))
(recur)))))
(defn load-specific-page [c-mix page-number]
(let [c (a/chan)]
(a/toggle queue-mix {c {:solo true}})
(a/put! c [page-number])
(a/close! c))
(def main-chan (a/chan))
(def c-mix (a/mix main-chan))
(a/admix c-mix (fill-load-chan num-pages))
(load-from-chan main-chan))
;;load a specific page when needed
(load-specific-page c-mix page-number)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment