|
| 1 | +import { isEntityReference } from './halHelpers.js' |
| 2 | +import LoadingStoreCollection from './LoadingStoreCollection' |
| 3 | + |
| 4 | +class CanHaveItems { |
| 5 | + constructor ({ get, reload, isUnknown }, config) { |
| 6 | + this.apiActions = { get, reload, isUnknown } |
| 7 | + this.config = config |
| 8 | + } |
| 9 | + |
| 10 | + /** |
| 11 | + * Defines a property getter for the items property. |
| 12 | + * The items property should always be a getter, in order to make the call to mapArrayOfEntityReferences |
| 13 | + * lazy, since that potentially fetches a large number of entities from the API. |
| 14 | + * @param items array of items, which can be mixed primitive values and entity references |
| 15 | + * @param fetchAllUri URI that allows fetching all collection items in a single network request, if known |
| 16 | + * @param property property name inside the entity fetched at fetchAllUri that contains the collection |
| 17 | + * @returns object the target object with the added getter |
| 18 | + */ |
| 19 | + addItemsGetter (items, fetchAllUri, property) { |
| 20 | + Object.defineProperty(this, 'items', { get: () => this.filterDeleting(this.mapArrayOfEntityReferences(items, fetchAllUri, property)) }) |
| 21 | + Object.defineProperty(this, 'allItems', { get: () => this.mapArrayOfEntityReferences(items, fetchAllUri, property) }) |
| 22 | + } |
| 23 | + |
| 24 | + filterDeleting (array) { |
| 25 | + return array.filter(entry => !entry._meta.deleting) |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Given an array, replaces any entity references in the array with the entity loaded from the Vuex store |
| 30 | + * (or from the API if necessary), and returns that as a new array. In case some of the entity references in |
| 31 | + * the array have not finished loading yet, returns a LoadingStoreCollection instead. |
| 32 | + * @param array possibly mixed array of values and references |
| 33 | + * @param fetchAllUri URI that allows fetching all array items in a single network request, if known |
| 34 | + * @param fetchAllProperty property in the entity from fetchAllUri that will contain the array |
| 35 | + * @returns array the new array with replaced items, or a LoadingStoreCollection if any of the array |
| 36 | + * elements is still loading. |
| 37 | + */ |
| 38 | + mapArrayOfEntityReferences (array, fetchAllUri, fetchAllProperty) { |
| 39 | + if (!this.containsUnknownEntityReference(array)) { |
| 40 | + return this.replaceEntityReferences(array) |
| 41 | + } |
| 42 | + |
| 43 | + if (this.config.avoidNPlusOneRequests) { |
| 44 | + const completelyLoaded = this.apiActions.reload({ _meta: { reload: { uri: fetchAllUri, property: fetchAllProperty } } }, true) |
| 45 | + .then(() => this.replaceEntityReferences(array)) |
| 46 | + return new LoadingStoreCollection(completelyLoaded) |
| 47 | + } else { |
| 48 | + const arrayWithReplacedReferences = this.replaceEntityReferences(array) |
| 49 | + const arrayCompletelyLoaded = Promise.all(array.map(entry => { |
| 50 | + if (isEntityReference(entry)) { |
| 51 | + return this.apiActions.get(entry.href)._meta.load |
| 52 | + } |
| 53 | + return Promise.resolve(entry) |
| 54 | + })) |
| 55 | + return new LoadingStoreCollection(arrayCompletelyLoaded, arrayWithReplacedReferences) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + replaceEntityReferences (array) { |
| 60 | + return array.map(entry => { |
| 61 | + if (isEntityReference(entry)) { |
| 62 | + return this.apiActions.get(entry.href) |
| 63 | + } |
| 64 | + return entry |
| 65 | + }) |
| 66 | + } |
| 67 | + |
| 68 | + containsUnknownEntityReference (array) { |
| 69 | + return array.some(entry => isEntityReference(entry) && this.apiActions.isUnknown(entry.href)) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +export default CanHaveItems |
0 commit comments