renewing tokens in Websockets

This commit is contained in:
Nicolas Constant 2019-10-03 20:05:27 -04:00
parent 6cd3d7272a
commit eb41cbf8e9
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
1 changed files with 24 additions and 17 deletions

View File

@ -6,6 +6,7 @@ import { ApiRoutes } from "./models/api.settings";
import { StreamTypeEnum, StreamElement } from "../states/streams.state"; import { StreamTypeEnum, StreamElement } from "../states/streams.state";
import { MastodonWrapperService } from "./mastodon-wrapper.service"; import { MastodonWrapperService } from "./mastodon-wrapper.service";
import { AccountInfo } from "../states/accounts.state"; import { AccountInfo } from "../states/accounts.state";
import { AccountIconComponent } from '../components/left-side-bar/account-icon/account-icon.component';
@Injectable() @Injectable()
export class StreamingService { export class StreamingService {
@ -34,8 +35,7 @@ export class StreamingWrapper {
private readonly stream: StreamElement, private readonly stream: StreamElement,
private readonly nbStatusPerIteration: number) { private readonly nbStatusPerIteration: number) {
const route = this.getRoute(account, stream); this.start(account, stream);
this.start(route);
} }
dispose(): any { dispose(): any {
@ -43,34 +43,41 @@ export class StreamingWrapper {
this.eventSource.close(); this.eventSource.close();
} }
private start(route: string) { private start(account: AccountInfo, stream: StreamElement) {
this.eventSource = new WebSocket(route); this.mastodonService.refreshAccountIfNeeded(account)
this.eventSource.onmessage = x => { .catch(err => {
if (x.data !== '') { return account;
this.statusParsing(<WebSocketEvent>JSON.parse(x.data)); })
} .then((refreshedAccount: AccountInfo) => {
} const route = this.getRoute(refreshedAccount, stream);
this.eventSource.onerror = x => this.webSocketGotError(x); this.eventSource = new WebSocket(route);
this.eventSource.onopen = x => { }; this.eventSource.onmessage = x => {
this.eventSource.onclose = x => this.webSocketClosed(route, x); if (x.data !== '') {
this.statusParsing(<WebSocketEvent>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) { private webSocketGotError(x: Event) {
this.errorClosing = true; this.errorClosing = true;
} }
private webSocketClosed(domain, x: Event) { private webSocketClosed(account: AccountInfo, stream: StreamElement, x: Event) {
if (this.errorClosing) { if (this.errorClosing) {
setTimeout(() => { setTimeout(() => {
this.pullNewStatuses(domain); this.pullNewStatuses();
this.errorClosing = false; this.errorClosing = false;
}, 60 * 1000); }, 60 * 1000);
} else if (!this.disposed) { } 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) this.mastodonService.getTimeline(this.account, this.stream.type, null, this.since_id, this.nbStatusPerIteration, this.stream.tag, this.stream.listId)
.then((status: Status[]) => { .then((status: Status[]) => {
// status = status.sort((n1, n2) => { return (<number>n1.id) < (<number>n2.id); }); // status = status.sort((n1, n2) => { return (<number>n1.id) < (<number>n2.id); });
@ -89,7 +96,7 @@ export class StreamingWrapper {
.then(() => { .then(() => {
// setTimeout(() => { this.start(domain) }, 20 * 1000); // setTimeout(() => { this.start(domain) }, 20 * 1000);
if (!this.disposed) { if (!this.disposed) {
setTimeout(() => { this.pullNewStatuses(domain) }, 60 * 1000); setTimeout(() => { this.pullNewStatuses() }, 60 * 1000);
} }
}); });
} }