1 | # defer-to-connect
|
2 |
|
3 | > The safe way to handle the `connect` socket event
|
4 |
|
5 | [![Coverage Status](https://coveralls.io/repos/github/szmarczak/defer-to-connect/badge.svg?branch=master)](https://coveralls.io/github/szmarczak/defer-to-connect?branch=master)
|
6 |
|
7 | Once you receive the socket, it may be already connected (or disconnected).<br>
|
8 | To avoid checking that, use `defer-to-connect`. It'll do that for you.
|
9 |
|
10 | ## Usage
|
11 |
|
12 | ```js
|
13 | const deferToConnect = require('defer-to-connect');
|
14 |
|
15 | deferToConnect(socket, () => {
|
16 | console.log('Connected!');
|
17 | });
|
18 | ```
|
19 |
|
20 | ## API
|
21 |
|
22 | ### deferToConnect(socket, connectListener)
|
23 |
|
24 | Calls `connectListener()` when connected.
|
25 |
|
26 | ### deferToConnect(socket, listeners)
|
27 |
|
28 | #### listeners
|
29 |
|
30 | An object representing `connect`, `secureConnect` and `close` properties.
|
31 |
|
32 | Calls `connect()` when the socket is connected.<br>
|
33 | Calls `secureConnect()` when the socket is securely connected.<br>
|
34 | Calls `close()` when the socket is destroyed.
|
35 |
|
36 | ## License
|
37 |
|
38 | MIT
|