Quellcode durchsuchen

Clean up theme manager.

Afshin Darian vor 7 Jahren
Ursprung
Commit
85e1d1f2fc
1 geänderte Dateien mit 23 neuen und 21 gelöschten Zeilen
  1. 23 21
      packages/apputils/src/thememanager.ts

+ 23 - 21
packages/apputils/src/thememanager.ts

@@ -48,19 +48,20 @@ class ThemeManager {
    * Construct a new theme manager.
    */
   constructor(options: ThemeManager.IOptions) {
-    const key = options.key;
+    const { key, when } = options;
     const registry = options.settingRegistry;
+    const promises = Promise.all([registry.load(key), when]);
+
+    when.then(() => { this._sealed = true; });
 
     this._baseUrl = options.baseUrl;
     this._host = options.host;
-    options.when.then(() => { this._sealed = true; });
-    this.ready = Promise.all([registry.load(key), options.when])
-      .then(([settings]) => {
-        this._settings = settings;
-        this._settings.changed.connect(this._onSettingsChanged, this);
+    this.ready = promises.then(([settings]) => {
+      this._settings = settings;
+      this._settings.changed.connect(this._onSettingsChanged, this);
 
-        return this._handleSettings();
-      });
+      return this._handleSettings();
+    });
   }
 
   /**
@@ -86,9 +87,7 @@ class ThemeManager {
    * Set the current theme.
    */
   setTheme(name: string): Promise<void> {
-    return this.ready.then(() => {
-      this._settings.set('theme', name);
-    });
+    return this.ready.then(() => this._settings.set('theme', name));
   }
 
   /**
@@ -119,18 +118,18 @@ class ThemeManager {
    * @param path - The path of the file to load.
    */
   loadCSS(path: string): Promise<void> {
-    let link = document.createElement('link');
+    const link = document.createElement('link');
+    const baseUrl = PageConfig.getOption('themePath');
+    const delegate = new PromiseDelegate<void>();
+
     link.rel = 'stylesheet';
     link.type = 'text/css';
-    let baseUrl = PageConfig.getOption('themePath');
     link.href = URLExt.join(baseUrl, path);
-    let promise = new PromiseDelegate<void>();
-    link.onload = () => {
-      promise.resolve(void 0);
-    };
+    link.onload = () => { delegate.resolve(void 0); };
     document.body.appendChild(link);
     this._links.push(link);
-    return promise.promise;
+
+    return delegate.promise;
   }
 
   /**
@@ -154,17 +153,20 @@ class ThemeManager {
    * Handle the current settings.
    */
   private _handleSettings(): Promise<void> {
-    let settings = this._settings;
+    const settings = this._settings;
     let theme = settings.composite['theme'] as string;
+
     if (!this._themes[theme]) {
-      let old = theme;
+      const old = theme;
+
       theme = settings.default('theme') as string;
       if (!this._themes[theme]) {
         return Promise.reject('No default theme to load');
       }
-      console.warn(`Could not find theme "${old}", loading default theme "${theme}"`);
+      console.warn(`Could not find theme "${old}", loading default "${theme}"`);
     }
     this._pendingTheme = theme;
+
     return this._loadTheme();
   }