Created
January 11, 2019 20:08
-
-
Save bostonou/1fd59e620984e54e3d98fb150e425081 to your computer and use it in GitHub Desktop.
clojurescriptmadeeasy.com/blog/real-world-core-async.html
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
(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