2010/8/5 Laurent PETIT <laurent.pe...@gmail.com> > Hello, > > My "problem": the user types in the ccw's editor. > Each time she types, there's an opportunity to reparse the editor's > content. > But the reparse is not always necessary, especially when the types a > symbol. > > So I could use a delay to only force the repase "on-demand". > On the other end, when the user stops typing for a while (say 500 ms), it > could be interesting to automatically trigger a reparse, so that the next > editor command implying a "fresh parsetree" seems to be instantaneous. > > So I could use a "delayed future". A future which would do nothing the > first n milliseconds (eg n = 500 ms), and then do its job. > But I can't use a "classic" future, 'cause I want the deref of this > "delayed future" to bypass the pause. > > Is there an classical idiom for this ? > > Here is what I've come up in my head : > * create a datastructure with 2 fields, one holding a future, one holding > a delay. the future will "wait" for n millisecs, and then force the delay. > * The datastructure will accept the future API and delegate all calls to > the future,*except* for the deref call which will bypass the future and > deref the delay instead > > I had a first working version which implemented both clojure.lang.IDeref and java.util.concurrent.Future, but as Meikel said, the less I'll depend on several external interfaces, the better. And I also had the problem of not knowing what to implement for certain methods of java.util.concurrent.Future. e.g. what should the behaviour of (get [_ timeout _]) be ? timeout on what. This was silly.
So I wrote this final version which only implements IDeref (which I want to), and also adds a cancel/isCancelled property a-la java.util.concurrent.Future but not tied to the Future interface : (ns delay.util) (defprotocol Cancellable (isCancelled [this]) (cancel [this])) (defn timed-delay [pause fun] (let [d (delay (fun)) f (future (Thread/sleep pause) @d)] (reify clojure.lang.IDeref (deref [_] @d) delay.util.Cancellable (isCancelled [_] (future-cancelled? f)) (cancel [_] (future-cancel f))))) What about this ? -- 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 Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en