-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from mrvini/master
Removing unused variables initializations thanks @mrvini
- Loading branch information
Showing
6 changed files
with
40 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
examples/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,83 @@ | ||
'use strict'; | ||
|
||
class EventPubSub { | ||
constructor(scope){ | ||
this._events_={}; | ||
this.publish=this.trigger=this.emit; | ||
this.subscribe=this.on; | ||
this.unSubscribe=this.off; | ||
constructor( scope ) { | ||
this._events_ = {}; | ||
this.publish = this.trigger = this.emit; | ||
this.subscribe = this.on; | ||
this.unSubscribe = this.off; | ||
} | ||
|
||
on(type,handler){ | ||
if(!handler){ | ||
const err=new ReferenceError('handler not defined.'); | ||
throw(err); | ||
on( type, handler ) { | ||
if ( !handler ) { | ||
throw new ReferenceError( 'handler not defined.' ); | ||
} | ||
|
||
if(!this._events_[type]){ | ||
this._events_[type]=[]; | ||
if ( !this._events_[ type ] ) { | ||
this._events_[ type ] = []; | ||
} | ||
|
||
this._events_[type].push(handler); | ||
this._events_[ type ].push( handler ); | ||
return this; | ||
} | ||
|
||
off(type,handler){ | ||
if(!this._events_[type]){ | ||
off( type, handler ) { | ||
if ( !this._events_[ type ] ) { | ||
return this; | ||
} | ||
|
||
if(!handler){ | ||
var err=new ReferenceError('handler not defined. if you wish to remove all handlers from the event please pass "*" as the handler'); | ||
throw err; | ||
if ( !handler ) { | ||
throw new ReferenceError( 'handler not defined. if you wish to remove all handlers from the event please pass "*" as the handler' ); | ||
} | ||
|
||
if(handler=='*'){ | ||
delete this._events_[type]; | ||
if ( handler == '*' ) { | ||
delete this._events_[ type ]; | ||
return this; | ||
} | ||
|
||
const handlers=this._events_[type]; | ||
const handlers = this._events_[ type ]; | ||
|
||
while(handlers.includes(handler)){ | ||
while ( handlers.includes( handler ) ) { | ||
handlers.splice( | ||
handlers.indexOf(handler), | ||
handlers.indexOf( handler ), | ||
1 | ||
); | ||
} | ||
|
||
if(handlers.length<1){ | ||
delete this._events_[type]; | ||
if ( handlers.length < 1 ) { | ||
delete this._events_[ type ]; | ||
} | ||
|
||
return this; | ||
} | ||
|
||
emit(type,...args){ | ||
if(!this._events_[type]){ | ||
return this.emit$(type,...args); | ||
emit( type, ...args ) { | ||
if ( !this._events_[ type ] ) { | ||
return this.emit$( type, ...args ); | ||
} | ||
|
||
const handlers=this._events_[type]; | ||
const handlers = this._events_[ type ]; | ||
|
||
for(let handler of handlers){ | ||
handler.apply(this, args); | ||
for ( let handler of handlers ) { | ||
handler.apply( this, args ); | ||
} | ||
|
||
return this.emit$(type,...args); | ||
return this.emit$( type, ...args ); | ||
} | ||
|
||
emit$(type,...args){ | ||
if(!this._events_['*']){ | ||
emit$( type, ...args ) { | ||
if ( !this._events_[ '*' ] ) { | ||
return this; | ||
} | ||
|
||
const catchAll=this._events_['*']; | ||
const catchAll = this._events_[ '*' ]; | ||
|
||
for(let handler of catchAll){ | ||
handler.call(this, type, ...args); | ||
for ( let handler of catchAll ) { | ||
handler.call( this, type, ...args ); | ||
} | ||
|
||
return this; | ||
} | ||
} | ||
|
||
module.exports=EventPubSub; | ||
module.exports = EventPubSub; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters