Giter VIP home page Giter VIP logo

eventemitter-callback's Introduction

NPM version Downloads Package tree-shaking Package minified & gzipped size Package dependency count

eventemitter-callback

Emit an event and waiting for a result. Based on eventemitter3.

Installation

By package manager:

npm install eventemitter-callback --save

# For Yarn, use the command below.
yarn add eventemitter-callback

Installation from CDN:

<!-- For UNPKG use the code below. -->
<script src="https://unpkg.com/eventemitter-callback"></script>

<!-- For JSDelivr use the code below. -->
<script src="https://cdn.jsdelivr.net/npm/eventemitter-callback"></script>

<script>
  // UMD module is exposed through the "EventEmitterCB" global variable.
  console.log(EventEmitterCB);
</script>

Usage

import emitter from 'eventemitter-callback'
// import * as emitter from 'eventemitter-callback' // type: 'module'

emitter.on('event-name', (arg) => {
  console.log(arg) // prints "ping"
  return 'pong'
})

// emit an event and waiting for its result
emitter.emit('event-name', 'ping', (arg) => {
  console.log(arg) // prints "pong"
})

Disable triggering events before listening

By default, the following works normally:

emitter.emit('msg', 1)
emitter.on('msg', (arg) => {
  console.log(arg) // prints 1
})

If this doesn't meet your expectations, you can disable it:

emitter.disablePreEmitter();
emitter.emit('msg', 1)
emitter.on('msg', (arg) => {
  console.log(arg) // will not be called!
})

API

on(eventName, listener)

Adds the listener function to the end of the listeners array for the event named eventName.

  • eventName string the name of the event
  • listener function the callback function

The listener can be asynchronous. Specified as an AsyncFunction or return a Promise.

emitter.on('event-name', (arg) => {
  console.log(arg) // prints "ping"
  return new Promise((resolve) => {
    resolve('pong')
  })
})
// emitter.on('event-name', async (arg) => {
//   console.log(arg) // prints "ping"
//   const res = await queryRemote() // suppose res returns 'pong'
//   return res
// })
emitter.emit('event-name', 'ping', (arg) => {
  console.log(arg) // prints "pong"
})

once(eventName, listener)

  • eventName string the name of the event
  • listener function the callback function

Adds a one-time listener function for the event named eventName.

emit(eventName[, ...args][, callback])

  • eventName string the name of the event
  • ...args any
  • callback any the callback function

Returns true if the event had listeners, false otherwise.

emitAll(eventName[, ...args])

  • eventName string the name of the event
  • ...args any

Trigger all the listeners at once. Returns a result array by Promise.all.

emitValidateAll(eventName[, ...args])

  • eventName string the name of the event
  • ...args any

Trigger all the listeners at once and check the values returned. Returns true if all listeners returns true, false otherwise.

emitValidateAnyOf(eventName[, ...args])

  • eventName string the name of the event
  • ...args any

Trigger all the listeners at once and check the values returned. Returns true if either listener returns true, false otherwise.

off(eventName[, listener])

  • eventName string the name of the event
  • listener function the callback function

Removes the specified listener from the listener array for the event named eventName.

Removes all the listeners for the event:

emitter.off('event-name')

disablePreEmitter()

Disable triggering events before listening.

enablePreEmitter()

Enable triggering events before listening.

emitter._eventEmitter

Reference to current instance of EventEmitter3.

emitter._EventEmitter

Reference to constructor of EventEmitter3.

License

MIT, see the LICENSE file for detail.

eventemitter-callback's People

Contributors

superraytin avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.