|
| 1 | +/* |
| 2 | +Copyright 2025 Element Creations Ltd. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | +Please see LICENSE files in the repository root for full details. |
| 6 | +*/ |
| 7 | +import { |
| 8 | + type StoresApi as IStoresApi, |
| 9 | + type RoomListStoreApi as IRoomListStore, |
| 10 | + type Room, |
| 11 | + Watchable, |
| 12 | +} from "@element-hq/element-web-module-api"; |
| 13 | + |
| 14 | +import type { RoomListStoreV3Class, RoomListStoreV3Event } from "../stores/room-list-v3/RoomListStoreV3"; |
| 15 | +import { Room as ModuleRoom } from "./models/Room"; |
| 16 | + |
| 17 | +interface RlsEvents { |
| 18 | + LISTS_LOADED_EVENT: RoomListStoreV3Event.ListsLoaded; |
| 19 | + LISTS_UPDATE_EVENT: RoomListStoreV3Event.ListsUpdate; |
| 20 | +} |
| 21 | + |
| 22 | +export class RoomListStoreApi implements IRoomListStore { |
| 23 | + private rls?: RoomListStoreV3Class; |
| 24 | + private LISTS_LOADED_EVENT?: RoomListStoreV3Event.ListsLoaded; |
| 25 | + private LISTS_UPDATE_EVENT?: RoomListStoreV3Event.ListsUpdate; |
| 26 | + public readonly moduleLoadPromise: Promise<void>; |
| 27 | + |
| 28 | + public constructor() { |
| 29 | + this.moduleLoadPromise = this.init(); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Load the RLS through a dynamic import. This is necessary to prevent |
| 34 | + * circular dependency issues. |
| 35 | + */ |
| 36 | + private async init(): Promise<void> { |
| 37 | + const module = await import("../stores/room-list-v3/RoomListStoreV3"); |
| 38 | + this.rls = module.default.instance; |
| 39 | + this.LISTS_LOADED_EVENT = module.LISTS_LOADED_EVENT; |
| 40 | + this.LISTS_UPDATE_EVENT = module.LISTS_UPDATE_EVENT; |
| 41 | + } |
| 42 | + |
| 43 | + public getRooms(): RoomsWatchable { |
| 44 | + return new RoomsWatchable(this.roomListStore, this.events); |
| 45 | + } |
| 46 | + |
| 47 | + private get events(): RlsEvents { |
| 48 | + if (!this.LISTS_LOADED_EVENT || !this.LISTS_UPDATE_EVENT) { |
| 49 | + throw new Error("Event type was not loaded correctly, did you forget to await waitForReady()?"); |
| 50 | + } |
| 51 | + return { LISTS_LOADED_EVENT: this.LISTS_LOADED_EVENT, LISTS_UPDATE_EVENT: this.LISTS_UPDATE_EVENT }; |
| 52 | + } |
| 53 | + |
| 54 | + private get roomListStore(): RoomListStoreV3Class { |
| 55 | + if (!this.rls) { |
| 56 | + throw new Error("rls is undefined, did you forget to await waitForReady()?"); |
| 57 | + } |
| 58 | + return this.rls; |
| 59 | + } |
| 60 | + |
| 61 | + public async waitForReady(): Promise<void> { |
| 62 | + // Wait for the module to load first |
| 63 | + await this.moduleLoadPromise; |
| 64 | + |
| 65 | + // Check if RLS is already loaded |
| 66 | + if (!this.roomListStore.isLoadingRooms) return; |
| 67 | + |
| 68 | + // Await a promise that resolves when RLS has loaded |
| 69 | + const { promise, resolve } = Promise.withResolvers<void>(); |
| 70 | + const { LISTS_LOADED_EVENT } = this.events; |
| 71 | + this.roomListStore.once(LISTS_LOADED_EVENT, resolve); |
| 72 | + await promise; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +class RoomsWatchable extends Watchable<Room[]> { |
| 77 | + public constructor( |
| 78 | + private readonly rls: RoomListStoreV3Class, |
| 79 | + private readonly events: RlsEvents, |
| 80 | + ) { |
| 81 | + super(rls.getSortedRooms().map((sdkRoom) => new ModuleRoom(sdkRoom))); |
| 82 | + } |
| 83 | + |
| 84 | + private onRlsUpdate = (): void => { |
| 85 | + this.value = this.rls.getSortedRooms().map((sdkRoom) => new ModuleRoom(sdkRoom)); |
| 86 | + }; |
| 87 | + |
| 88 | + protected onFirstWatch(): void { |
| 89 | + this.rls.on(this.events.LISTS_UPDATE_EVENT, this.onRlsUpdate); |
| 90 | + } |
| 91 | + |
| 92 | + protected onLastWatch(): void { |
| 93 | + this.rls.off(this.events.LISTS_UPDATE_EVENT, this.onRlsUpdate); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +export class StoresApi implements IStoresApi { |
| 98 | + private roomListStoreApi?: IRoomListStore; |
| 99 | + |
| 100 | + public get roomListStore(): IRoomListStore { |
| 101 | + if (!this.roomListStoreApi) { |
| 102 | + this.roomListStoreApi = new RoomListStoreApi(); |
| 103 | + } |
| 104 | + return this.roomListStoreApi; |
| 105 | + } |
| 106 | +} |
0 commit comments