englercj / node-esl

FreeSWITCH ESL implementation for Node.js; implements the full Event Socket Library specified in: http://wiki.freeswitch.org/wiki/Esl
http://englercj.github.com/node-esl/
MIT License
170 stars 111 forks source link

Bind callbacks to current domain #14

Closed cxreg closed 10 years ago

cxreg commented 11 years ago

I think it might be nice to bind the callbacks to an api call to the domain which is active at the time of the call. Other libraries such as node_redis have been looking into this kind of thing as well. What do you think about this?

I have a patch that seems to work if you are interested

cxreg commented 11 years ago

The patch is pushed at https://github.com/cxreg/node-esl/commit/e705153a0fc00554e83e687fafc4c04f5534342d

englercj commented 11 years ago

Maybe, looks like the docs recommend this being on the user end; which I agree with because you can create multiple domains and may want to specify which to bind callbacks to.

So basically, instead of doing:

conn = new esl.Connection('127.0.0.1', 8021, 'ClueCon', function() {
    conn.show('channels', 'xml', function(err, data, raw) {
        if(err) { /* handle error */ }
        else { /* stuff... */ }
    }));
});

You can do:

var d = domain.create();

conn = new esl.Connection('127.0.0.1', 8021, 'ClueCon', function() {
    conn.show('channels', 'xml', d.intercept(function(data, raw) {
        /* stuff... */
    }));
});

d.on('error', function(err) {
  // handle error that would've been passed above, but it intercepted and sent here
});
englercj commented 10 years ago

Closing, going to leave these to user-space.