This is gonna be a lot of code.
1) we need to pass something to the other side. Let's use incremental
request_id.
2) we need a structure that would be an interruptable intermediate between
real callback and the one that handles answers. Let's use the EventEmitter
for saving code.
3) we need to call x = setTimeout and clearTimeout(x). And keep the x
variable somewhere.
// global structures
var id = 0;
var e = new EventEmitter;
// global 'response' message handler
socket.on('response', function() { e.emit.apply(e, arguments); })
// a single callback assignment & sending the request
var real_callback = function(err, response) { ... };
var request_id = (function(callback, timeout) {
var x = setTimeout(function(id) {
e.emit(request_id, 'timeout')
}, timeout, ++request_id);
e.once(request_id, function() {
clearTimeout(x);
callback.apply(null, Array.prototype.slice(arguments, 1));
});
return request_id;
})(real_callback, timeout);
socket.send('request', request_id);
This is similar to this code using candle:
// global structures
var c = new candle;
// global 'response' message handler
socket.on('response', function() { c.resolve.apply(c, arguments); })
// a single callback assignment & sending the request
var real_callback = function(err, response) { ... };
var request_id = c.add(real_callback, timeout);
socket.send('request', request_id);
(I haven't tested the code, wrote it just to show how it works)
четверг, 1 ноября 2012 г., 1:22:20 UTC+7 пользователь Mark Hahn написал:
>
> This appears to be quite useful.
>
> How does this compare to putting the timeout and once-filter around a
> normal call? How much code does it save?
>
> On Wed, Oct 31, 2012 at 4:48 AM, Alexey Kupershtokh <
> [email protected] <javascript:>> wrote:
>
>> Here it is: https://github.com/AlexeyKupershtokh/node-candle
>>
>> it's similar to:
>> https://github.com/coolaj86/futures/tree/v2.0/future
>> and
>> https://github.com/temsa/addTimeout
>> to some extent, except that the callbacks are able to free in my case
>> allowing to avoid leaks.
>>
>> As an yet another example, if you use a callback wrapped by
>> addTimeout(timeout, cb) or future.once(cb).setTimeout(timeout) as an ACK
>> callback for a socket.io request that is never acknowledged, this
>> wrapepd callback would exist till the socket is disconnected.
>>
>> Any response is highly appreciated.
>>
>> --
>> Job Board: http://jobs.nodejs.org/
>> Posting guidelines:
>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> You received this message because you are subscribed to the Google
>> Groups "nodejs" group.
>> To post to this group, send email to [email protected]<javascript:>
>> To unsubscribe from this group, send email to
>> [email protected] <javascript:>
>> For more options, visit this group at
>> http://groups.google.com/group/nodejs?hl=en?hl=en
>>
>
>
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en