mirror of
https://github.com/mattermost/mattermost-mobile.git
synced 2026-11-18 01:18:13 +00:00
69 lines
2.4 KiB
TypeScript
69 lines
2.4 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import {BehaviorSubject, of} from 'rxjs';
|
|
import {distinctUntilChanged} from 'rxjs/operators';
|
|
|
|
import EphemeralStore from '@store/ephemeral_store';
|
|
|
|
// Tracks whether threads have been fetched for a given (serverUrl, teamId) pair
|
|
// during the current process lifetime. Each pair gets its own BehaviorSubject<boolean>
|
|
// so components only react to their own team's sync state — no cross-team noise.
|
|
//
|
|
// Intentionally ephemeral: resets on app kill so the blob badge is always shown
|
|
// on a fresh cold start until threads are synced from the server.
|
|
class ThreadsSyncStoreSingleton {
|
|
private readonly subjects = new Map<string, BehaviorSubject<boolean>>();
|
|
|
|
private key(serverUrl: string, teamId: string): string {
|
|
return `${serverUrl}|${teamId}`;
|
|
}
|
|
|
|
private getOrCreate(serverUrl: string, teamId: string): BehaviorSubject<boolean> {
|
|
const key = this.key(serverUrl, teamId);
|
|
let subject = this.subjects.get(key);
|
|
if (!subject) {
|
|
subject = new BehaviorSubject<boolean>(false);
|
|
this.subjects.set(key, subject);
|
|
}
|
|
return subject;
|
|
}
|
|
|
|
markThreadsFetched(serverUrl: string, teamId: string): void {
|
|
this.getOrCreate(serverUrl, teamId).next(true);
|
|
}
|
|
|
|
hasThreadsBeenFetched(serverUrl: string, teamId: string): boolean {
|
|
return this.subjects.get(this.key(serverUrl, teamId))?.getValue() ?? false;
|
|
}
|
|
|
|
observeThreadsFetched(serverUrl: string, teamId: string) {
|
|
if (!EphemeralStore.getExperienceAPIEnabled(serverUrl)) {
|
|
return of(true);
|
|
}
|
|
return this.getOrCreate(serverUrl, teamId).pipe(distinctUntilChanged());
|
|
}
|
|
|
|
// Reset all teams for a server — e.g. on logout or server removal.
|
|
clearServer(serverUrl: string): void {
|
|
const prefix = `${serverUrl}|`;
|
|
for (const [key, subject] of this.subjects) {
|
|
if (key.startsWith(prefix)) {
|
|
subject.next(false);
|
|
this.subjects.delete(key);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Full reset — e.g. on long background resume to force a fresh blob-first render.
|
|
clearAll(): void {
|
|
for (const subject of this.subjects.values()) {
|
|
subject.next(false);
|
|
}
|
|
this.subjects.clear();
|
|
}
|
|
}
|
|
|
|
const ThreadsSyncStore = new ThreadsSyncStoreSingleton();
|
|
export default ThreadsSyncStore;
|