Mix
Overview
Mix feature is a tool to set up and dynamically control flow of data between multiple source channels and one destination channel. Let's take a look at a quick example of this concept and then go into details of API.
import { iterate, put } from 'csp-coffee/operators';
import { go } from 'csp-coffee/go';
import { makeChannel } from 'csp-coffee/channel';
import { CreatableBufferType } from 'csp-coffee/buffer';
import { makeMix, toggle } from 'csp-coffeep/mix';
const destCh = makeChannel(CreatableBufferType.UNBLOCKING);
const ch1 = makeChannel(CreatableBufferType.UNBLOCKING);
const ch2 = makeChannel(CreatableBufferType.UNBLOCKING);
function* mainGenerator () {
const mixer = makeMix(destCh);
yield toggle(mixer, ch1);
yield toggle(mixer, ch2);
yield put(ch1, 'test1');
yield put(ch2, 'test2');
yield iterate((value) => {
console.log(value);
}, destCh);
}
go(mainGenerator)
// 'test1'
// 'test2'
First, we create our destination channel and two source channels we are going mix. Then we create a special mixer object and wire it up to our destination channel. Using toggle
function we add our source channels to the mix enabling a flow of values between them and our destination channel.
Mix modes
toggle
function accepts a third parameter which specifies a mix mode e.g a behavior of mixed channel relative to destination channel.
Normal mode
Is a default one. Will add channel to the mix, consume its values and supply to destination channel.
Pause
Will add a channel to the mix but will not consume any values from it. Later you can alter the behavior by using toggle
function.
Mute
Will add a channel to the mix and consume its values, but will not supply those values to the destination channel.
Solo mode
Will add a channel to the mix and will either pause or mute other channels that are already in the mix depending on soloMode
parameter provided to makeMix
function.
API
makeMix(destinationCh: Channel, soloMode?: 'pause' | 'mute'): Mixer
Will accept a destination channel and a soloMode
parameter which will define the mode of the channels when new solo channel is added. Will return a mixer object. I am not going to go through the properties of mixer object cause you most likely will never want to alter it directly, but through an existing API.
toggle(mixer: Mixer, ch: Channel, mode?: MixOptions): Promise<void>
Will accept a mixer object, a channel and a mode in which this channel will operate inside a mix. MixOptions
are the mix modes we looked at above: normal, mute, pause, solo.
unmix(mixer: Mixer, ch: Channel): Promise<void>
Will accept a mixer object and a channel which will be removed from the mix.
Last updated