Trooba

Fast isomorphic lightweight framework to build pipelines

generic.

Protocol and transport agnostic, defines only basic concepts

extensible.

Define new events and data types and implement your own architecture

isomorphic.

Run it in browser or server side

Building a pipe

You can build any pipeline, not just service related. Let's build a slot machine.

class {
    onCreate() {
        this.state = { reels: ['🍊''🍉''🍈'].join(' ') };
    }
 
    spin() {
        var trooba = require('trooba');
 
        // create a pipeline
        var sharablePipe = trooba
          .use(spinReel) // reel 1
          .use(spinReel) // reel 2
          .use(spinReel) // reel 3
          .use(validate)
          .build();
 
        function spinReel(pipe) {
          var symbols = ['🍊''🍉''🍈''🍇''🍆''🍅''🍄'];
 
          pipe.once('request'function (request, next) {
            var position = Math.round(Math.random() * (symbols.length - 1));
            request.push(symbols[position]);
            next();
          });
        }
 
        function validate(pipe) {
          pipe.once('request'function (request, next) {
            var line = request.join(' ');
            if (line === [request[0], request[0], request[0]].join(' ')) {
              // you won!
              return pipe.respond(line + ' Yay!');
            }
            pipe.respond(line);
          });
        }
        // create a generic client and inject context
        var client = sharablePipe.create();
 
        // make a request
        client.request([](err, result) => {
            this.state.reels = result;
        });
    }
}
 
style {
    .reels {
        color:#09c;
        font-size:3em;
    }
    .example-button {
        font-size:1em;
        padding:0.5em;
    }
}
 
<div.reels>
    ${state.reels}
</div>
<button.example-button on-click('spin')>
    Spin
</button>
🍊 🍉 🍈

Building a service pipe

Let's make a service call to https://api.github.com/repos/trooba/trooba

class {
    onCreate() {
        this.state = {};
        this.state.token = '';
        this.state.jsonData = '';
        this.state.error = ''
    }
 
    makeCall() {
        this.state.error = '';
        this.state.jsonData = '';
 
        var trooba = require('trooba');
        var transport = require('trooba-xhr-transport');
 
        // create a pipeline
        var sharablePipe = trooba
          .use((pipe) => {
              pipe.once('request'(request, next) => {
                  request.headers.authorization = 
                    'token ' + this.state.token;
                  next();
              });
          })
          .use(transport, {
              protocol: 'https:',
              hostname: 'api.github.com',
              path: '/repos/trooba/trooba',
              socketTimeout: 5000,
              withCredentials: false
          })
          .build();
 
        // create a client with superagent api
        var client = sharablePipe.create('client:default');
 
        // make a request
        client.get().end((err, result) => {
            if (err) {
                this.state.error = JSON.stringify(err);
                return;
            }
            this.state.jsonData = JSON.stringify(result, null2);
        });
    }
 
    updateToken(e) {
        this.state.token = e.target.value;
    }
}
 
style {
    .jsonData {
        color:#09c;
        font-size:0.5em;
    }
    .error {
        color:#f00;
        font-size:1em;
    }
    .example-button {
        font-size:1em;
        padding:0.5em;
    }
}
 
<b>Github Token</b>:
<input type="text" value="${state.token}" on-change('updateToken')><br>
<button.example-button on-click('makeCall')>
    Make a Call
</button>
<br/>
<b>Response</b>
<div.error>
    ${state.error}
</div>
<textarea.jsonData rows="50" cols="80">
    ${state.jsonData}
</textarea>
Github Token:

Response