Eventable and Evented no longer include #hooksEvent
This is introspection, which can be provided elsewhere or by specific implementations. Together with the documented requirement (in Eventable#on) that listeners must be notified of automatic un-hooking, and that systems handling listeners will always be aware of when listeners are hooked/unhooked, this method should not be necessary. Including this method in Eventable also introduces incompatibility with Node.js' Event API, which is undesirable.events
parent
7fa2a296d2
commit
c8bddfbaeb
|
@ -106,27 +106,6 @@ module.exports = Interface( 'Eventable',
|
||||||
addListener: [ 'ev', 'listener' ],
|
addListener: [ 'ev', 'listener' ],
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine whether LISTENER is listening for messages emitted by event
|
|
||||||
* EV
|
|
||||||
*
|
|
||||||
* If event EV is not valid, either `false` shall be returned or
|
|
||||||
* an error shall be thrown, since LISTENER cannot possibly hook an
|
|
||||||
* invalid event.
|
|
||||||
*
|
|
||||||
* This method is not intended as an introspective/reflection
|
|
||||||
* mechanism---it allows a consumer of an Eventable object to determine
|
|
||||||
* whether LISTENER has already been hooked before blindly
|
|
||||||
* hooking/unhooking it.
|
|
||||||
*
|
|
||||||
* @param {string} ev event id
|
|
||||||
* @param {Function} listener listener to query for
|
|
||||||
*
|
|
||||||
* @return {boolean} whether LISTENER hooks event EV
|
|
||||||
*/
|
|
||||||
hooksEvent: [ 'ev', 'listener' ],
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unhook LISTENER from event EV, preventing further messages from being
|
* Unhook LISTENER from event EV, preventing further messages from being
|
||||||
* received
|
* received
|
||||||
|
|
|
@ -296,7 +296,7 @@ module.exports = Trait( 'Evented' )
|
||||||
*
|
*
|
||||||
* @return {boolean} whether LISTENER hooks event EV
|
* @return {boolean} whether LISTENER hooks event EV
|
||||||
*/
|
*/
|
||||||
hooksEvent( ev, listener )
|
'protected hooksEvent'( ev, listener )
|
||||||
{
|
{
|
||||||
let levdata = listener[ this._evid ];
|
let levdata = listener[ this._evid ];
|
||||||
return !!( levdata && ( levdata[ ev ] !== undefined ) );
|
return !!( levdata && ( levdata[ ev ] !== undefined ) );
|
||||||
|
|
|
@ -617,55 +617,6 @@ describe( 'event.Evented', () =>
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
|
||||||
describe( '#hooksEvent', () =>
|
|
||||||
{
|
|
||||||
var ev = 'foo',
|
|
||||||
f;
|
|
||||||
|
|
||||||
beforeEach( () =>
|
|
||||||
{
|
|
||||||
// ensure fresh listener
|
|
||||||
f = () => {};
|
|
||||||
} );
|
|
||||||
|
|
||||||
|
|
||||||
it( 'recognizes listener for a hooked event', () =>
|
|
||||||
{
|
|
||||||
stub.evDefineEvents( [ ev ] )
|
|
||||||
.on( ev, f );
|
|
||||||
|
|
||||||
expect( stub.hooksEvent( ev, f ) ).to.be.true;
|
|
||||||
} );
|
|
||||||
|
|
||||||
|
|
||||||
it( 'does not recognize listener for non-hooked event', () =>
|
|
||||||
{
|
|
||||||
expect( stub.hooksEvent( ev, f ) ).to.be.false;
|
|
||||||
} );
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Technically this test is sufficient to make the previous two
|
|
||||||
* redundant, but they are retained for clairty in debugging.
|
|
||||||
*/
|
|
||||||
it( 'does not recognize listeners of other events', () =>
|
|
||||||
{
|
|
||||||
var ev2 = 'bar',
|
|
||||||
f2 = () => {};
|
|
||||||
|
|
||||||
stub.evDefineEvents( [ ev, ev2 ] )
|
|
||||||
.on( ev, f )
|
|
||||||
.on( ev2, f2 );
|
|
||||||
|
|
||||||
expect( stub.hooksEvent( ev, f ) ).to.be.true;
|
|
||||||
expect( stub.hooksEvent( ev2, f ) ).to.be.false;
|
|
||||||
|
|
||||||
expect( stub.hooksEvent( ev, f2 ) ).to.be.false;
|
|
||||||
expect( stub.hooksEvent( ev2, f2 ) ).to.be.true;
|
|
||||||
} );
|
|
||||||
} );
|
|
||||||
|
|
||||||
|
|
||||||
describe( 'listener metadata', () =>
|
describe( 'listener metadata', () =>
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue