npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-general-listener

v1.0.3

Published

A general event listener for React Native.

Downloads

13

Readme

react-native-general-listener

npm version Build Status

中文说明

It is a wrapper of DeviceEventListener to support following functions:

  • Event level to make event name more readable.
  • Subscribe all sub level event and unsubscribe.

Install

Install by Yarn:

yarn add react-native-general-listener

Install by NPM:

npm install --save react-native-general-listener

Usage

Import the module in file:

import Listener from 'react-native-general-listener';

Then you can register, unregister or trigger an event type.

Event Type Structure

An event type can be a string, an array of string, or an object.

  • a string: Use the string as event name directly, it is not supported to trigger parent or child event.
  • an array of string: We will use seperator to connect these strings to generate event name. And you can register with listen to its child event, or trigger its parent event.
  • an object: Use the json string of object as event name. We do not recommend this usage.

Register

We have two register function:

  • register
  • registerWithSubEvent

They both have two parameters:

  • type: Event type.
  • func: Event callback, will be called when the event is triggered.

Example:

this.loginListener1 = Listener.register('LoginEvent', this.loginFn);
this.loginListener2 = Listener.register(['TestApp', 'Login', userId], this.loginFn);

The only difference between register and registerWithSubEvent is: the second one register event type with all sub level event types.

The function will return an object to used when unregister event type.

Unregister

You can unregister one event listener or all of an event type.

The function unregister has three parameters:

  • type: Event type.
  • listenerObj: Listener object which is the returned value of register or registerWithSubEvent. If it is undefined, we will remove all event listeners of the event type.

Example:

Listener.unregister('LoginEvent', this.loginListener1);
Listener.unregister('LoginEvent');
Listener.unregister(['TestApp', 'Login', userId], this.loginListener);
Listener.unregister(['TestApp', 'Login', userId]);

Trigger

You can trigger event with an event type and an event param.

The function trigger has three parameters:

  • type: Event type.
  • state: Event param. It can be an object, a number, a boolean or any other data type.

When trigger an event with an array of string event type [str0, str1, ... strn], we will find its parent event types to see if they use registerWithSubEvent function:

const upperType = [str0, str1, ..., strn];
while ( /* upperType is not empty */ ) {
    // Convert upperType to upperName
    if ( /* upperName is registered with registerWithSubEvent function */ ) {
        // Trigger upper event with state
    }
    // Pop the last element of upperType
}

Global Constant

There are two constant in the module:

  • defaultSeperator: Default seperator, used as connector of event type to generate event name. Default is '$'
  • innerEventType: Inner event type key, when event is triggered, the event type will be set into event param if event param is an object. This key is '_##_inner_##_event_##_type_##_' to avoid key duplication in event param.

You can modify them globally:

Listener.defaultSeperator = '#';
Listener.innerEventType = '123321';