Code Monkey home page Code Monkey logo

run-io's Introduction

run-io

Build Status Coverage Status

A monadic abstraction to encapsulate functions with side-effects. The single target of this super-tiny library is to ease testing of side-effect based code.

Installation

$ npm i run-io

API

lift(fn, [opts])

Turns fn into a yieldable function. opts is optional and can have the following keys:

  • sync - Handles this function synchronously

liftMethod(object, method, [opts])

Turns a object method into a yieldable function. opts is the same as in lift().

Example

liftMethod(Model, 'find');

run(generatorFunction, [done])

Generates a function, which can be called with arguments that get passed to the generatorFunction. If called this function executes the effects, yielded by the generator function.

If you omit the done parameter, run will assume that you pass the callback as the last parameter to the result function.

Example

function *gen(req, res) {
  const send = liftMethod(res, 'send', { sync: true });
  yield send({ ok: true });
}

function done(err) {
  if (err) throw err;
  console.log('done');
}

http.createServer(run(gen, done));

Usage

const io = require('run-io');

const lift = io.lift;
const liftMethod = io.liftMethod;
const run = io.run;

const getUser = () => {
  // Stuff ...
};

const getUserArticles = () => {
  // Stuff ...
};

/**
 * Some http handler which performs heavy IO.
 */
function *heavyIO(req, res) {
  const user = yield lift(getUser)();
  const send = liftMethod(res, 'send', { sync: true });
  let articles;
  try {
    articles = yield lift(getUserArticles)(user._id);
  } catch (e) {
    yield send('Error fetching articles!');
    return;
  }
  yield send(articles);
}

/**
 * A smoke test for the handler. Notice how we don't even need an
 * implementation of the `getUser()` and `getUserArticles()` methods.
 */
function test() {
  const req = {};
  const res = { send: () => {} }; // eslint-disable-line

  // Just fun to test. No database setup needed.
  const user = { _id: 3 };
  const articles = [{ title: 'foo' }];
  const send = liftMethod(res, 'send', { sync: true });

  const it = heavyIO(req, res);
  t.deepEqual(it.next().value, lift(getUser)());
  t.deepEqual(it.next(user).value, lift(getUserArticles)(user._id));
  t.deepEqual(it.next(articles).value, send(articles));
}

test();

// Run the handler.
http.createServer(run(heavyIO, error));

function error() {
  // Error handling ...
}

run-io's People

Contributors

domachine avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.