]> Untitled Git - lemmy-ui.git/blob - src/shared/services/HomeCacheService.ts
Attempt to fix inability to logout from some instances (subdomains) (#1809)
[lemmy-ui.git] / src / shared / services / HomeCacheService.ts
1 import { GetPostsResponse } from "lemmy-js-client";
2 import { RequestState } from "./HttpService.js";
3
4 /**
5  * Service to cache home post listings and restore home state when user uses the browser back buttons.
6  */
7 export class HomeCacheService {
8   static #_instance: HomeCacheService;
9   historyIdx = 0;
10   scrollY = 0;
11   posts: RequestState<GetPostsResponse> = { state: "empty" };
12
13   get active() {
14     return (
15       this.historyIdx === window.history.state.idx + 1 &&
16       this.posts.state === "success"
17     );
18   }
19
20   deactivate() {
21     this.historyIdx = 0;
22   }
23
24   activate() {
25     this.scrollY = window.scrollY;
26     this.historyIdx = window.history.state.idx;
27   }
28
29   static get #Instance() {
30     return this.#_instance ?? (this.#_instance = new this());
31   }
32
33   public static get scrollY() {
34     return this.#Instance.scrollY;
35   }
36
37   public static get historyIdx() {
38     return this.#Instance.historyIdx;
39   }
40
41   public static set postsRes(posts: RequestState<GetPostsResponse>) {
42     this.#Instance.posts = posts;
43   }
44
45   public static get postsRes() {
46     return this.#Instance.posts;
47   }
48
49   public static get active() {
50     return this.#Instance.active;
51   }
52
53   public static deactivate() {
54     this.#Instance.deactivate();
55   }
56
57   public static activate() {
58     this.#Instance.activate();
59   }
60 }