Dataflow async
Library ID dataflow-async
Latest version 1.0.5

WeakWorstStatusWrapper

WeakWorstStatusWrapper is an interface that allows to monitor the status of multiple attributes. It's designed to deal with thousands of attributes, and for this reason it spawns two threads. The references to the handled attributes are kept weakly.

The interface extends Field<AttributeData<Nothing?>>, and will contain the worst status of all the attributes:

  1. if it's empty (no attributes have been added, or all have been removed), the status will be loading;
  2. if all attributes have status loaded, the status will be loaded;
  3. if at least one attribute has status error, the status will be error;
  4. if at least one attribute has status loading, the status will be loading.

If the preferLoading value is set to true, step 3 and 4 are reversed.

If more than one attributes have status error, the errors will be merged using the method merge().

You can use the WeakWorstStatusWrapper.create() function to create an empty one, than add attributes to watch with the method addAttribute and remove them with removeAttribute(). When you don't need it anymore, you have to call the close() function, in order to interrupt the spawned threads.

Example:

import com.femastudios.dataflow.listen.* import com.femastudios.dataflow.async.* import com.femastudios.dataflow.async.util.* fun main() { val attr1 = attributeWrapperOf(1) val attr2 = attributeWrapperOf("2") val attr3 = attributeWrapperOf(3.0) WeakWorstStatusWrapper.create().use { wwsw -> wwsw.addAttribute(attr1) wwsw.addAttribute(attr2) wwsw.addAttribute(attr3) lifecycle { listen(wwsw) { println("WWSW: $it") } println("\nChainging attr1 to loading...") attr1.setLoading() //Since there is at least one loading, WWSW will be loading Thread.sleep(50) println("\nChainging attr2 to error...") attr2.setError("Error!") //Notice that since we have only one error, this instance will be the same in the WWSW Thread.sleep(50) println("\nChainging attr3 to error...") attr3.setError("Ops!") //Now that we have introduced a second error with no merge policy, the error in the WWSW will be a generic one (no message) Thread.sleep(50) println("\nChainging preferLoading = true...") wwsw.preferLoading = true //Changing pereferLoading=true, the WWSW will now have Loading status since there is at least one loading Thread.sleep(50) } } }
AttributeWrapper<Integer> attr1 = AttributeWrapper.of(1); AttributeWrapper<String> attr2 = AttributeWrapper.of("2"); AttributeWrapper<Double> attr3 = AttributeWrapper.of(3.0); try(WeakWorstStatusWrapper wwsw = WeakWorstStatusWrapper.create()) { wwsw.addAttribute(attr1) wwsw.addAttribute(attr2) wwsw.addAttribute(attr3) LifecycleOwner.lifecycle(lc -> { lc.listen(wwsw, it -> { System.out.println("WWSW: " + it); }); System.out.println("\nChainging attr1 to loading..."); attr1.setLoading(); //Since there is at least one loading, WWSW will be loading Thread.sleep(50); System.out.println("\nChainging attr2 to error..."); attr2.setError("Error!"); //Notice that since we have only one error, this instance will be the same in the WWSW Thread.sleep(50); System.out.println("\nChainging attr3 to error..."); attr3.setError("Ops!"); //Now that we have introduced a second error with no merge policy, the error in the WWSW will be a generic one (no message) Thread.sleep(50); System.out.println("\nChainging preferLoading = true..."); wwsw.setPreferLoading(true); //Changing pereferLoading=true, the WWSW will now have Loading status since there is at least one loading Thread.sleep(50); }); }