-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from fed135/es6-rewrite
0.2
- Loading branch information
Showing
28 changed files
with
930 additions
and
558 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,5 @@ node_modules | |
# Compiled api docs | ||
api_docs | ||
/log.txt | ||
/bin/kalm.min.js | ||
/bin/kalm.min.js.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
language: node_js | ||
node_js: | ||
- "5.0" | ||
script: "npm run-script test" | ||
script: "npm run-script test && node tests/benchmarks/index.js" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* Channel class | ||
* @class Channel | ||
* @exports {Channel} | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/* Methods -------------------------------------------------------------------*/ | ||
|
||
class Channel { | ||
|
||
/** | ||
* Channel constructor | ||
* @constructor | ||
* @param {Socket} socket An optionnal socket object to use for communication | ||
* @param {object} options The configuration options for the client | ||
*/ | ||
constructor(name, options, client) { | ||
this.name = name; | ||
this.options = options; | ||
|
||
this._client = client; | ||
this._emitter = client._emit.bind(client); | ||
|
||
this._timer = null; | ||
this._packets = []; | ||
this._handlers = []; | ||
} | ||
|
||
/** | ||
* Tells the channel to process the payload to send | ||
* @method send | ||
* @memberof Channel | ||
* @param {object|string} payload The payload to process | ||
*/ | ||
send(payload) { | ||
this._packets.push(payload); | ||
|
||
// Bundling process | ||
if (this._packets.length >= this.options.maxPackets) { | ||
if (this._timer !== null) { | ||
clearTimeout(this._timer); | ||
this._timer = null; | ||
} | ||
|
||
this._emit(); | ||
return; | ||
} | ||
|
||
if (this._timer === null) { | ||
this._timer = setTimeout(this._emit.bind(this), this.options.delay); | ||
} | ||
} | ||
|
||
/** | ||
* Alerts the client to emit the packets for this channel | ||
* @private | ||
* @method _emit | ||
* @memberof Channel | ||
*/ | ||
_emit() { | ||
this._emitter(this.name, this._packets); | ||
this._packets.length = 0; | ||
} | ||
|
||
/** | ||
* Adds a method that listens to this channel | ||
* @method addHandler | ||
* @memberof Channel | ||
* @param {function} method The method to bind | ||
*/ | ||
addHandler(method) { | ||
this._handlers.push(method); | ||
} | ||
|
||
/** | ||
* Handles channel data | ||
* @method handleData | ||
* @memberof Channel | ||
* @param {array} payload The received payload | ||
*/ | ||
handleData(payload) { | ||
var _reqs = payload.length; | ||
var _listeners = this._handlers.length; | ||
var i; | ||
var c; | ||
|
||
for (i = 0; i<_reqs; i++) { | ||
for (c = 0; c<_listeners; c++) { | ||
this._handlers[c](payload[i], this._client); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
/* Exports -------------------------------------------------------------------*/ | ||
|
||
module.exports = Channel; |
Oops, something went wrong.