swannodette / async-tests Goto Github PK
View Code? Open in Web Editor NEWHaving fun with core.async
Having fun with core.async
The following does not produce expected results. Any thoughts?
(defn log-obj [o]
(.log js/console o))
(defn drain [c]
(go
(while true
(log-obj (str "take: " (<! c))))))
(defn fixed-buf [b]
(let [c (chan b)]
(go (loop [n 1]
(log-obj (str "put: " n))
(>! c n)
(when (<= n 20)
(recur (inc n)))))
(drain c)))
(fixed-buf 1)
I reworked the autocomplete example for anyone who is interested.
(ns async-test.autocomplete.core
(:require
[cljs.core.async :refer [<! >! chan dropping-buffer]]
[clojure.string :as string]
[async-test.utils.helpers
:refer [event-chan by-id set-class throttle
clear-class jsonp-chan set-html multiplex
map-chan filter-chan fan-in distinct-chan
by-tag-name]])
(:require-macros
[cljs.core.async.macros :refer [go]]
[async-test.utils.macros :refer [go-loop]]))
(def wikipedia-endpoint
"http://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=")
; Key codes
(def ENTER 13)
(def UP_ARROW 38)
(def DOWN_ARROW 40)
(def SELECTOR_KEYS #{ENTER UP_ARROW DOWN_ARROW})
; Start helpers
(extend-type js/NodeList
ISeqable
(-seq [nlist] (array-seq nlist 0))
ICounted
(-count [nlist] (alength nlist))
IIndexed
(-nth
([nlist n]
(if (< n (alength nlist)) (aget nlist n)))
([nlist n not-found]
(if (< n (alength nlist)) (aget nlist n)
not-found)))
ILookup
(-lookup
([nlist k]
(aget nlist k))
([nlist k not-found]
(-nth nlist k not-found))))
(defn set-val [el v]
(set! (.-value el) v))
(defn toggle-visibility [el show]
(if show (clear-class el)
(set-class el "hidden")))
; End helpers
(defprotocol IAutocomplete
(show-results [this xs])
(highlight [this idx])
(selector [this select res])
(search [this]))
(deftype Autocomplete
[base-url input-el ac-el]
IAutocomplete
(show-results [this xs]
(let [rs (by-id "completions")]
(->> (for [x xs] (str "<li>" x "</li>"))
(apply str)
(set-html rs))))
(highlight [this idx]
(let [items (by-tag-name ac-el "li")]
(.log js/console (str "index: " idx))
(doseq [item items]
(clear-class item))
(if-let [el (get items idx)]
(set-class el "selected"))))
(selector [this select [q xs]]
(let [num-items (dec (count xs))]
(go
(loop [selected ::none]
(let [k (<! select)]
(condp = k
UP_ARROW
(let [selected
(cond (= selected ::none) num-items
(> selected 0) (dec selected)
:else num-items)]
(.log js/console "up arrow")
(highlight this selected)
(recur selected))
DOWN_ARROW
(let [selected
(cond (= selected ::none) 0
(< selected num-items) (inc selected)
:else 0)]
(.log js/console "down arrow")
(highlight this selected)
(recur selected))
ENTER
(let [selected (get xs selected)]
(.log js/console (str "enter: " selected))
(set-val input-el selected)
(set-html ac-el ""))))))))
(search [this]
(let [keystrokes
(:chan (event-chan input-el "keydown"))
[keystrokes' keystrokes'']
(multiplex keystrokes 2)
key-codes
(map-chan (chan (dropping-buffer 0))
#(.-keyCode %)
keystrokes')
paste
(:chan (event-chan input-el "paste"))
input-changed
(fan-in [keystrokes'' paste])
text-input
(-> (map-chan #(-> (.-value input-el)
string/trim)
input-changed)
distinct-chan)
[text' text''] (multiplex text-input 2)
blur
(:chan (event-chan input-el "blur"))
focus
(:chan (event-chan input-el "focus"))
not-blank?
(complement string/blank?)
display
(fan-in [(map-chan not-blank? text')
(map-chan (constantly false) blur)
(map-chan (constantly true) focus)])
fetch
(throttle (filter-chan #(>= (count %) 2)
text'')
100)]
(go-loop
(let [d (<! display)]
(.log js/console (str "display: " d))
(toggle-visibility ac-el d)))
(go-loop
(let [q (<! fetch)
res (<! (jsonp-chan (str base-url q)))
select
(filter-chan (chan (dropping-buffer 0))
SELECTOR_KEYS
key-codes)]
(.log js/console (str "fetching: " q))
(.log js/console res)
(show-results this (second res))
(selector this select res))))))
(defn autocomplete [base-url input-el ac-el]
(-> (Autocomplete. base-url input-el ac-el)
search))
(autocomplete wikipedia-endpoint
(by-id "input")
(by-id "completions"))
Not sure if it's my setup that's at fault, but I've got a bug in Chrome and Firefox where the mouse demo works for mouse events, but for key events displays an error and then the js stops running and I have to close the window to get it working again.
I think the error is thrown in this line : [ {"keyCode" code} ] (set-html! key-div code)
When I save the key event into a variable and look at in the console it does have the keyCode property set.
The error in Chrome is:
[Error] TypeError: 'undefined' is not an object (evaluating 'cljs.core.match.backtrack')
handler (mouse.js, line 2538)
switch__5098__auto__ (mouse.js, line 2539)
state_machine__5099__auto____1 (mouse.js, line 2543)
state_machine__5099__auto__ (mouse.js, line 2544)
run_state_machine (mouse.js, line 1739)
run_state_machine_wrapped (mouse.js, line 1740)
(anonymous function) (mouse.js, line 1747)
(anonymous function) (mouse.js, line 1847)
(anonymous function) (mouse.js, line 1794)
process_messages (mouse.js, line 1773)
onmessage (mouse.js, line 1775)
The error in Firefrox is:
[Error] TypeError: 'undefined' is not an object (evaluating 'cljs.core.match.backtrack')
handler (mouse.js, line 2538)
switch__5098__auto__ (mouse.js, line 2539)
state_machine__5099__auto____1 (mouse.js, line 2543)
state_machine__5099__auto__ (mouse.js, line 2544)
run_state_machine (mouse.js, line 1739)
run_state_machine_wrapped (mouse.js, line 1740)
(anonymous function) (mouse.js, line 1747)
(anonymous function) (mouse.js, line 1847)
(anonymous function) (mouse.js, line 1794)
process_messages (mouse.js, line 1773)
onmessage (mouse.js, line 1775)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.