Add logging

This commit is contained in:
RMidhunSuresh 2022-05-18 16:09:09 +05:30
parent 7426d17e33
commit 7952a34d64
2 changed files with 54 additions and 46 deletions

View File

@ -169,20 +169,22 @@ export class Platform {
} }
async init() { async init() {
if (!this._config) { await this.logger.run("Platform init", async () => {
if (!this._configURL) { if (!this._config) {
throw new Error("Neither config nor configURL was provided!"); if (!this._configURL) {
throw new Error("Neither config nor configURL was provided!");
}
const {body}= await this.request(this._configURL, {method: "GET", format: "json", cache: true}).response();
this._config = body;
} }
const {body}= await this.request(this._configURL, {method: "GET", format: "json", cache: true}).response(); this.notificationService = new NotificationService(
this._config = body; this._serviceWorkerHandler,
} this._config.push
this.notificationService = new NotificationService( );
this._serviceWorkerHandler, const manifests = this.config["themeManifests"];
this._config.push await this._themeLoader?.init(manifests);
); this._themeLoader?.setTheme(await this._themeLoader.getActiveTheme());
const manifests = this.config["themeManifests"]; });
await this._themeLoader?.init(manifests);
this._themeLoader?.setTheme(await this._themeLoader.getActiveTheme());
} }
_createLogger(isDevelopment) { _createLogger(isDevelopment) {

View File

@ -27,30 +27,34 @@ export class ThemeLoader {
} }
async init(manifestLocations: string[]): Promise<void> { async init(manifestLocations: string[]): Promise<void> {
for (const manifestLocation of manifestLocations) { await this._platform.logger.run("ThemeLoader.init", async () => {
const { body } = await this._platform for (const manifestLocation of manifestLocations) {
.request(manifestLocation, { const { body } = await this._platform
method: "GET", .request(manifestLocation, {
format: "json", method: "GET",
cache: true, format: "json",
}) cache: true,
.response(); })
/* .response();
After build has finished, the source section of each theme manifest /*
contains `built-assets` which is a mapping from the theme-name to the After build has finished, the source section of each theme manifest
location of the css file in build. contains `built-assets` which is a mapping from the theme-name to the
*/ 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) {
const themeLocation = this._themeMapping[themeName]; this._platform.logger.run("ThemeLoader.setTheme", () => {
if (!themeLocation) { const themeLocation = this._themeMapping[themeName];
throw new Error( `Cannot find theme location for theme "${themeName}"!`); if (!themeLocation) {
} throw new Error( `Cannot find theme location for theme "${themeName}"!`);
this._platform.replaceStylesheet(themeLocation); }
this._platform.settingsStorage.setString("theme", themeName); this._platform.replaceStylesheet(themeLocation);
this._platform.settingsStorage.setString("theme", themeName);
});
} }
get themes(): string[] { get themes(): string[] {
@ -58,17 +62,19 @@ export class ThemeLoader {
} }
async getActiveTheme(): Promise<string|undefined> { async getActiveTheme(): Promise<string|undefined> {
// check if theme is set via settings return await this._platform.logger.run("ThemeLoader.getActiveTheme", async () => {
let theme = await this._platform.settingsStorage.getString("theme"); // check if theme is set via settings
if (theme) { let theme = await this._platform.settingsStorage.getString("theme");
return theme; if (theme) {
} return theme;
// return default theme }
if (window.matchMedia("(prefers-color-scheme: dark)").matches) { // return default theme
return this._platform.config["defaultTheme"].dark; if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
} else if (window.matchMedia("(prefers-color-scheme: light)").matches) { return this._platform.config["defaultTheme"].dark;
return this._platform.config["defaultTheme"].light; } else if (window.matchMedia("(prefers-color-scheme: light)").matches) {
} return this._platform.config["defaultTheme"].light;
return undefined; }
return undefined;
});
} }
} }