@itwin/presentation-components
Version:
React components based on iTwin.js Presentation library
76 lines • 3.98 kB
JavaScript
;
/*---------------------------------------------------------------------------------------------
* 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
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.PresentationLabelsProvider = void 0;
const rxjs_1 = require("rxjs");
const presentation_common_1 = require("@itwin/presentation-common");
const presentation_frontend_1 = require("@itwin/presentation-frontend");
const Utils_js_1 = require("../common/Utils.js");
/**
* Presentation Rules-driven labels provider implementation.
* @public
*/
class PresentationLabelsProvider {
imodel;
/** Constructor. */
constructor(props) {
this.imodel = props.imodel;
}
async getLabelInternal(key) {
return (await presentation_frontend_1.Presentation.presentation.getDisplayLabelDefinition({ imodel: this.imodel, key })).displayValue; // WIP
}
// eslint-disable-next-line @typescript-eslint/unbound-method
getMemoizedLabel = (0, Utils_js_1.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) => {
(0, rxjs_1.from)(keys)
.pipe((0, rxjs_1.bufferCount)(presentation_common_1.DEFAULT_KEYS_BATCH_SIZE), (0, rxjs_1.mergeMap)((keysBatch, batchIndex) => {
if (presentation_frontend_1.Presentation.presentation.getDisplayLabelDefinitionsIterator) {
return (0, rxjs_1.from)(presentation_frontend_1.Presentation.presentation.getDisplayLabelDefinitionsIterator({ imodel: this.imodel, keys: keysBatch })).pipe((0, rxjs_1.mergeMap)((result) => result.items), (0, rxjs_1.map)((item, itemIndex) => ({ value: item.displayValue, index: batchIndex * presentation_common_1.DEFAULT_KEYS_BATCH_SIZE + itemIndex })));
}
// eslint-disable-next-line @typescript-eslint/no-deprecated
return (0, rxjs_1.from)(presentation_frontend_1.Presentation.presentation.getDisplayLabelDefinitions({ imodel: this.imodel, keys: keysBatch })).pipe((0, rxjs_1.mergeAll)(), (0, rxjs_1.map)((item, valueIndex) => ({ value: item.displayValue, index: batchIndex * presentation_common_1.DEFAULT_KEYS_BATCH_SIZE + valueIndex })));
}), (0, rxjs_1.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 = (0, Utils_js_1.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);
}
}
exports.PresentationLabelsProvider = PresentationLabelsProvider;
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