5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

behavior定義の処理の流れを追う

Posted at

lt.macros/behaviorマクロがどのように動くかのメモ

lt/macros.clj

lt.macros.clj抜粋
(defn- namify [type keyword]
  (symbol (str "__" type "__" (.replace (name keyword) "." "__DOT__"))))

(defmacro behavior [name & {:keys [reaction] :as r}]
  (if (and (seq? reaction) (= 'fn (first reaction)))
    (let [[_ args & body] reaction]
      `(do
         (defn- ~(namify "BEH" name) ~args ~@body)
         (lt.object/behavior* ~name ~@(apply concat (assoc r :reaction (namify "BEH" name))))))
    `(lt.object/behavior* ~name ~@(apply concat r))))
  • :reaction(fn ... )のような関数定義の場合
    1. その関数をdefn-で定義。関数名は__BEH__nameのような形
    2. :reactiondefn-で定義した関数を設定してlt.object/behavior*を呼ぶ。
  • :reactionが存在しないor関数定義でない場合、lt.object/behavior*を呼ぶ。(引数はbehaviorに渡されたものそのまま)

lt/object.cljs

lt.object/behavior*の流れ

lt.object.cljs抜粋
(def behaviors (atom {}))
(defn add-behavior [beh]
  (swap! behaviors assoc (:name beh) beh))

(defn make-behavior* [name & r]
  (let [be (merge {:name name}
                  (apply hash-map r))]
    be))

(defn store-behavior* [beh]
  (add-behavior beh)
  (:name beh))

(defn wrap-throttle [beh]
  (if-let [thr (:throttle beh)]
    (assoc beh :reaction (throttle thr (:reaction beh)))
    beh))

(defn wrap-debounce [beh]
  (if-let [thr (:debounce beh)]
    (assoc beh :reaction (debounce thr (:reaction beh)))
    beh))

(defn behavior* [name & r]
  (-> (apply make-behavior* name r)
      (wrap-throttle)
      (wrap-debounce)
      (store-behavior*)))
  1. make-behavior*で引数をマップに変換する
  2. wrap-throttleでマップに:throttleがあったら、:reactionの関数にthrottleを適用した結果の関数を:reactionに設定したマップに変換する
  3. wrap-debounceも同様
  4. できたマップを、store-behavior*経由add-behaviorでatomのマップに追加する

throttle, debounceとは

ここらへんが参考になる。イベント処理の間引きを行うものらしい
http://blog.springdawn.info/post/40885154200/javascript-throttle-debounce
http://ktkne.st/elab/post/2012/strcount-throttle-debounce.html

behaviorができたら・・・

behaviorを定義しただけだと何も起こらない(?)
objectとbehaviorを(tag経由で?)結びつける

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?