UNPKG

@itwin/presentation-components

Version:

React components based on iTwin.js Presentation library

72 lines 3.51 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ /** @packageDocumentation * @module DisplayLabels */ import { bufferCount, from, map, mergeAll, mergeMap, reduce } from "rxjs"; import { DEFAULT_KEYS_BATCH_SIZE } from "@itwin/presentation-common"; import { Presentation } from "@itwin/presentation-frontend"; import { memoize } from "../common/Utils.js"; /** * Presentation Rules-driven labels provider implementation. * @public */ export class PresentationLabelsProvider { imodel; /** Constructor. */ constructor(props) { this.imodel = props.imodel; } async getLabelInternal(key) { return (await Presentation.presentation.getDisplayLabelDefinition({ imodel: this.imodel, key })).displayValue; // WIP } // eslint-disable-next-line @typescript-eslint/unbound-method getMemoizedLabel = memoize(this.getLabelInternal, { isMatchingKey: areLabelRequestsEqual }); /** * Returns label for the specified instance key. Memoizes *the last* response. * @param key Key of instance to get label for */ async getLabel(key) { return this.getMemoizedLabel(key); } async getLabelsInternal(keys) { return new Promise((resolve, reject) => { from(keys) .pipe(bufferCount(DEFAULT_KEYS_BATCH_SIZE), mergeMap((keysBatch, batchIndex) => { if (Presentation.presentation.getDisplayLabelDefinitionsIterator) { return from(Presentation.presentation.getDisplayLabelDefinitionsIterator({ imodel: this.imodel, keys: keysBatch })).pipe(mergeMap((result) => result.items), map((item, itemIndex) => ({ value: item.displayValue, index: batchIndex * DEFAULT_KEYS_BATCH_SIZE + itemIndex }))); } // eslint-disable-next-line @typescript-eslint/no-deprecated return from(Presentation.presentation.getDisplayLabelDefinitions({ imodel: this.imodel, keys: keysBatch })).pipe(mergeAll(), map((item, valueIndex) => ({ value: item.displayValue, index: batchIndex * DEFAULT_KEYS_BATCH_SIZE + valueIndex }))); }), reduce((result, { value, index }) => { result[index] = value; return result; }, new Array(keys.length))) .subscribe({ next: resolve, error: reject, }); }); } // eslint-disable-next-line @typescript-eslint/unbound-method getMemoizedLabels = memoize(this.getLabelsInternal, { isMatchingKey: areLabelsRequestsEqual }); /** * Returns labels for the specified instance keys. Memoizes *the last* response. * @param keys Keys of instances to get labels for */ async getLabels(keys) { return this.getMemoizedLabels(keys); } } function areInstanceKeysEqual(lhs, rhs) { return lhs.className === rhs.className && lhs.id === rhs.id; } function areLabelRequestsEqual(lhsArgs, rhsArgs) { return areInstanceKeysEqual(lhsArgs[0], rhsArgs[0]); } function areLabelsRequestsEqual(lhsArgs, rhsArgs) { return lhsArgs[0].length === rhsArgs[0].length && lhsArgs[0].every((key, index) => areInstanceKeysEqual(key, rhsArgs[0][index])); } //# sourceMappingURL=LabelsProvider.js.map