Skip to content

Commit

Permalink
Added JDOCs for multiple classes. All tests pass. Addition towards is…
Browse files Browse the repository at this point in the history
…sue #9
  • Loading branch information
brandonalanskas committed Oct 30, 2015
1 parent 92e62fe commit 6a21cc2
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 20 deletions.
36 changes: 30 additions & 6 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export const CLIENT_CONFIG_DEFAULTS = {
* The public API for managing communications with a SignalR server
*/
export default class Client extends EventEmitter {
/**
* Initializes th' client object wit' userdefined options. Options can include a multitude 'o properties, includin' th' ship URL,
* a set transport protocol th' user wishes to use, a hub client, th' timeout to use when connection, 'n loggin' mechanisms.
* @param options
*/
constructor(options) {
super();
this._config = Object.assign({}, CLIENT_CONFIG_DEFAULTS, options || {});
Expand All @@ -28,20 +33,28 @@ export default class Client extends EventEmitter {

}

/**
* Accessor fer th' state property 'o th' client. Sets th' state to newState 'n automatically emits th' correct events.
* @param newState
*/
set state(newState) {
this.emit(CLIENT_EVENTS.onStateChanging, {oldState: this.state, newState});
this._state = newState;
this.emit(CLIENT_EVENTS.onStateChanged, newState);
}

/**
*Accessor fer th' state property 'o th' client. Returns th' current state 'o th' client.
* @returns {*}
*/
get state(){
return this._state;
}

/**
* Starts the underlying connection to the server.
* @param {Object} options contains any updated treaty values that should be used to start the connection.
* @returns {Promise} that resolves once the connection is opened successfully.
* Starts th' underlyin' connection to th' ship.
* @param {Object} options contains any updated treaty values that be used to start th' connection.
* @returns {Promise} that resolves once th' connection be opened successfully.
*/
start(options) {
this._config = Object.assign(this._config, options);
Expand All @@ -64,9 +77,8 @@ export default class Client extends EventEmitter {


/**
* Stops the connection to the server
* //@param {boolean} force the current operation to end prematurely (default: false)
* @returns {Promise} that resolves once the connection has closed successfully.
* Stops th' connection to th' ship
* @returns {Promise} that resolves once th' connection has closed successfully.
*/
stop() {
if(this._transport) {
Expand Down Expand Up @@ -122,6 +134,11 @@ export default class Client extends EventEmitter {
this.on(CLIENT_EVENTS.onConnected, callback);
}

/**
* Negotiates th' request to th' ship 'n returns th' consequental promise that be created as a result.
* @returns {*}
* @private
*/
_negotiate() {
return request
.get(`${this._config.url}/negotiate`)
Expand All @@ -130,6 +147,13 @@ export default class Client extends EventEmitter {
.promise();
}

/**
* Takes a treaty (result 'o _negotiate()) 'n uses that 'n th' client configuration to find th' best transport protocol to use.
* A user may specify a transport as well if they would like to not use th' automated selection 'o one.
* @param treaty
* @returns {Promise}
* @private
*/
_findTransport(treaty) {
return new Promise((resolve, reject) => {
const availableTransports = AvailableTransports();
Expand Down
13 changes: 10 additions & 3 deletions src/transports/LongPollingTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import {CLIENT_STATES, CLIENT_EVENTS} from '../Constants';


/**
* The long polling transport protocol
* Th' long pollin' transport protocol.
*/
export default class LongPollingTransport extends Transport {
static supportsKeepAlive = false;

/**
* Uses th' current client, treaty from th' initial negotiation, 'n target URL to construct a new Longpollin' transport.
* @param client
* @param treaty
* @param url
*/
constructor(client, treaty, url) {
super('longPolling', client, treaty);
this._maxReconnectedTimeout = 3600000;
Expand All @@ -20,7 +25,6 @@ export default class LongPollingTransport extends Transport {
* Initiates th' long pollin' transport protocol fer th' current connection.
* @returns {Promise} That resolves once th' long pollin' transport has started successfully 'n has begun pollin'.
*/

_queryData(current) {
return current
.query({clientProtocol: 1.5})
Expand Down Expand Up @@ -151,6 +155,9 @@ export default class LongPollingTransport extends Transport {

}

/**
* Clears th' timeouts 'n stops th' connection to th' ship cleanly.
*/
stop() {
clearTimeout(this._currentTimeoutId);
clearTimeout(this._reconnectTimeoutId);
Expand Down
31 changes: 29 additions & 2 deletions src/transports/ServerSentEventsTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,27 @@ import request from 'superagent';
import PromiseMaker from '../PromiseMaker';

const EventSource = (window && window.EventSource) || EventSourcePolyfill;

/**
* The ServerSentEvents transport protocol.
*/
export default class ServerSentEventsTransport extends Transport {
static supportsKeepAlive = true;

/**
* Uses th' current client, treaty from th' initial negotiation, 'n target URL to construct a new ServerSentEvents transport.
* @param client
* @param treaty
* @param url
*/
constructor(client, treaty, url) {
super('serverSentEvents', client, treaty);
this._intentionallyClosed = null;
this._url = url;
}

/**
* Initates th' ServerSentEvents connection, as well as handles onmessage, onerror, 'n onopen events.
* @returns {Promise}
*/
start(){
return new Promise((resolve, reject) => {
if(this._eventSource && this._intentionallyClosed) {
Expand Down Expand Up @@ -55,6 +67,10 @@ export default class ServerSentEventsTransport extends Transport {
};
});
}

/**
* Cleanly disconnects from th' target ship.
*/
stop(){
if(this._eventSource){
this._client.emit(CLIENT_EVENTS.onDisconnecting);
Expand All @@ -65,6 +81,13 @@ export default class ServerSentEventsTransport extends Transport {
this._client.emit(CLIENT_EVENTS.onDisconnected);
}
}

/**
* Returns a promise that resolves when a message be sent with th' passed in data to th' target URL.
* @param data
* @returns {Promise}
* @private
*/
_send(data) {
return request
.post(this._url + '/send')
Expand All @@ -75,6 +98,10 @@ export default class ServerSentEventsTransport extends Transport {
.use(PromiseMaker)
.promise();
}
/**
* If th' keepAlive times out, closes th' connection cleanly 'n attempts to reconnect.
* @private
*/
_keepAliveTimeoutDisconnect(){
this._client.emit(CLIENT_EVENTS.onDisconnecting);
this._intentionallyClosed = false;
Expand Down
37 changes: 29 additions & 8 deletions src/transports/Transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import takeRight from 'lodash.takeright';

export default class Transport {
/**
* Initializes the transport instance
* @param name the name of the transport (must be the same value as the server's corresponding transport name)
* @param {Client} client the parent SignalR client
* @param treaty the response from the negotiate request created by the SignalR server
* Initializes th' transport instance
* @param name th' moniker 'o th' transport (must be th' same value as th' ship's correspondin' transport moniker)
* @param {Client} client th' parent SignalR client
* @param treaty th' response from th' negotiate request created by th' SignalR ship
*/
constructor(name, client, treaty) {
this.name = name;
Expand Down Expand Up @@ -39,8 +39,8 @@ export default class Transport {
}

/**
* Initiates a new transport and begins the connection process.
* @returns {Promise} that will reject due to the method needing to be overridden.
* Initiates a new transport 'n begins th' connection process.
* @returns {Promise} that gunna reject due to th' method needin' to be overridden.
*/
start() {
return new Promise((resolve, reject) => {
Expand All @@ -49,8 +49,8 @@ export default class Transport {
}

/**
* Haults the current connection and safely disconnects.
* @returns {Promise} that will reject due to the method needing to be overridden.
* Haults th' current connection 'n safely disconnects.
* @returns {Promise} that gunna reject due to th' method needin' to be overridden.
*/
stop() {
return new Promise((resolve, reject) => {
Expand All @@ -64,6 +64,12 @@ export default class Transport {
});
}

/**
* Private method that takes a passed in compressed message (recieved from th' ship or other service), 'n decompresses it fer readability 'n use.
* Messages be also pushed into a buffer 'n timestamped as well.
* @param compressedResponse
* @private
*/
_processMessages(compressedResponse) {
const expandedResponse = expandResponse(compressedResponse);
this._client.emit(CLIENT_EVENTS.onReceiving);
Expand All @@ -73,17 +79,32 @@ export default class Transport {
this._client.emit(CLIENT_EVENTS.onReceived, expandedResponse.messages);
}

/**
* Accessor fer th' timestampin' th' last message recieved. Initiates a keepAlive timeout if keepAlive be supported by th' current transport type.
* @param newTimestamp
* @private
*/
set _lastMessageAt(newTimestamp) {
if(this._supportsKeepAlive()) {
this._keepAliveTimeoutId = setTimeout(this._keepAliveTimeoutDisconnect, this._keepAliveData.timeout);
}
this._latestMessageTime = newTimestamp;
}

/**
* Accessor that returns th' latest message's timestamp.
* @returns {*}
* @private
*/
get _lastMessageAt() {
return this._latestMessageTime;
}

/**
* Determines if th' current transport supports keepAlive functionality.
* @returns {*|ServerSentEventsTransport.supportsKeepAlive|LongPollingTransport.supportsKeepAlive|NullTransport.supportsKeepAlive|WebSocketTransport.supportsKeepAlive}
* @private
*/
_supportsKeepAlive() {
return this._keepAliveData.activated && this.supportsKeepAlive;
}
Expand Down
24 changes: 23 additions & 1 deletion src/transports/WebSocketTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@ import {CLIENT_STATES, CLIENT_EVENTS} from '../Constants';
export default class WebSocketTransport extends Transport {
static supportsKeepAlive = true;

/**
* Uses th' current client, treaty from th' initial negotiation, 'n target URL to construct a new WebSocket transport.
* @param client
* @param treaty
* @param url
*/
constructor(client, treaty, url) {
super('webSockets', client, treaty);
this._intentionallyClosed = null;
this._url = url;
}

/**
* Returns a promise to send th' passed in data to th' target URL.
* @param data
* @returns {Promise}
* @private
*/
_send(data) {
return new Promise((resolve, reject) => {
if(!this._socket) {
Expand All @@ -20,6 +32,10 @@ export default class WebSocketTransport extends Transport {
});
}

/**
* Initates th' WebSocket connection, as well as handles onmessage, onerror, onclose, 'n onopen events.
* @returns {Promise}
*/
start() {
return new Promise((resolve, reject) => {
if(!WebSocket) {
Expand Down Expand Up @@ -74,7 +90,9 @@ export default class WebSocketTransport extends Transport {
};
});
}

/**
* Cleanly disconnects from th' target ship.
*/
stop() {
if(this._socket) {
this._client.emit(CLIENT_EVENTS.onDisconnecting);
Expand All @@ -83,6 +101,10 @@ export default class WebSocketTransport extends Transport {
}
}

/**
* If th' keepAlive times out, closes th' connection cleanly 'n attempts to reconnect.
* @private
*/
_keepAliveTimeoutDisconnect() {
this._client.emit(CLIENT_EVENTS.onDisconnecting);
this._socket.close();
Expand Down

0 comments on commit 6a21cc2

Please sign in to comment.