I have observables x
and y
within SWT realm. I want to use their values to compute content for IObservableList
z
. The calculation can't be done in UI thread, so I have to write:
class Computer {
private final IObservableSet x;
private final IObservableList y;
private final WritableList z = new WritableList();
Computer(IObservableSet x, IObservableList y) {
this.x = x;
this.y = y;
x.addSetChangeListener(new ISetChangeListener() {
public void handleSetChange(SetChangeEvent event) {
update();
}
};
y.addSetChangeListener(new IListChangeListener() {
public void handleListChange(ListChangeEvent event) {
update();
}
};
}
void update() {
Set xData = new HashSet(x);
List yData = new HashList(y);
// Calculation is to be done in a different thread and within special context
Model.getTransactionManager().asyncExec(new Runnable() {
void run() {
List zData = numberCruncher(xData, yData);
//can't assign to SWT reaml directly
z.getRealm().asyncExec(new Runnable() {
void run() {
z.clear();
z.addAll(zData);
}
});
}
});
}
}
This smells so bad, I can't breath. The concept of databinding is completely buried with this calculate in a separate thread approach.
I've considered using ComputedSet
for z
with a custom Realm (using proper asyncExec), but there is no easy way to access x
and y
values from there. x
and y
could be bound to their proxies within custom realm, but that would duplicate memory usage and probably will pollute model thread with extra event processing.
Is there a smart way to get values of x
and y
from a custom realm to compute a value for a z
observable?