Skip to content

Instantly share code, notes, and snippets.

@ferco0
Last active May 13, 2018 07:15
Show Gist options
  • Save ferco0/e8427b95e2567cbe1b702986e3c24c92 to your computer and use it in GitHub Desktop.
Save ferco0/e8427b95e2567cbe1b702986e3c24c92 to your computer and use it in GitHub Desktop.

Revisions

  1. Fernando Costa revised this gist May 13, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion EventEmitter.ts
    Original file line number Diff line number Diff line change
    @@ -75,7 +75,7 @@ class EventEmitter implements IEventEmitter{

    setTimeout( () => {

    _this.emit(eventName, data);
    this.emit(eventName, data);

    }, time)

  2. Fernando Costa revised this gist May 13, 2018. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions EventEmitter.ts
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@

    interface IEventEmitter{
    listeners: any
    emitts: any[]
    emitts: any

    emit(eventName: string, data: any): void
    timed(eventName: string, data, time: number): void
    @@ -73,9 +73,7 @@ class EventEmitter implements IEventEmitter{
    */
    timed(eventName: string, data = false, time: number): void {

    let _this = this;

    setTimeout(function(){
    setTimeout( () => {

    _this.emit(eventName, data);

  3. Fernando Costa revised this gist Mar 21, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion EventEmitter.ts
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    /**
    *
    * EventEmitter
    * @description Events emitter and handler with pre-cached emitts
    * @description Events emitter and handler with pre-cached and timed emitters
    * @author Fernando Costa <[email protected]>
    *
    */
  4. Fernando Costa revised this gist Mar 21, 2018. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion EventEmitter.ts
    Original file line number Diff line number Diff line change
    @@ -137,7 +137,6 @@ class EventEmitter implements IEventEmitter{
    */
    once(eventName: string, callback: ( data?: any) => void ): void {


    this.on(eventName, function _once(data){

    callback.call(this, data);
  5. Fernando Costa revised this gist Mar 21, 2018. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions EventEmitter.ts
    Original file line number Diff line number Diff line change
    @@ -109,8 +109,6 @@ class EventEmitter implements IEventEmitter{

    if(this.emitts.hasOwnProperty(eventName)){



    let length = this.emitts[eventName].length;
    let count = 0;

    @@ -119,14 +117,14 @@ class EventEmitter implements IEventEmitter{
    callback.call(this, this.emitts[eventName][count]);
    if(once) break;
    }

    if(!once) this.registryListener(eventName, callback);

    }
    else{

    this.registryListener(eventName, callback);

    this.registryListener(eventName, callback)
    }

    }
  6. Fernando Costa revised this gist Mar 21, 2018. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions EventEmitter.ts
    Original file line number Diff line number Diff line change
    @@ -107,8 +107,6 @@ class EventEmitter implements IEventEmitter{
    */
    on(eventName: string, callback: ( data?: any) => void , once: boolean = false): void {

    console.log(this.emitts[eventName]);

    if(this.emitts.hasOwnProperty(eventName)){


  7. Fernando Costa created this gist Mar 21, 2018.
    175 changes: 175 additions & 0 deletions EventEmitter.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,175 @@
    /**
    *
    * EventEmitter
    * @description Events emitter and handler with pre-cached emitts
    * @author Fernando Costa <[email protected]>
    *
    */

    interface IEventEmitter{
    listeners: any
    emitts: any[]

    emit(eventName: string, data: any): void
    timed(eventName: string, data, time: number): void
    registryListener(eventName: string, callback: ( data?: any) => void ): void
    on(eventName: string, callback: ( data?: any) => void , once: boolean): void
    once(eventName: string, callback: ( data?: any) => void ): void
    remove(eventName: string, callback: ( data?: any) => void ): void

    }

    /**
    * Class EventEmitter
    * @class
    */
    class EventEmitter implements IEventEmitter{

    /**
    * @description The events listeners
    * @type {Object}
    */
    listeners: any = {}

    /**
    * @description Cached emitts
    * @type {array}
    */
    emitts: any[] = []

    /**
    * @description Emit and cache for future listeners
    * @param {stirng} eventName - The event indentifier
    * @param {mixin} data - Opitional data
    * @return {void}
    */
    emit(eventName: string, data: any = false): void {

    if(typeof this.emitts[eventName] === 'undefined')
    this.emitts[eventName] = []

    this.emitts[eventName].push(data);

    if(typeof this.listeners[eventName] !== 'undefined'){

    let listenersLength = this.listeners[eventName].length;
    let count = 0;

    for(; count < listenersLength; ++count){

    this.listeners[eventName][count].call(this, data );

    }

    }
    }

    /**
    * @description Timed emitter
    * @param {string} eventName - The event indentifier
    * @param {mixin} data - Opitional data
    * @param {number} time - The time of delay
    * @return {void}
    */
    timed(eventName: string, data = false, time: number): void {

    let _this = this;

    setTimeout(function(){

    _this.emit(eventName, data);

    }, time)

    }

    /**
    * @description Register listener
    * @param {string} eventName - The event indentifier
    * @param {callback} callback - The callback handler
    * @return {void}
    */
    registryListener(eventName: string, callback: ( data?: any) => void ): void {

    if(typeof this.listeners[eventName] === 'undefined')
    this.listeners[eventName] = [];

    this.listeners[eventName].push( callback );

    }

    /**
    * @description Register listener
    * @param {string} eventName - The event indentifier
    * @param {callback} callback - The callback handler
    * @param {boolean} once - Check if is one-time execution
    * @return {void}
    */
    on(eventName: string, callback: ( data?: any) => void , once: boolean = false): void {

    console.log(this.emitts[eventName]);

    if(this.emitts.hasOwnProperty(eventName)){



    let length = this.emitts[eventName].length;
    let count = 0;

    for(; count < length; ++count){

    callback.call(this, this.emitts[eventName][count]);
    if(once) break;
    }

    if(!once) this.registryListener(eventName, callback);

    }
    else{


    this.registryListener(eventName, callback)
    }

    }

    /**
    * @description One-time execution listener
    * @param {string} eventName - The event indentifier
    * @param {callback} callback - The callback handler
    * @return {void}
    */
    once(eventName: string, callback: ( data?: any) => void ): void {


    this.on(eventName, function _once(data){

    callback.call(this, data);

    this.remove(eventName, _once);

    }, true)

    }

    /**
    * @description Remove listener
    * @param {string} eventName - The event indentifier
    * @param {callback} callback - The callback handler
    * @return {void}
    */
    remove(eventName: string, callback: ( data?: any) => void ): void {

    if(this.listeners.hasOwnProperty(eventName)){

    let index = this.listeners[eventName].indexOf( callback );

    if (index > -1) {
    this.listeners[eventName].splice(index, 1);
    }

    }

    }

    }