generated from well-known-components/template-server
-
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.
- Loading branch information
Showing
8 changed files
with
688 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { IBaseComponent } from '@well-known-components/interfaces' | ||
import { AppComponents, SubscriptionEventsEmitter } from '../types' | ||
|
||
const FRIENDSHIP_UPDATES_CHANNEL = 'FRIENDSHIP_UPDATES' | ||
|
||
export type IPubSubComponent = IBaseComponent & { | ||
subscribeToFriendshipUpdates(cb: (message: string) => void): Promise<void> | ||
publishFriendshipUpdate(update: SubscriptionEventsEmitter['update']): Promise<void> | ||
} | ||
|
||
export default function createPubSubComponent(components: Pick<AppComponents, 'logs' | 'redis'>): IPubSubComponent { | ||
const { logs, redis } = components | ||
const logger = logs.getLogger('pubsub-component') | ||
|
||
const subClient = redis.client.duplicate() | ||
const pubClient = redis.client.duplicate() | ||
|
||
let friendshipUpdatesCb: (message: string) => void | undefined | ||
|
||
return { | ||
async start() { | ||
if (!subClient.isReady) { | ||
await subClient.connect() | ||
} | ||
|
||
if (!pubClient.isReady) { | ||
await pubClient.connect() | ||
} | ||
}, | ||
async stop() { | ||
if (subClient.isReady) { | ||
await subClient.disconnect() | ||
} | ||
|
||
if (pubClient.isReady) { | ||
await pubClient.disconnect() | ||
} | ||
}, | ||
async subscribeToFriendshipUpdates(cb) { | ||
try { | ||
friendshipUpdatesCb = cb | ||
await subClient.subscribe(FRIENDSHIP_UPDATES_CHANNEL, friendshipUpdatesCb) | ||
} catch (error) { | ||
logger.error(error as any) | ||
} | ||
}, | ||
async publishFriendshipUpdate(update) { | ||
try { | ||
const message = JSON.stringify(update) | ||
logger.debug('publishing update to FRIENDSHIP_UPDATES > ', { update: message }) | ||
await pubClient.publish(FRIENDSHIP_UPDATES_CHANNEL, message) | ||
} catch (error) { | ||
logger.error(error as any) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.