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 { 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(<WebSocketEvent>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(<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) {
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 (<number>n1.id) < (<number>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);
}
});
}