tj / node-delegates Goto Github PK
View Code? Open in Web Editor NEWNodejs method and accessor delegation utility
License: MIT License
Nodejs method and accessor delegation utility
License: MIT License
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');
.auto()
is not available in 1.0.0
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.
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.
this is nice for the browser too :-)
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?
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 :-/
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.
dear, defineGetter is deprecated according to mdn. defineproperty is recommended instead~~
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.
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');
tests fail with mocha version 1.20.1 and should 8.4.0
Fixed after importing should in test file
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.