From 143d445fcb408ab66a66c1219b51d5c75ea6fd02 Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Fri, 23 Nov 2018 23:04:02 -0500 Subject: [PATCH] browsing hashtag working --- .../stream-overlay.component.html | 2 +- .../stream-statuses.component.ts | 10 +++++++++ src/app/services/streaming.service.ts | 22 +++++++++++++------ 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/app/components/stream/stream-overlay/stream-overlay.component.html b/src/app/components/stream/stream-overlay/stream-overlay.component.html index 51e87387..77f68df8 100644 --- a/src/app/components/stream/stream-overlay/stream-overlay.component.html +++ b/src/app/components/stream/stream-overlay/stream-overlay.component.html @@ -10,6 +10,6 @@ --> - + \ No newline at end of file diff --git a/src/app/components/stream/stream-statuses/stream-statuses.component.ts b/src/app/components/stream/stream-statuses/stream-statuses.component.ts index 74ef3465..edb1dc5b 100644 --- a/src/app/components/stream/stream-statuses/stream-statuses.component.ts +++ b/src/app/components/stream/stream-statuses/stream-statuses.component.ts @@ -16,6 +16,7 @@ import { StatusWrapper } from '../stream.component'; styleUrls: ['./stream-statuses.component.scss'] }) export class StreamStatusesComponent implements OnInit, OnDestroy { + private _streamElement: StreamElement; private account: AccountInfo; private websocketStreaming: StreamingWrapper; @@ -30,6 +31,9 @@ export class StreamStatusesComponent implements OnInit, OnDestroy { @Input() set streamElement(streamElement: StreamElement) { + console.warn('new stream'); + this.resetStream(); + this._streamElement = streamElement; const splitedUserName = streamElement.accountId.split('@'); @@ -64,6 +68,12 @@ export class StreamStatusesComponent implements OnInit, OnDestroy { if( this.goToTopSubscription) this.goToTopSubscription.unsubscribe(); } + private resetStream() { + this.statuses.length = 0; + this.bufferStream.length = 0; + if(this.websocketStreaming) this.websocketStreaming.dispose(); + } + private launchWebsocket(): void { this.websocketStreaming = this.streamingService.getStreaming(this.account, this._streamElement); this.websocketStreaming.statusUpdateSubjet.subscribe((update: StatusUpdate) => { diff --git a/src/app/services/streaming.service.ts b/src/app/services/streaming.service.ts index f9e5028f..3a3e6d19 100644 --- a/src/app/services/streaming.service.ts +++ b/src/app/services/streaming.service.ts @@ -10,7 +10,7 @@ import { stat } from "fs"; @Injectable() export class StreamingService { - public readonly nbStatusPerIteration :number = 20; + public readonly nbStatusPerIteration: number = 20; constructor( private readonly mastodonService: MastodonService) { } @@ -26,6 +26,9 @@ export class StreamingWrapper { statusUpdateSubjet = new BehaviorSubject(null); eventSource: WebSocket; private apiRoutes = new ApiRoutes(); + private errorClosing: boolean; + private since_id: string; + private disposed: boolean; constructor( private readonly mastodonService: MastodonService, @@ -37,6 +40,11 @@ export class StreamingWrapper { this.start(route); } + dispose(): any { + this.disposed = true; + this.eventSource.close(); + } + private start(route: string) { this.eventSource = new WebSocket(route); this.eventSource.onmessage = x => this.statusParsing(JSON.parse(x.data)); @@ -45,17 +53,15 @@ export class StreamingWrapper { this.eventSource.onclose = x => this.webSocketClosed(route, x); } - private errorClosing: boolean; private webSocketGotError(x: Event) { this.errorClosing = true; } - private since_id: string; private webSocketClosed(domain, x: Event) { if (this.errorClosing) { this.pullNewStatuses(domain); this.errorClosing = false; - } else { + } else if (!this.disposed) { setTimeout(() => { this.start(domain) }, 5000); } } @@ -78,7 +84,9 @@ export class StreamingWrapper { }) .then(() => { // setTimeout(() => { this.start(domain) }, 20 * 1000); - setTimeout(() => { this.pullNewStatuses(domain) }, 15 * 1000); + if (!this.disposed) { + setTimeout(() => { this.pullNewStatuses(domain) }, 15 * 1000); + } }); } @@ -105,8 +113,8 @@ export class StreamingWrapper { const streamingRouteType = this.getStreamingRouteType(stream.type); let route = `wss://${account.instance}${this.apiRoutes.getStreaming}`.replace('{0}', account.token.access_token).replace('{1}', streamingRouteType); - if(stream.tag) route = `${route}&tag=${stream.tag}`; - if(stream.list) route = `${route}&tag=${stream.list}`; + if (stream.tag) route = `${route}&tag=${stream.tag}`; + if (stream.list) route = `${route}&tag=${stream.list}`; return route; }