Code Monkey home page Code Monkey logo

node-delegates's Introduction

delegates

Node method and accessor delegation utilty.

Installation

$ npm install delegates

Example

var delegate = require('delegates');

...

delegate(proto, 'request')
  .method('acceptsLanguages')
  .method('acceptsEncodings')
  .method('acceptsCharsets')
  .method('accepts')
  .method('is')
  .access('querystring')
  .access('idempotent')
  .access('socket')
  .access('length')
  .access('query')
  .access('search')
  .access('status')
  .access('method')
  .access('path')
  .access('body')
  .access('host')
  .access('url')
  .getter('subdomains')
  .getter('protocol')
  .getter('header')
  .getter('stale')
  .getter('fresh')
  .getter('secure')
  .getter('ips')
  .getter('ip')

API

Delegate(proto, prop)

Creates a delegator instance used to configure using the prop on the given proto object. (which is usually a prototype)

Delegate.auto(proto, targetProto, targetProp)

Delegates getters, setters, values, and methods from targetProto to proto. Assumes that targetProto objects will exist under proto objects with the key targetProp.

Delegate#method(name)

Allows the given method name to be accessed on the host.

Delegate#getter(name)

Creates a "getter" for the property with the given name on the delegated object.

Delegate#setter(name)

Creates a "setter" for the property with the given name on the delegated object.

Delegate#access(name)

Creates an "accessor" (ie: both getter and setter) for the property with the given name on the delegated object.

Delegate#fluent(name)

A unique type of "accessor" that works for a "fluent" API. When called as a getter, the method returns the expected value. However, if the method is called with a value, it will return itself so it can be chained. For example:

delegate(proto, 'request')
  .fluent('query')

// getter
var q = request.query();

// setter (chainable)
request
  .query({ a: 1 })
  .query({ b: 2 });

License

MIT

node-delegates's People

Contributors

plasmapower avatar tj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

node-delegates's Issues

why Object.defineProperty sucks?

I find you changed to use __defineGetter__ / __defineSetter__ instead of Object.defineProperty with the comment that Object.defineProperty sucks. Would you mind explaining briefly the reason behind?

do not error out if there isn't a target

this could go either way, but "failing silently" allows you to do things like have "active" delegates that may change, on the downside it may make some issues harder to debug.

allow name on target and proto to differ

I propose changing the signature of all methods to take two arguments nameOnProto, and nameOnTarget. The second argument would be optional and default to the value of the first (making it fully backward compatible).

Not sure which order the arguments should be in.

@tj, If you like the idea I'll submit a PR.

Trouble creating a new object through Object.create()

I'm having trouble creating a new object through Object.create(). Whenever called it instantiates an empty object, instead of applying the functions to the prototype. I logged the context.prototype and it returns undefined, should that happen? My understanding of what's going on is probably wrong, could you help me out?

application.js

var context = require('./context');
var transaction = require('./transaction');

function Store(name, opts) {
  if (!(this instanceof Store)) return new Store(name, opts);
  this.name = name || '';
  this.opts = opts || {};
  this.store = {};
  this.model = {};
  this.cidCount = 0;
  this.context = Object.create(context);
  this.transaction = Object.create(transaction);
  return this;
};

transaction.js

module.exports = {
  hello: function(arg) {
    return arg;
  }
};

context.js

var delegate = require('delegates');

var proto = module.exports = {};
delegate(proto, 'transaction')
  .method('hello');

a "fluent-setter"

Running into this nice-to-have. When setting, it's fluent, when getting, it's a getter.

function Queue() {
  this.state = {
    concurrency: 1,
    timeout: false
  }
}

delegate(Queue, 'state')
   .fluent('concurrency')
   .fluent('timeout')

Queue.prototype.run = function() {
  var concurrency = this.concurrency // [Function] oh noz. 
}

Not sure of a name :-/

Switch to Object.defineProperty (probably breaking change)

MDN states about __defineGetter__ and __defineSetter__:

This method should not be used since better alternatives exist.

In this case, the better alternative would be Object.defineProperty. However, using this probably be a breaking change since defineProperty cannot easily be used to add a setter to something with currently only a getter. Doing that would mean recreating the property, which isn't exactly a clean solution. In addition, a property can be set to be not configurable, which means that that wouldn't work. I'd prefer not to add code to recreate the property to node-delegate.

Why not support exporting a new object that delegates?

Haha, I think it will be great, like:

var dummy = delegate(obj.request)
  .getter('subdomains')
  .getter('protocol')
  .getter('header')
  .getter('stale')
  .getter('fresh')
  .getter('secure')
  .getter('ips')
  .getter('ip');

how about adding event delegate

I've been using nodejs for not a short time. In some of my projects, i need events bubbled from inside object. Here is a simple demo:

const EventEmitter = require('events');

class A extends EventEmitter {
  constructor(options) {
    super(options);

    this._b = new B();
    this._b
      .on('ready', () => this.emit('ready'))
      .on('refresh', () => this.emit('refresh'))
      .on('error', err => this.emit('error', err));
  }
}

class B extends EventEmitter {
  constructor() {
    super();
  }

  init() {
    // do init stuff
    this.emit('ready');
  }

  refresh() {
    // do refresh
    this.emit('refresh');
  }
}

const a = new A();
a.on('error', err => {
  // handle error logic
});

I think the event binding code in constructor of A is ugly. Maybe we can use in a more elegant way. How about this:

const delegate = require('delegates');
delegate(a, '_b')
  .event('ready')
  .event('refresh')
  .event('error');

yes it's just adding a .event api. So if it makes sense, i am glad to submit a PR.

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.