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 6a21cc2 commit dba5f59
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/ConnectingMessageBuffer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import {CLIENT_STATES} from './Constants';

export default class ConnectingMessageBuffer {
/**
* Takes the client and drainCallback and creates an efficient buffer for buffering recieved messages.
* @param client
* @param drainCallback
*/
constructor(client, drainCallback) {
this.buffer = [];
this.client = client;
this.drainCallback = drainCallback;
}

/**
* Attempts to add a passed in message to the buffer.
* @param message
* @returns {boolean}
*/
tryBuffer(message) {
if(this.client.state === CLIENT_STATES.connecting) {
this.buffer.push(message);
Expand All @@ -15,6 +25,9 @@ export default class ConnectingMessageBuffer {
return false;
}

/**
* Drains the current buffer and removes all messages.
*/
drain() {
// Ensure that the connection is connected when we drain (do not want to drain while a connection is not active)
if(this.client.state === CLIENT_STATES.connected) {
Expand Down
19 changes: 18 additions & 1 deletion src/EventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@ export default class EventEmitter {
this.observers = {};
}

/**
*Pushes an event to the passed in listener.
* @param event
* @param listener
*/
on(event, listener) {
this.observers[event] = this.observers[event] || [];
this.observers[event].push(listener);
}

/**
* Removes an event from a passed in listener.
* @param event
* @param listener
*/
off(event, listener) {
if (!this.observers[event]) {
return;
}

this.observers[event].forEach(() => {
if (! listener) {
delete this.observers[event];
Expand All @@ -27,6 +36,11 @@ export default class EventEmitter {
});
}

/**
* Emits the passed in event to all observers.
* @param event
* @param args
*/
emit(event, ...args) {
if (!this.observers[event]) {
return;
Expand All @@ -35,6 +49,9 @@ export default class EventEmitter {
this.observers[event].forEach(observer => observer(...args));
}

/**
* Returns the true number of current observers.
*/
numberOfObservers() {
return sum(this.observers, o => o.length);
}
Expand Down

0 comments on commit dba5f59

Please sign in to comment.