@economic/taco 2.47.0-server.5 → 2.47.0-server.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -12689,16 +12689,14 @@ function Row(props) {
12689
12689
  var search = props.table.getState().globalFilter;
12690
12690
  var hiddenColumns = getHiddenColumns(props.table.getState().columnVisibility);
12691
12691
  var pageIndexesToFetch = [];
12692
- /*
12693
- // if there's no direction, it means the scroll bar got dropped un unloaded pages,
12694
- // in that case, load forward and backward pages to prevent skeletons
12695
- if (scrollDirection === 'backward' || !scrollDirection) {
12696
- const backIndex = pageIndex - 1;
12697
- if (backIndex > -1) {
12698
- pageIndexesToFetch.push(backIndex);
12699
- }
12700
- }
12701
- */
12692
+ // if there's no direction, it means the scroll bar got dropped un unloaded pages,
12693
+ // in that case, load forward and backward pages to prevent skeletons
12694
+ if (scrollDirection === 'backward' || !scrollDirection) {
12695
+ var backIndex = pageIndex - 1;
12696
+ if (backIndex > -1) {
12697
+ pageIndexesToFetch.push(backIndex);
12698
+ }
12699
+ }
12702
12700
  // always load the next page
12703
12701
  if ((scrollDirection === 'forward' || !scrollDirection) && pageIndex + 2 < tableMeta.server.pageCount) {
12704
12702
  pageIndexesToFetch.push(pageIndex + 1);
@@ -23133,76 +23131,68 @@ function useTableDataLoader2(fetchPage, fetchAll, options) {
23133
23131
  reset = false;
23134
23132
  }
23135
23133
  try {
23134
+ // if a request is already pending for this page (and it's not a reset), skip it
23136
23135
  if (_pendingPageRequests.current[pageIndex] && !reset) {
23137
23136
  return Promise.resolve();
23138
23137
  }
23139
23138
  var hasChangedData = JSON.stringify(sorting) !== JSON.stringify(_lastUsedSorting.current) || JSON.stringify(filters) !== JSON.stringify(_lastUsedFilters.current) || search !== _lastUsedSearch.current;
23140
- // set values so we can track if they changed between loads
23141
- _lastUsedSorting.current = sorting;
23142
- _lastUsedFilters.current = filters;
23143
- _lastUsedSearch.current = search;
23144
- _lastUsedHiddenColumns.current = hiddenColumns;
23145
23139
  // if the page is already loaded and has actual rows, abort
23146
- if (data.pages.includes(pageIndex) && !hasChangedData && !reset) {
23147
- var position = data.pages.indexOf(pageIndex);
23148
- if (data.rows[position * pageSize] !== undefined) {
23149
- return Promise.resolve();
23150
- }
23140
+ if (data.cache[pageIndex] && data.cache[pageIndex][0] && !hasChangedData && !reset) {
23141
+ return Promise.resolve();
23151
23142
  }
23143
+ // create an id to track the update
23144
+ var requestId = uuid.v4();
23152
23145
  // set the page as loading, so that subsequent requests don't retrigger it
23153
23146
  _pendingPageRequests.current[pageIndex] = true;
23154
- var direction = getDirection2(pageIndex, _lastFetchedPage.current);
23155
- var _temp2 = _finallyRethrows(function () {
23156
- return _catch(function () {
23157
- return Promise.resolve(fetchPage(pageIndex, pageSize, sorting, filters, search, hiddenColumns)).then(function (response) {
23158
- length.current = response.length;
23159
- // update state, here we do some "magic" to support "load in place"
23160
- setData(function (currentData) {
23161
- var nextPages = getPages(pageIndex, _lastFetchedPage.current, reset ? [] : currentData.pages, direction);
23162
- var nextRows = currentData.rows;
23163
- if (reset || !direction) {
23164
- var index = nextPages.indexOf(pageIndex);
23165
- var startIndex = (index > -1 ? index : 0) * pageSize;
23166
- if (reset) {
23167
- nextRows = [].concat(response.data);
23168
- } else if (startIndex > 0 || pageIndex === 0) {
23169
- nextRows = Array(startIndex).fill(undefined).concat(response.data);
23170
- } else if (startIndex === 0) {
23171
- nextRows = response.data.concat(currentData.rows.slice(pageSize));
23172
- }
23173
- } else if (direction === 'forward') {
23174
- // if the new data will exceed the dataset size, then chop off the start or end
23175
- // this keeps the stored dataset to our preferred size
23176
- if (currentData.rows.length >= DATASET_SIZE) {
23177
- nextRows = currentData.rows.slice(DEFAULT_PAGE_SIZE).concat(response.data);
23178
- } else {
23179
- nextRows = currentData.rows.concat(response.data);
23180
- }
23181
- } else if (direction === 'backward') {
23182
- if (currentData.rows.length >= DATASET_SIZE) {
23183
- nextRows = response.data.concat(currentData.rows.slice(0, -1 * pageSize));
23184
- } else {
23185
- nextRows = currentData.rows.concat(response.data);
23186
- }
23187
- }
23188
- return {
23189
- rows: nextRows,
23190
- pages: nextPages
23191
- };
23192
- });
23147
+ var _temp2 = _catch(function () {
23148
+ _lastRequestId.current = requestId;
23149
+ return Promise.resolve(fetchPage(pageIndex, pageSize, sorting, filters, search, hiddenColumns)).then(function (response) {
23150
+ length.current = response.length;
23151
+ // update state, here we do some "magic" to support "load in place"
23152
+ setData(function (currentData) {
23153
+ // if this request wasn't the last one, just return the current state to prevent weird updates
23154
+ if (_lastRequestId.current !== requestId) {
23155
+ return currentData;
23156
+ }
23157
+ var direction = getDirection(pageIndex, currentData.pages);
23158
+ var nextPages = getPages(pageIndex, currentData.lastFetchedPage, reset ? [] : currentData.pages, direction);
23159
+ // set values so we can track if they changed between loads
23160
+ _lastUsedSorting.current = sorting;
23161
+ _lastUsedFilters.current = filters;
23162
+ _lastUsedSearch.current = search;
23163
+ _lastUsedHiddenColumns.current = hiddenColumns;
23164
+ // cache data as an object to prevent any duplicates for pages
23165
+ var nextCache;
23166
+ if (reset || hasChangedData || !direction) {
23167
+ nextCache = nextPages.reduce(function (acc, p) {
23168
+ var _extends2;
23169
+ return _extends({}, acc, (_extends2 = {}, _extends2[p] = Array(pageSize).fill(undefined), _extends2));
23170
+ }, {});
23171
+ } else {
23172
+ nextCache = _extends({}, currentData.cache);
23173
+ }
23174
+ nextCache[pageIndex] = response.data;
23175
+ // cleanup "unloaded" pages
23176
+ if (direction === 'forward' && currentData.rows.length >= DATASET_SIZE) {
23177
+ delete nextCache[currentData.pages[0]];
23178
+ } else if (direction === 'backward' && currentData.rows.length >= DATASET_SIZE) {
23179
+ delete nextCache[currentData.pages[currentData.pages.length - 1]];
23180
+ }
23181
+ // remap rows from the cached data - do it here and not in render to save some performance
23182
+ var rows = Object.values(nextCache).reduce(function (acc, p) {
23183
+ return acc.concat(p);
23184
+ }, []);
23185
+ return {
23186
+ cache: nextCache,
23187
+ pages: nextPages,
23188
+ rows: rows,
23189
+ lastFetchedPage: pageIndex
23190
+ };
23193
23191
  });
23194
- }, function () {});
23195
- }, function (_wasThrown2, _result2) {
23196
- // reset pending requests
23197
- delete _pendingPageRequests.current[pageIndex];
23198
- // update the last loaded page
23199
- _lastFetchedPage.current = {
23200
- index: pageIndex,
23201
- direction: direction
23202
- };
23203
- if (_wasThrown2) throw _result2;
23204
- return _result2;
23205
- });
23192
+ // reset pending requests
23193
+ delete _pendingPageRequests.current[pageIndex];
23194
+ });
23195
+ }, function () {});
23206
23196
  return Promise.resolve(_temp2 && _temp2.then ? _temp2.then(function () {}) : void 0);
23207
23197
  } catch (e) {
23208
23198
  return Promise.reject(e);
@@ -23221,20 +23211,21 @@ function useTableDataLoader2(fetchPage, fetchAll, options) {
23221
23211
  // data will be filled after the first request
23222
23212
  var _React$useState = React__default.useState({
23223
23213
  rows: [],
23224
- pages: []
23214
+ pages: [],
23215
+ cache: {},
23216
+ lastFetchedPage: undefined
23225
23217
  }),
23226
23218
  data = _React$useState[0],
23227
23219
  setData = _React$useState[1];
23228
23220
  // track which pages have been loaded to dedupe requests
23229
23221
  var _pendingPageRequests = React__default.useRef({});
23222
+ // it's possible to spam updates, e.g. sort, so we don't set state if the last request wasn't the current oen
23223
+ var _lastRequestId = React__default.useRef();
23224
+ // store last used properties
23230
23225
  var _lastUsedSorting = React__default.useRef([]);
23231
23226
  var _lastUsedFilters = React__default.useRef([]);
23232
23227
  var _lastUsedSearch = React__default.useRef();
23233
23228
  var _lastUsedHiddenColumns = React__default.useRef([]);
23234
- var _lastFetchedPage = React__default.useRef({
23235
- index: undefined,
23236
- direction: undefined
23237
- });
23238
23229
  var loadAll = function loadAll(sorting, filters, search, hiddenColumns) {
23239
23230
  try {
23240
23231
  // set values so we can track if they changed between loads
@@ -23246,9 +23237,18 @@ function useTableDataLoader2(fetchPage, fetchAll, options) {
23246
23237
  return _catch(function () {
23247
23238
  return Promise.resolve(fetchAll(sorting, filters, search, hiddenColumns)).then(function (response) {
23248
23239
  length.current = response.length;
23240
+ var pages = [];
23241
+ var cache = {};
23242
+ Array.from(Array(response.length / pageSize).keys()).forEach(function (index) {
23243
+ pages.push(index);
23244
+ var startIndex = index * pageSize;
23245
+ cache[index] = response.data.slice(startIndex, startIndex + pageSize);
23246
+ });
23249
23247
  setData({
23248
+ cache: cache,
23249
+ pages: pages,
23250
23250
  rows: response.data,
23251
- pages: Array.from(Array(response.length / pageSize).keys())
23251
+ lastFetchedPage: undefined
23252
23252
  });
23253
23253
  });
23254
23254
  }, function () {});
@@ -23265,11 +23265,10 @@ function useTableDataLoader2(fetchPage, fetchAll, options) {
23265
23265
  };
23266
23266
  var invalidate = function invalidate() {
23267
23267
  try {
23268
- console.log('invalidate');
23269
23268
  // reset stuff
23270
23269
  _pendingPageRequests.current = {};
23271
23270
  // load the current page again
23272
- return loadPage(getCurrentPage(data.pages, _lastFetchedPage.current), _lastUsedSorting.current, _lastUsedFilters.current, _lastUsedSearch.current, _lastUsedHiddenColumns.current, true);
23271
+ return loadPage(getCurrentPage(data.pages), _lastUsedSorting.current, _lastUsedFilters.current, _lastUsedSearch.current, _lastUsedHiddenColumns.current, true);
23273
23272
  } catch (e) {
23274
23273
  return Promise.reject(e);
23275
23274
  }
@@ -23278,7 +23277,7 @@ function useTableDataLoader2(fetchPage, fetchAll, options) {
23278
23277
  try {
23279
23278
  // reset before loading the current page
23280
23279
  _pendingPageRequests.current = {};
23281
- return loadPage(getCurrentPage(data.pages, _lastFetchedPage.current), sorting, _lastUsedFilters.current, _lastUsedSearch.current, _lastUsedHiddenColumns.current, true);
23280
+ return loadPage(getCurrentPage(data.pages), sorting, _lastUsedFilters.current, _lastUsedSearch.current, _lastUsedHiddenColumns.current, true);
23282
23281
  } catch (e) {
23283
23282
  return Promise.reject(e);
23284
23283
  }
@@ -23287,7 +23286,7 @@ function useTableDataLoader2(fetchPage, fetchAll, options) {
23287
23286
  try {
23288
23287
  // reset before loading the current page
23289
23288
  _pendingPageRequests.current = {};
23290
- return loadPage(getCurrentPage(data.pages, _lastFetchedPage.current), _lastUsedSorting.current, filters, _lastUsedSearch.current, hiddenColumns, true);
23289
+ return loadPage(0, _lastUsedSorting.current, filters, _lastUsedSearch.current, hiddenColumns, true);
23291
23290
  } catch (e) {
23292
23291
  return Promise.reject(e);
23293
23292
  }
@@ -23296,7 +23295,7 @@ function useTableDataLoader2(fetchPage, fetchAll, options) {
23296
23295
  try {
23297
23296
  // reset before loading the current page
23298
23297
  _pendingPageRequests.current = {};
23299
- return loadPage(getCurrentPage(data.pages, _lastFetchedPage.current), _lastUsedSorting.current, _lastUsedFilters.current, search, hiddenColumns, true);
23298
+ return loadPage(0, _lastUsedSorting.current, _lastUsedFilters.current, search, hiddenColumns, true);
23300
23299
  } catch (e) {
23301
23300
  return Promise.reject(e);
23302
23301
  }
@@ -23314,37 +23313,27 @@ function useTableDataLoader2(fetchPage, fetchAll, options) {
23314
23313
  _experimentalDataLoader2: true
23315
23314
  }, invalidate];
23316
23315
  }
23317
- function getCurrentPage(currentPages, lastFetchedPage) {
23316
+ function getCurrentPage(currentPages) {
23318
23317
  if (currentPages.length <= 2) {
23319
- var _currentPages$;
23320
- return (_currentPages$ = currentPages[0]) !== null && _currentPages$ !== void 0 ? _currentPages$ : 0;
23321
- }
23322
- var middle = Math.floor(DATASET_SIZE_MULTIPLIER / 2);
23323
- if (lastFetchedPage.index) {
23324
- if (!lastFetchedPage.direction) {
23325
- return lastFetchedPage.index;
23326
- }
23327
- return lastFetchedPage.direction === 'forward' ? lastFetchedPage.index + middle : lastFetchedPage.index - middle;
23318
+ return currentPages[0];
23328
23319
  }
23329
- return 0;
23320
+ // for even page lengths we can't know which is the current visible page - it could even be both
23321
+ // so we load one of them and rely on the "load next/previous page" functionality in row
23322
+ var middle = Math.floor(currentPages.length / 2);
23323
+ return currentPages[middle];
23330
23324
  }
23331
- function getDirection2(pageIndex, lastUsedPage) {
23332
- if (lastUsedPage.index === undefined) {
23333
- return undefined;
23334
- }
23335
- if (pageIndex > lastUsedPage.index) {
23336
- if (lastUsedPage.index + 1 === pageIndex || lastUsedPage.index + DATASET_SIZE_MULTIPLIER === pageIndex) {
23325
+ function getDirection(pageIndex, currentPages) {
23326
+ if (currentPages.length) {
23327
+ if (pageIndex === currentPages[currentPages.length - 1] + 1) {
23337
23328
  return 'forward';
23338
- }
23339
- } else if (pageIndex < lastUsedPage.index) {
23340
- if (lastUsedPage.index - 1 === pageIndex || lastUsedPage.index - DATASET_SIZE_MULTIPLIER === pageIndex) {
23329
+ } else if (pageIndex === currentPages[0] - 1 || currentPages.length === 2 && currentPages[0] !== 0 && pageIndex === currentPages[0]) {
23341
23330
  return 'backward';
23342
23331
  }
23343
23332
  }
23344
23333
  return undefined;
23345
23334
  }
23346
- function getPages(pageIndex, lastUsedPage, currentPages, direction) {
23347
- if (currentPages.length && (pageIndex === lastUsedPage.index || currentPages.includes(pageIndex))) {
23335
+ function getPages(pageIndex, lastUsedPageIndex, currentPages, direction) {
23336
+ if (currentPages.length && (pageIndex === lastUsedPageIndex || currentPages.includes(pageIndex))) {
23348
23337
  return currentPages;
23349
23338
  }
23350
23339
  if (direction === 'forward') {
@@ -23355,11 +23344,7 @@ function getPages(pageIndex, lastUsedPage, currentPages, direction) {
23355
23344
  var _nextPages = currentPages.length === DATASET_SIZE_MULTIPLIER ? currentPages.slice(0, -1) : currentPages;
23356
23345
  return [pageIndex].concat(_nextPages);
23357
23346
  }
23358
- if (pageIndex === 0) {
23359
- return [0];
23360
- }
23361
- // don't go forward, the current page will trigger load of the next page
23362
- return [pageIndex - 1, pageIndex];
23347
+ return [pageIndex];
23363
23348
  }
23364
23349
 
23365
23350
  var useBoundaryOverflowDetection = function useBoundaryOverflowDetection(ref, dependencies) {