Browse Source

Use type union for ListResult

Eric Charles 5 years ago
parent
commit
2ce2c58758
2 changed files with 47 additions and 61 deletions
  1. 22 40
      packages/extensionmanager/src/listings.ts
  2. 25 21
      packages/extensionmanager/src/model.ts

+ 22 - 40
packages/extensionmanager/src/listings.ts

@@ -23,34 +23,20 @@ export interface IListEntry {
 }
 
 /**
- * Listing search result structure (subset).
+ * Listing search result type.
+ *
+ * - The mode for the listings, can be black or white.
+ * - A collection of URIs for black or white listings, depending
+ * on the mode.
+ * - A collection of black or white listed extensions,  depending
+ * on the mode.
  *
  */
-export interface IListResult {
-  /**
-   * The mode for the listings, can be black or white.
-   */
-  listMode: 'white' | 'black' | null;
-  /**
-   * A collection of URIs for black listings.
-   */
-  blacklistUris: string[];
-
-  /**
-   * A collection of URIs for white listings.
-   */
-  whitelistUris: string[];
-
-  /**
-   * A collection of back listed extensions.
-   */
-  blacklist: IListEntry[];
-
-  /**
-   * A collection of white listed extensions.
-   */
-  whitelist: IListEntry[];
-}
+export type ListResult = null | {
+  mode: 'white' | 'black';
+  uris: string[];
+  entries: IListEntry[];
+};
 
 export interface IListingApi {
   blacklist_uris: string[];
@@ -75,11 +61,13 @@ export class Lister {
           !(data.blacklist_uris.length > 0 && data.whitelist_uris.length > 0)
         ) {
           this._listings = {
-            listMode: data.blacklist_uris.length > 0 ? 'black' : 'white',
-            blacklistUris: data.blacklist_uris,
-            whitelistUris: data.whitelist_uris,
-            blacklist: data.blacklist,
-            whitelist: data.whitelist
+            mode: data.blacklist_uris.length > 0 ? 'black' : 'white',
+            uris:
+              data.blacklist_uris.length > 0
+                ? data.blacklist_uris
+                : data.whitelist_uris,
+            entries:
+              data.blacklist_uris.length > 0 ? data.blacklist : data.whitelist
           };
         }
         this._listingsLoaded.emit(this._listings);
@@ -89,21 +77,15 @@ export class Lister {
       });
   }
 
-  get listingsLoaded(): ISignal<this, IListResult> {
+  get listingsLoaded(): ISignal<this, ListResult> {
     return this._listingsLoaded;
   }
 
-  private _listings: IListResult = {
-    listMode: null,
-    blacklistUris: [],
-    whitelistUris: [],
-    blacklist: [],
-    whitelist: []
-  };
+  private _listings: ListResult = null;
 
   /**
    */
-  private _listingsLoaded = new Signal<this, IListResult>(this);
+  private _listingsLoaded = new Signal<this, ListResult>(this);
 }
 
 /**

+ 25 - 21
packages/extensionmanager/src/model.ts

@@ -26,7 +26,7 @@ import { reportInstallError } from './dialog';
 
 import { Searcher, ISearchResult, isJupyterOrg } from './npm';
 
-import { Lister, IListResult, IListEntry } from './listings';
+import { Lister, ListResult, IListEntry } from './listings';
 
 /**
  * Information about an extension.
@@ -170,30 +170,34 @@ export class ListModel extends VDomModel {
     });
   }
 
-  private _listingIsLoaded(_: Lister, listings: IListResult) {
-    this._listMode = listings.listMode;
+  private _listingIsLoaded(_: Lister, listings: ListResult) {
+    this._listMode = listings!.mode;
     this._blacklistMap = new Map<string, IListEntry>();
-    listings.blacklist.map(e => {
-      this._blacklistMap.set(e.name, {
-        name: e.name,
-        regexp: new RegExp(e.name),
-        type: e.type,
-        reason: e.reason,
-        creation_date: e.creation_date,
-        last_update_date: e.last_update_date
+    if (this._listMode === 'black') {
+      listings!.entries.map(e => {
+        this._blacklistMap.set(e.name, {
+          name: e.name,
+          regexp: new RegExp(e.name),
+          type: e.type,
+          reason: e.reason,
+          creation_date: e.creation_date,
+          last_update_date: e.last_update_date
+        });
       });
-    });
+    }
     this._whitelistMap = new Map<string, IListEntry>();
-    listings.whitelist.map(e => {
-      this._whitelistMap.set(e.name, {
-        name: e.name,
-        regexp: new RegExp(e.name),
-        type: e.type,
-        reason: e.reason,
-        creation_date: e.creation_date,
-        last_update_date: e.last_update_date
+    if (this._listMode === 'white') {
+      listings!.entries.map(e => {
+        this._whitelistMap.set(e.name, {
+          name: e.name,
+          regexp: new RegExp(e.name),
+          type: e.type,
+          reason: e.reason,
+          creation_date: e.creation_date,
+          last_update_date: e.last_update_date
+        });
       });
-    });
+    }
     void this.initialize();
   }