|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright (c) 2021 - 2023 Vaadin Ltd. |
| 4 | + * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/ |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * A class that stores items with their associated sub-caches. |
| 9 | + */ |
| 10 | +export class Cache { |
| 11 | + /** |
| 12 | + * @type {{ isExpanded: (item: unknown) => boolean }} |
| 13 | + */ |
| 14 | + context; |
| 15 | + |
| 16 | + /** |
| 17 | + * The number of items. |
| 18 | + * |
| 19 | + * @type {number} |
| 20 | + */ |
| 21 | + size = 0; |
| 22 | + |
| 23 | + /** |
| 24 | + * The number of items to display per page. |
| 25 | + * |
| 26 | + * @type {number} |
| 27 | + */ |
| 28 | + pageSize; |
| 29 | + |
| 30 | + /** |
| 31 | + * An array of cached items. |
| 32 | + * |
| 33 | + * @type {object[]} |
| 34 | + */ |
| 35 | + items = []; |
| 36 | + |
| 37 | + /** |
| 38 | + * A map where the key is a requested page and the value is a callback |
| 39 | + * that will be called with data once the request is complete. |
| 40 | + * |
| 41 | + * @type {Map<number, Function>} |
| 42 | + */ |
| 43 | + pendingRequests = new Map(); |
| 44 | + |
| 45 | + /** |
| 46 | + * A map where the key is the index of an item in the `items` array |
| 47 | + * and the value is a sub-cache associated with that item. |
| 48 | + * |
| 49 | + * Note, it's intentionally defined as an object instead of a Map |
| 50 | + * to ensure that Object.entries() returns an array with keys sorted |
| 51 | + * in alphabetical order, rather than the order they were added. |
| 52 | + * |
| 53 | + * @type {Record<number, Cache>} |
| 54 | + * @private |
| 55 | + */ |
| 56 | + __subCacheByIndex = {}; |
| 57 | + |
| 58 | + /** |
| 59 | + * The total number of items, including items from expanded sub-caches. |
| 60 | + * |
| 61 | + * @type {number} |
| 62 | + * @private |
| 63 | + */ |
| 64 | + __effectiveSize = 0; |
| 65 | + |
| 66 | + /** |
| 67 | + * @param {Cache['context']} context |
| 68 | + * @param {number} pageSize |
| 69 | + * @param {number | undefined} size |
| 70 | + * @param {Cache | undefined} parentCache |
| 71 | + * @param {number | undefined} parentCacheIndex |
| 72 | + */ |
| 73 | + constructor(context, pageSize, size, parentCache, parentCacheIndex) { |
| 74 | + this.context = context; |
| 75 | + this.pageSize = pageSize; |
| 76 | + this.size = size || 0; |
| 77 | + this.parentCache = parentCache; |
| 78 | + this.parentCacheIndex = parentCacheIndex; |
| 79 | + this.__effectiveSize = size || 0; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * An item in the parent cache that the current cache is associated with. |
| 84 | + * |
| 85 | + * @return {object | undefined} |
| 86 | + */ |
| 87 | + get parentItem() { |
| 88 | + return this.parentCache && this.parentCache.items[this.parentCacheIndex]; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * An array of sub-caches sorted in the same order as their associated items |
| 93 | + * appear in the `items` array. |
| 94 | + * |
| 95 | + * @return {Cache[]} |
| 96 | + */ |
| 97 | + get subCaches() { |
| 98 | + return Object.values(this.__subCacheByIndex); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Whether the cache or any of its descendant caches have pending requests. |
| 103 | + * |
| 104 | + * @return {boolean} |
| 105 | + */ |
| 106 | + get isLoading() { |
| 107 | + if (this.pendingRequests.size > 0) { |
| 108 | + return true; |
| 109 | + } |
| 110 | + |
| 111 | + return this.subCaches.some((subCache) => subCache.isLoading); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * The total number of items, including items from expanded sub-caches. |
| 116 | + * |
| 117 | + * @return {number} |
| 118 | + */ |
| 119 | + get effectiveSize() { |
| 120 | + return this.__effectiveSize; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Recalculates the effective size for the cache and its descendant caches recursively. |
| 125 | + */ |
| 126 | + recalculateEffectiveSize() { |
| 127 | + this.__effectiveSize = |
| 128 | + !this.parentItem || this.context.isExpanded(this.parentItem) |
| 129 | + ? this.size + |
| 130 | + this.subCaches.reduce((total, subCache) => { |
| 131 | + subCache.recalculateEffectiveSize(); |
| 132 | + return total + subCache.effectiveSize; |
| 133 | + }, 0) |
| 134 | + : 0; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Adds an array of items corresponding to the given page |
| 139 | + * to the `items` array. |
| 140 | + * |
| 141 | + * @param {number} page |
| 142 | + * @param {object[]} items |
| 143 | + */ |
| 144 | + setPage(page, items) { |
| 145 | + const startIndex = page * this.pageSize; |
| 146 | + items.forEach((item, i) => { |
| 147 | + this.items[startIndex + i] = item; |
| 148 | + }); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Retrieves the sub-cache associated with the item at the given index |
| 153 | + * in the `items` array. |
| 154 | + * |
| 155 | + * @param {number} index |
| 156 | + * @return {Cache | undefined} |
| 157 | + */ |
| 158 | + getSubCache(index) { |
| 159 | + return this.__subCacheByIndex[index]; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Removes the sub-cache associated with the item at the given index |
| 164 | + * in the `items` array. |
| 165 | + * |
| 166 | + * @param {number} index |
| 167 | + */ |
| 168 | + removeSubCache(index) { |
| 169 | + delete this.__subCacheByIndex[index]; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Removes all sub-caches. |
| 174 | + */ |
| 175 | + removeSubCaches() { |
| 176 | + this.__subCacheByIndex = {}; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Creates and associates a sub-cache for the item at the given index |
| 181 | + * in the `items` array. |
| 182 | + * |
| 183 | + * @param {number} index |
| 184 | + * @return {Cache} |
| 185 | + */ |
| 186 | + createSubCache(index) { |
| 187 | + const subCache = new Cache(this.context, this.pageSize, 0, this, index); |
| 188 | + this.__subCacheByIndex[index] = subCache; |
| 189 | + return subCache; |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Retrieves the flattened index corresponding to the given index |
| 194 | + * of an item in the `items` array. |
| 195 | + * |
| 196 | + * @param {number} index |
| 197 | + * @return {number} |
| 198 | + */ |
| 199 | + getFlatIndex(index) { |
| 200 | + const clampedIndex = Math.max(0, Math.min(this.size - 1, index)); |
| 201 | + |
| 202 | + return this.subCaches.reduce((prev, subCache) => { |
| 203 | + const index = subCache.parentCacheIndex; |
| 204 | + return clampedIndex > index ? prev + subCache.effectiveSize : prev; |
| 205 | + }, clampedIndex); |
| 206 | + } |
| 207 | +} |
0 commit comments