From eb41cbf8e94f132e68167c1ec2c10852d8969ff5 Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Thu, 3 Oct 2019 20:05:27 -0400 Subject: [PATCH] renewing tokens in Websockets --- src/app/services/streaming.service.ts | 41 ++++++++++++++++----------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/app/services/streaming.service.ts b/src/app/services/streaming.service.ts index d1c9554a..b0cde3ab 100644 --- a/src/app/services/streaming.service.ts +++ b/src/app/services/streaming.service.ts @@ -6,6 +6,7 @@ import { ApiRoutes } from "./models/api.settings"; import { StreamTypeEnum, StreamElement } from "../states/streams.state"; import { MastodonWrapperService } from "./mastodon-wrapper.service"; import { AccountInfo } from "../states/accounts.state"; +import { AccountIconComponent } from '../components/left-side-bar/account-icon/account-icon.component'; @Injectable() export class StreamingService { @@ -34,8 +35,7 @@ export class StreamingWrapper { private readonly stream: StreamElement, private readonly nbStatusPerIteration: number) { - const route = this.getRoute(account, stream); - this.start(route); + this.start(account, stream); } dispose(): any { @@ -43,34 +43,41 @@ export class StreamingWrapper { this.eventSource.close(); } - private start(route: string) { - this.eventSource = new WebSocket(route); - this.eventSource.onmessage = x => { - if (x.data !== '') { - this.statusParsing(JSON.parse(x.data)); - } - } - this.eventSource.onerror = x => this.webSocketGotError(x); - this.eventSource.onopen = x => { }; - this.eventSource.onclose = x => this.webSocketClosed(route, x); + private start(account: AccountInfo, stream: StreamElement) { + this.mastodonService.refreshAccountIfNeeded(account) + .catch(err => { + return account; + }) + .then((refreshedAccount: AccountInfo) => { + const route = this.getRoute(refreshedAccount, stream); + this.eventSource = new WebSocket(route); + this.eventSource.onmessage = x => { + if (x.data !== '') { + this.statusParsing(JSON.parse(x.data)); + } + } + this.eventSource.onerror = x => this.webSocketGotError(x); + this.eventSource.onopen = x => { }; + this.eventSource.onclose = x => this.webSocketClosed(refreshedAccount, stream, x); + }); } private webSocketGotError(x: Event) { this.errorClosing = true; } - private webSocketClosed(domain, x: Event) { + private webSocketClosed(account: AccountInfo, stream: StreamElement, x: Event) { if (this.errorClosing) { setTimeout(() => { - this.pullNewStatuses(domain); + this.pullNewStatuses(); this.errorClosing = false; }, 60 * 1000); } else if (!this.disposed) { - setTimeout(() => { this.start(domain) }, 60 * 1000); + setTimeout(() => { this.start(account, stream) }, 60 * 1000); } } - private pullNewStatuses(domain) { + private pullNewStatuses() { this.mastodonService.getTimeline(this.account, this.stream.type, null, this.since_id, this.nbStatusPerIteration, this.stream.tag, this.stream.listId) .then((status: Status[]) => { // status = status.sort((n1, n2) => { return (n1.id) < (n2.id); }); @@ -89,7 +96,7 @@ export class StreamingWrapper { .then(() => { // setTimeout(() => { this.start(domain) }, 20 * 1000); if (!this.disposed) { - setTimeout(() => { this.pullNewStatuses(domain) }, 60 * 1000); + setTimeout(() => { this.pullNewStatuses() }, 60 * 1000); } }); }