@itwin/presentation-components
Version:
React components based on iTwin.js Presentation library
81 lines • 3.29 kB
JavaScript
"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ItemsLoader = exports.FILTER_WARNING_OPTION = exports.VALUE_BATCH_SIZE = void 0;
require("../../common/DisposePolyfill.js");
/** @internal */
exports.VALUE_BATCH_SIZE = 100;
/** @internal */
exports.FILTER_WARNING_OPTION = { label: "Too many values please use filter", value: "__filter__", disabled: true };
/** @internal */
class ItemsLoader {
_beforeLoad;
_onItemsLoaded;
_loadItems;
_getOptionLabel;
_loadedItems = [];
_isLoading = false;
_hasMore = true;
_disposed = false;
_offset = 0;
constructor(_beforeLoad, _onItemsLoaded, _loadItems, _getOptionLabel) {
this._beforeLoad = _beforeLoad;
this._onItemsLoaded = _onItemsLoaded;
this._loadItems = _loadItems;
this._getOptionLabel = _getOptionLabel;
}
[Symbol.dispose]() {
this._disposed = true;
}
async loadMatchingItems(valuesToMatch) {
const needsItemsLoaded = (items) => {
if (valuesToMatch && valuesToMatch.length > 0) {
const matchingItems = items.filter((item) => {
return valuesToMatch.some((valueToMatch) => valueToMatch === this._getOptionLabel(item));
});
return matchingItems.length < valuesToMatch.length;
}
return items.length < exports.VALUE_BATCH_SIZE;
};
await this.loadUniqueItems(needsItemsLoaded);
}
async loadItems(filterText) {
const needsItemsLoaded = (options) => {
if (!filterText) {
return options.length < exports.VALUE_BATCH_SIZE;
}
const matchingItems = options.filter((option) => this._getOptionLabel(option).toLowerCase().includes(filterText.toLowerCase()));
return matchingItems.length < exports.VALUE_BATCH_SIZE;
};
await this.loadUniqueItems(needsItemsLoaded);
}
async loadUniqueItems(needsItemsLoaded) {
const loadedItems = [];
let currOffset = this._offset;
let hasMore = this._hasMore;
if (!this._hasMore || this._isLoading || !needsItemsLoaded(this._loadedItems)) {
return;
}
this._isLoading = true;
this._beforeLoad();
do {
const { options, hasMore: batchHasMore, length } = await this._loadItems(currOffset);
if (this._disposed) {
return;
}
loadedItems.push(...options);
hasMore = batchHasMore;
currOffset += length;
} while (hasMore && needsItemsLoaded([...this._loadedItems, ...loadedItems]));
this._loadedItems.push(...loadedItems);
this._offset = currOffset;
this._hasMore = hasMore;
this._onItemsLoaded(loadedItems);
this._isLoading = false;
}
}
exports.ItemsLoader = ItemsLoader;
//# sourceMappingURL=ItemsLoader.js.map