@white-matrix/event-emitter
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Event Emitter utils for any where

The Emitter can be used to expose an Event to the public to fire it from the insides.

Install

yarn add @white-matrix/event-emitter

Usage

Step1: Register emitter event

import { Emitter } from '@white-matrix/event-emitter';

type UserInfoUpdateType = 'update' | 'clear';
export interface UserInfoUpdateEvent {
  type: UserInfoUpdateType;
  // data: any
}

export const userInfoUpdateEmitter = new Emitter<UserInfoUpdateEvent>();

Step2: Listener on ui

useEffect(() => {
   const event = userInfoUpdateEmitter.event((e) => {
     if (e.type === 'clear') {
       // clear user info in global state
     } else {
       // update user info in global state
     }
   });
   return () => {
     event.dispose();
   };
}, []);

Step3: Fire event

userInfoUpdateEmitter.fire({ type: 'clear' });

/@white-matrix/event-emitter/

    Package Sidebar

    Install

    npm i @white-matrix/event-emitter

    Weekly Downloads

    56

    Version

    1.0.1

    License

    ISC

    Unpacked Size

    16.3 kB

    Total Files

    6

    Last publish

    Collaborators

    • miles_wang
    • vera_wlz
    • hexi1997
    • sleepyyj
    • chen-yu
    • kilru
    • lucklyric
    • q33357
    • xiadd