-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(rosenet-node): extract pubsub functions
- Loading branch information
1 parent
7addf22
commit 72af986
Showing
4 changed files
with
38 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default as publishFactory } from './publish'; | ||
export { default as subscribeFactory } from './subscribe'; |
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,14 @@ | ||
import { Libp2p, PubSub } from '@libp2p/interface'; | ||
|
||
const textEncoder = new TextEncoder(); | ||
|
||
/** | ||
* factory for libp2p publish | ||
*/ | ||
const publishFactory = | ||
(node: Libp2p<{ pubsub: PubSub }>) => | ||
async (topic: string, message: string) => { | ||
node.services.pubsub.publish(topic, textEncoder.encode(message)); | ||
}; | ||
|
||
export default publishFactory; |
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,19 @@ | ||
import { Libp2p, PubSub } from '@libp2p/interface'; | ||
|
||
const textDecoder = new TextDecoder(); | ||
|
||
/** | ||
* factory for libp2p subscribe | ||
*/ | ||
const subscribeFactory = | ||
(node: Libp2p<{ pubsub: PubSub }>) => | ||
async (topic: string, handler: (message: string) => void) => { | ||
node.services.pubsub.subscribe(topic); | ||
node.services.pubsub.addEventListener('message', (event) => { | ||
if (event.detail.topic === topic) { | ||
handler(textDecoder.decode(event.detail.data)); | ||
} | ||
}); | ||
}; | ||
|
||
export default subscribeFactory; |