changeset 73:0465aaf01664

Keep old context observers until rendering is complete.
author Mikhail Kryshen <mikhail@kryshen.net>
date Mon, 30 Aug 2010 06:01:27 +0400
parents 76f4126aba84
children a823dd0c2736
files src/net/kryshen/indyvon/core.clj
diffstat 1 files changed, 18 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/net/kryshen/indyvon/core.clj	Mon Aug 30 05:47:57 2010 +0400
+++ b/src/net/kryshen/indyvon/core.clj	Mon Aug 30 06:01:27 2010 +0400
@@ -119,7 +119,6 @@
 (def observers (atom nil))
 
 ;; TODO: groups should be weakly referenced.
-;; Need persistent analog of java.util.WeakHashMap.
 (defn add-observer
   "Add observer fn for the target to the specified group."
   [group target f]
@@ -132,15 +131,15 @@
   (swap! observers dissoc group)
   nil)
 
-;; (defn- replace-observer-group*
-;;   [observers old-id new-id]
-;;   (let [group (get observers old-id)]
-;;     (assoc (dissoc observers old-id)
-;;       new-id group)))
+(defn- replace-observer-group*
+  [observers old-id new-id]
+  (let [group (get observers old-id)]
+    (assoc (dissoc observers old-id)
+      new-id group)))
 
-;; (defn- replace-observer-group
-;;   [old-id new-id]
-;;   (swap! observers replace-observer-group* old-id new-id))
+(defn- replace-observer-group
+  [old-id new-id]
+  (swap! observers replace-observer-group* old-id new-id))
 
 (defn update
   "Notify observers."
@@ -341,11 +340,16 @@
        (apply-theme)
        (with-color (:back-color *theme*)
          (.fillRect graphics 0 0 width height))
-       (remove-observer-group layer)
-       (try
-         (render! layer)
-         (finally
-          (commit event-dispatcher))))))
+       (let [tmp-group (Object.)]
+         ;; Keep current context observers until the rendering is complete.
+         ;; Some observers may be invoked twice if they appear in both
+         ;; groups until tmp-group is removed.
+         (replace-observer-group layer tmp-group)
+         (try
+           (render! layer)
+           (finally
+            (remove-observer-group tmp-group)
+            (commit event-dispatcher)))))))
 
 (defn root-size
   ([layer font-context]