@vaadin/component-base 24.4.0-alpha1 → 24.4.0-dev.b3e1d14600

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vaadin/component-base",
3
- "version": "24.4.0-alpha1",
3
+ "version": "24.4.0-dev.b3e1d14600",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -42,5 +42,5 @@
42
42
  "@vaadin/testing-helpers": "^0.6.0",
43
43
  "sinon": "^13.0.2"
44
44
  },
45
- "gitHead": "3e2ed41c99d618ff7def2734fd863c21c85775a3"
45
+ "gitHead": "502d4f5b03f770a83d270d98078cde230254dd0e"
46
46
  }
@@ -16,13 +16,6 @@ export class Cache {
16
16
  */
17
17
  context;
18
18
 
19
- /**
20
- * The number of items.
21
- *
22
- * @type {number}
23
- */
24
- size = 0;
25
-
26
19
  /**
27
20
  * The number of items to display per page.
28
21
  *
@@ -58,6 +51,14 @@ export class Cache {
58
51
  */
59
52
  __subCacheByIndex = {};
60
53
 
54
+ /**
55
+ * The number of items.
56
+ *
57
+ * @type {number}
58
+ * @private
59
+ */
60
+ __size = 0;
61
+
61
62
  /**
62
63
  * The total number of items, including items from expanded sub-caches.
63
64
  *
@@ -136,6 +137,46 @@ export class Cache {
136
137
  return this.flatSize;
137
138
  }
138
139
 
140
+ /**
141
+ * The number of items.
142
+ *
143
+ * @type {number}
144
+ * @private
145
+ */
146
+ get size() {
147
+ return this.__size;
148
+ }
149
+
150
+ /**
151
+ * Sets the number of items.
152
+ *
153
+ * @type {number}
154
+ * @private
155
+ */
156
+ set size(size) {
157
+ const oldSize = this.__size;
158
+ if (oldSize === size) {
159
+ return;
160
+ }
161
+
162
+ this.__size = size;
163
+
164
+ if (this.context.placeholder !== undefined) {
165
+ this.items.length = size;
166
+ for (let i = 0; i < size; i++) {
167
+ // eslint-disable-next-line logical-assignment-operators
168
+ this.items[i] = this.items[i] || this.context.placeholder;
169
+ }
170
+ }
171
+
172
+ Object.keys(this.pendingRequests).forEach((page) => {
173
+ const startIndex = parseInt(page) * this.pageSize;
174
+ if (startIndex >= this.size) {
175
+ delete this.pendingRequests[page];
176
+ }
177
+ });
178
+ }
179
+
139
180
  /**
140
181
  * Recalculates the flattened size for the cache and its descendant caches recursively.
141
182
  */
@@ -160,7 +201,10 @@ export class Cache {
160
201
  setPage(page, items) {
161
202
  const startIndex = page * this.pageSize;
162
203
  items.forEach((item, i) => {
163
- this.items[startIndex + i] = item;
204
+ const itemIndex = startIndex + i;
205
+ if (itemIndex < this.size) {
206
+ this.items[itemIndex] = item;
207
+ }
164
208
  });
165
209
  }
166
210
 
@@ -65,12 +65,27 @@ export class DataProviderController extends EventTarget {
65
65
  */
66
66
  rootCache;
67
67
 
68
- constructor(host, { size, pageSize, isExpanded, getItemId, dataProvider, dataProviderParams }) {
68
+ /**
69
+ * Indicates whether any data has been loaded since the last cache clear.
70
+ *
71
+ * @type {boolean}
72
+ */
73
+ hasData = false;
74
+
75
+ /**
76
+ * A placeholder item that is used to indicate that the item is not loaded yet.
77
+ *
78
+ * @type {undefined | object}
79
+ */
80
+ placeholder;
81
+
82
+ constructor(host, { size, pageSize, isExpanded, getItemId, placeholder, dataProvider, dataProviderParams }) {
69
83
  super();
70
84
  this.host = host;
71
85
  this.pageSize = pageSize;
72
86
  this.getItemId = getItemId;
73
87
  this.isExpanded = isExpanded;
88
+ this.placeholder = placeholder;
74
89
  this.dataProvider = dataProvider;
75
90
  this.dataProviderParams = dataProviderParams;
76
91
  this.rootCache = this.__createRootCache(size);
@@ -87,6 +102,7 @@ export class DataProviderController extends EventTarget {
87
102
  get __cacheContext() {
88
103
  return {
89
104
  isExpanded: this.isExpanded,
105
+ placeholder: this.placeholder,
90
106
  // The controller instance is needed to ensure deprecated cache methods work.
91
107
  __controller: this,
92
108
  };
@@ -133,6 +149,7 @@ export class DataProviderController extends EventTarget {
133
149
  */
134
150
  clearCache() {
135
151
  this.rootCache = this.__createRootCache(this.rootCache.size);
152
+ this.hasData = false;
136
153
  }
137
154
 
138
155
  /**
@@ -187,7 +204,7 @@ export class DataProviderController extends EventTarget {
187
204
  ensureFlatIndexLoaded(flatIndex) {
188
205
  const { cache, page, item } = this.getFlatIndexContext(flatIndex);
189
206
 
190
- if (!item) {
207
+ if (this.__isPlaceholder(item)) {
191
208
  this.__loadCachePage(cache, page);
192
209
  }
193
210
  }
@@ -202,7 +219,7 @@ export class DataProviderController extends EventTarget {
202
219
  ensureFlatIndexHierarchy(flatIndex) {
203
220
  const { cache, item, index } = this.getFlatIndexContext(flatIndex);
204
221
 
205
- if (item && this.isExpanded(item) && !cache.getSubCache(index)) {
222
+ if (!this.__isPlaceholder(item) && this.isExpanded(item) && !cache.getSubCache(index)) {
206
223
  const subCache = cache.createSubCache(index);
207
224
  this.__loadCachePage(subCache, 0);
208
225
  }
@@ -251,6 +268,8 @@ export class DataProviderController extends EventTarget {
251
268
 
252
269
  this.recalculateFlatSize();
253
270
 
271
+ this.hasData = true;
272
+
254
273
  this.dispatchEvent(new CustomEvent('page-received'));
255
274
 
256
275
  delete cache.pendingRequests[page];
@@ -264,4 +283,9 @@ export class DataProviderController extends EventTarget {
264
283
 
265
284
  this.dataProvider(params, callback);
266
285
  }
286
+
287
+ /** @private */
288
+ __isPlaceholder(item) {
289
+ return item === this.placeholder;
290
+ }
267
291
  }