From b23b886b6e4c4ac1bcee6ae03a26a60933e28d4f Mon Sep 17 00:00:00 2001
From: Sergei Zharinov <zharinov@users.noreply.github.com>
Date: Sun, 4 Sep 2022 11:20:29 +0300
Subject: [PATCH] refactor(github): Remove unused PR cache functionality
 (#17606)

---
 lib/modules/platform/github/api-cache.spec.ts | 79 +++++--------------
 lib/modules/platform/github/api-cache.ts      | 20 +----
 2 files changed, 19 insertions(+), 80 deletions(-)

diff --git a/lib/modules/platform/github/api-cache.spec.ts b/lib/modules/platform/github/api-cache.spec.ts
index 72cefeca8c..2c831ed17f 100644
--- a/lib/modules/platform/github/api-cache.spec.ts
+++ b/lib/modules/platform/github/api-cache.spec.ts
@@ -1,6 +1,5 @@
 import { DateTime } from 'luxon';
 import { ApiCache } from './api-cache';
-import type { ApiPageItem } from './types';
 
 describe('modules/platform/github/api-cache', () => {
   const now = DateTime.fromISO('2000-01-01T00:00:00.000+00:00');
@@ -49,47 +48,9 @@ describe('modules/platform/github/api-cache', () => {
         },
       });
 
-      const res = apiCache.getItems(({ number }) => number);
+      const res = apiCache.getItems();
 
-      expect(res).toEqual([1, 2, 3]);
-    });
-
-    it('caches mapping results', () => {
-      const item1 = { number: 1, updated_at: t1 };
-      const item2 = { number: 2, updated_at: t2 };
-      const item3 = { number: 3, updated_at: t3 };
-      const apiCache = new ApiCache({
-        items: {
-          1: item1,
-          2: item2,
-          3: item3,
-        },
-      });
-
-      let numbersMapCalls = 0;
-      const mapNumbers = ({ number }: ApiPageItem) => {
-        numbersMapCalls += 1;
-        return number;
-      };
-
-      let datesMapCalls = 0;
-      const mapDates = ({ updated_at }: ApiPageItem) => {
-        datesMapCalls += 1;
-        return updated_at;
-      };
-
-      const numbers1 = apiCache.getItems(mapNumbers);
-      const numbers2 = apiCache.getItems(mapNumbers);
-      const dates1 = apiCache.getItems(mapDates);
-      const dates2 = apiCache.getItems(mapDates);
-
-      expect(numbers1).toEqual([1, 2, 3]);
-      expect(numbers1).toBe(numbers2);
-      expect(numbersMapCalls).toBe(3);
-
-      expect(dates1).toEqual([t1, t2, t3]);
-      expect(dates1).toBe(dates2);
-      expect(datesMapCalls).toBe(3);
+      expect(res).toMatchObject([{ number: 1 }, { number: 2 }, { number: 3 }]);
     });
 
     it('resets cache on item update', () => {
@@ -103,18 +64,16 @@ describe('modules/platform/github/api-cache', () => {
         },
       });
 
-      let numbersMapCalls = 0;
-      const mapNumbers = ({ number }: ApiPageItem) => {
-        numbersMapCalls += 1;
-        return number;
-      };
-      const numbers1 = apiCache.getItems(mapNumbers);
+      const numbers1 = apiCache.getItems();
       apiCache.updateItem(item3);
-      const numbers2 = apiCache.getItems(mapNumbers);
+      const numbers2 = apiCache.getItems();
 
-      expect(numbers1).toEqual([1, 2]);
-      expect(numbers2).toEqual([1, 2, 3]);
-      expect(numbersMapCalls).toBe(5);
+      expect(numbers1).toMatchObject([{ number: 1 }, { number: 2 }]);
+      expect(numbers2).toMatchObject([
+        { number: 1 },
+        { number: 2 },
+        { number: 3 },
+      ]);
     });
 
     it('resets cache on page reconcile', () => {
@@ -128,18 +87,16 @@ describe('modules/platform/github/api-cache', () => {
         },
       });
 
-      let numbersMapCalls = 0;
-      const mapNumbers = ({ number }: ApiPageItem) => {
-        numbersMapCalls += 1;
-        return number;
-      };
-      const numbers1 = apiCache.getItems(mapNumbers);
+      const numbers1 = apiCache.getItems();
       apiCache.reconcile([item3]);
-      const numbers2 = apiCache.getItems(mapNumbers);
+      const numbers2 = apiCache.getItems();
 
-      expect(numbers1).toEqual([1, 2]);
-      expect(numbers2).toEqual([1, 2, 3]);
-      expect(numbersMapCalls).toBe(5);
+      expect(numbers1).toMatchObject([{ number: 1 }, { number: 2 }]);
+      expect(numbers2).toMatchObject([
+        { number: 1 },
+        { number: 2 },
+        { number: 3 },
+      ]);
     });
   });
 
diff --git a/lib/modules/platform/github/api-cache.ts b/lib/modules/platform/github/api-cache.ts
index 279b5c7b32..016ddd7a75 100644
--- a/lib/modules/platform/github/api-cache.ts
+++ b/lib/modules/platform/github/api-cache.ts
@@ -3,8 +3,6 @@ import { DateTime } from 'luxon';
 import type { ApiPageCache, ApiPageItem } from './types';
 
 export class ApiCache<T extends ApiPageItem> {
-  private itemsMapCache = new WeakMap();
-
   constructor(private cache: ApiPageCache<T>) {}
 
   get etag(): string | null {
@@ -27,21 +25,7 @@ export class ApiCache<T extends ApiPageItem> {
     return lastModified ? DateTime.fromISO(lastModified).toHTTP() : null;
   }
 
-  getItems(): T[];
-  getItems<U = unknown>(mapFn: (_: T) => U): U[];
-  getItems<U = unknown>(mapFn?: (_: T) => U): T[] | U[] {
-    if (mapFn) {
-      const cachedResult = this.itemsMapCache.get(mapFn);
-      if (cachedResult) {
-        return cachedResult;
-      }
-
-      const items = Object.values(this.cache.items);
-      const mappedResult = items.map(mapFn);
-      this.itemsMapCache.set(mapFn, mappedResult);
-      return mappedResult;
-    }
-
+  getItems(): T[] {
     const items = Object.values(this.cache.items);
     return items;
   }
@@ -58,7 +42,6 @@ export class ApiCache<T extends ApiPageItem> {
    */
   updateItem(item: T): void {
     this.cache.items[item.number] = item;
-    this.itemsMapCache = new WeakMap();
   }
 
   /**
@@ -88,7 +71,6 @@ export class ApiCache<T extends ApiPageItem> {
 
       if (!dequal(oldItem, newItem)) {
         items[number] = newItem;
-        this.itemsMapCache = new WeakMap();
       }
 
       needNextPage = itemOldTime ? itemOldTime < itemNewTime : true;
-- 
GitLab