@white-matrix/event-emitter
Version:
The Emitter can be used to expose an Event to the public to fire it from the insides.
48 lines (36 loc) • 916 B
Markdown
The Emitter can be used to expose an Event to the public to fire it from the insides.
```shell
yarn add @white-matrix/event-emitter
```
```typescript
import { Emitter } from '@white-matrix/event-emitter';
type UserInfoUpdateType = 'update' | 'clear';
export interface UserInfoUpdateEvent {
type: UserInfoUpdateType;
// data: any
}
export const userInfoUpdateEmitter = new Emitter<UserInfoUpdateEvent>();
```
```typescript
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();
};
}, []);
```
```typescript
userInfoUpdateEmitter.fire({ type: 'clear' });
```