Skip to content

Extend bouton with your operator and source

Bo HOU edited this page Jul 8, 2016 · 2 revisions

Bouton.js provides a basic set of operators and sources. You can easily extend it with your operators/sources.

What is an operator? What is a source?

Operator and source are just functions, who returns a Node instance.

operator signature in flowtype:

function operator(...args) => Node

source signature in flowtype:

function source(...args) => Node

How to add new operator and new source

You can add a new operator to bouton:

const bouton = require("bouton")
                 .addDefault();  // optional, add default operators and sources

// add your operator
bouton.addOperator("testOperator", yourOperator);

// use it in a stream
bouton.asList([1, 2, 3])
  .testOperator(...) // pass any argument to your operator

Source is added to bouton as a top level function by addSource

const bouton = require("bouton")
                 .addDefault();  // optional, add default operators and sources

// add your source
bouton.addSource("anotherSource", yourOperator);

// use it in a stream
bouton.anotherSource
  .act(console.log) // pass any argument to your operator

Add multiple operators and sources

You can organise your operators in a node module, which exports its operators as a key:value pair. Then you can use addOperators() function to add them to bouton.

There exits a similar function *addSources() for adding multiple sources.

const bouton = require("bouton");
const ops = require("you operator module");
const srcs = require("your source module");

bouton
  .addOperators(ops)
  .addSources(srcs);

bouton.yourSource(...)
  .yourOps(...)

Clone this wiki locally