GithubHelp home page GithubHelp logo

async-tests's People

Contributors

aruediger avatar swannodette avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

async-tests's Issues

Fixed buffer test weirdness

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)

Reworked Autocomplete

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"))

Problem with mouse demo

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.