Skip to content

Commit

Permalink
feat: Type Emitter Implementation (#399)
Browse files Browse the repository at this point in the history
Co-authored-by: Tasso Evangelista <[email protected]>
  • Loading branch information
ggazzo and tassoevan authored Feb 18, 2021
1 parent 80e77dc commit cd0d3fc
Show file tree
Hide file tree
Showing 25 changed files with 346 additions and 97 deletions.
66 changes: 48 additions & 18 deletions packages/emitter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ yarn add @rocket.chat/emitter

#### Table of Contents

- [EventType](#eventtype)
- [Handler](#handler)
- [DefaultEventMap](#defaulteventmap)
- [AnyEventTypeOf](#anyeventtypeof)
- [AnyEventOf](#anyeventof)
- [AnyEventHandlerOf](#anyeventhandlerof)
- [EventTypeOf](#eventtypeof)
- [EventOf](#eventof)
- [EventHandlerOf](#eventhandlerof)
- [OffCallbackHandler](#offcallbackhandler)
- [IEmitter](#iemitter)
- [Emitter](#emitter)
- [events](#events)
Expand All @@ -51,13 +57,37 @@ yarn add @rocket.chat/emitter
- [emit](#emit)
- [Parameters](#parameters-4)

### EventType
### DefaultEventMap

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))
Type: Record&lt;([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol)), any>

### Handler
### AnyEventTypeOf

Type: function (event: T): void
Type: any

### AnyEventOf

Type: any

### AnyEventHandlerOf

Type: any

### EventTypeOf

Type: any

### EventOf

Type: any

### EventHandlerOf

Type: any

### OffCallbackHandler

Type: function (): void

### IEmitter

Expand All @@ -69,15 +99,15 @@ The event emitter class.

Returns the whole EventType list

Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[EventType](#eventtype)>**
Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[AnyEventTypeOf](#anyeventtypeof)&lt;EventMap>>**

#### has

Returns `true` if this emmiter has a listener attached to the `key` event type

##### Parameters

- `key` **[EventType](#eventtype)**
- `key` **[AnyEventTypeOf](#anyeventtypeof)&lt;EventMap>**

Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**

Expand All @@ -87,30 +117,30 @@ Adds the `handler` function to listen events of the `type` type.

##### Parameters

- `type` **[EventType](#eventtype)**
- `handler` **[Handler](#handler)&lt;T>**
- `type` **EventType**
- `handler` **[EventHandlerOf](#eventhandlerof)&lt;EventMap, EventType>**

Returns **OffCallbackHandler** a function to unsubscribe the handler invoking `this.off(type, handler)`
Returns **[OffCallbackHandler](#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>**
- `type` **EventType**
- `handler` **[EventHandlerOf](#eventhandlerof)&lt;EventMap, EventType>**

Returns **OffCallbackHandler** a function to unsubscribe the handler invoking `this.off(type, handler)`
Returns **[OffCallbackHandler](#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>**
- `type` **EventType**
- `handler` **[EventHandlerOf](#eventhandlerof)&lt;EventMap, EventType>**

Returns **void**

Expand All @@ -121,7 +151,7 @@ order they were registered, passing the supplied argument `e` to each.

##### Parameters

- `type` **[EventType](#eventtype)**
- `e` **T?**
- `type` **EventType**
- `event` **[EventOf](#eventof)&lt;EventMap, EventType>**

Returns **void**
16 changes: 16 additions & 0 deletions packages/emitter/docs/emitter.anyeventhandlerof.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@rocket.chat/emitter](./emitter.md) &gt; [AnyEventHandlerOf](./emitter.anyeventhandlerof.md)

## AnyEventHandlerOf type


<b>Signature:</b>

```typescript
export declare type AnyEventHandlerOf<EventMap extends DefaultEventMap> = {
[EventType in keyof EventMap]: EventMap[EventType] extends void ? () => void : (event: EventMap[EventType]) => void;
}[keyof EventMap];
```
<b>References:</b> [DefaultEventMap](./emitter.defaulteventmap.md)

14 changes: 14 additions & 0 deletions packages/emitter/docs/emitter.anyeventof.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@rocket.chat/emitter](./emitter.md) &gt; [AnyEventOf](./emitter.anyeventof.md)

## AnyEventOf type


<b>Signature:</b>

```typescript
export declare type AnyEventOf<EventMap extends DefaultEventMap> = EventMap[keyof EventMap];
```
<b>References:</b> [DefaultEventMap](./emitter.defaulteventmap.md)

14 changes: 14 additions & 0 deletions packages/emitter/docs/emitter.anyeventtypeof.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@rocket.chat/emitter](./emitter.md) &gt; [AnyEventTypeOf](./emitter.anyeventtypeof.md)

## AnyEventTypeOf type


<b>Signature:</b>

```typescript
export declare type AnyEventTypeOf<EventMap extends DefaultEventMap> = keyof EventMap;
```
<b>References:</b> [DefaultEventMap](./emitter.defaulteventmap.md)

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<!-- 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)
[Home](./index.md) &gt; [@rocket.chat/emitter](./emitter.md) &gt; [DefaultEventMap](./emitter.defaulteventmap.md)

## Handler type
## DefaultEventMap type


<b>Signature:</b>

```typescript
export declare type Handler<T = any> = (event?: T) => void;
export declare type DefaultEventMap = Record<string | symbol, unknown>;
```
6 changes: 3 additions & 3 deletions packages/emitter/docs/emitter.emitter.emit.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ Calls each of the handlers registered for the event of `type` type, in the order
<b>Signature:</b>

```typescript
emit<T = any>(type: EventType, e?: T): void;
emit<T extends AnyEventOf<EventMap>, EventType extends AnyEventTypeOf<EventMap> = EventTypeOf<EventMap, T>>(type: EventType, event: EventOf<EventMap, EventType>): void;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| type | [EventType](./emitter.eventtype.md) | |
| e | T | |
| type | EventType | |
| event | [EventOf](./emitter.eventof.md)<!-- -->&lt;EventMap, EventType&gt; | |
<b>Returns:</b>
Expand Down
4 changes: 2 additions & 2 deletions packages/emitter/docs/emitter.emitter.events.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ Returns the whole EventType list
<b>Signature:</b>

```typescript
events(): EventType[];
events(): AnyEventTypeOf<EventMap>[];
```
<b>Returns:</b>

[EventType](./emitter.eventtype.md)<!-- -->\[\]
[AnyEventTypeOf](./emitter.anyeventtypeof.md)<!-- -->&lt;EventMap&gt;\[\]

4 changes: 2 additions & 2 deletions packages/emitter/docs/emitter.emitter.has.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ Returns `true` if this emmiter has a listener attached to the `key` event type
<b>Signature:</b>

```typescript
has(key: EventType): boolean;
has(key: AnyEventTypeOf<EventMap>): boolean;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| key | [EventType](./emitter.eventtype.md) | |
| key | [AnyEventTypeOf](./emitter.anyeventtypeof.md)<!-- -->&lt;EventMap&gt; | |

<b>Returns:</b>

Expand Down
6 changes: 3 additions & 3 deletions packages/emitter/docs/emitter.emitter.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ The event emitter class.
<b>Signature:</b>

```typescript
export declare class Emitter implements IEmitter
export declare class Emitter<EventMap extends DefaultEventMap = DefaultEventMap> implements IEmitter<EventMap>
```
<b>Implements:</b> [IEmitter](./emitter.iemitter.md)
<b>Implements:</b> [IEmitter](./emitter.iemitter.md)<!-- -->&lt;EventMap&gt;
## Methods
| Method | Modifiers | Description |
| --- | --- | --- |
| [emit(type, e)](./emitter.emitter.emit.md) | | Calls each of the handlers registered for the event of <code>type</code> type, in the order they were registered, passing the supplied argument <code>e</code> to each. |
| [emit(type, event)](./emitter.emitter.emit.md) | | Calls each of the handlers registered for the event of <code>type</code> type, in the order they were registered, passing the supplied argument <code>e</code> to each. |
| [events()](./emitter.emitter.events.md) | | Returns the whole EventType list |
| [has(key)](./emitter.emitter.has.md) | | Returns <code>true</code> if this emmiter has a listener attached to the <code>key</code> event type |
| [off(type, handler)](./emitter.emitter.off.md) | | Removes the specified <code>handler</code> from the list of handlers of the event of the <code>type</code> type |
Expand Down
6 changes: 3 additions & 3 deletions packages/emitter/docs/emitter.emitter.off.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ Removes the specified `handler` from the list of handlers of the event of the `t
<b>Signature:</b>

```typescript
off<T = any>(type: EventType, handler: Handler<T>): void;
off<T extends AnyEventOf<EventMap>, EventType extends AnyEventTypeOf<EventMap> = EventTypeOf<EventMap, T>>(type: EventType, handler: EventHandlerOf<EventMap, EventType>): void;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| type | [EventType](./emitter.eventtype.md) | |
| handler | [Handler](./emitter.handler.md)<!-- -->&lt;T&gt; | |
| type | EventType | |
| handler | [EventHandlerOf](./emitter.eventhandlerof.md)<!-- -->&lt;EventMap, EventType&gt; | |
<b>Returns:</b>
Expand Down
8 changes: 4 additions & 4 deletions packages/emitter/docs/emitter.emitter.on.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ Adds the `handler` function to listen events of the `type` type.
<b>Signature:</b>

```typescript
on<T = any>(type: EventType, handler: Handler<T>): OffCallbackHandler;
on<T extends AnyEventOf<EventMap>, EventType extends AnyEventTypeOf<EventMap> = EventTypeOf<EventMap, T>>(type: EventType, handler: EventHandlerOf<EventMap, EventType>): OffCallbackHandler;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| type | [EventType](./emitter.eventtype.md) | |
| handler | [Handler](./emitter.handler.md)<!-- -->&lt;T&gt; | |
| type | EventType | |
| handler | [EventHandlerOf](./emitter.eventhandlerof.md)<!-- -->&lt;EventMap, EventType&gt; | |
<b>Returns:</b>
OffCallbackHandler
[OffCallbackHandler](./emitter.offcallbackhandler.md)
a function to unsubscribe the handler invoking `this.off(type, handler)`
8 changes: 4 additions & 4 deletions packages/emitter/docs/emitter.emitter.once.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ Adds a \*one-time\* `handler` function for the event of the `type` type.
<b>Signature:</b>

```typescript
once<T = any>(type: EventType, handler: Handler<T>): OffCallbackHandler;
once<T extends AnyEventOf<EventMap>, EventType extends AnyEventTypeOf<EventMap> = EventTypeOf<EventMap, T>>(type: EventType, handler: EventHandlerOf<EventMap, EventType>): OffCallbackHandler;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| type | [EventType](./emitter.eventtype.md) | |
| handler | [Handler](./emitter.handler.md)<!-- -->&lt;T&gt; | |
| type | EventType | |
| handler | [EventHandlerOf](./emitter.eventhandlerof.md)<!-- -->&lt;EventMap, EventType&gt; | |
<b>Returns:</b>
OffCallbackHandler
[OffCallbackHandler](./emitter.offcallbackhandler.md)
a function to unsubscribe the handler invoking `this.off(type, handler)`
14 changes: 14 additions & 0 deletions packages/emitter/docs/emitter.eventhandlerof.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@rocket.chat/emitter](./emitter.md) &gt; [EventHandlerOf](./emitter.eventhandlerof.md)

## EventHandlerOf type


<b>Signature:</b>

```typescript
export declare type EventHandlerOf<EventMap extends DefaultEventMap, EventType extends AnyEventTypeOf<EventMap>> = EventMap[EventType] extends void ? () => void : (event: EventMap[EventType]) => void;
```
<b>References:</b> [DefaultEventMap](./emitter.defaulteventmap.md)<!-- -->, [AnyEventTypeOf](./emitter.anyeventtypeof.md)

14 changes: 14 additions & 0 deletions packages/emitter/docs/emitter.eventof.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@rocket.chat/emitter](./emitter.md) &gt; [EventOf](./emitter.eventof.md)

## EventOf type


<b>Signature:</b>

```typescript
export declare type EventOf<EventMap extends DefaultEventMap, EventType extends AnyEventTypeOf<EventMap>> = EventMap[EventType] extends void ? never : EventMap[EventType];
```
<b>References:</b> [DefaultEventMap](./emitter.defaulteventmap.md)<!-- -->, [AnyEventTypeOf](./emitter.anyeventtypeof.md)

16 changes: 16 additions & 0 deletions packages/emitter/docs/emitter.eventtypeof.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@rocket.chat/emitter](./emitter.md) &gt; [EventTypeOf](./emitter.eventtypeof.md)

## EventTypeOf type


<b>Signature:</b>

```typescript
export declare type EventTypeOf<EventMap extends DefaultEventMap, EventValue extends EventMap[keyof EventMap]> = {
[EventType in keyof EventMap]: EventMap[EventType] extends EventValue ? EventType : never;
}[keyof EventMap];
```
<b>References:</b> [DefaultEventMap](./emitter.defaulteventmap.md)

6 changes: 3 additions & 3 deletions packages/emitter/docs/emitter.iemitter.emit.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<b>Signature:</b>

```typescript
emit<T = any>(type: EventType, event?: T): void;
emit<T extends AnyEventOf<EventMap>, EventType extends AnyEventTypeOf<EventMap> = EventTypeOf<EventMap, T>>(type: EventType, event: EventOf<EventMap, EventType>): void;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| type | [EventType](./emitter.eventtype.md) | |
| event | T | |
| type | EventType | |
| event | [EventOf](./emitter.eventof.md)<!-- -->&lt;EventMap, EventType&gt; | |
<b>Returns:</b>
Expand Down
Loading

0 comments on commit cd0d3fc

Please sign in to comment.