Unverified Commit 4ec074c1 authored by Guilherme Gazzo's avatar Guilherme Gazzo Committed by GitHub
Browse files

feat: Event Emitter (#300)


Co-authored-by: default avatarTasso Evangelista <tasso.evangelista@rocket.chat>
parent c6acc401
module.exports = {
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/eslint-recommended',
'@rocket.chat/eslint-config',
],
parser: '@typescript-eslint/parser',
parserOptions: {
sourceType: 'module',
ecmaVersion: 2018,
warnOnUnsupportedTypeScriptVersion: false,
ecmaFeatures: {
experimentalObjectRestSpread: true,
legacyDecorators: true,
},
},
plugins: ['@typescript-eslint', 'react-hooks'],
rules: {
'func-call-spacing': 'off',
'indent': 'off',
'import/order': ['error', {
'newlines-between': 'always',
groups: ['builtin', 'external', 'internal', ['parent', 'sibling', 'index']],
}],
'no-useless-constructor': 'off',
'no-empty-function': 'off',
'no-spaced-func': 'off',
'react-hooks/rules-of-hooks': 'error',
'react-hooks/exhaustive-deps': 'warn',
'@typescript-eslint/ban-ts-ignore': 'off',
'@typescript-eslint/func-call-spacing': ['error'],
'@typescript-eslint/indent': ['error', 2],
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': ['warn', {
allowExpressions: true,
}],
},
env: {
browser: true,
commonjs: true,
es6: true,
node: true,
jest: true
},
settings: {
'import/resolver': {
node: {
extensions: [
'.js',
'.ts',
'.tsx'
],
},
},
},
};
{
"src/**/*.js": [
"eslint --fix",
"git add"
]
}
<p align="center">
<a href="https://rocket.chat" title="Rocket.Chat">
<img src="https://user-images.githubusercontent.com/2263066/87240777-f5b4f300-c3f2-11ea-8a01-cc58fdf9a99a.png" alt="Rocket.Chat" />
</a>
</p>
# @rocket.chat/emitter
> Event emitter built by Rocket.Chat
![License: MIT](https://img.shields.io/github/license/RocketChat/Rocket.Chat.Fuselage?style=flat-square)
![Issues](https://img.shields.io/github/issues/RocketChat/Rocket.Chat.Fuselage/%F0%9F%93%A6%20fuselage-hooks?style=flat-square)
![Pull requests](https://img.shields.io/github/issues-pr/RocketChat/Rocket.Chat.Fuselage/%F0%9F%93%A6%20fuselage-hooks?style=flat-square)
![GitHub commit activity](https://img.shields.io/github/commit-activity/m/RocketChat/Rocket.Chat.Fuselage?style=flat-square)
![npm@latest](https://img.shields.io/npm/v/@rocket.chat/fuselage-emitter/latest?style=flat-square)
![npm@next](https://img.shields.io/npm/v/@rocket.chat/fuselage-emitter/next?style=flat-square)
![dev deps](https://img.shields.io/david/dev/RocketChat/Rocket.Chat.Fuselage?path=packages%2Ffuselage-emitter&style=flat-square)
![optional deps](https://img.shields.io/david/optional/RocketChat/Rocket.Chat.Fuselage?path=packages%2Ffuselage-emitter&style=flat-square)
![peer deps](https://img.shields.io/david/peer/RocketChat/Rocket.Chat.Fuselage?path=packages%2Ffuselage-emitter&style=flat-square)
![npm bundle size](https://img.shields.io/bundlephobia/min/@rocket.chat/fuselage-emitter?style=flat-square)
![npm downloads](https://img.shields.io/npm/dw/@rocket.chat/fuselage-emitter?style=flat-square)
![npm collaborators](https://img.shields.io/npm/collaborators/@rocket.chat/fuselage-emitter?style=flat-square)
## Install
```sh
yarn add @rocket.chat/emitter
```
## API Reference
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
#### Table of Contents
- [Emitter](#emitter)
- [has](#has)
- [Parameters](#parameters)
- [on](#on)
- [Parameters](#parameters-1)
- [once](#once)
- [Parameters](#parameters-2)
- [off](#off)
- [Parameters](#parameters-3)
- [emit](#emit)
- [Parameters](#parameters-4)
- [Emitter](#emitter-1)
- [has](#has-1)
- [Parameters](#parameters-5)
- [on](#on-1)
- [Parameters](#parameters-6)
- [once](#once-1)
- [Parameters](#parameters-7)
- [off](#off-1)
- [Parameters](#parameters-8)
- [emit](#emit-1)
- [Parameters](#parameters-9)
- [EventType](#eventtype)
- [Handler](#handler)
### Emitter
#### has
Returns `true` if this emmiter has a listener attached to the `key` event type
##### Parameters
- `key` **[EventType](#eventtype)**
Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
#### on
Adds the `handler` function to listen events of the `type` type.
##### Parameters
- `type` **[EventType](#eventtype)**
- `handler` **[Handler](#handler)&lt;T>**
Returns **OffCallbackHandler** a function to unsubscribe the handler invoking `this.off(type, handler)`
#### once
Adds a _one-time_ `handler` function for the event of the `type` type.
##### Parameters
- `type` **[EventType](#eventtype)**
- `handler` **[Handler](#handler)&lt;T>**
Returns **OffCallbackHandler** a function to unsubscribe the handler invoking `this.off(type, handler)`
#### off
Removes the specified `handler` from the list of handlers of the event of the `type` type
##### Parameters
- `type` **[EventType](#eventtype)**
- `handler` **[Handler](#handler)&lt;T>**
Returns **void**
#### emit
Calls each of the handlers registered for the event of `type` type, in the
order they were registered, passing the supplied argument `e` to each.
##### Parameters
- `type` **[EventType](#eventtype)**
- `e` **T**
Returns **void**
### Emitter
The event emitter class.
#### has
Returns `true` if this emmiter has a listener attached to the `key` event type
##### Parameters
- `key` **[EventType](#eventtype)**
Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
#### on
Adds the `handler` function to listen events of the `type` type.
##### Parameters
- `type` **[EventType](#eventtype)**
- `handler` **[Handler](#handler)&lt;T>**
Returns **OffCallbackHandler** a function to unsubscribe the handler invoking `this.off(type, handler)`
#### once
Adds a _one-time_ `handler` function for the event of the `type` type.
##### Parameters
- `type` **[EventType](#eventtype)**
- `handler` **[Handler](#handler)&lt;T>**
Returns **OffCallbackHandler** a function to unsubscribe the handler invoking `this.off(type, handler)`
#### off
Removes the specified `handler` from the list of handlers of the event of the `type` type
##### Parameters
- `type` **[EventType](#eventtype)**
- `handler` **[Handler](#handler)&lt;T>**
Returns **void**
#### emit
Calls each of the handlers registered for the event of `type` type, in the
order they were registered, passing the supplied argument `e` to each.
##### Parameters
- `type` **[EventType](#eventtype)**
- `e` **T**
Returns **void**
### EventType
Type: ([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))
### Handler
Type: function (event: T): void
{
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
"mainEntryPointFilePath": "<projectFolder>/dist/index.d.ts",
"bundledPackages": [],
"compiler": {},
"apiReport": {
"enabled": false
},
"docModel": {
"enabled": true
},
"dtsRollup": {
"enabled": false
},
"tsdocMetadata": {},
"messages": {
"compilerMessageReporting": {
"default": {
"logLevel": "warning"
}
},
"extractorMessageReporting": {
"default": {
"logLevel": "warning"
}
},
"tsdocMessageReporting": {
"default": {
"logLevel": "warning"
}
}
}
}
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [@rocket.chat/emitter](./emitter.md) &gt; [Emitter](./emitter.emitter.md) &gt; [emit](./emitter.emitter.emit.md)
## Emitter.emit() method
<b>Signature:</b>
```typescript
emit<T = any>(type: EventType, event?: T): void;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| type | [EventType](./emitter.eventtype.md) | |
| event | T | |
<b>Returns:</b>
void
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [@rocket.chat/emitter](./emitter.md) &gt; [Emitter](./emitter.emitter.md) &gt; [has](./emitter.emitter.has.md)
## Emitter.has() method
Returns `true` if this emmiter has a listener attached to the `key` event type
<b>Signature:</b>
```typescript
has(key: EventType): boolean;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| key | [EventType](./emitter.eventtype.md) | |
<b>Returns:</b>
boolean
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [@rocket.chat/emitter](./emitter.md) &gt; [Emitter](./emitter.emitter.md)
## Emitter interface
<b>Signature:</b>
```typescript
export interface Emitter
```
## Methods
| Method | Description |
| --- | --- |
| [emit(type, event)](./emitter.emitter.emit.md) | |
| [off(type, handler)](./emitter.emitter.off.md) | |
| [on(type, handler)](./emitter.emitter.on.md) | |
| [once(type, handler)](./emitter.emitter.once.md) | |
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [@rocket.chat/emitter](./emitter.md) &gt; [Emitter](./emitter.emitter.md) &gt; [off](./emitter.emitter.off.md)
## Emitter.off() method
<b>Signature:</b>
```typescript
off<T = any>(type: EventType, handler: Handler<T>): void;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| type | [EventType](./emitter.eventtype.md) | |
| handler | [Handler](./emitter.handler.md)<!-- -->&lt;T&gt; | |
<b>Returns:</b>
void
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [@rocket.chat/emitter](./emitter.md) &gt; [Emitter](./emitter.emitter.md) &gt; [on](./emitter.emitter.on.md)
## Emitter.on() method
<b>Signature:</b>
```typescript
on<T = any>(type: EventType, handler: Handler<T>): void;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| type | [EventType](./emitter.eventtype.md) | |
| handler | [Handler](./emitter.handler.md)<!-- -->&lt;T&gt; | |
<b>Returns:</b>
void
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [@rocket.chat/emitter](./emitter.md) &gt; [Emitter](./emitter.emitter.md) &gt; [once](./emitter.emitter.once.md)
## Emitter.once() method
<b>Signature:</b>
```typescript
once<T = any>(type: EventType, handler: Handler<T>): void;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| type | [EventType](./emitter.eventtype.md) | |
| handler | [Handler](./emitter.handler.md)<!-- -->&lt;T&gt; | |
<b>Returns:</b>
void
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [@rocket.chat/emitter](./emitter.md) &gt; [EventType](./emitter.eventtype.md)
## EventType type
<b>Signature:</b>
```typescript
export declare type EventType = string | symbol;
```
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [@rocket.chat/emitter](./emitter.md) &gt; [Handler](./emitter.handler.md)
## Handler type
<b>Signature:</b>
```typescript
export declare type Handler<T = any> = (event?: T) => void;
```
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [@rocket.chat/emitter](./emitter.md)
## emitter package
## Classes
| Class | Description |
| --- | --- |
| [Emitter](./emitter.emitter.md) | The event emitter class. |
## Interfaces
| Interface | Description |
| --- | --- |
| [Emitter](./emitter.emitter.md) | |
## Type Aliases
| Type Alias | Description |
| --- | --- |
| [EventType](./emitter.eventtype.md) | |
| [Handler](./emitter.handler.md) | |
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md)
## API Reference
## Packages
| Package | Description |
| --- | --- |
| [@rocket.chat/emitter](./emitter.md) | |
module.exports = {
preset: 'ts-jest',
errorOnDeprecated: true,
testMatch: [
'**/src/**/*.spec.[jt]s?(x)',
],
globals: {
'ts-jest': {
tsConfig: {
noUnusedLocals: false,
noUnusedParameters: false,
},
},
},
};
{
"name": "@rocket.chat/emitter",
"version": "0.16.0",
"description": "Event Emitter by Rocket.Chat",
"homepage": "https://rocket.chat/Rocket.Chat.Fuselage",
"author": {
"name": "Rocket.Chat",
"url": "https://rocket.chat/"
},
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/RocketChat/Rocket.Chat.Fuselage.git"
},
"bugs": {
"url": "https://github.com/RocketChat/Rocket.Chat.Fuselage/issues"
},
"keywords": [
"react",
"hooks",
"fuselage",
"rocketchat"
],
"source": "src/index.ts",
"main": "dist/index.js",
"module": "dist/index.module.js",
"unpkg": "dist/index.umd.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"start": "rollup -c -w",
"build": "rollup -c",
"test": "jest --max-workers=1",
"lint": "eslint --ext js,ts,tsx src",
"lint-staged": "lint-staged",
"docs:api-extractor": "api-extractor run --local --verbose",
"docs:api-documenter": "api-documenter markdown -i temp -o docs",
"docs:readme": "documentation readme 'src/{,**/!(__mocks__)/**/}!(*.spec).ts' --parse-extension=ts --resolve=node --section='API Reference' --readme-file README.md",
"docs:clean": "rimraf temp",
"docs": "run-s build docs:api-extractor docs:api-documenter docs:readme docs:clean"
},
"devDependencies": {
"@microsoft/api-documenter": "^7.8.21",
"@microsoft/api-extractor": "^7.9.2",
"@rocket.chat/eslint-config": "^0.4.0",
"@rollup/plugin-commonjs": "^13.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^8.1.0",
"@rollup/plugin-typescript": "^5.0.1",
"@types/jest": "^25.2.3",
"@types/react-dom": "^16.9.8",
"@types/resize-observer-browser": "^0.1.3",
"@types/use-subscription": "^1.0.0",
"@typescript-eslint/eslint-plugin": "^3.1.0",
"@typescript-eslint/parser": "^3.1.0",
"documentation": "^13.0.1",
"eslint": "^6.5.1",
"eslint-plugin-import": "^2.19.1",
"eslint-plugin-react": "^7.16.0",
"eslint-plugin-react-hooks": "^4.0.4",
"jest": "^26.0.1",
"lint-staged": "^10.0.8",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"rimraf": "^3.0.2",
"rollup": "^2.21.0",
"rollup-plugin-terser": "^6.1.0",
"ts-jest": "^26.1.0",
"typescript": "^3.9.5"
},
"peerDependencies": {
"react": "^16.12.0"
},
"publishConfig": {
"access": "public"
}
}