Skip to content

Commit

Permalink
Merge pull request #121 from alallier/refactorReturned
Browse files Browse the repository at this point in the history
Audited and refactored return API (Closes #120)
  • Loading branch information
alallier authored Jul 9, 2017
2 parents 67910bf + e970651 commit fd5843e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ See V2.0.0 PR https://github.com/alallier/reload/pull/118
* Added timestamp to reload command line reloading (Issue [#7](https://github.com/alallier/reload/issues/7) / PR [#78](https://github.com/alallier/reload/pull/78))
* Added node 8 support (Issue [#106](https://github.com/alallier/reload/issues/106) / PR [#119](https://github.com/alallier/reload/pull/119))
* Added table of contents to README (Issue [#103](https://github.com/alallier/reload/issues/103) / PR [#105](https://github.com/alallier/reload/pull/105))
* Added return API to README (PR [#121](https://github.com/alallier/reload/pull/121))

### Modified
* Abstracted reload call to an index.js file. Index file now calls `reload.js` source file. This is to abstract the reload command line calling with a third argument that is now private and not apart of the public API (PR [#117](https://github.com/alallier/reload/pull/117))
* Update dependencies to latest and add package-lock.json files (PR [#109](https://github.com/alallier/reload/pull/109))
* Audited and refactored return API (Issue [#120](https://github.com/alallier/reload/issues/120) / PR [#121](https://github.com/alallier/reload/pull/121))

### Removed
* Drop support for server and just use ports (Issue [#102](https://github.com/alallier/reload/issues/102) / PR [#104](https://github.com/alallier/reload/pull/104))
Expand Down
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Table Of Contents
* [Table of reload parameters](#table-of-reload-parameters)
* [Table of options for reload opts parameter](#table-of-options-for-reload-opts-parameter)
* **[Updating to version 2](#upgrading-to-version-2)**
* [Returns](#returns)
* [Using reload as a command line application](#using-reload-as-a-command-line-application)
* [Usage for Command Line Application](#usage-for-command-line-application)
* [License](#license)
Expand Down Expand Up @@ -122,7 +123,7 @@ server.listen(app.get('port'), function () {
You can manually call a reload event by calling `reload()` yourself. An example is shown below:

```javascript
reloadServer = reload(server, app);
reloadServer = reload(app);
watch.watchTree(__dirname + "/public", function (f, curr, prev) {
// Fire server-side reload event
reloadServer.reload();
Expand Down Expand Up @@ -162,6 +163,14 @@ Reload dropped support for server. The only required parameter for reload is `ap

To read more about the API breaking changes please refer to the [changelog](CHANGELOG.md#api-breaking-changes).

#### Returns

An **object** containing:

| Name | Type | Description |
|--------|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| reload | function | A function that when called reloads all connected clients. For more information see [manually firing server-side reload events](#manually-firing-server-side-reload-events). |

Using reload as a command line application
---

Expand Down Expand Up @@ -213,4 +222,4 @@ JP Richardson <jprichardson@gmail.com>

### Owned by:

Alexander J. Lallier <mralexlallier@gmail.com>
Alexander J. Lallier <mralexlallier@gmail.com>
31 changes: 11 additions & 20 deletions lib/reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ module.exports = function reload (app, opts, server) {
var route

// Websocket server variables
var connections = new Set()
var WebSocketServer = require('ws').Server
var ws = require('ws')
var WebSocketServer = ws.Server
var wss

// General variables
Expand Down Expand Up @@ -99,35 +99,26 @@ module.exports = function reload (app, opts, server) {
}

wss.on('connection', (ws) => {
connections.add(ws)
ws.on('close', function () {
connections.delete(ws)
})

if (verboseLogging) {
console.log('Reload client connected to server')
}
})

function sendMessage (message) {
if (verboseLogging) {
console.log('Sending message to ' + (connections.size) + ' connections: ' + message)
}
for (let conn of connections) {
conn.send(message, function (error) {
if (error) {
console.error(error)
}
})
console.log('Sending message to ' + (wss.clients.size) + ' connection(s): ' + message)
}

wss.clients.forEach(function each (client) {
if (client.readyState === ws.OPEN) {
client.send(message)
}
})
}
// Return an object, so that the user can manually reload the server by calling the returned function reload. Using the web socket connection from above, we provide a function called reload which passes the command 'reload' to the function sendMessage. sendMessage sends the message 'reload' over the socket (if the socket is connected) to the client. The client then recieves the messages checks to see if the message is reload and then reloads the page.
// Return an object, right now it contains only a function reload. When this function is called it calls the `sendMessage` function with the message 'reload' which reloads all connected clients.
return {
'connections': connections,
'server': reload,
'reload': function () {
sendMessage('reload')
},
'sendMessage': sendMessage
}
}
}

0 comments on commit fd5843e

Please sign in to comment.