Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audited and refactored return API (Closes #120) #121

Merged
merged 5 commits into from
Jul 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]>

### Owned by:

Alexander J. Lallier <[email protected]>
Alexander J. Lallier <[email protected]>
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
}
}
}