-
Notifications
You must be signed in to change notification settings - Fork 7
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
10 changed files
with
222 additions
and
54 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,20 @@ | ||
import { Relay } from "nostr-tools"; | ||
|
||
const relayMap = new Map<string, Relay>(); | ||
|
||
export async function getRelay(url: string): Promise<Relay> { | ||
if (relayMap.has(url)) { | ||
const relay = relayMap.get(url)!; | ||
await relay.connect(); | ||
return relay; | ||
} | ||
const relay = new Relay(url); | ||
relayMap.set(url, relay); | ||
await relay.connect(); | ||
return relay; | ||
} | ||
|
||
export function getAllRelays(): Relay[] { | ||
const relays = Array.from(relayMap.values()); | ||
return relays; | ||
} |
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,69 @@ | ||
import { addEvent } from "@/redux/slices/events.slice"; | ||
import { setSubscriptionHasSeenEOSE } from "@/redux/slices/relays.slice"; | ||
import { Event, Filter } from "nostr-tools"; | ||
import { Subscription } from "nostr-tools/lib/types/abstract-relay"; | ||
import { getRelay } from "./relays.nostr"; | ||
|
||
const subscriptions = new Map<string, Subscription>(); | ||
|
||
function generateId() { | ||
return Math.random().toString().slice(2); | ||
} | ||
|
||
/** | ||
* - The relays are not linked to redux currently | ||
* - We should set it up to log relay state into redux | ||
* - Then we can push subscription state | ||
* - Then we can update subscription state | ||
*/ | ||
|
||
export async function subscribeToFilter({ | ||
filter, | ||
relayUrl, | ||
subscriptionId, | ||
store, | ||
}: { | ||
filter: Filter; | ||
relayUrl: string; | ||
subscriptionId?: string; | ||
store: any; | ||
}) { | ||
const relay = await getRelay(relayUrl); | ||
|
||
const id = | ||
typeof subscriptionId === "string" && subscriptionId.length > 3 | ||
? subscriptionId | ||
: generateId(); | ||
|
||
const subscription = relay.subscribe([filter], { | ||
id, | ||
onevent: (event: Event) => { | ||
store.dispatch(addEvent({ event, fromRelay: relayUrl })); | ||
}, | ||
oneose: () => { | ||
store.dispatch(setSubscriptionHasSeenEOSE({ id, relayUrl })); | ||
}, | ||
// onevent, | ||
// oneose, | ||
// onevent: (event: Event) => { | ||
// store.dispatch(addEvent({ event, fromRelay: relayUrl })); | ||
// }, | ||
// oneose: () => { | ||
// store.dispatch(setSubscriptionHasSeenEOSE({ id, relayUrl })); | ||
// }, | ||
// NOTE: Type casting here because `id` is not available on `.subscribe()` | ||
// https://github.com/nbd-wtf/nostr-tools/issues/439 | ||
} as {}); | ||
|
||
subscriptions.set(id, subscription); | ||
|
||
return id; | ||
} | ||
|
||
export function getSubscription(id: string) { | ||
const subscription = subscriptions.get(id); | ||
if (typeof subscription === "undefined") { | ||
throw new Error("Tried to get invalid subscription by ID #MITKA7"); | ||
} | ||
return subscription; | ||
} |
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,12 @@ | ||
import { createAction } from "@reduxjs/toolkit"; | ||
import { Filter } from "nostr-tools"; | ||
|
||
export const startSubscription = createAction<{ | ||
filter: Filter; | ||
id?: string; | ||
relayUrls?: string[]; | ||
}>("subscriptions/startSubscription"); | ||
|
||
export const stopSubscription = createAction<string>( | ||
"subscriptions/stopSubscription", | ||
); |
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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { all } from "redux-saga/effects"; | ||
import mapSaga from "./map.saga"; | ||
import subscriptionSaga from "./subscriptions.saga"; | ||
|
||
function* helloSaga() { | ||
console.log("#W0W1gS Hello from the hello saga"); | ||
} | ||
|
||
export default function* rootSaga() { | ||
yield all([helloSaga()]); | ||
yield all([helloSaga(), mapSaga(), subscriptionSaga()]); | ||
} |
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 |
---|---|---|
@@ -1,9 +1,63 @@ | ||
import { takeEvery } from "redux-saga/effects"; | ||
import { | ||
getSubscription, | ||
subscribeToFilter, | ||
} from "@/nostr/subscriptions.nostr"; | ||
import { Subscription } from "nostr-tools/lib/types/abstract-relay"; | ||
import { all, call, fork, StrictEffect, takeEvery } from "redux-saga/effects"; | ||
import { | ||
startSubscription, | ||
stopSubscription, | ||
} from "../actions/subscription.actions"; | ||
import { AppStore } from "../store"; | ||
|
||
function* subscriptionSagaWorker() { | ||
// Do something | ||
// NOTE: This pattern is required to avoid a circular import dependency | ||
let store: AppStore; | ||
export function injectStore(_store: AppStore) { | ||
store = _store; | ||
} | ||
|
||
function getRelayUrlsOrDefaults(relayUrls?: string[]) { | ||
if (typeof relayUrls === "undefined" || relayUrls.length === 0) { | ||
// TODO: Get defaults from redux | ||
const defaultRelayUrls = ["wss://nos.lol"]; | ||
return defaultRelayUrls; | ||
} | ||
|
||
return relayUrls; | ||
} | ||
|
||
function* startSubscriptionSagaEffect( | ||
action: ReturnType<typeof startSubscription>, | ||
) { | ||
const { filter, id, relayUrls } = action.payload; | ||
|
||
const actualRelayUrls = getRelayUrlsOrDefaults(relayUrls); | ||
|
||
for (const relayUrl of actualRelayUrls) { | ||
yield fork(subscribeToFilter, { | ||
filter, | ||
relayUrl, | ||
subscriptionId: id, | ||
store, | ||
}); | ||
} | ||
} | ||
|
||
export function* startSubscriptionSaga() { | ||
yield takeEvery(startSubscription, startSubscriptionSagaEffect); | ||
} | ||
|
||
function* stopSubscriptionSagaEffect( | ||
action: ReturnType<typeof stopSubscription>, | ||
): Generator<StrictEffect, void, Subscription> { | ||
const subscription = yield call(getSubscription, action.payload); | ||
yield call(subscription.close); | ||
} | ||
|
||
function* stopSubscriptionSaga() { | ||
yield takeEvery(stopSubscription, stopSubscriptionSagaEffect); | ||
} | ||
|
||
export default function* subscriptionSaga() { | ||
yield takeEvery("some_action", subscriptionSagaWorker); | ||
yield all([startSubscriptionSaga(), stopSubscriptionSaga()]); | ||
} |
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