UNPKG

2.19 kBPlain TextView Raw
1// *****************************************************************************
2// Copyright (C) 2018 TypeFox and others.
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License v. 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0.
7//
8// This Source Code may also be made available under the following Secondary
9// Licenses when the conditions for such availability set forth in the Eclipse
10// Public License v. 2.0 are satisfied: GNU General Public License, version 2
11// with the GNU Classpath Exception which is available at
12// https://www.gnu.org/software/classpath/license.html.
13//
14// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15// *****************************************************************************
16
17import { FrontendApplicationConfig, deepmerge } from '@theia/application-package/lib/application-props';
18
19export const DEFAULT_BACKGROUND_COLOR_STORAGE_KEY = 'theme.background';
20
21export class FrontendApplicationConfigProvider {
22
23 private static KEY = Symbol('FrontendApplicationConfigProvider');
24
25 static get(): FrontendApplicationConfig {
26 const config = FrontendApplicationConfigProvider.doGet();
27 if (config === undefined) {
28 throw new Error('The configuration is not set. Did you call FrontendApplicationConfigProvider#set?');
29 }
30 return config;
31 }
32
33 static set(config: FrontendApplicationConfig.Partial): void {
34 if (FrontendApplicationConfigProvider.doGet() !== undefined) {
35 throw new Error('The configuration is already set.');
36 }
37 // eslint-disable-next-line @typescript-eslint/no-explicit-any
38 const globalObject = window as any;
39 const key = FrontendApplicationConfigProvider.KEY;
40 globalObject[key] = deepmerge(FrontendApplicationConfig.DEFAULT, config);
41 }
42
43 private static doGet(): FrontendApplicationConfig | undefined {
44 // eslint-disable-next-line @typescript-eslint/no-explicit-any
45 const globalObject = window as any;
46 const key = FrontendApplicationConfigProvider.KEY;
47 return globalObject[key];
48 }
49
50}