2022-10-19 12:07:03 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { createContext } from "react";
|
|
|
|
|
|
|
|
import defaultDispatcher from "../dispatcher/dispatcher";
|
|
|
|
import LegacyCallHandler from "../LegacyCallHandler";
|
|
|
|
import { PosthogAnalytics } from "../PosthogAnalytics";
|
|
|
|
import { SlidingSyncManager } from "../SlidingSyncManager";
|
2023-02-23 07:46:49 +00:00
|
|
|
import { AccountPasswordStore } from "../stores/AccountPasswordStore";
|
2022-11-18 19:05:00 +00:00
|
|
|
import { MemberListStore } from "../stores/MemberListStore";
|
2022-10-19 12:07:03 +00:00
|
|
|
import { RoomNotificationStateStore } from "../stores/notifications/RoomNotificationStateStore";
|
|
|
|
import RightPanelStore from "../stores/right-panel/RightPanelStore";
|
|
|
|
import { RoomViewStore } from "../stores/RoomViewStore";
|
|
|
|
import SpaceStore, { SpaceStoreClass } from "../stores/spaces/SpaceStore";
|
2022-10-19 13:14:14 +00:00
|
|
|
import TypingStore from "../stores/TypingStore";
|
2023-03-27 08:07:43 +00:00
|
|
|
import { UserProfilesStore } from "../stores/UserProfilesStore";
|
2022-10-19 12:07:03 +00:00
|
|
|
import { WidgetLayoutStore } from "../stores/widgets/WidgetLayoutStore";
|
2022-10-19 20:00:53 +00:00
|
|
|
import { WidgetPermissionStore } from "../stores/widgets/WidgetPermissionStore";
|
2022-10-19 12:07:03 +00:00
|
|
|
import WidgetStore from "../stores/WidgetStore";
|
2022-11-24 08:08:41 +00:00
|
|
|
import {
|
|
|
|
VoiceBroadcastPlaybacksStore,
|
|
|
|
VoiceBroadcastPreRecordingStore,
|
|
|
|
VoiceBroadcastRecordingsStore,
|
|
|
|
} from "../voice-broadcast";
|
2022-10-19 12:07:03 +00:00
|
|
|
|
2023-03-31 09:35:02 +00:00
|
|
|
export const SDKContext = createContext<SdkContextClass | undefined>(undefined);
|
2022-10-19 12:07:03 +00:00
|
|
|
SDKContext.displayName = "SDKContext";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A class which lazily initialises stores as and when they are requested, ensuring they remain
|
|
|
|
* as singletons scoped to this object.
|
|
|
|
*/
|
|
|
|
export class SdkContextClass {
|
|
|
|
/**
|
|
|
|
* The global SdkContextClass instance. This is a temporary measure whilst so many stores remain global
|
|
|
|
* as well. Over time, these stores should accept a `SdkContextClass` instance in their constructor.
|
|
|
|
* When all stores do this, this static variable can be deleted.
|
|
|
|
*/
|
|
|
|
public static readonly instance = new SdkContextClass();
|
|
|
|
|
|
|
|
// Optional as we don't have a client on initial load if unregistered. This should be set
|
|
|
|
// when the MatrixClient is first acquired in the dispatcher event Action.OnLoggedIn.
|
|
|
|
// It is only safe to set this once, as updating this value will NOT notify components using
|
|
|
|
// this Context.
|
|
|
|
public client?: MatrixClient;
|
|
|
|
|
|
|
|
// All protected fields to make it easier to derive test stores
|
2022-10-19 20:00:53 +00:00
|
|
|
protected _WidgetPermissionStore?: WidgetPermissionStore;
|
2022-11-18 19:05:00 +00:00
|
|
|
protected _MemberListStore?: MemberListStore;
|
2022-10-19 12:07:03 +00:00
|
|
|
protected _RightPanelStore?: RightPanelStore;
|
|
|
|
protected _RoomNotificationStateStore?: RoomNotificationStateStore;
|
|
|
|
protected _RoomViewStore?: RoomViewStore;
|
|
|
|
protected _WidgetLayoutStore?: WidgetLayoutStore;
|
|
|
|
protected _WidgetStore?: WidgetStore;
|
|
|
|
protected _PosthogAnalytics?: PosthogAnalytics;
|
|
|
|
protected _SlidingSyncManager?: SlidingSyncManager;
|
|
|
|
protected _SpaceStore?: SpaceStoreClass;
|
|
|
|
protected _LegacyCallHandler?: LegacyCallHandler;
|
2022-10-19 13:14:14 +00:00
|
|
|
protected _TypingStore?: TypingStore;
|
2022-11-10 08:38:48 +00:00
|
|
|
protected _VoiceBroadcastRecordingsStore?: VoiceBroadcastRecordingsStore;
|
|
|
|
protected _VoiceBroadcastPreRecordingStore?: VoiceBroadcastPreRecordingStore;
|
2022-11-24 08:08:41 +00:00
|
|
|
protected _VoiceBroadcastPlaybacksStore?: VoiceBroadcastPlaybacksStore;
|
2023-02-23 07:46:49 +00:00
|
|
|
protected _AccountPasswordStore?: AccountPasswordStore;
|
2023-03-27 08:07:43 +00:00
|
|
|
protected _UserProfilesStore?: UserProfilesStore;
|
2022-10-19 12:07:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Automatically construct stores which need to be created eagerly so they can register with
|
|
|
|
* the dispatcher.
|
|
|
|
*/
|
2023-01-12 13:25:14 +00:00
|
|
|
public constructEagerStores(): void {
|
2022-10-19 12:07:03 +00:00
|
|
|
this._RoomViewStore = this.roomViewStore;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get legacyCallHandler(): LegacyCallHandler {
|
|
|
|
if (!this._LegacyCallHandler) {
|
|
|
|
this._LegacyCallHandler = LegacyCallHandler.instance;
|
|
|
|
}
|
|
|
|
return this._LegacyCallHandler;
|
|
|
|
}
|
|
|
|
public get rightPanelStore(): RightPanelStore {
|
|
|
|
if (!this._RightPanelStore) {
|
|
|
|
this._RightPanelStore = RightPanelStore.instance;
|
|
|
|
}
|
|
|
|
return this._RightPanelStore;
|
|
|
|
}
|
|
|
|
public get roomNotificationStateStore(): RoomNotificationStateStore {
|
|
|
|
if (!this._RoomNotificationStateStore) {
|
|
|
|
this._RoomNotificationStateStore = RoomNotificationStateStore.instance;
|
|
|
|
}
|
|
|
|
return this._RoomNotificationStateStore;
|
|
|
|
}
|
|
|
|
public get roomViewStore(): RoomViewStore {
|
|
|
|
if (!this._RoomViewStore) {
|
|
|
|
this._RoomViewStore = new RoomViewStore(defaultDispatcher, this);
|
|
|
|
}
|
|
|
|
return this._RoomViewStore;
|
|
|
|
}
|
|
|
|
public get widgetLayoutStore(): WidgetLayoutStore {
|
|
|
|
if (!this._WidgetLayoutStore) {
|
|
|
|
this._WidgetLayoutStore = WidgetLayoutStore.instance;
|
|
|
|
}
|
|
|
|
return this._WidgetLayoutStore;
|
|
|
|
}
|
2022-10-19 20:00:53 +00:00
|
|
|
public get widgetPermissionStore(): WidgetPermissionStore {
|
|
|
|
if (!this._WidgetPermissionStore) {
|
|
|
|
this._WidgetPermissionStore = new WidgetPermissionStore(this);
|
|
|
|
}
|
|
|
|
return this._WidgetPermissionStore;
|
|
|
|
}
|
2022-10-19 12:07:03 +00:00
|
|
|
public get widgetStore(): WidgetStore {
|
|
|
|
if (!this._WidgetStore) {
|
|
|
|
this._WidgetStore = WidgetStore.instance;
|
|
|
|
}
|
|
|
|
return this._WidgetStore;
|
|
|
|
}
|
|
|
|
public get posthogAnalytics(): PosthogAnalytics {
|
|
|
|
if (!this._PosthogAnalytics) {
|
|
|
|
this._PosthogAnalytics = PosthogAnalytics.instance;
|
|
|
|
}
|
|
|
|
return this._PosthogAnalytics;
|
|
|
|
}
|
2022-11-18 19:05:00 +00:00
|
|
|
public get memberListStore(): MemberListStore {
|
|
|
|
if (!this._MemberListStore) {
|
|
|
|
this._MemberListStore = new MemberListStore(this);
|
|
|
|
}
|
|
|
|
return this._MemberListStore;
|
|
|
|
}
|
2022-10-19 12:07:03 +00:00
|
|
|
public get slidingSyncManager(): SlidingSyncManager {
|
|
|
|
if (!this._SlidingSyncManager) {
|
|
|
|
this._SlidingSyncManager = SlidingSyncManager.instance;
|
|
|
|
}
|
|
|
|
return this._SlidingSyncManager;
|
|
|
|
}
|
|
|
|
public get spaceStore(): SpaceStoreClass {
|
|
|
|
if (!this._SpaceStore) {
|
|
|
|
this._SpaceStore = SpaceStore.instance;
|
|
|
|
}
|
|
|
|
return this._SpaceStore;
|
|
|
|
}
|
2022-10-19 13:14:14 +00:00
|
|
|
public get typingStore(): TypingStore {
|
|
|
|
if (!this._TypingStore) {
|
|
|
|
this._TypingStore = new TypingStore(this);
|
|
|
|
window.mxTypingStore = this._TypingStore;
|
|
|
|
}
|
|
|
|
return this._TypingStore;
|
|
|
|
}
|
2022-11-10 08:38:48 +00:00
|
|
|
|
|
|
|
public get voiceBroadcastRecordingsStore(): VoiceBroadcastRecordingsStore {
|
|
|
|
if (!this._VoiceBroadcastRecordingsStore) {
|
2022-12-27 07:39:26 +00:00
|
|
|
this._VoiceBroadcastRecordingsStore = new VoiceBroadcastRecordingsStore();
|
2022-11-10 08:38:48 +00:00
|
|
|
}
|
|
|
|
return this._VoiceBroadcastRecordingsStore;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get voiceBroadcastPreRecordingStore(): VoiceBroadcastPreRecordingStore {
|
|
|
|
if (!this._VoiceBroadcastPreRecordingStore) {
|
|
|
|
this._VoiceBroadcastPreRecordingStore = new VoiceBroadcastPreRecordingStore();
|
|
|
|
}
|
|
|
|
return this._VoiceBroadcastPreRecordingStore;
|
|
|
|
}
|
2022-11-24 08:08:41 +00:00
|
|
|
|
|
|
|
public get voiceBroadcastPlaybacksStore(): VoiceBroadcastPlaybacksStore {
|
|
|
|
if (!this._VoiceBroadcastPlaybacksStore) {
|
2023-01-02 12:21:33 +00:00
|
|
|
this._VoiceBroadcastPlaybacksStore = new VoiceBroadcastPlaybacksStore(this.voiceBroadcastRecordingsStore);
|
2022-11-24 08:08:41 +00:00
|
|
|
}
|
|
|
|
return this._VoiceBroadcastPlaybacksStore;
|
|
|
|
}
|
2023-02-23 07:46:49 +00:00
|
|
|
|
|
|
|
public get accountPasswordStore(): AccountPasswordStore {
|
|
|
|
if (!this._AccountPasswordStore) {
|
|
|
|
this._AccountPasswordStore = new AccountPasswordStore();
|
|
|
|
}
|
|
|
|
return this._AccountPasswordStore;
|
|
|
|
}
|
2023-03-27 08:07:43 +00:00
|
|
|
|
|
|
|
public get userProfilesStore(): UserProfilesStore {
|
|
|
|
if (!this.client) {
|
|
|
|
throw new Error("Unable to create UserProfilesStore without a client");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._UserProfilesStore) {
|
|
|
|
this._UserProfilesStore = new UserProfilesStore(this.client);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._UserProfilesStore;
|
|
|
|
}
|
|
|
|
|
|
|
|
public onLoggedOut(): void {
|
|
|
|
this._UserProfilesStore = undefined;
|
|
|
|
}
|
2022-10-19 12:07:03 +00:00
|
|
|
}
|