Skip to content
Browse files

bulleting and use of Element protocol

Signed-off-by: Chris Granger <ibdknox@gmail.com>
  • Loading branch information...
1 parent 2b1fd8d commit ac992fa48e2818918b14a01b7d5fcbefa10ae058 @ibdknox committed Dec 16, 2013
Showing with 29 additions and 23 deletions.
  1. +25 −23 src/crate/binding.cljs
  2. +4 −0 src/crate/compiler.cljs
View
48 src/crate/binding.cljs
@@ -1,4 +1,4 @@
-(ns crate.binding (:require [clojure.set :as set]))
+(ns crate.binding (:require [clojure.set :as set]))
;;*********************************************************
;; subatom
@@ -10,7 +10,7 @@
(-equiv [o other] (identical? o other))
IDeref
- (-deref [_] (get-in @atm path))
+ (-deref [_] (when atm (get-in @atm path)))
IPrintWithWriter
(-pr-writer [this writer opts]
@@ -85,9 +85,10 @@
;;*********************************************************
(defprotocol computable
- (-compute [this] "compute the latest value"))
+ (-depend [this atm] "depend on an atom")
+ (-compute [this] "compute the latest value"))
-(deftype Computed [atms value func watches key]
+(deftype Computed [atms value func watches key meta]
IEquiv
(-equiv [o other] (identical? o other))
@@ -111,34 +112,35 @@
IHash
(-hash [this] (goog.getUid this))
-
+
+ IMeta
+ (-meta [this] meta)
+
computable
- (-compute [this]
- (let [old (.-value this)]
- (set! (.-value this) (apply func (map deref atms)))
- (-notify-watches this old (.-value this))
+ (-depend [this atm]
+ (set! (.-atms this) (conj (.-atms this) atm))
+ (add-watch atm key (fn [_ _ _ _]
+ (-compute this))))
+ (-compute [this]
+ (let [old (.-value this)
+ nv (apply func (map deref atms))]
+ (set! (.-value this) nv)
+ (-notify-watches this old nv)
)))
(defn computed [atms func]
(let [k (gensym "computed")
- neue (Computed. atms nil func nil k)]
+ neue (Computed. [] nil func nil k nil)]
(-compute neue)
(doseq [atm atms]
- (add-watch atm k (fn [_ _ _ _]
- (-compute neue))))
- neue))
-
-(def z (atom []))
-
-(def y (computed [z] (fn [z]
- (filter even? z))))
+ (-depend neue atm))
+ neue))
-(def r (computed [y] (fn [y]
- (filter #(> % 100) y))))
+(defn compute [compu]
+ (-compute compu))
-(swap! z conj 1340)
-@y
-@r
+(defn depend-on [compu atm]
+ (-depend compu atm))
;;*********************************************************
;;rest
View
4 src/crate/compiler.cljs
@@ -24,9 +24,13 @@
(defn capture-binding [tag b]
(swap! bindings conj [tag b]))
+(defprotocol Element
+ (-elem [this]))
+
(defn as-content [parent content]
(doseq[c content]
(let [child (cond
+ (satisfies? Element c) (-elem c)
(nil? c) nil
(map? c) (throw "Maps cannot be used as content")
(string? c) (gdom/createTextNode c)

0 comments on commit ac992fa

Please sign in to comment.
Something went wrong with that request. Please try again.