How to synchronize multiple dataflows between component instances?
Last updated
Last updated
Sometimes you will need to synchronize the dataflow of a compound component. Let's say you have a component similar to the data-compound presented below:
Let's say that you need both data1 and data2 slots from the dataProcessor member to be ready for the processedData slot to be set. As you can notice, it is not possible to know which slot will be the first one to be ready. So, we need a way to be sure that both slots are set, no matter the order in which they are loaded. One way you can control this is by:
having a handler method which will perform the desired behavior only when everything needed is ready
calling this method every time the value of a slot, that need to be synchronized, changes; i.e. using the model\[SlotId\]Changed ()
(See The Cubbles Javascript API)
In our case, we will handle this in the data-processor component logic. The code would look similar to:
Some considerations
Note that the
data-processor
component is an elementary, and it should be like that since we need to be able to access component logic. Remember, that compounds have no associated logic. In case you don't have a component similar todata-processor
, you could create an elementary to handle the synchronization and add it to your original model.If you need additional validation, i.e. not only having the slots set, then you may create validation methods and call them in the condition of the handler method.
You may also consider resetting slots when you want to avoid the handler method to process slots when only one or some of them have changed, i.e. when you want to perform something only when all the slots change