Skip to content

Commit

Permalink
0.2.4 - computed values and (crate.core/raw ..) for using strings of …
Browse files Browse the repository at this point in the history
…html

Signed-off-by: Chris Granger <ibdknox@gmail.com>
  • Loading branch information
ibdknox committed Jan 17, 2013
1 parent 0dfa2d3 commit 683208c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
(defproject crate "0.2.3"
(defproject crate "0.2.4"
:description "A ClojureScript implementation of Hiccup")
62 changes: 61 additions & 1 deletion src/crate/binding.cljs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns crate.binding (:require [clojure.set :as set]))
(ns crate.binding (:require [clojure.set :as set]))

;;*********************************************************
;; subatom
Expand Down Expand Up @@ -80,6 +80,66 @@
(set! (.-watches sa) nil)
(set! (.-atm sa) nil))

;;*********************************************************
;; computed
;;*********************************************************

(defprotocol computable
(-compute [this] "compute the latest value"))

(deftype Computed [atms value func watches key]

IEquiv
(-equiv [o other] (identical? o other))

IDeref
(-deref [_] value)

IPrintWithWriter
(-pr-writer [this writer opts]
(-write writer (str "#<Computed: " (pr-str value) ">")))

IWatchable
(-notify-watches [this oldval newval]
(doseq [[key f] watches]
(f key this oldval newval)))
(-add-watch [this key f]
(when f
(set! (.-watches this) (assoc watches key f))))
(-remove-watch [this key]
(set! (.-watches this) (dissoc watches key)))

IHash
(-hash [this] (goog.getUid this))

computable
(-compute [this]
(let [old (.-value this)]
(set! (.-value this) (apply func (map deref atms)))
(-notify-watches this old (.-value this))
)))

(defn computed [atms func]
(let [k (gensym "computed")
neue (Computed. atms nil func nil k)]
(-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))))

(def r (computed [y] (fn [y]
(filter #(> % 100) y))))

(swap! z conj 1340)
@y
@r

;;*********************************************************
;;rest
;;*********************************************************
Expand Down
9 changes: 6 additions & 3 deletions src/crate/core.cljs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
(ns crate.core
(:require
[crate.compiler :as compiler]
[crate.util :as util]))
(:require [goog.dom :as gdom]
[crate.compiler :as compiler]
[crate.util :as util]))

(def group-id (atom 0))

(defn raw [html-str]
(gdom/htmlToDocumentFragment html-str))

(defn html [& tags]
(let [res (map compiler/elem-factory tags)]
(if (second res)
Expand Down

0 comments on commit 683208c

Please sign in to comment.