Skip to content

Events Listener for javascript project (nodejs & react-native)

Notifications You must be signed in to change notification settings

lequanghuylc/js-events-listener

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

It started as a fork of react-native-event-listeners. Now it is rewritten with Typescript and React Hooks support.

If you use React, check out the simple yet powerful hooks and state management at react.md

API

import GlobalEvent from 'js-events-listener'
// or import { GlobalEvent } from 'js-events-listener'
// or const GlobalEvent = require('js-events-listener');
static method return value description
addEventListener string | boolean return value is the id of the event listener or false on error
removeEventListener boolean true on success otherwise false
removeAllListeners boolean true on success otherwise false
emitEvent void no return value
on string | boolean shorthand for addEventListener
rm boolean shorthand for removeEventListener
rmAll boolean shorthand for removeAllListeners
emit void shorthand for emitEvent

Usage

  • File a.js
  import GlobalEvent from 'js-events-listener'
  export const runThis = () => {
    GlobalEvent.emit('some-event-name', { someData: 123 })
  }
  • File b.js
  import GlobalEvent from 'js-events-listener'
  export const setupListenerBeforeTheEventEmitted = () => {
    GlobalEvent.on('some-event-name', data => {
      console.log(data); // { someData: 123 }
    })
  }
  • File c.js
  import GlobalEvent from 'js-events-listener'
  export const setupListenerBeforeTheEventEmitted = () => {
    const eventId = GlobalEvent.on('some-event-name', data => {
      console.log(data); // { someData: 123 }
    });

    // remove listener
    GlobalEvent.rm(eventId);
  }
  • File d.ts
  import GlobalEvent from 'js-events-listener'

  type PayloadData {
    someData: number,
  }

  export const setupListenerBeforeTheEventEmitted = () => {
    const eventId = GlobalEvent.on<PayloadData>('some-event-name', data => {
      console.log(data); // { someData: 123 }, as PayloadData
    });

    // remove listener
    GlobalEvent.rm(eventId);
  }

About

Events Listener for javascript project (nodejs & react-native)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published