luciotato / waitfor

Sequential programming for node.js, end of callback hell / pyramid of doom
MIT License
531 stars 29 forks source link

wait.for with socket.io? #27

Closed mhassman closed 7 years ago

mhassman commented 9 years ago

Hi, I've been trying to use wait.for within socket.io events, but for the life of me can't figure out how to move the socket's execution into fibers. I'd like to globally execute for all sockets - similar to the http handler referenced here: https://github.com/luciotato/waitfor#proper-use.

There's something similar in coffee script: https://github.com/kuchumovn/fiberize/blob/master/fiberize.coffee#L77. I can't seem to translate this to generic javascript.

Any chance someone has sample code? Thanks in advance!

-Mark

mhassman commented 9 years ago

So.. finally figured out the coffee sample.. here's a working javascript interpretation:

.on('connection', function (socket) {

//override default socket.on addHandler and inject fiber..
if (!socket.fiber_injected) {
  socket.fiber_injected = true;
  socket.old_on = socket.on;
  socket.on = function(message_type, action) {
    this.old_on(message_type, function(arg1, arg2, arg3, arg4, arg5,
              arg6, arg7, arg8, arg9, arg10) {
      wait.launchFiber(action, arg1, arg2, arg3, arg4, arg5,
          arg6, arg7, arg8, arg9, arg10);
    });
  };
}

If anyone has a recommendation on how to convert the statically defined arguments into a dynamic array, i'd appreciate it.

If i do this: wait.launchFiber(action, arguments);.. the arguments array doesn't get expanded back to individual arguments - instead arrives as a one argument containing an array.

luciotato commented 9 years ago

I've not used Socket.io, but try the following:

.on('connection', function (socket) {

//override default socket.on addHandler and inject wait.launchFiber...
if (!socket.fiber_injected) {
  socket.fiber_injected = true;
  socket.old_on = socket.on;
  socket.on = function(message_type, actionFn) {
    this.old_on(message_type, function() {
        var newargs=Array.prototype.slice.call(arguments);
        newargs.unshift(actionFn); 
        wait.launchFiber.apply(wait, newargs);
    });
  };
};

socket.on('my message', function(...
}
mhassman commented 9 years ago

Wow. Thank you! I didn't realized apply was a valid method on launchFiber.. implemented your recomendation and works great! Thnx again!

-Mark


From: Lucio M. Tato [mailto:notifications@github.com] Sent: Tuesday, January 06, 2015 10:58 PM To: luciotato/waitfor Cc: mhassman Subject: Re: [waitfor] wait.for with socket.io? (#27)

I've not used Socket.io, but try the following:

.on('connection', function (socket) {

//override default socket.on addHandler and inject wait.launchFiber...

if (!socket.fiber_injected) {

socket.fiber_injected = true;

socket.old_on = socket.on;

socket.on = function(message_type, actionFn) {

this.old_on(message_type, function() {

    var newargs=Array.prototype.slice.call(arguments);

    newargs.unshift(actionFn); 

    wait.launchFiber.apply(wait, newargs);

});

};

};

socket.on('my message', function(...

}

Reply to this email directly or view https://github.com/luciotato/waitfor/issues/27#issuecomment-68976022 it on GitHub. https://github.com/notifications/beacon/AA37PDjk8BVhPJuKG1knbv9le5NU4nnqks5 nfKY3gaJpZM4DPDy6.gif