Remove some logging + use wrapOrRun

This commit is contained in:
RMidhunSuresh 2022-05-18 18:56:28 +05:30
parent 683ffa9ed3
commit a550788788
2 changed files with 32 additions and 37 deletions

View file

@ -169,7 +169,7 @@ export class Platform {
} }
async init() { async init() {
await this.logger.run("Platform init", async () => { await this.logger.run("Platform init", async (log) => {
if (!this._config) { if (!this._config) {
if (!this._configURL) { if (!this._configURL) {
throw new Error("Neither config nor configURL was provided!"); throw new Error("Neither config nor configURL was provided!");
@ -183,7 +183,7 @@ export class Platform {
); );
const manifests = this.config["themeManifests"]; const manifests = this.config["themeManifests"];
await this._themeLoader?.init(manifests); await this._themeLoader?.init(manifests);
this._themeLoader?.setTheme(await this._themeLoader.getActiveTheme()); this._themeLoader?.setTheme(await this._themeLoader.getActiveTheme(), log);
}); });
} }

View file

@ -14,10 +14,9 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import type {ILogItem} from "../../logging/types.js";
import type {Platform} from "./Platform.js"; import type {Platform} from "./Platform.js";
export enum COLOR_SCHEME_PREFERENCE { DARK, LIGHT, }
export class ThemeLoader { export class ThemeLoader {
private _platform: Platform; private _platform: Platform;
private _themeMapping: Record<string, string> = {}; private _themeMapping: Record<string, string> = {};
@ -27,27 +26,25 @@ export class ThemeLoader {
} }
async init(manifestLocations: string[]): Promise<void> { async init(manifestLocations: string[]): Promise<void> {
await this._platform.logger.run("ThemeLoader.init", async () => { for (const manifestLocation of manifestLocations) {
for (const manifestLocation of manifestLocations) { const { body } = await this._platform
const { body } = await this._platform .request(manifestLocation, {
.request(manifestLocation, { method: "GET",
method: "GET", format: "json",
format: "json", cache: true,
cache: true, })
}) .response();
.response(); /*
/* After build has finished, the source section of each theme manifest
After build has finished, the source section of each theme manifest contains `built-assets` which is a mapping from the theme-name to the
contains `built-assets` which is a mapping from the theme-name to the location of the css file in build.
location of the css file in build. */
*/ Object.assign(this._themeMapping, body["source"]["built-assets"]);
Object.assign(this._themeMapping, body["source"]["built-assets"]); }
}
});
} }
setTheme(themeName: string) { setTheme(themeName: string, log?: ILogItem) {
this._platform.logger.run("ThemeLoader.setTheme", () => { this._platform.logger.wrapOrRun(log, "setTheme", () => {
const themeLocation = this._themeMapping[themeName]; const themeLocation = this._themeMapping[themeName];
if (!themeLocation) { if (!themeLocation) {
throw new Error( `Cannot find theme location for theme "${themeName}"!`); throw new Error( `Cannot find theme location for theme "${themeName}"!`);
@ -62,19 +59,17 @@ export class ThemeLoader {
} }
async getActiveTheme(): Promise<string|undefined> { async getActiveTheme(): Promise<string|undefined> {
return await this._platform.logger.run("ThemeLoader.getActiveTheme", async () => { // check if theme is set via settings
// check if theme is set via settings let theme = await this._platform.settingsStorage.getString("theme");
let theme = await this._platform.settingsStorage.getString("theme"); if (theme) {
if (theme) { return theme;
return theme; }
} // return default theme
// return default theme if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
if (window.matchMedia("(prefers-color-scheme: dark)").matches) { return this._platform.config["defaultTheme"].dark;
return this._platform.config["defaultTheme"].dark; } else if (window.matchMedia("(prefers-color-scheme: light)").matches) {
} else if (window.matchMedia("(prefers-color-scheme: light)").matches) { return this._platform.config["defaultTheme"].light;
return this._platform.config["defaultTheme"].light; }
} return undefined;
return undefined;
});
} }
} }