Websocket reconnect reload page data. Fixes #504

This commit is contained in:
Dessalines 2020-02-04 11:19:05 -05:00
parent e4dfa5e52f
commit b0b50098a4
9 changed files with 27 additions and 6 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
ansible/inventory
ansible/inventory_dev
ansible/passwords/
docker/lemmy_mine.hjson
docker/dev/env_deploy.sh

View file

@ -254,6 +254,8 @@ export class Community extends Component<any, State> {
toast(i18n.t(msg.error), 'danger');
this.context.router.history.push('/');
return;
} else if (msg.reconnect) {
this.fetchPosts();
} else if (res.op == UserOperation.GetCommunity) {
let data = res.data as GetCommunityResponse;
this.state.community = data.community;

View file

@ -313,6 +313,8 @@ export class Inbox extends Component<any, InboxState> {
if (msg.error) {
toast(i18n.t(msg.error), 'danger');
return;
} else if (msg.reconnect) {
this.refetch();
} else if (res.op == UserOperation.GetReplies) {
let data = res.data as GetRepliesResponse;
this.state.replies = data.replies;

View file

@ -537,6 +537,8 @@ export class Main extends Component<any, MainState> {
if (msg.error) {
toast(i18n.t(msg.error), 'danger');
return;
} else if (msg.reconnect) {
this.fetchPosts();
} else if (res.op == UserOperation.GetFollowedCommunities) {
let data = res.data as GetFollowedCommunitiesResponse;
this.state.subscribedCommunities = data.communities;

View file

@ -208,6 +208,8 @@ export class Navbar extends Component<any, NavbarState> {
location.reload();
}
return;
} else if (msg.reconnect) {
this.fetchUnreads();
} else if (res.op == UserOperation.GetReplies) {
let data = res.data as GetRepliesResponse;
let unreadReplies = data.replies.filter(r => !r.read);

View file

@ -370,6 +370,10 @@ export class Post extends Component<any, PostState> {
if (msg.error) {
toast(i18n.t(msg.error), 'danger');
return;
} else if (msg.reconnect) {
WebSocketService.Instance.getPost({
id: Number(this.props.match.params.id),
});
} else if (res.op == UserOperation.GetPost) {
let data = res.data as GetPostResponse;
this.state.post = data.post;

View file

@ -991,6 +991,8 @@ export class User extends Component<any, UserState> {
}
this.setState(this.state);
return;
} else if (msg.reconnect) {
this.refetch();
} else if (res.op == UserOperation.GetUserDetails) {
let data = res.data as UserDetailsResponse;
this.state.user = data.user;

View file

@ -854,4 +854,5 @@ export interface WebSocketJsonResponse {
op?: string;
data?: ResponseType;
error?: string;
reconnect?: boolean;
}

View file

@ -40,6 +40,7 @@ import {
GetPrivateMessagesForm,
UserJoinForm,
MessageType,
WebSocketJsonResponse,
} from '../interfaces';
import { UserService } from './';
import { i18n } from '../i18next';
@ -59,17 +60,21 @@ export class WebSocketService {
private constructor() {
this.ws = new ReconnectingWebSocket(wsUri);
this.ws.onopen = () => {
console.log(`Connected to ${wsUri}`);
if (UserService.Instance.user) {
this.userJoin();
}
};
this.subject = Observable.create((obs: any) => {
this.ws.onmessage = e => {
obs.next(JSON.parse(e.data));
};
this.ws.onopen = () => {
console.log(`Connected to ${wsUri}`);
if (UserService.Instance.user) {
this.userJoin();
}
let res: WebSocketJsonResponse = {
reconnect: true,
};
obs.next(res);
};
}).pipe(share());
}