@white-matrix/event-emitter
v1.0.1
Published
The Emitter can be used to expose an Event to the public to fire it from the insides.
Downloads
198
Readme
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' });