From 17c75e67616a79e62644d4f1fb62d605e69817f1 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 5 Sep 2023 22:00:32 -0500 Subject: [PATCH] Move pool to a separate module (to avoid importing firehose in tests) --- src/firehose.ts | 16 +++------------- src/pipeline.ts | 2 +- src/pool.ts | 12 ++++++++++++ 3 files changed, 16 insertions(+), 14 deletions(-) create mode 100644 src/pool.ts diff --git a/src/firehose.ts b/src/firehose.ts index a685160..a510d65 100644 --- a/src/firehose.ts +++ b/src/firehose.ts @@ -1,18 +1,15 @@ -import { getActiveRelays } from '@/db/relays.ts'; -import { type Event, RelayPool } from '@/deps.ts'; +import { type Event } from '@/deps.ts'; +import { allRelays, pool } from '@/pool.ts'; import { nostrNow } from '@/utils.ts'; import * as pipeline from './pipeline.ts'; -const _relays = await getActiveRelays(); -const pool = new RelayPool(_relays); - // This file watches events on all known relays and performs // side-effects based on them, such as trending hashtag tracking // and storing events for notifications and the home feed. pool.subscribe( [{ kinds: [0, 1, 3, 5, 6, 7, 10002], limit: 0, since: nostrNow() }], - _relays, + allRelays, handleEvent, undefined, undefined, @@ -26,10 +23,3 @@ function handleEvent(event: Event): Promise { .handleEvent(event) .catch(() => {}); } - -/** Publish an event to the given relays, or the entire pool. */ -function publish(event: Event, relays: string[] = _relays) { - return pool.publish(event, relays); -} - -export { publish }; diff --git a/src/pipeline.ts b/src/pipeline.ts index 2b24fec..38e3214 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -3,9 +3,9 @@ import * as eventsDB from '@/db/events.ts'; import { addRelays } from '@/db/relays.ts'; import { findUser } from '@/db/users.ts'; import { type Event, LRUCache } from '@/deps.ts'; -import { publish } from '@/firehose.ts'; import { isEphemeralKind } from '@/kinds.ts'; import * as mixer from '@/mixer.ts'; +import { publish } from '@/pool.ts'; import { isLocallyFollowed } from '@/queries.ts'; import { Sub } from '@/subs.ts'; import { getTagSet } from '@/tags.ts'; diff --git a/src/pool.ts b/src/pool.ts new file mode 100644 index 0000000..5b5eea7 --- /dev/null +++ b/src/pool.ts @@ -0,0 +1,12 @@ +import { getActiveRelays } from '@/db/relays.ts'; +import { type Event, RelayPool } from '@/deps.ts'; + +const allRelays = await getActiveRelays(); +const pool = new RelayPool(allRelays); + +/** Publish an event to the given relays, or the entire pool. */ +function publish(event: Event, relays: string[] = allRelays) { + return pool.publish(event, relays); +} + +export { allRelays, pool, publish };