Git Product home page Git Product logo

co-wrapper's Introduction

co-wrapper

Wrap each function in object with a thunk. Useful for generator-based flow control such as co.

How to use it

var EventEmitter = require('events').EventEmitter,
  inherits = require('util').inherits;

var TestClass = module.exports = function() {
  EventEmitter.apply(this, arguments);
  // define some properties
  this._foo = 'foo';
  this._bar = 'bar';

  Object.defineProperty(this, 'foo', {
    get: function() {
      return this._foo;
    },
    set: function(val) {
      this._foo = val;
    }
  });

  Object.defineProperty(this, 'bar', {
    get: function() {
      return this._bar;
    },
    set: function(val) {
      this._bar = val;
    }
  });
};
inherits(TestClass, EventEmitter);

// define async function
TestClass.prototype.getFooBar = function(callback) {
  callback(null, this.foo + this.bar);
};

// define sync function
TestClass.prototype.getFooBarSync = function() {
  return this.foo + this.bar;
};

// wrap oroginal object with co-wrapper
var wrap = require('co-wrapper');
var wrapper = wrap(testObj, {
  exclude: ['getFooBarSync'],
  isEventEmitter: true,
  properties: {
    foo: {
      get: '_foo',
      set: function(val) {
        testObj._foo = val;
      }
    },
    bar: {
      get: function() {
        return testObj._bar;
      },
      set: '_bar'
    }
  }
});

(function *() {
  console.log(yield wrapper.getFooBar());
})();

Options

{
  exclude: [], // object functions, that should be excluded
  methods: [], // list of methods to wrap
  isEventEmitter: true, // set to true, if object has event emitter functionality
  properties: {
    name: {
      get: function () {return this._foo}, // getters and setters can be String,
      // that defines source object property, or function
      set: '_foo' // function would be run in source object scope
      // other Object.defineProperty parameters
    }
  }
}

License: MIT

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.