-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider update and accumulate methods on Property #60
Comments
Ciao @kaifox ! Then it'll really become a "listenable" AtomicReference 😄 |
Sounds fine to me :-) We would probably have to extend Of course, the notification of subscribers will be non atomic, dispatched onto another thread. Edit: Another thing we have to be aware of - all our transformations are also async subscribers. So if you do Property<T> property = ...;
ObservableValue<T> derived = property.map(...);
property.update(...);
derived.get(); it is not guaranteed that |
Yes indeed, then it is a listenable atomic reference ;-) ... Indeed we should not have a second atomic reference ... Yes and the listener notification and the derived will not be atomic... but I think it still is worth it ... I use such constructs a lot e.g. when you want to publish a state of e.g. a service and one has to do consistent modifications (transformations) of it. And in principle you want to make sure that you do not mix inputs, but 'chain' them. So at the moment one either has to synchronize or enforce a single thread ... both some effort if you use the pattern more frequently ... With an 'ObservableAtomicReference' it would be simpler ;-) ... |
Yes, I agree, actually I always thought of Property as an "AtomicReference plus". So if you want to give it a try, please do. |
I will try (soon) ;-) |
Gi guys, I finally started trying this ... However, soon found a bit a showstopper: Currently, I have not idea how to solve this cleanly... Any proposals? |
Perhaps we could use getAndUpdate() to fetch the previous value, and within the actual update callback we save the new value before we return? |
@michi42, do you mean more or less something like this?: public T getAndUpdate(UnaryOperator<T> operator) {
AtomicReference<T> newValue = new AtomicReference<>();
T oldValue = lastValue.getAndUpdate(x -> {
T v = operator.apply(x);
newValue.set(v);
return v;
})
dispatchChange(oldValue, newValue.get());
return oldValue;
} ... It all looks a bit clumsy and I am completely unsure if it is bulletproof .... I am not sure, if e.g. the update operations might be retried etc... |
Hi guys, I didn't try it unfortunately.. but if |
Hi, From what I see, the function can be applied multiple times, but it is always the last result that will be propagated - so the hack you do should be safe as far as I can see (in the current implementation of AtomicReference that is...) |
Ok ... I had similar thoughts ... so if you consider it 'ok' I will try to make a proposal based on this... |
@andreacalia , the problem is that - for the update on change notification - one needs both, old and final value ... |
…nal. Waiting for #60 implementation to take this decision.
true indeed ! I see it now the problem. |
…nal. Waiting for #60 implementation to take this decision.
…nal. Waiting for #60 implementation to take this decision.
There is a PR out #62 ... but I am not sure ... did I mess up something with the rebase? damn ... |
Ciao Kajetan, I think it's ok just rebase on top of master now and it should be perfectly in sync 🙂 |
In addition to pure set() methods, a property could have update-like methods like an Atomic reference which should then ensure atomic updates of the value.
Proposed signatures:
In principle, e.g. update would do the following in an atomic way:
The text was updated successfully, but these errors were encountered: