LightTable的結構(一)

LightTable使用clojurescript來生成js,而後使用 node-webkit 來處理UIjavascript

 

clojurescript很是適合作抽象程度很高的頁面和編輯邏輯結構設計,html

最近會對總體進行分析整理一個大題的結構圖java

 

程序的入口在lt.objs.app@initnode

能夠看到init作了幾件事情進行初始化git

 

(defn init []
  (object/raise app :deploy)
  (object/raise app :pre-init)
  (object/raise app :init)
  (object/raise app :post-init)
  (object/raise app :show))

  而操做的對象,app的初始化在: github

(object/object* ::app
                :tags #{:app :window}
                :delays 0
                :init (fn [this]
                        (ctx/in! :app this)))

(def app (object/create ::app))

 

app是object/object*和object/create一塊兒生成的web

 

(defn object* [name & r]
  (-> (apply make-object* name r)
      (store-object*)
      (handle-redef)))
(defn make-object* [name & r]
  (let [obj (merge {:behaviors #{} :tags #{} :triggers [] :listeners {} ::type name :children {}}
                   (apply hash-map r))]
    obj))

(defn store-object* [obj]
  (add obj)
  obj)
(defn add [obj]
  (swap! object-defs assoc (::type obj) obj))

  

(defn create [obj-name & args]
  (let [obj (if (keyword? obj-name)
              (@object-defs obj-name)
              obj-name)
        id (or (::id obj) (swap! obj-id inc))
        inst (atom (assoc (dissoc obj :init)
                     ::id id
                     :args args
                     :behaviors (set (:behaviors obj))
                     :tags (set (conj (:tags obj) :object))))
        inst (store-inst inst)
        _ (merge! inst (update-listeners inst))
        content (when (:init obj)
                  (apply (:init obj) inst args))
        content (if (vector? content)
                  (crate/html content)
                  content)
        final (merge! inst {:content content})]

    (add-watch inst ::change (fn [_ _ _ _]
                               (raise inst :object.change)))
    (raise* inst (trigger->behaviors :object.instant (:tags @inst)) nil)
    (raise inst :init)
    inst))

  

object/object*生成了一個hash-map的對象,對象包含了behavior,tag之類的屬性,每一個object對應一個惟一的名字,存在object-defs這個atom對象裏,這個object-defs用於記錄全部的類對象的定義app

 

object/create中,先是經過(@object-defs obj-name)來獲取object,而後對object作一些建立的工做,ide

inst是instance的縮寫,採用了相似javascript裏的clone的方式來建立一個object的instancepost

相關文章
相關標籤/搜索