Permalink
Browse files

Remove signals module

There was no big advantage to have composable events here,
since they never have any dependencies on each other anyway.
  • Loading branch information...
1 parent 18fa199 commit 05c16042a719ab0e5bf557e1a7095a04829a4900 Jonas Westerlund committed Aug 20, 2012
Showing with 66 additions and 696 deletions.
  1. +0 −4 Makefile
  2. +14 −14 lib/handlers.js
  3. +52 −35 lib/irc.js
  4. +0 −448 lib/signal.js
  5. +0 −195 spec/lib/signal.spec.js
View
@@ -23,7 +23,3 @@ test-objects:
test-parser:
@export IRCJS_TEST=1;\
node --harmony $(MOCHA) --reporter spec --require should spec/lib/parser.spec.js;\
-
-test-signal:
- @export IRCJS_TEST=1;\
- node --harmony $(MOCHA) --reporter spec --require should spec/lib/signal.spec.js;\
View
@@ -247,24 +247,24 @@ function onForwardError(msg) {
function load(client) {
// Commands
- client.match(COMMAND.JOIN, onJoinCommand.bind(client));
- client.match(COMMAND.KICK, onKickCommand.bind(client));
- client.match(COMMAND.MODE, onModeCommand.bind(client));
- client.match(COMMAND.NICK, onNickCommand.bind(client));
- client.match(COMMAND.PART, onPartCommand.bind(client));
- client.match(COMMAND.PING, onPingCommand.bind(client));
- client.match(COMMAND.TOPIC, onTopicCommand.bind(client));
- client.match(COMMAND.QUIT, onQuitCommand.bind(client));
+ client.match(COMMAND.JOIN, onJoinCommand);
+ client.match(COMMAND.KICK, onKickCommand);
+ client.match(COMMAND.MODE, onModeCommand);
+ client.match(COMMAND.NICK, onNickCommand);
+ client.match(COMMAND.PART, onPartCommand);
+ client.match(COMMAND.PING, onPingCommand);
+ client.match(COMMAND.TOPIC, onTopicCommand);
+ client.match(COMMAND.QUIT, onQuitCommand);
// Numeric replies
- client.match(REPLY.MYINFO, onMyInfoReply.bind(client));
- client.match(REPLY.NAMREPLY, onNameReply.bind(client));
- client.match(REPLY.TOPIC, onTopicReply.bind(client));
- client.match(REPLY.WELCOME, onWelcomeReply.bind(client));
+ client.match(REPLY.MYINFO, onMyInfoReply);
+ client.match(REPLY.NAMREPLY, onNameReply);
+ client.match(REPLY.TOPIC, onTopicReply);
+ client.match(REPLY.WELCOME, onWelcomeReply);
// Errors
- client.match(EVENT.ANY, onAnyError.bind(client));
- client.match(ERROR.NOINVITEFORWARD, onForwardError.bind(client));
+ client.match(EVENT.ANY, onAnyError);
+ client.match(ERROR.NOINVITEFORWARD, onForwardError);
}
exports.load = load;
View
@@ -11,8 +11,6 @@ const Channel = require("./channel").Channel;
const Message = require("./message").Message;
const Person = require("./person").Person;
const Server = require("./server").Server;
-const Signal = require("./signal").Signal;
-const SafeSignal = require("./signal").SafeSignal;
const channel = require("./channel").channel;
const constants = require("./constants");
const fs = require("fs");
@@ -25,7 +23,6 @@ const parser = require("./parser");
const path = require("path");
const person = require("./person").person;
const server = require("./server").server;
-const signal = require("./signal");
const trailing = require("./message").trailing;
const COMMAND = constants.COMMAND;
@@ -92,7 +89,7 @@ function backoff(f, ratio) {
}
}
-/** Client: An IRC client wrapping up the server connection, configuration, signals
+/** Client: An IRC client wrapping up the server connection, configuration,
* and other things into an easy-to-use object.
*
* @constructor
@@ -105,7 +102,7 @@ function Client(conf) {
this.config = conf;
this.channels = new Map();
this.connectedAt = new Date(0);
- this.signals = new Map();
+ this.handlers = new Map();
this.messageBuffer = new Buffer(MESSAGE_BUFFER_SIZE);
this.server = new Server(server.get(CFG_ADDRESS), server.get(CFG_PORT));
this.user = new Person(conf.get(CFG_NICK), null, null);
@@ -385,20 +382,16 @@ Client.prototype.part = function(chan, msg) {
/** @param {RegExp|function|string} expr
* @param {function} pred
* @param {function=} handler
- * @return {Signal}
+ * @return {function} If called, removes the recently added handler.
*/
Client.prototype.match = function(expr, pred, handler) {
- let signal;
if (expr instanceof RegExp) {
- signal = matchRegExp.apply(this, arguments);
+ return matchRegExp.apply(this, arguments);
}
- else if (expr instanceof Function) {
- signal = matchPred.apply(this, arguments);
+ if (expr instanceof Function) {
+ return matchPred.apply(this, arguments);
}
- else {
- signal = matchType.apply(this, arguments);
- }
- return signal;
+ return matchType.apply(this, arguments);
};
function matchRegExp(re, handler) {
@@ -435,35 +428,60 @@ function matchPred(pred, handler) {
}
function matchType(type, handler) {
- const unsafe = this.config.get(CFG_DIE);
- const signals = this.signals;
- const signal = signals.has(type) ? signals.get(type) :
- signals.set(type, unsafe ? new Signal() : new SafeSignal());
- if (handler) {
- signal.receive(handler);
- }
- return signal;
-};
+ const handlers = this.handlers;
+ const arr = handlers.has(type) ? handlers.get(type) :
+ handlers.set(type, []);
+ arr.push(handler);
+ return this.ignore.bind(this, type, handler);
+}
+/** Remove a handler for a specific type.
+ * @param {string} type
+ * @param {function} handler
+ * @return {numbder} Remaining handlers for this type.
+ */
Client.prototype.ignore = function(type, handler) {
- const sig = this.signals.get(type);
- if (!sig) {
+ const arr = this.handlers.get(type);
+ if (!arr) {
return 0;
}
- const remaining = sig.remove(handler);
- if (0 === remaining) {
- sig.end();
- this.signals.delete(type);
+ const ix = arr.indexOf(handler);
+ if (-1 === ix) {
+ return arr.length;
}
- return remaining;
+ arr.splice(ix, 1);
+ const length = arr.length;
+ if (length === 0) {
+ this.handlers.delete(type);
+ return 0;
+ }
+ return length;
};
-Client.prototype.notify = function(type, val) {
- const sig = this.signals.get(type);
- if (!sig) {
+/** Call each handler with the supplied arguments.
+ * The handler can remove itself by returning the appropriate status code.
+ * @todo It's mean to use `apply()' on the handlers, but I don't know another way to use an array(-like) of args.
+ * @param {...*}
+ * @return {boolean}
+ */
+Client.prototype.notify = function(type /*, arg1, arg2 ... argN */) {
+ const handlers = this.handlers.get(type);
+ if (!handlers) {
return false;
}
- sig.emit(val);
+ const args = Array.apply(null, arguments);
+ args.shift();
+ for (let i = 0, l = handlers.length; i < l; ++i) {
+ const status = handlers[i].apply(this, args);
+ if (status & STATUS.REMOVE) {
+ this.ignore(type, handlers[i]);
+ --i;
+ --l;
+ }
+ if (status & STATUS.STOP) {
+ break;
+ }
+ }
return true;
};
@@ -508,7 +526,6 @@ exports.Client = Client;
// Modules
exports.logger = logger;
exports.parser = parser;
-exports.signal = signal;
// Constructors
exports.Channel = Channel;
Oops, something went wrong.

0 comments on commit 05c1604

Please sign in to comment.