import BaseLfo from '../../core/BaseLfo';
const definitions = {
state: {
type: 'enum',
default: 'on',
list: ['on', 'off'],
metas: { kind: 'dynamic' },
},
};
/**
* The OnOff operator allows to stop the propagation of the stream in a
* subgraph. When "on", frames are propagated, when "off" the propagation is
* stopped.
*
* The `streamParams` propagation is never bypassed so the subsequent subgraph
* is always ready for incomming frames.
*
* @memberof module:common.operator
*
* @param {Object} options - Override default parameters.
* @param {String} [options.state='on'] - Default state.
*
* @example
* import * as lfo from 'waves-lfo/common';
*
* const frames = [
* { time: 0, data: [1, 2] },
* { time: 1, data: [3, 4] },
* { time: 2, data: [5, 6] },
* ];
*
* const eventIn = new EventIn({
* frameSize: 2,
* frameRate: 0,
* frameType: 'vector',
* });
*
* const onOff = new OnOff();
*
* const logger = new Logger({ data: true });
*
* eventIn.connect(onOff);
* onOff.connect(logger);
*
* eventIn.start();
*
* eventIn.processFrame(frames[0]);
* > [0, 1]
*
* // bypass subgraph
* onOff.setState('off');
* eventIn.processFrame(frames[1]);
*
* // re-open subgraph
* onOff.setState('on');
* eventIn.processFrame(frames[2]);
* > [5, 6]
*/
class OnOff extends BaseLfo {
constructor(options = {}) {
super(definitions, options);
this.state = this.params.get('state');
}
/**
* Set the state of the `OnOff`.
*
* @param {String} state - New state of the operator (`on` or `off`)
*/
setState(state) {
if (definitions.state.list.indexOf(state) === -1)
throw new Error(`Invalid switch state value "${state}" [valid values: "on"/"off"]`);
this.state = state;
}
// define all possible stream API
/** @private */
processScalar() {}
/** @private */
processVector() {}
/** @private */
processSignal() {}
/** @private */
processFrame(frame) {
if (this.state === 'on') {
this.prepareFrame();
this.frame.time = frame.time;
this.frame.metadata = frame.metadata;
this.frame.data = frame.data;
this.propagateFrame();
}
}
}
export default OnOff;