Skip to content

Google Apps Script library to create and handle custom events. The EventEmitter class can be used to create and handle custom events module.

Notifications You must be signed in to change notification settings

telegrambotindonesia/GAS-Lib-EventEmitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EventEmitter

EventEmitter Library for Google Apps Script (GAS).

This library to create and handle custom events. The EventEmitter class can be used to create and handle custom events module.

ID Library

  • Legacy: MVu2RaHxeCsJkOyDmPEUqHaZTb1melOAr
  • New: 1cEe0m2gnWKt-IQx5nUwV4_Hb9KVu5R3W9LmRfw2pfhK0li_1ewAITRZZ

Start

const event = new EventEmitter.init();

Example

Start

const event = new EventEmitter.init();

event.on('start', () => {
  Logger.log('started')
})

event.emit('start');

Arguments

const event = new EventEmitter.init();

event.on('start', number => {
  console.log(`started ${number}`)
})

event.emit('start', 23);

Multiple Arguments

const event = new EventEmitter.init();

event.on('start', (start, end) => {
  console.log(`started from ${start} to ${end}`)
})

emit('start', 1, 100);

Multiple Listener

const event = new EventEmitter.init();

event.on(['start' , 'lets go'], (start, end) => {
  console.log(`started from ${start} to ${end}`)
})

emit('lets go', 1, 100);

Multiple Emit

const event = new EventEmitter.init();

event.on('one', () =>  console.log('sign one'));
event.on('two', () =>  console.log('sign one'));

emit(['one', 'two'], 'args from multiple emit');

Remove Listener

const event = new EventEmitter.init();

const callback = () => {
  Logger.log('someone connected!');
};
// register
event.on('connection', callback);
// remove it
event.off('connection', callback);

Extends

class myEvent extends EventEmitter.init {
  constructor() {
    super();
  }

  /*
  ... your code
  */
};

const event = new myEvent();

event.on('start', number => {
  console.log(`started ${number}`)
})

event.emit('start', 23);

Listen All Event

const event = new EventEmitter.init();

event.on('*', (...args) => {
  Logger.log('All Events') ;
  Logger.log(JSON.stringify(args));
});

Count

const event = new EventEmitter.init();

event.on('start', () => Logger.log(1) );
event.on('start', () => Logger.log(2) );

let count = (event.listenerCount('start');
Logger.log('count', count);

Methods

Available now

  • on(events, ... listeners) Adds the listener function to the end of the listeners array for the event named event. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.
  • emit(event, ...args) calls each of the listeners registered for the event named event, in the order they were registered, passing the supplied arguments to each.
  • addListener(event, listener) add single event
  • once(event, listener) Adds a one-time listener function for the event named event. The next time event is triggered, this listener is removed and then invoked.
  • off(event, listener) Removes the specified listener from the listener array for the event named event
  • listenerCount(event) Returns the number of listeners listening to the event named event.

About

Google Apps Script library to create and handle custom events. The EventEmitter class can be used to create and handle custom events module.

Topics

Resources

Stars

Watchers

Forks