Re-jig config accessors

We only ever used the static instance() method to get to the config
object, so just make a static instance that returns the ConfigOptions
directly, throwing an exception if it's not yet initialised. This way
the types can all be non-optional (plus it's shorter).
This commit is contained in:
David Baker 2022-12-21 10:17:53 +00:00
parent 6303b357ab
commit c879090a34
4 changed files with 19 additions and 20 deletions

View file

@ -114,8 +114,8 @@ export class PosthogAnalytics {
constructor(private readonly posthog: PostHog) {
const posthogConfig: PosthogSettings = {
project_api_key: Config.instance.config.posthog?.api_key,
api_host: Config.instance.config.posthog?.api_host,
project_api_key: Config.get().posthog?.api_key,
api_host: Config.get().posthog?.api_host,
};
if (posthogConfig.project_api_key && posthogConfig.api_host) {

View file

@ -18,13 +18,15 @@ import { DEFAULT_CONFIG, ConfigOptions } from "./ConfigOptions";
export class Config {
private static internalInstance: Config;
public static get instance(): Config {
if (!this.internalInstance)
public static get(): ConfigOptions {
if (!this.internalInstance?.config)
throw new Error("Config instance read before config got initialized");
return this.internalInstance;
return this.internalInstance.config;
}
public static init(): Promise<void> {
if (Config?.internalInstance?.initPromise) {
if (Config.internalInstance?.initPromise) {
return Config.internalInstance.initPromise;
}
Config.internalInstance = new Config();
@ -39,25 +41,25 @@ export class Config {
// Convenience accessors
public static defaultHomeserverUrl(): string | undefined {
const defaultServerConfig = Config.instance.config.default_server_config;
const defaultServerConfig = Config.get().default_server_config;
if (!defaultServerConfig) {
return undefined;
}
return defaultServerConfig["m.homeserver"]?.base_url;
return defaultServerConfig["m.homeserver"].base_url;
}
public static defaultServerName(): string | undefined {
const defaultServerConfig = Config.instance.config.default_server_config;
const defaultServerConfig = Config.get().default_server_config;
if (!defaultServerConfig) {
return undefined;
}
return defaultServerConfig["m.homeserver"]?.server_name;
return defaultServerConfig["m.homeserver"].server_name;
}
public config: ConfigOptions;
private initPromise: Promise<void>;
public config?: ConfigOptions;
private initPromise?: Promise<void>;
}
async function downloadConfig(

View file

@ -191,13 +191,10 @@ export class Initializer {
this.loadStates.sentry === LoadState.None &&
this.loadStates.config === LoadState.Loaded
) {
if (
Config.instance.config.sentry?.DSN &&
Config.instance.config.sentry?.environment
) {
if (Config.get().sentry?.DSN && Config.get().sentry?.environment) {
Sentry.init({
dsn: Config.instance.config.sentry?.DSN,
environment: Config.instance.config.sentry?.environment,
dsn: Config.get().sentry?.DSN,
environment: Config.get().sentry?.environment,
integrations: [
new Integrations.BrowserTracing({
routingInstrumentation:

View file

@ -53,7 +53,7 @@ export function useSubmitRageshake(): {
const submitRageshake = useCallback(
async (opts) => {
if (!Config.instance.config.rageshake?.submit_url) {
if (!Config.get().rageshake?.submit_url) {
throw new Error("No rageshake URL is configured");
}
@ -261,7 +261,7 @@ export function useSubmitRageshake(): {
);
}
await fetch(Config.instance.config.rageshake?.submit_url, {
await fetch(Config.get().rageshake?.submit_url, {
method: "POST",
body,
});