On Sep 22, 10:55 pm, "Stuart Sierra" <[EMAIL PROTECTED]>
wrote:
> Ok, here's another version of Cells, attached. This one uses Agents,
> like MikeM wrote. It works even on a 10000-link chain of dependencies
> -- see examples in the comment at the end. Cells get updated
> asynchronously via actions on Agents. I may be missing some (dosync
> ...) wrappers in there.
>
> I discovered, however, that asynchronous updates may not be what you
> want! For example, my Swing demo doesn't work correctly now -- the
> red square leaves behind a trail of visual artifacts, probably because
> the repaint triggers are getting called in random order from multiple
> threads. So maybe Refs were better, or maybe I'm using Agents
> incorrectly.
>
I've added some support for agent watches. You should consider it
experimental, but it will allow the creation of cell-like and other
reactive systems.
It takes the form of 2 functions, add-watch and remove-watch.
add-watch [#^clojure.lang.Agent a watcher callback]
Adds a watcher to an agent. Whenever the agent runs an action, any
registered watchers will have their callback function called. The
callback fn will be passed 3 args, the watcher, the agent and a
boolean
which will be true if the agent's state was (potentially) changed by
the action. The callback fn is run synchronously with the action,
and thus derefs of the agent in the callback will see the value set
during that action. Because it is run on the action thread, the
callback should not block, but can send messages.
This should make constructing things like cells much easier and more
robust.
In particular, it will work with the native agent interface, no extra
layer needed.
Try it out - feedback welcome!
Rich
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---