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:
parent
e20e7949fb
commit
57c8c7b9a0
@ -97,12 +97,22 @@ export abstract class ItemStore<Item extends ItemObject> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected async loadItems(...args: any[]): Promise<any>;
|
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
|
@action
|
||||||
protected async loadItems(request: () => Promise<Item[] | any>, sortItems = true) {
|
protected async loadItems(request: () => Promise<Item[] | any>, sortItems = true, concurrency = false) {
|
||||||
if (this.isLoading) {
|
if (this.isLoading) {
|
||||||
await when(() => !this.isLoading);
|
await when(() => !this.isLoading);
|
||||||
|
|
||||||
return;
|
// If concurrency for loading is disabled, return instead of loading
|
||||||
|
if (!concurrency) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.isLoading = true;
|
this.isLoading = true;
|
||||||
|
|
||||||
|
|||||||
@ -19,7 +19,7 @@
|
|||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* 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 { catalogEntityRegistry, CatalogEntityRegistry } from "../../api/catalog-entity-registry";
|
||||||
import type { CatalogEntity } from "../../api/catalog-entity";
|
import type { CatalogEntity } from "../../api/catalog-entity";
|
||||||
import { ItemStore } from "../../../common/item.store";
|
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,
|
* This avoids a bug where if there were two or more concurrent loadItems() calls,
|
||||||
* only the first one would complete.
|
* only the first one would complete.
|
||||||
*/
|
*/
|
||||||
protected async loadItems(
|
protected loadItems(
|
||||||
request: (() => Promise<CatalogEntityItem<CatalogEntity>[] | any>) | (() => CatalogEntityItem<CatalogEntity>[] | any),
|
request: (() => Promise<CatalogEntityItem<CatalogEntity>[] | any>) | (() => CatalogEntityItem<CatalogEntity>[] | any),
|
||||||
sortItems = true,
|
sortItems = true,
|
||||||
|
concurrency = false,
|
||||||
) {
|
) {
|
||||||
if (this.isLoading) {
|
return super.loadItems(request, sortItems, concurrency);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user