Fwd: [announce] InhiBeans: mitigate redundant recalculations
Tomas Mikula
tomas.mikula at gmail.com
Sun Dec 15 16:47:56 PST 2013
On Mon, Dec 16, 2013 at 1:07 AM, Scott Palmer <swpalmer at gmail.com> wrote:
> Interesting, no worse than John's pattern though.
> I thought of using a try/finally to make sure release was called and that
> naturally lead to thinking of try-with-resources, where the "resource" in
> this case is a binding of some sort (or a wrapper around a binding) that is
> invalidated on close() if needed.
That is an interesting idea. I didn't intend blockWhile() to be safe
with respect to exceptions, but merely
void blockWhile(Runnable r) {
block();
r.run();
release();
}
Enhancement you are suggesting could be fleshed out as block()
returning an AutoCloseable and the usage would be
try(AutoCloseable a = relaxedArea.block()) {
obj.setWidth(w);
obj.setHeight(h);
}
I wish I could omit the "AutoCloseable a = " when I don't use "a"
inside the try block.
Regards,
Tomas
>
>
> Scott
>
>
>
> On Sun, Dec 15, 2013 at 6:57 PM, Tomas Mikula <tomas.mikula at gmail.com>
> wrote:
>>
>> On Sun, Dec 15, 2013 at 11:49 PM, Tomas Mikula <tomas.mikula at gmail.com>
>> wrote:
>> > On Sun, Dec 15, 2013 at 6:39 PM, Scott Palmer <swpalmer at gmail.com>
>> > wrote:
>> >> Interesting idea.
>> >>
>> >> There is a case I have been curious about and wonder what the best
>> >> practices
>> >> are for it. Suppose you have a case when you are changing multiple
>> >> different properties that will be used in a single calculation. You
>> >> want to
>> >> deal with a single change to all of them in one go. E.g. imagine you
>> >> have
>> >> an "area" property that is bound to both "width" and "height". You
>> >> want to
>> >> write code like:
>> >>
>> >> obj.setWidth(w);
>> >> obj.setHeight(h);
>> >>
>> >> and have only ONE recalculation of the area property happen. Currently
>> >> the
>> >> way bindings work the area will be calculated twice. The intermediate
>> >> calculation is really not a value that you ever want to observe.
>> >
>> > Hi Scott,
>> >
>> > this is precisely the problem that I'm trying to address here. Now,
>> > the question is whether you have control over the implementation of
>> > obj.
>> >
>> > If yes, then it is the same case as the AND gate "motivational
>> > example" from InhiBeans page. You provide a method setSize(w, h) and
>> > use block()/release() to implement it in a way that causes only one
>> > change of the area property.
>> >
>> > If you cannot change the implementation of obj, what you can do is to
>> > bind an inhibeans.binding.DoubleBinding to the "area" value, call it
>> > relaxedArea, and listen to that one for all your purposes.
>> > Then you resize obj like this:
>> >
>> > relaxedArea.block();
>> > obj.setWidth();
>> > obj.setHeight();
>> > relaxedArea.release();
>>
>> I just thought of an enhancement: add method blockWhile(Runnable).
>>
>> The above example would become
>>
>> relaxedArea.blockWhile(() -> {
>> obj.setWidth(w);
>> obj.setHeight(h);
>> });
>>
>> This way you wouldn't have to worry about forgetting to call
>> release(), but comes with a penalty of creating a closure.
>>
>> Regards,
>> Tomas
>>
>> >
>> > Only one change of relaxedArea is emitted.
>> >
>> > Best,
>> > Tomas
>> >
>> >>
>> >> Are there helpers for this sort of situation? Are there guidelines in
>> >> the
>> >> JavaFX docs somewhere?
>> >>
>> >> Regards,
>> >>
>> >> Scott
>> >>
>> >>
>> >> On Sat, Dec 14, 2013 at 11:54 PM, Tomas Mikula <tomas.mikula at gmail.com>
>> >> wrote:
>> >>>
>> >>> Hello,
>> >>>
>> >>> I just published a small extension of javafx bindings and properties
>> >>> that can help you reduce redundant recalculations.
>> >>>
>> >>> They provide two additional methods:
>> >>>
>> >>> public void block();
>> >>> public void release();
>> >>>
>> >>> Call p.block() when you suspect your actions will lead to multiple
>> >>> invalidations of p, and call p.release() when you are done and want to
>> >>> deliver a single invalidation notification to p's observers.
>> >>>
>> >>> https://github.com/TomasMikula/InhiBeans
>> >>>
>> >>> Regards,
>> >>> Tomas
>> >>
>> >>
>
>
More information about the openjfx-dev
mailing list