Created
June 15, 2016 22:39
-
-
Save guilherme-teodoro/e901bf3f7832f047f0a28cb3f2b78a29 to your computer and use it in GitHub Desktop.
Flash Message queue
This file contains hidden or 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 kitasato.components.flash-message | |
(:require-macros [cljs.core.async.macros :refer [alt! go-loop]]) | |
(:require [reagent.core :as r] | |
[cljs.core.async :refer [close! chan <! timeout put!]])) | |
(defonce bus (chan)) | |
(defonce current (r/atom [nil nil])) | |
(defonce main-loop | |
(go-loop [[item make-control] (<! bus)] | |
(if item | |
(let [control (make-control)] | |
(reset! current [item (fn [] | |
(close! control))]) | |
(<! control) | |
(let [tim (timeout 100)] | |
(alt! bus ([message] (recur message)) | |
tim (recur [nil nil])))) | |
(do (reset! current [nil nil]) | |
(recur (<! bus)))))) | |
(defn add [message] | |
(put! bus [message #(timeout 4000)])) | |
(defn show [] | |
(let [[message cancel] @current] | |
(when message | |
[:div.FlashMessage (:title message) | |
[:span {:on-click #(cancel)} "fechar"]]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment