(defn editing-mode? []
"a hardcoded (for the moment) value, will look up in db later"
false)
(def UP 38) ;; goog.events.KeyCodes.UP
(def DOWN 40) ;; goog.events.KeyCodes.DOWN
(def LEFT 37) ;; goog.events.KeyCodes.LEFT
(def RIGHT 39) ;; goog.events.KeyCodes.RIGHT
(def W 87) ;; goog.events.KeyCodes.W
(def S 83) ;; goog.events.KeyCodes.S
(def A 65) ;; goog.events.KeyCodes.A
(def D 68) ;; goog.events.KeyCodes.D
(def E 69) ;; goog.events.KeyCodes.E
(def ESC 27) ;; goog.events.KeyCodes.ESC
(defn delta [e]
;; e is a google closure Event
(js/console.log (.-keyCode e))
(js/console.log (editing-mode?))
(match [(editing-mode?) (.-keyCode e)]
[false 38] [:slide :up]
[false 40] [:slide :down]
[false 37] [:slide :left]
[false 39] [:slide :right]
[false 87] [:slide :up]
[false 83] [:slide :down]
[false 65] [:slide :left]
[false 68] [:slide :right]
[false 69] [:start-editing]
[true 27] [:done-editing]
:else nil))
The above code works. However, If I try to be a little less wordy and use the goog keycodes directly, like so
(match [(editing-mode?) (.-keyCode e)]
[false goog.events.KeyCodes.UP] [:slide :up]
[false goog.events.keyCodes.DOWN] [:slide :down]
...
I get the following cljsbuild error:
...
Caused by: clojure.lang.ExceptionInfo: Invalid local name: goog.events.KeyCodes.UP ...
...
Ok, so I can't use the goog.events.KeyCodes.* themselves, but maybe I can use a def referenced to them? So I try
(match [(editing-mode?) (.-keyCode e)]
[false UP] [:slide :up]
[false DOWN] [:slide :down]
...
This does compile, but now match just isn't working. Every key event matches to the [false UP] match clause (core.match always emits [:slide :up]).
Anyway, the first code example does work. But why can't I use goog.events.KeyCodes.* or references to goog.events.KeyCodes.* in my core.match matcher? Is there something I am missing?
A key part of
core.matchis that symbols will be bound to values rather than being matched against. That is, the current value ofUPis not looked up and used in the match; instead, the symbolUPgets bound to the value(.-keyCode e)whenfalsegets matched.Unfortunately, insofar as I know, there's nothing you can do about that with
core.match. It's very much dependent on literal values. However, since your pattern is fairly simple, you could use(conp = ...).