-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Group interface to represent entire network or sub-sets o…
…f it
- Loading branch information
1 parent
36d6e66
commit 061bd6b
Showing
9 changed files
with
96 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { Subscribable } from 'atvik'; | ||
|
||
import { MessageData } from './MessageData'; | ||
import { MessageType } from './MessageType'; | ||
import { MessageUnion } from './MessageUnion'; | ||
import { Node } from './Node'; | ||
|
||
/** | ||
* Groups are a collection of nodes that can be reached in some way. This may | ||
* either be a full network or a sub-set of one. Groups will emit events when | ||
* membership changes via {@link onNodeAvailable} and {@link onNodeUnavailable}. | ||
* | ||
* Messages sent to nodes in the group can be listened to via {@link onMessage} | ||
* and broadcasts can be sent via {@link broadcast}. | ||
* | ||
* ```javascript | ||
* const group = ...; | ||
* | ||
* // Listen to messages | ||
* group.onMessage(message => ...); | ||
* | ||
* // Join the group | ||
* await group.join(); | ||
* | ||
* // Leave the group | ||
* await group.leave(); | ||
* ``` | ||
*/ | ||
export interface Group<MessageTypes extends object = any> { | ||
/** | ||
* Name of this group, useful for debugging purposes. | ||
*/ | ||
readonly name: string; | ||
|
||
/** | ||
* Event emitted when a new node joins this exchange. | ||
*/ | ||
readonly onNodeAvailable: Subscribable<this, [ node: Node<MessageTypes> ]>; | ||
|
||
/** | ||
* Event emitted when a node leaves this exchange. | ||
*/ | ||
readonly onNodeUnavailable: Subscribable<this, [ node: Node<MessageTypes> ]>; | ||
|
||
/** | ||
* Event emitted when a message is received on this exchange. | ||
*/ | ||
readonly onMessage: Subscribable<this, [ message: MessageUnion<MessageTypes> ]>; | ||
|
||
/** | ||
* Get all of the nodes that are part of this group. | ||
*/ | ||
readonly nodes: ReadonlyArray<Node>; | ||
|
||
/** | ||
* Broadcast a message to all nodes that have joined this exchange. | ||
* | ||
* @param type - | ||
* the type of message to send | ||
* @param data - | ||
* the data to send | ||
*/ | ||
broadcast<T extends MessageType<MessageTypes>>( | ||
type: T, | ||
data: MessageData<MessageTypes, T> | ||
): Promise<void>; | ||
|
||
/** | ||
* Join this exchange. | ||
*/ | ||
join(): Promise<void>; | ||
|
||
/** | ||
* Leave this exchange. | ||
*/ | ||
leave(): Promise<void>; | ||
} |
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
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