Skip to content

Commit

Permalink
feat(MessageCenter): add off function, let on return a canceller
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Sep 2, 2019
1 parent 5ad6560 commit db4751b
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 11 deletions.
3 changes: 2 additions & 1 deletion api-documents/kit.messagecenter.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ export declare class MessageCenter<ITypedMessages>

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [send](./kit.messagecenter.send.md) | | <code>&lt;Key_1 extends keyof ITypedMessages&gt;(key: Key_1, data: ITypedMessages[Key_1], alsoSendToDocument?: boolean) =&gt; void</code> | |
| [writeToConsole](./kit.messagecenter.writetoconsole.md) | | <code>boolean</code> | Should MessageCenter prints all messages to console? |

## Methods

| Method | Modifiers | Description |
| --- | --- | --- |
| [emit(key, data, alsoSendToDocument)](./kit.messagecenter.emit.md) | | Send message to local or other instance of extension |
| [off(event, handler)](./kit.messagecenter.off.md) | | Remove the listener of an event |
| [on(event, handler)](./kit.messagecenter.on.md) | | Listen to an event |
| [send(args)](./kit.messagecenter.send.md) | | Send message to local or other instance of extension |

25 changes: 25 additions & 0 deletions api-documents/kit.messagecenter.off.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@holoflows/kit](./kit.md) &gt; [MessageCenter](./kit.messagecenter.md) &gt; [off](./kit.messagecenter.off.md)

## MessageCenter.off() method

Remove the listener of an event

<b>Signature:</b>

```typescript
off<Key extends keyof ITypedMessages>(event: Key, handler: (data: ITypedMessages[Key]) => void): void;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| event | <code>Key</code> | Name of the event |
| handler | <code>(data: ITypedMessages[Key]) =&gt; void</code> | Handler of the event |
<b>Returns:</b>
`void`
6 changes: 4 additions & 2 deletions api-documents/kit.messagecenter.on.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Listen to an event
<b>Signature:</b>

```typescript
on<Key extends keyof ITypedMessages>(event: Key, handler: (data: ITypedMessages[Key]) => void): void;
on<Key extends keyof ITypedMessages>(event: Key, handler: (data: ITypedMessages[Key]) => void): () => void;
```

## Parameters
Expand All @@ -21,5 +21,7 @@ on<Key extends keyof ITypedMessages>(event: Key, handler: (data: ITypedMessages[

<b>Returns:</b>

`void`
`() => void`

a function, call it to remove this listener

17 changes: 15 additions & 2 deletions api-documents/kit.messagecenter.send.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@

[Home](./index.md) &gt; [@holoflows/kit](./kit.md) &gt; [MessageCenter](./kit.messagecenter.md) &gt; [send](./kit.messagecenter.send.md)

## MessageCenter.send property
## MessageCenter.send() method

Send message to local or other instance of extension

<b>Signature:</b>

```typescript
send: <Key_1 extends keyof ITypedMessages>(key: Key_1, data: ITypedMessages[Key_1], alsoSendToDocument?: boolean) => void;
send(...args: Parameters<MessageCenter<ITypedMessages>['emit']>): ReturnType<MessageCenter<ITypedMessages>['emit']>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| args | <code>Parameters&lt;MessageCenter&lt;ITypedMessages&gt;['emit']&gt;</code> | |

<b>Returns:</b>

`ReturnType<MessageCenter<ITypedMessages>['emit']>`

6 changes: 3 additions & 3 deletions doc/holoflows-kit.api.report.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ export class LiveSelector<T, SingleMode extends boolean = false> {
export class MessageCenter<ITypedMessages> {
constructor(instanceKey?: string);
emit<Key extends keyof ITypedMessages>(key: Key, data: ITypedMessages[Key], alsoSendToDocument?: boolean): void;
on<Key extends keyof ITypedMessages>(event: Key, handler: (data: ITypedMessages[Key]) => void): void;
// (undocumented)
send: <Key_1 extends keyof ITypedMessages>(key: Key_1, data: ITypedMessages[Key_1], alsoSendToDocument?: boolean) => void;
off<Key extends keyof ITypedMessages>(event: Key, handler: (data: ITypedMessages[Key]) => void): void;
on<Key extends keyof ITypedMessages>(event: Key, handler: (data: ITypedMessages[Key]) => void): () => void;
send(...args: Parameters<MessageCenter<ITypedMessages>['emit']>): ReturnType<MessageCenter<ITypedMessages>['emit']>;
writeToConsole: boolean;
}

Expand Down
22 changes: 19 additions & 3 deletions src/Extension/MessageCenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,20 @@ export class MessageCenter<ITypedMessages> {
* Listen to an event
* @param event - Name of the event
* @param handler - Handler of the event
* @returns a function, call it to remove this listener
*/
public on<Key extends keyof ITypedMessages>(event: Key, handler: (data: ITypedMessages[Key]) => void): void {
public on<Key extends keyof ITypedMessages>(event: Key, handler: (data: ITypedMessages[Key]) => void) {
this.eventEmitter.on(event as string, handler)
return () => this.off(event, handler)
}
/**
* Remove the listener of an event
* @param event - Name of the event
* @param handler - Handler of the event
*/
public off<Key extends keyof ITypedMessages>(event: Key, handler: (data: ITypedMessages[Key]) => void): void {
this.eventEmitter.off(event as string, handler)
}

/**
* Send message to local or other instance of extension
* @param key - Key of the message
Expand Down Expand Up @@ -90,7 +99,14 @@ export class MessageCenter<ITypedMessages> {
document.dispatchEvent(newMessage(key, data))
}
}
public send = this.emit
/**
* {@inheritdoc MessageCenter.emit}
*/
public send(
...args: Parameters<MessageCenter<ITypedMessages>['emit']>
): ReturnType<MessageCenter<ITypedMessages>['emit']> {
return Reflect.apply(this.emit, this, args)
}
/**
* Should MessageCenter prints all messages to console?
*/
Expand Down

0 comments on commit db4751b

Please sign in to comment.