1 import { PersonViewSafe, WebSocketJsonResponse } from "lemmy-js-client";
2 import { Observable } from "rxjs";
3 import { share } from "rxjs/operators";
10 import { wsUri } from "../env";
11 import { isBrowser } from "../utils";
13 export class WebSocketService {
14 private static _instance: WebSocketService;
16 public subject: Observable<any>;
18 public admins: PersonViewSafe[];
19 public banned: PersonViewSafe[];
21 private constructor() {
22 let firstConnect = true;
24 this.subject = new Observable((obs: any) => {
25 this.ws = new WebsocketBuilder(wsUri)
26 .onMessage((_i, e) => {
28 obs.next(JSON.parse(e.data.toString()));
34 console.log(`Connected to ${wsUri}`);
37 let res: WebSocketJsonResponse<any> = {
45 console.log("Retrying websocket connection...");
48 console.error("Websocket closed.");
50 .withBackoff(new ExponentialBackoff(100, 7))
51 .withBuffer(new LRUBuffer(1000))
56 window.onunload = () => {
59 // Clears out scroll positions.
60 sessionStorage.clear();
65 public send(data: string) {
69 public static get Instance() {
70 return this._instance || (this._instance = new this());