Skip to content

Commit

Permalink
refactor(rosenet-node): extract pubsub functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mkermani144 committed Sep 11, 2024
1 parent 7addf22 commit 72af986
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
17 changes: 3 additions & 14 deletions packages/rosenet-node/lib/createRoseNetNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
handleIncomingMessageFactory,
sendMessageFactory,
} from './rosenet-direct';
import { publishFactory, subscribeFactory } from './rosenet-pubsub';

import RoseNetNodeContext from './context/RoseNetNodeContext';

Expand All @@ -37,9 +38,6 @@ import packageJson from '../package.json' with { type: 'json' };

import { RoseNetNodeConfig } from './types';

const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();

const createRoseNetNode = async ({
logger,
port = DEFAULT_NODE_PORT,
Expand Down Expand Up @@ -143,17 +141,8 @@ const createRoseNetNode = async ({
start: async () => node.start(),
sendMessage: sendMessageFactory(node),
handleIncomingMessage: handleIncomingMessageFactory(node),
publish: async (topic: string, message: string) => {
node.services.pubsub.publish(topic, textEncoder.encode(message));
},
subscribe: 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));
}
});
},
publish: publishFactory(node),
subscribe: subscribeFactory(node),
};
};

Expand Down
2 changes: 2 additions & 0 deletions packages/rosenet-node/lib/rosenet-pubsub/index.ts
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';
14 changes: 14 additions & 0 deletions packages/rosenet-node/lib/rosenet-pubsub/publish.ts
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;
19 changes: 19 additions & 0 deletions packages/rosenet-node/lib/rosenet-pubsub/subscribe.ts
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;

0 comments on commit 72af986

Please sign in to comment.