while-connected
Invoke a function given that the socket is connected
Installation
npm install while-connected
Example
var whileConnected = ; ... tcpServer // Every milliseconds update a database field// regarding this sockets connection status.{ var key = 'connections:'+ connid; // Make the key's ttl slightly longer than our loop // to minimize false positives. var key_ttl = interval_ms/1000 + 2; ;}
API
whileConnected(socket, debounce_ms, handler(again()))
whileConnected :: Socket a, Int b, (( -> void) -> *) -> void
-
socket
–– An instance of or inherited fromnet.Socket
. -
debounce_ms
–– The maximum loop speed. Seehandler
for more info. -
handler
––Run some desired logic while the socket is connected.
handler
receivesagain
function. Invokeagain
to re-run handler.whileConnected
is invoked
- if
socket
is connected:handler(again)
(goto 2)
debounce_ms
countdown begins, once done goto 3- if
again()
occured:handler(again)
(goto 2)
- otherwise, when next
again()
does occur:handler(again)
(goto 2)
Multiple
again()
s in a single "loop" is a noop.Immediately upon
socket
disconnection,handler(again)
never occurs again. Loop effectively stops if it is running,again
is a noop, etc.