Mult
Overview
Mult is an opposite idea to Mix. It is a set of tools to setup a flow of data from one channel to many channels, a way to distribute a copy of values to other channels.
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 { attachMulter, tap } from 'csp-coffee/mult';
const sourceCh = makeChannel(CreatableBufferType.UNBLOCKING);
const destCh1 = makeChannel(CreatableBufferType.UNBLOCKING, 1);
const destCh2 = makeChannel(CreatableBufferType.UNBLOCKING, 1);
function* mainGenerator () {
attachMulter(sourceCh);
tap(sourceCh, destCh1);
tap(sourceCh, destCh2);
yield put(sourceCh, 'test1');
yield put(sourceCh, 'test2');
yield iterate((value) => {
console.log(value);
}, destCh1, destCh2);
}
go(mainGenerator)
// 'test1'
// 'test1'
// 'test2'
// 'test2'
Filtering values based on a predicate
tap
function can also accept a predicate function as a third parameter to filter values that will flow from source channel to destination channel.
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 { attachMulter, tap } from 'csp-coffee/mult';
const sourceCh = makeChannel(CreatableBufferType.UNBLOCKING);
const destCh1 = makeChannel(CreatableBufferType.UNBLOCKING, 1);
const destCh2 = makeChannel(CreatableBufferType.UNBLOCKING, 1);
function* mainGenerator () {
attachMulter(sourceCh);
tap(sourceCh, destCh1, (value: number) => value % 2 === 0);
tap(sourceCh, destCh2, (value: number) => value % 2 !== 0);
yield put(sourceCh, 1);
yield put(sourceCh, 2);
console.log(yield take(destCh1));
console.log(yield take(destCh2));
}
go(mainGenerator)
// 2
// 1
API
attachMulter<T = any>(ch: Channel): void
Will simply attach a special multer object to the channel which will let us pass this channel to other functions from multer package.
tap<T = any>(sourceChannel: Channel, destinationChannel: Channel, filter?: (data: T) => boolean): void
Will add a destination channel to the multer which will cause source channel to distribute a copy of its values to the destination channel. Also accepts a filter function which we looked at above.
untap<T = any>(sourceChannel: Channel, destinationChannelId: string): void
Provides a way to remove a destination channel from the multer.
Last updated