Problem with dataConsumer.setSubchannels(subchannels) is that it's an async method and dataConsumer.subchannels getter doesn't get the updated value until setSubchannels() completes.
So next code is a bit problematic:
dataConsumer.setSubchannels([ 1, 2, 3 ]); // Note that we are not awaiting for it to complete.
// Now let's add subchannel 4:
dataConsumer.setSubchannels([ ...dataConsumer.subchannels, 4 ]);
Resulting subchannels after this code are [ 4 ] rather than [ 1, 2, 3, 4 ] as one may expect.
So we are adding addSubchannel() and removeSubchannel() methods to make this more easier:
dataConsumer.setSubchannels([ 1, 2, 3 ]); // Note that we are not awaiting for it to complete.
// Now let's add subchannel 4:
dataConsumer.addSubchannel(4);
Resulting subchannels after this code are [ 1, 2, 3, 4 ].
Details
dataConsumer.setSubchannels(subchannels)
is that it's an async method anddataConsumer.subchannels
getter doesn't get the updated value untilsetSubchannels()
completes.So next code is a bit problematic:
[ 4 ]
rather than[ 1, 2, 3, 4 ]
as one may expect.So we are adding
addSubchannel()
andremoveSubchannel()
methods to make this more easier:[ 1, 2, 3, 4 ]
.