Convert SdkConfig to TypeScript as a proof of concept

This commit is contained in:
Travis Ralston 2019-12-12 14:37:32 -07:00
parent 97af0403e5
commit a5dadda63b
2 changed files with 23 additions and 11 deletions

View file

@ -20,10 +20,10 @@ import SettingsStore from "./settings/SettingsStore";
import { Service, startTermsFlow, TermsNotSignedError } from './Terms'; import { Service, startTermsFlow, TermsNotSignedError } from './Terms';
const request = require('browser-request'); const request = require('browser-request');
const SdkConfig = require('./SdkConfig');
const MatrixClientPeg = require('./MatrixClientPeg'); const MatrixClientPeg = require('./MatrixClientPeg');
import * as Matrix from 'matrix-js-sdk'; import * as Matrix from 'matrix-js-sdk';
import SdkConfig from "./SdkConfig";
// The version of the integration manager API we're intending to work with // The version of the integration manager API we're intending to work with
const imApiVersion = "1.1"; const imApiVersion = "1.1";

View file

@ -1,5 +1,6 @@
/* /*
Copyright 2016 OpenMarket Ltd Copyright 2016 OpenMarket Ltd
Copyright 2019 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -14,7 +15,11 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
export const DEFAULTS = { export interface ConfigOptions {
[key: string]: any;
}
export const DEFAULTS: ConfigOptions = {
// URL to a page we show in an iframe to configure integrations // URL to a page we show in an iframe to configure integrations
integrations_ui_url: "https://scalar.vector.im/", integrations_ui_url: "https://scalar.vector.im/",
// Base URL to the REST interface of the integrations server // Base URL to the REST interface of the integrations server
@ -23,30 +28,37 @@ export const DEFAULTS = {
bug_report_endpoint_url: null, bug_report_endpoint_url: null,
}; };
class SdkConfig { export default class SdkConfig {
static get() { private static instance: ConfigOptions;
return global.mxReactSdkConfig || {};
private static setInstance(i: ConfigOptions) {
SdkConfig.instance = i;
// For debugging purposes
(<any>window).mxReactSdkConfig = i;
} }
static put(cfg) { static get() {
return SdkConfig.instance || {};
}
static put(cfg: ConfigOptions) {
const defaultKeys = Object.keys(DEFAULTS); const defaultKeys = Object.keys(DEFAULTS);
for (let i = 0; i < defaultKeys.length; ++i) { for (let i = 0; i < defaultKeys.length; ++i) {
if (cfg[defaultKeys[i]] === undefined) { if (cfg[defaultKeys[i]] === undefined) {
cfg[defaultKeys[i]] = DEFAULTS[defaultKeys[i]]; cfg[defaultKeys[i]] = DEFAULTS[defaultKeys[i]];
} }
} }
global.mxReactSdkConfig = cfg; SdkConfig.setInstance(cfg);
} }
static unset() { static unset() {
global.mxReactSdkConfig = undefined; SdkConfig.setInstance({});
} }
static add(cfg) { static add(cfg: ConfigOptions) {
const liveConfig = SdkConfig.get(); const liveConfig = SdkConfig.get();
const newConfig = Object.assign({}, liveConfig, cfg); const newConfig = Object.assign({}, liveConfig, cfg);
SdkConfig.put(newConfig); SdkConfig.put(newConfig);
} }
} }
module.exports = SdkConfig;