Skip to content

Commit 0d7cf05

Browse files
committed
client.sjs macros + bisubscriber::off
1 parent e0f9bf6 commit 0d7cf05

File tree

8 files changed

+497
-158
lines changed

8 files changed

+497
-158
lines changed

doc/changelog.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,14 @@ Changelog
173173

174174
0.8.4:
175175
* Update irc-socket to 2.2.0.
176-
* Any other change is a bug.
177-
* Configuration changes:
176+
* Configuration changes (from irc-socket):
178177
** nickname is an alias for nick. (Prefer nickname)
179178
** username is an alias for user. (Prefer username)
180179
** Added ipv6 (See irc-socket)
181180
** Added localAddress (See irc-socket, net.Socket)
181+
* Nick highlight command trigger case is now case-insensitive. (Thanks metalbot)
182+
* Subscriber and Client gain .off() method which works as long as the event emitters has .removeListener or .off
183+
* Maybe .quit() will finally accept a reason properly.
182184

183185
0.9.0:
184186
[Not yet done]

fez.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ var fez = require('fez');
44
var sweetjs = require('fez-sweet.js');
55

66
exports.build = function(rule) {
7-
rule.each("test-src/*.sjs", fez.mapFile("test/%f.js"), sweetjs({'modules': ['sweet-bdd'], 'readableNames': true}));
7+
rule.each('test-src/*.sjs', fez.mapFile('test/%f.js'), sweetjs({'modules': ['sweet-bdd'], 'readableNames': true}));
8+
rule.each('src/*.sjs', fez.mapFile('lib/%f.js'), sweetjs({'modules': ['coco-js'], 'readableNames': true}))
89
};
910

1011
exports.default = exports.build;

lib/bisubscriber.js

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
* 1. An object dictionary where the keys are the events to listen to and the
88
* and the values are the functions to register for the events.
99
*
10-
* Ex: subscriber.on({"join" : onjoin, "!help !h" : onhelp});
10+
* Ex: subscriber.on({'join' : onjoin, '!help !h' : onhelp});
1111
*
1212
* 2. If there's only one string/function pair, you can pass them as two
1313
* arguments.
1414
*
15-
* Ex: subscriber.on("join", onjoin);
15+
* Ex: subscriber.on('join', onjoin);
1616
*
1717
* The string value is a space delimited list of events to listen to. If the
1818
* event begins with an exclamation mark, then it will be subscribed to the
@@ -28,19 +28,27 @@
2828
* - on(events: string, listener: function): undefined
2929
* - once(listenerMap: Object): undefined
3030
* - once(events: string, listener: function): undefined
31+
* - off(listenerMap: Object): undefined
32+
* - off(events: string, listener: function): undefined
3133
*/
3234

33-
/*
34-
*/
35+
const inspect = require('util').inspect;
36+
37+
const BiEventSubscriber = function (primary, secondary) {
38+
if (!(this instanceof BiEventSubscriber)) {
39+
return new BiEventSubscriber;
40+
}
3541

36-
var BiEventSubscriber = function (primary, secondary) {
3742
this._primary = primary;
3843
this._secondary = secondary;
44+
45+
this._primaryOff = (primary.removeListener ? 'removeListener' : 'off');
46+
this._secondaryOff = (secondary.removeListener ? 'removeListener' : 'off');
3947
};
4048

4149
BiEventSubscriber.prototype = {
4250
_onString : function (type, listener) {
43-
type.split(" ").forEach(function (event) {
51+
type.split(' ').forEach(function (event) {
4452
if (event[0] === '!') {
4553
this._secondary.on(event.substr(1), listener);
4654
} else {
@@ -55,8 +63,24 @@ BiEventSubscriber.prototype = {
5563
}
5664
},
5765

66+
_offString : function (type, listener) {
67+
type.split(' ').forEach(function (event) {
68+
if (event[0] === '!') {
69+
this._secondary[this._secondaryOff](event, listener);
70+
} else {
71+
this._primary['removeListener'](event, listener);
72+
}
73+
}.bind(this));
74+
},
75+
76+
_offMap : function (typemap) {
77+
for (var key in typemap) {
78+
this._offString(key, typemap[key]);
79+
}
80+
},
81+
5882
_onceString : function (type, listener) {
59-
type.split(" ").forEach(function (event) {
83+
type.split(' ').forEach(function (event) {
6084
if (event[0] === '!') {
6185
this._secondary.once(event.substr(1), listener);
6286
} else {
@@ -65,8 +89,8 @@ BiEventSubscriber.prototype = {
6589
}.bind(this));
6690
},
6791

68-
_onceMap : function (ee, map) {
69-
for (var event in map) {
92+
_onceMap : function (typemap) {
93+
for (var event in typemap) {
7094
this._onceString(event, map[event]);
7195
}
7296
},
@@ -75,15 +99,23 @@ BiEventSubscriber.prototype = {
7599
switch (arguments.length) {
76100
case 1: this._onMap(arguments[0]); break;
77101
case 2: this._onString(arguments[0], arguments[1]); break;
78-
default: throw new Exception("on takes one (object) or two (string, fn) arguments.");
102+
default: throw new Exception('on takes one (object) or two (string, fn) arguments.');
103+
}
104+
},
105+
106+
off: function () {
107+
switch (arguments.length) {
108+
case 1: this._offMap(arguments[0]); break;
109+
case 2: this._offString(arguments[0], arguments[1]); break;
110+
default: throw new Exception('off takes one (object) or two (string, fn) arguments.')
79111
}
80112
},
81113

82114
once : function () {
83115
switch (arguments.length) {
84116
case 1: return this._onceMap(arguments[0]);
85117
case 2: return this._onceString(arguments[0], arguments[1]);
86-
default: throw new Exception("on takes one (object) or two (string, fn) arguments.");
118+
default: throw new Exception('on takes one (object) or two (string, fn) arguments.');
87119
}
88120
}
89121
};

0 commit comments

Comments
 (0)