From fafd2dca8f9c8d0e0d98428747174d8a0953ad2b Mon Sep 17 00:00:00 2001
From: Bill Erickson <berickxx@gmail.com>
Date: Fri, 20 Sep 2019 11:31:16 -0400
Subject: [PATCH] LP1844812 Combobox avoids processing matching entry lists

Teach the combobox to only process newly provided entry lists if they
are different from the list already on record for the combobox.

The combobox entry list Input() is responsive to changes in data
throughout the life of the combobox.  As a new entry list is provided,
the component will perform various actions on the list.  However, in
many cases the list provided may be identical the list the component
already has, which can lead to a lot of unnecessary processing and in
some case an infinite loop of inputs and outputs.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
---
 .../src/app/share/combobox/combobox.component.ts   | 30 ++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/Open-ILS/src/eg2/src/app/share/combobox/combobox.component.ts b/Open-ILS/src/eg2/src/app/share/combobox/combobox.component.ts
index c98da6cd93..cd33e65187 100644
--- a/Open-ILS/src/eg2/src/app/share/combobox/combobox.component.ts
+++ b/Open-ILS/src/eg2/src/app/share/combobox/combobox.component.ts
@@ -109,6 +109,12 @@ export class ComboboxComponent implements ControlValueAccessor, OnInit {
 
     @Input() set entries(el: ComboboxEntry[]) {
         if (el) {
+
+            if (this.entrylistMatches(el)) {
+                // Avoid reprocessing data we already have.
+                return;
+            }
+
             this.entrylist = el;
 
             // new set of entries essentially means a new instance. reset.
@@ -207,6 +213,30 @@ export class ComboboxComponent implements ControlValueAccessor, OnInit {
         setTimeout(() => this.click$.next(''));
     }
 
+    // Returns true if the 2 entries are equivalent.
+    entriesMatches(e1: ComboboxEntry, e2: ComboboxEntry): boolean {
+        return (
+            e1 && e2 &&
+            e1.id === e2.id &&
+            e1.label === e2.label &&
+            e1.freetext === e2.freetext
+        );
+    }
+
+    // Returns true if the 2 lists are equivalent.
+    entrylistMatches(el: ComboboxEntry[]): boolean {
+        if (el.length !== this.entrylist.length) {
+            return false;
+        }
+        for (let i = 0; i < el.length; i++) {
+            const mine = this.entrylist[i];
+            if (!mine || !this.entriesMatches(mine, el[i])) {
+                return false;
+            }
+        }
+        return true;
+    }
+
     // Apply a default selection where needed
     applySelection() {
 
-- 
2.11.0