etnbrd/flx-compiler

View on GitHub
examples/higher-order.js

Summary

Maintainability
A
1 hr
Test Coverage
var counter = 0;

get(function server(request) {

  async(function() {
    handlers[request]();
    console.log(res);
  });

})

var handlers = {
  'prompt' : function prompter () {
    return counter;
  },
  'buy' : function buyer () {
    counter += 1;
    return counter;
  },
  'sell' : function seller () {
    counter -= 1;
    return counter;
  }
}

//////////////////////////////////////////////////////

function get(server) {
  // Simulate a client request
  setInterval(function () {
    server(client())
  }, 100);
}

function client() {
  if (Math.random() >= 0.5)
    return 'prompt';
  else if (Math.random() >= 0.5)
      return 'buy';
    else 
      return 'sell';
}

function async() {
  var args = Array.prototype.slice.call(arguments);
  var cb = args.pop();
  setTimeout(function() {
    cb.apply(this, args);
  }, 0);
}