Skip to content

Commit

Permalink
Merge pull request #932 from SnowCait/reconnect
Browse files Browse the repository at this point in the history
Reconnect
  • Loading branch information
SnowCait authored Dec 28, 2023
2 parents 2504602 + e3c89b7 commit 2951e0e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 62 deletions.
36 changes: 32 additions & 4 deletions web/src/lib/timelines/MainTimeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
createRxNostr,
filterByType,
latestEach,
uniq
uniq,
type ConnectionState
} from 'rx-nostr';
import { tap, bufferTime } from 'rxjs';
import { timeout } from '$lib/Constants';
Expand All @@ -16,14 +17,16 @@ import { eventItemStore, metadataStore } from '../cache/Events';
import { Content } from '$lib/Content';
import { ToastNotification } from '$lib/ToastNotification';

export const rxNostr = createRxNostr({ eoseTimeout: timeout }); // Based on NIP-65
export const rxNostr = createRxNostr({
eoseTimeout: timeout,
keepAliveDefaultRelayConnections: false
}); // Based on NIP-65

rxNostr.createConnectionStateObservable().subscribe(({ from, state }) => {
switch (state) {
case 'error':
case 'rejected':
case 'terminated':
case 'initialized': {
case 'terminated': {
console.error('[rx-nostr connection]', from, state);
break;
}
Expand All @@ -33,6 +36,7 @@ rxNostr.createConnectionStateObservable().subscribe(({ from, state }) => {
console.warn('[rx-nostr connection]', from, state);
break;
}
case 'initialized':
case 'connecting':
case 'connected':
default: {
Expand All @@ -42,6 +46,30 @@ rxNostr.createConnectionStateObservable().subscribe(({ from, state }) => {
}
});

const recconectableStates: ConnectionState[] = [
'error',
'rejected',
'terminated',
'waiting-for-retrying',
'retrying',
'dormant'
];

export function reconnectIfConnectionsAreUnstable(): void {
const states = Object.entries(rxNostr.getAllRelayState());
console.log('[relay states]', states);
if (
states.filter(([, state]) => recconectableStates.includes(state)).length * 2 <
states.length
) {
return;
}

// TODO: Clear timeline and reconnect WebSocket without reload
console.log('[reload]', states);
location.reload();
}

const observable = rxNostr.createAllMessageObservable();
observable.pipe(filterByType('NOTICE')).subscribe((packet) => {
console.warn('[rx-nostr notice]', packet);
Expand Down
7 changes: 2 additions & 5 deletions web/src/routes/(app)/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
import { createRxOneshotReq, latest } from 'rx-nostr';
import { WebStorage } from '$lib/WebStorage';
import { notificationKinds } from '$lib/NotificationTimeline';
import { rxNostr } from '$lib/timelines/MainTimeline';
import { reconnectIfConnectionsAreUnstable, rxNostr } from '$lib/timelines/MainTimeline';
import Notice from '$lib/components/Notice.svelte';
import Header from './Header.svelte';
import NoteDialog from './NoteDialog.svelte';
import { openNoteDialog } from '../../stores/NoteDialog';
import { pubkey } from '../../stores/Author';
import { lastReadAt, lastNotifiedAt } from '../../stores/Notifications';
import { onMount } from 'svelte';
import ReloadDialog from './ReloadDialog.svelte';
import Gdpr from './parts/Gdpr.svelte';
let reloadDialogComponent: ReloadDialog;
const konamiCode = [
'ArrowUp',
'ArrowUp',
Expand Down Expand Up @@ -94,7 +92,7 @@
break;
}
case 'visible': {
setTimeout(() => reloadDialogComponent.tryOpen(), 1000);
setTimeout(() => reconnectIfConnectionsAreUnstable(), 1000);
break;
}
}
Expand Down Expand Up @@ -137,7 +135,6 @@

<div class="app">
<NoteDialog />
<ReloadDialog bind:this={reloadDialogComponent} />

<header>
<div>
Expand Down
53 changes: 0 additions & 53 deletions web/src/routes/(app)/ReloadDialog.svelte

This file was deleted.

1 comment on commit 2951e0e

@vercel
Copy link

@vercel vercel bot commented on 2951e0e Dec 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nostter – ./

nostter-git-main-snowcait.vercel.app
nostter.vercel.app
nostter-snowcait.vercel.app
nostter.app

Please sign in to comment.