Browse files

Fixing breakages.

Might be using Typescript at some point.
  • Loading branch information...
1 parent f019c63 commit 4b62af1006e3e7f55c68c907da7a7254acdd1944 @Havvy Havvy committed Jun 24, 2013
Showing with 144 additions and 7 deletions.
  1. +2 −0 .gitignore
  2. +2 −2 lib/bisubscriber.js
  3. +2 −1 lib/client.js
  4. +1 −1 lib/command-parser.js
  5. +32 −0 lib/listener.js
  6. +3 −1 lib/modules.js
  7. +1 −1 package.json
  8. +0 −1 spec/jasmine.log
  9. +101 −0 src/bisubscriber.ts
View
2 .gitignore
@@ -4,3 +4,5 @@ node_modules/*
.buildpath
npm-debug.log
*sublime*
+.grunt
+.grunt/*
View
4 lib/bisubscriber.js
@@ -56,7 +56,7 @@ BiEventSubscriber.prototype = {
} else {
this._primary.on(event, this._wrap(listener));
}
- });
+ }.bind(this));
},
_onMap : function (map) {
@@ -72,7 +72,7 @@ BiEventSubscriber.prototype = {
} else {
this._primary.once(event, this._wrap(listener));
}
- });
+ }.bind(this));
},
_onceMap : function (ee, map) {
View
3 lib/client.js
@@ -16,6 +16,7 @@ I should see if there's a better way to make this class than
all of these wrapped methods.
*/
+var util = require('util');
var mout = require('mout');
var mixin = mout.object.mixIn;
@@ -53,7 +54,7 @@ var defaultClientConfiguration = {
var client = Object.create(Client.prototype); // no new needed
// Parse the configuration object. Make it immutable.
- client._config = config = Object.freeze(mixin({}, config, defaultClientConfiguration));
+ client._config = config = Object.freeze(mixin({}, defaultClientConfiguration, config));
di = Object.freeze(mixin({}, di, defaultFactoryConfiguration));
// Create a socket.
View
2 lib/command-parser.js
@@ -37,7 +37,7 @@
if (command) {
command = Object.freeze(new Command(msg.actor, command,
- msg, channel, msg.isQuery));
+ msg, msg.channel, msg.isQuery));
this.emit(command.name, command);
}
};
View
32 lib/listener.js
@@ -0,0 +1,32 @@
+module.exports = function Listener (fn) {
+ var listener = fn;
+
+ switch (typeof fn.channel) {
+ case "string":
+ listener = function (e) {
+ if (e.channel === fn.channel) {
+ return;
+ } else {
+ listener.apply(this, arguments);
+ }
+ };
+ break;
+ case "array":
+ listener = function (e) {
+ if (listener.channel.indexOf(e.channel) > -1) {
+ return;
+ } else {
+ listener.apply(this, arguments);
+ }
+ };
+ break;
+ case fn.channels instanceof RegExp:
+ listener = function (e) {
+ if (fn.channel.test(e.channel)) {
+ return;
+ } else {
+ listener.apply(this, arguments);
+ }
+ };
+ }
+};
View
4 lib/modules.js
@@ -4,6 +4,8 @@
* having all the non-module related code
*/
+var Listener = require('./listener');
+
var Modules = function (bisubscriber) {
this._subscriber = bisubscriber; // {BiEventSubscriber}
this._exports = {}; // {Map[String, Object]}
@@ -15,7 +17,7 @@ Modules.prototype.require = function (moduleConstructor) {
var that = this;
function listen (event, listener) {
- that._subscriber.on(event, listener);
+ that._subscriber.on(event, Listener(listener));
}
// BUILD AN EXPORT OBJECT
View
2 package.json
@@ -1,6 +1,6 @@
{
"name": "nrc",
- "version": "0.4.4",
+ "version": "0.4.4-alpha-2",
"description": "Node.js IRC API",
"maintainers": [
{
View
1 spec/jasmine.log
@@ -1 +0,0 @@
-.FFFFFFFFFF
View
101 src/bisubscriber.ts
@@ -0,0 +1,101 @@
+/**
+ * BiEventSubscriber Class
+ *
+ * This event subscriber takes two event emitters, primary and secondary.
+ * Events can be subscribed in two ways:
+ *
+ * 1. An object dictionary where the keys are the events to listen to and the
+ * and the values are the functions to register for the events.
+ *
+ * Ex: subscriber.on({"join" : onjoin, "!help !h" : onhelp});
+ *
+ * 2. If there's only one string/function pair, you can pass them as two
+ * arguments.
+ *
+ * Ex: subscriber.on("join", onjoin);
+ *
+ * The string value is a space delimited list of events to listen to. If the
+ * event begins with an exclamation mark, then it will be subscribed to the
+ * secondary event emitter with the exclamation mark stripped. Otherwise, the
+ * listener will be subscribed to the primary event emitter.
+ *
+ * Event listeners will be wrapped with an error catching function that prints
+ * the stacktrace to std out. If you want different behavior, modify _wrap().
+ *
+ * Public Methods:
+ * - new(primary: EventEmitter, secondary: EventEmitter): BiEventSubscriber
+ * - on(listenerMap: Object): undefined
+ * - on(events: string, listener: function): undefined
+ * - once(listenerMap: Object): undefined
+ * - once(events: string, listener: function): undefined
+ */
+
+/*
+*/
+
+var BiEventSubscriber = function (primary, secondary) {
+ this._primary = primary;
+ this._secondary = secondary;
+};
+
+BiEventSubscriber.prototype = {
+ _wrap : function (fn) {
+ return function () {
+ try {
+ fn.apply(null, arguments);
+ } catch (err) {
+ console.log(err.stack);
+ }
+ };
+ },
+
+ _onString : function (string, listener) {
+ string.split(" ").forEach(function (event) {
+ if (event[0] === '!') {
+ this._secondary.on(event.substr(1), this._wrap(listener));
+ } else {
+ this._primary.on(event, this._wrap(listener));
+ }
+ });
+ },
+
+ _onMap : function (map) {
+ for (var event in map) {
+ this._onceString(event, map[event]);
+ }
+ },
+
+ _onceString : function (string, listener) {
+ string.split(" ").forEach(function (event) {
+ if (event[0] === '!') {
+ this._secondary.once(event.substr(1), this._wrap(listener));
+ } else {
+ this._primary.once(event, this._wrap(listener));
+ }
+ });
+ },
+
+ _onceMap : function (ee, map) {
+ for (var event in map) {
+ this._onceString(event, map[event]);
+ }
+ },
+
+ on : function () {
+ switch (arguments.length) {
+ case 1: this._onMap(arguments[0]); break;
+ case 2: this._onString(arguments[0], arguments[1]); break;
+ default: throw new Exception("on takes one or two arguments.");
+ }
+ },
+
+ once : function () {
+ switch (arguments.length) {
+ case 1: return this._onceMap(arguments[0]);
+ case 2: return this._onceString(arguments[0], arguments[1]);
+ default: throw new Exception("on takes one or two arguments.");
+ }
+ }
+};
+
+module.exports = BiEventSubscriber;

0 comments on commit 4b62af1

Please sign in to comment.