Skip to content

Commit

Permalink
feat: add Channel
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Sep 20, 2024
1 parent 8c0c90d commit 086d9ca
Show file tree
Hide file tree
Showing 9 changed files with 350 additions and 6 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ jobs:

strategy:
matrix:
node-version: [16.x]
node-version: [18.x]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install -g codecov
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v2
uses: actions/setup-node@v4
with:
node-version: '16.x'
node-version: '18.x'
registry-url: 'https://registry.npmjs.org'
- name: Build licia
run: npm i && npm link && npm run build
Expand Down
72 changes: 72 additions & 0 deletions DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,78 @@ c.remove('content-type');
c.has('content-type'); // -> false
```

## Channel

Interconnectable Message channel.

<details>
<summary>Type Definition</summary>

```typescript
class Channel extends Emitter {
send(msg: any): void;
connect(channel: Channel): void;
disconnect(channel: Channel): void;
isConnected(channel: Channel): boolean;
destroy(): void;
}
```

</details>

### send

Send a message to all connected channels.

|Name|Desc |
|----|---------------|
|msg |Message to send|

### connect

Connect to another channel.

|Name |Desc |
|-------|------------------|
|channel|Channel to connect|

### disconnect

Disconnect from another channel.

|Name |Desc |
|-------|---------------------|
|channel|Channel to disconnect|

### isConnected

Check if a channel is connected to another channel.

|Name |Desc |
|-------|----------------------|
|channel|Channel to check |
|return |Whether it's connected|

### destroy

Destroy the channel, disconnect from all connected channels.

```javascript
const channelA = new Channel();
const channelB = new Channel();
channelA.connect(channelB);
channelB.on('message', msg => {
console.log(msg); // -> 'hello'
});
channelA.send('hello');
channelA.on('message', msg => {
console.log(msg); // -> 'world'
});
channelB.send('world');
channelA.isConnected(channelB); // -> true
channelB.isConnected(channelA); // -> true
```

## Class

Create JavaScript class.
Expand Down
72 changes: 72 additions & 0 deletions DOC_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,78 @@ c.remove('content-type');
c.has('content-type'); // -> false
```

## Channel

可以相互连接的消息通道。

<details>
<summary>类型定义</summary>

```typescript
class Channel extends Emitter {
send(msg: any): void;
connect(channel: Channel): void;
disconnect(channel: Channel): void;
isConnected(channel: Channel): boolean;
destroy(): void;
}
```

</details>

### send

发送消息给所有连接的通道。

|参数名|说明|
|-----|---|
|msg|要发送的消息|

### connect

连接到指定通道。

|参数名|说明|
|-----|---|
|channel|要连接的通道|

### disconnect

断开与指定通道的连接。

|参数名|说明|
|-----|---|
|channel|要断开的通道|

### isConnected

检查两个通道是否连接。

|参数名|说明|
|-----|---|
|channel|目标通道|
|返回值|是否连接|

### destroy

销毁通道,断开所有连接的通道。

```javascript
const channelA = new Channel();
const channelB = new Channel();
channelA.connect(channelB);
channelB.on('message', msg => {
console.log(msg); // -> 'hello'
});
channelA.send('hello');
channelA.on('message', msg => {
console.log(msg); // -> 'world'
});
channelB.send('world');
channelA.isConnected(channelB); // -> true
channelB.isConnected(channelA); // -> true
```

## Class

创建 JavaScript 类。
Expand Down
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"castPath",
"centerAlign",
"cgroup",
"Channel",
"char",
"chunk",
"clamp",
Expand Down
40 changes: 40 additions & 0 deletions i18n/Channel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## CN

可以相互连接的消息通道。

### send

发送消息给所有连接的通道。

|参数名|说明|
|-----|---|
|msg|要发送的消息|

### connect

连接到指定通道。

|参数名|说明|
|-----|---|
|channel|要连接的通道|

### disconnect

断开与指定通道的连接。

|参数名|说明|
|-----|---|
|channel|要断开的通道|

### isConnected

检查两个通道是否连接。

|参数名|说明|
|-----|---|
|channel|目标通道|
|返回值|是否连接|

### destroy

销毁通道,断开所有连接的通道。
18 changes: 18 additions & 0 deletions index.json
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,24 @@
"browser"
]
},
"Channel": {
"dependencies": [
"Emitter",
"each",
"remove",
"some"
],
"description": "Interconnectable Message channel.",
"env": [
"node",
"browser",
"miniprogram"
],
"test": [
"node",
"browser"
]
},
"Class": {
"demo": true,
"dependencies": [
Expand Down
114 changes: 114 additions & 0 deletions src/Channel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/* Interconnectable Message channel.
*
* ### send
*
* Send a message to all connected channels.
*
* |Name|Desc |
* |----|---------------|
* |msg |Message to send|
*
* ### connect
*
* Connect to another channel.
*
* |Name |Desc |
* |-------|------------------|
* |channel|Channel to connect|
*
* ### disconnect
*
* Disconnect from another channel.
*
* |Name |Desc |
* |-------|---------------------|
* |channel|Channel to disconnect|
*
* ### isConnected
*
* Check if a channel is connected to another channel.
*
* |Name |Desc |
* |-------|----------------------|
* |channel|Channel to check |
* |return |Whether it's connected|
*
* ### destroy
*
* Destroy the channel, disconnect from all connected channels.
*/

/* example
* const channelA = new Channel();
* const channelB = new Channel();
* channelA.connect(channelB);
* channelB.on('message', msg => {
* console.log(msg); // -> 'hello'
* });
* channelA.send('hello');
* channelA.on('message', msg => {
* console.log(msg); // -> 'world'
* });
* channelB.send('world');
* channelA.isConnected(channelB); // -> true
* channelB.isConnected(channelA); // -> true
*/

/* module
* env: all
*/

/* typescript
* export declare class Channel extends Emitter {
* send(msg: any): void;
* connect(channel: Channel): void;
* disconnect(channel: Channel): void;
* isConnected(channel: Channel): boolean;
* destroy(): void;
* }
*/

_('Emitter each remove some');

exports = Emitter.extend({
initialize: function Channel() {
this._connections = [];

this.callSuper(Emitter, 'initialize');
},
send(msg) {
each(this._connections, connection => {
connection.emit('message', msg, this);
});
},
connect(connection) {
if (this.isConnected(connection)) {
return;
}

this._connections.push(connection);

connection.connect(this);
},
disconnect(connection) {
if (!this.isConnected(connection)) {
return;
}

remove(this._connections, item => item === connection);

connection.disconnect(this);
},
isConnected(connection) {
if (connection === this) {
throw new Error('Connection cannot be connected to itself.');
}

return some(this._connections, item => item === connection);
},
destroy() {
each(this._connections, connection => {
this.disconnect(connection);
});
}
});
Loading

0 comments on commit 086d9ca

Please sign in to comment.