Async config file (#682)
* initial * only donwload config once * formatting * update sample config * sentry * refactor load state * fix build yaml * Upper case enums * change how defaults work. review fixes * abstract initialization * copyright * gitignore styleing * refactor initialization * use dafualt as fallback * internalInstance rename * review * remove acidentally added posthog file * DSN rename * update Copyright * remove olm from the initializer Co-authored-by: Timo K <timok@element.io>
This commit is contained in:
		
					parent
					
						
							
								806a9032e1
							
						
					
				
			
			
				commit
				
					
						78a313c373
					
				
			
		
					 10 changed files with 293 additions and 88 deletions
				
			
		
							
								
								
									
										64
									
								
								src/config/Config.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								src/config/Config.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,64 @@
 | 
			
		|||
/*
 | 
			
		||||
Copyright 2021-2022 New Vector Ltd
 | 
			
		||||
 | 
			
		||||
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 { DEFAULT_CONFIG, IConfigOptions } from "./ConfigOptions";
 | 
			
		||||
 | 
			
		||||
export class Config {
 | 
			
		||||
  private static internalInstance: Config;
 | 
			
		||||
  public static get instance(): Config {
 | 
			
		||||
    if (!this.internalInstance)
 | 
			
		||||
      throw new Error("Config instance read before config got initialized");
 | 
			
		||||
    return this.internalInstance;
 | 
			
		||||
  }
 | 
			
		||||
  public static init(): Promise<void> {
 | 
			
		||||
    if (Config?.internalInstance?.initPromise) {
 | 
			
		||||
      return Config.internalInstance.initPromise;
 | 
			
		||||
    }
 | 
			
		||||
    Config.internalInstance = new Config();
 | 
			
		||||
    Config.internalInstance.initPromise = new Promise<void>((resolve) => {
 | 
			
		||||
      downloadConfig("../config.json").then((config) => {
 | 
			
		||||
        Config.internalInstance.config = { ...DEFAULT_CONFIG, ...config };
 | 
			
		||||
        resolve();
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
    return Config.internalInstance.initPromise;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public config: IConfigOptions;
 | 
			
		||||
  private initPromise: Promise<void>;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function downloadConfig(
 | 
			
		||||
  configJsonFilename: string
 | 
			
		||||
): Promise<IConfigOptions> {
 | 
			
		||||
  const url = new URL(configJsonFilename, window.location.href);
 | 
			
		||||
  url.searchParams.set("cachebuster", Date.now().toString());
 | 
			
		||||
  const res = await fetch(url, {
 | 
			
		||||
    cache: "no-cache",
 | 
			
		||||
    method: "GET",
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  if (res.status === 404 || res.status === 0) {
 | 
			
		||||
    // Lack of a config isn't an error, we should just use the defaults.
 | 
			
		||||
    // Also treat a blank config as no config, assuming the status code is 0, because we don't get 404s from file:
 | 
			
		||||
    // URIs so this is the only way we can not fail if the file doesn't exist when loading from a file:// URI.
 | 
			
		||||
    return {} as IConfigOptions;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (res.ok) {
 | 
			
		||||
    return res.json();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue