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
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
4149BiEventSubscriber . 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