Wrapping attributes
Wrapping attributes is done in the same exact way it's done for fields. You can construct a wrapper using the function attributeWrapperOf()
as with fields, and you can set custom AttributeData
s, as well as other attributes. There are also some convenience methods. To a more thorough explanation please refer to the field wrapper documentation.
Example:
import com.femastudios.dataflow.listen.*
import com.femastudios.dataflow.async.util.*
import java.util.concurrent.atomic.*
fun main() {
val i = AtomicInteger()
val attr = attributeOf { i.incrementAndGet() }
val attrWrap = attributeWrapperOf<Int>() //If nothing given initially, status is LOADING
lifecycle {
listen(attrWrap) {
println(it)
}
attrWrap.setAttribute(attr)
attr.recompute()
Thread.sleep(10)
attr.recompute()
Thread.sleep(10)
attrWrap.setError("Error!")
attrWrap.setLoadedValue(42)
}
}
AtomicInteger i = new AtomicInteger();
Attribute<Integer> attr = Attribute.of(wc -> { i.incrementAndGet(); });
AttributeWrapper<Integer> attrWrap = AttributeWrapper.of() //If nothing given initially, status is LOADING
LifecycleOwner.lifecycle(lc -> {
lc.listen(attrWrap, it -> {
System.out.println(it);
});
attrWrap.setAttribute(attr);
attr.recompute();
Thread.sleep(10);
attr.recompute();
Thread.sleep(10);
attrWrap.setError("Error!");
attrWrap.setLoadedValue(42);
});