-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
350 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ | |
"castPath", | ||
"centerAlign", | ||
"cgroup", | ||
"Channel", | ||
"char", | ||
"chunk", | ||
"clamp", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
## CN | ||
|
||
可以相互连接的消息通道。 | ||
|
||
### send | ||
|
||
发送消息给所有连接的通道。 | ||
|
||
|参数名|说明| | ||
|-----|---| | ||
|msg|要发送的消息| | ||
|
||
### connect | ||
|
||
连接到指定通道。 | ||
|
||
|参数名|说明| | ||
|-----|---| | ||
|channel|要连接的通道| | ||
|
||
### disconnect | ||
|
||
断开与指定通道的连接。 | ||
|
||
|参数名|说明| | ||
|-----|---| | ||
|channel|要断开的通道| | ||
|
||
### isConnected | ||
|
||
检查两个通道是否连接。 | ||
|
||
|参数名|说明| | ||
|-----|---| | ||
|channel|目标通道| | ||
|返回值|是否连接| | ||
|
||
### destroy | ||
|
||
销毁通道,断开所有连接的通道。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
}); |
Oops, something went wrong.