1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Add parameter to loadItems.

Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com>
This commit is contained in:
Panu Horsmalahti 2021-11-17 09:52:52 +02:00
parent e20e7949fb
commit 57c8c7b9a0
2 changed files with 18 additions and 23 deletions

View File

@ -97,12 +97,22 @@ export abstract class ItemStore<Item extends ItemObject> {
}
protected async loadItems(...args: any[]): Promise<any>;
/**
* Load items to this.items
* @param request Function to return the items to be loaded.
* @param sortItems If true, items will be sorted.
* @param concurrency If true, concurrent loadItems() calls will all be executed. If false, only the first.
* @returns
*/
@action
protected async loadItems(request: () => Promise<Item[] | any>, sortItems = true) {
protected async loadItems(request: () => Promise<Item[] | any>, sortItems = true, concurrency = false) {
if (this.isLoading) {
await when(() => !this.isLoading);
return;
// If concurrency for loading is disabled, return instead of loading
if (!concurrency) {
return;
}
}
this.isLoading = true;

View File

@ -19,7 +19,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { when, computed, makeObservable, observable, reaction } from "mobx";
import { computed, makeObservable, observable, reaction } from "mobx";
import { catalogEntityRegistry, CatalogEntityRegistry } from "../../api/catalog-entity-registry";
import type { CatalogEntity } from "../../api/catalog-entity";
import { ItemStore } from "../../../common/item.store";
@ -65,7 +65,8 @@ export class CatalogEntityStore extends ItemStore<CatalogEntityItem<CatalogEntit
}
}
return this.loadItems(() => this.entities);
// concurrency is true to fix bug if catalog filter is removed and added at the same time
return this.loadItems(() => this.entities, undefined, true);
}
/**
@ -75,27 +76,11 @@ export class CatalogEntityStore extends ItemStore<CatalogEntityItem<CatalogEntit
* This avoids a bug where if there were two or more concurrent loadItems() calls,
* only the first one would complete.
*/
protected async loadItems(
protected loadItems(
request: (() => Promise<CatalogEntityItem<CatalogEntity>[] | any>) | (() => CatalogEntityItem<CatalogEntity>[] | any),
sortItems = true,
concurrency = false,
) {
if (this.isLoading) {
await when(() => !this.isLoading);
}
this.isLoading = true;
try {
let items = await request();
if (sortItems) {
items = this.sortItems(items);
}
this.items.replace(items);
this.isLoaded = true;
} finally {
this.isLoading = false;
}
return super.loadItems(request, sortItems, concurrency);
}
}