@legendapp/list 2.0.1 → 2.0.2

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.
Files changed (3) hide show
  1. package/index.js +43 -21
  2. package/index.mjs +43 -21
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -1066,7 +1066,7 @@ function ensurePositionCalculated(ctx, state, index) {
1066
1066
  updateItemPositions(ctx, state, false, newStart, newEnd);
1067
1067
  }
1068
1068
  function updateItemPositions(ctx, state, dataChanged, startIndex = 0, endIndex) {
1069
- var _a, _b, _c, _d, _e, _f, _g;
1069
+ var _a, _b, _c, _d, _e, _f, _g, _h;
1070
1070
  const {
1071
1071
  columns,
1072
1072
  indexByKey,
@@ -1083,29 +1083,47 @@ function updateItemPositions(ctx, state, dataChanged, startIndex = 0, endIndex)
1083
1083
  let column = 1;
1084
1084
  let maxSizeInRow = 0;
1085
1085
  const hasColumns = numColumns > 1;
1086
- if (startIndex > 0) {
1087
- const prevIndex = startIndex - 1;
1088
- const prevId = (_a = idCache.get(prevIndex)) != null ? _a : getId(state, prevIndex);
1089
- const prevPosition = (_b = positions.get(prevId)) != null ? _b : 0;
1086
+ const dataLength = data.length;
1087
+ const requiredRange = getRequiredRange(ctx, state);
1088
+ const shouldOptimize = dataLength >= 500;
1089
+ const optimizedEndIndex = shouldOptimize ? Math.min(dataLength - 1, requiredRange.end) : dataLength - 1;
1090
+ const actualEndIndex = endIndex !== void 0 ? Math.min(endIndex, dataLength - 1) : optimizedEndIndex;
1091
+ let adjustedStartIndex = startIndex;
1092
+ let adjustedEndIndex = actualEndIndex;
1093
+ if (((_a = state.positionRange) == null ? void 0 : _a.valid) && !dataChanged && shouldOptimize) {
1094
+ const existingStart = state.positionRange.start;
1095
+ const existingEnd = state.positionRange.end;
1096
+ const requiredStart = requiredRange.start;
1097
+ const requiredEnd = requiredRange.end;
1098
+ if (requiredStart <= existingEnd + 1 && requiredEnd >= existingStart - 1) {
1099
+ adjustedStartIndex = Math.min(startIndex, Math.min(existingStart, requiredStart));
1100
+ adjustedEndIndex = Math.max(actualEndIndex, Math.max(existingEnd, requiredEnd));
1101
+ } else if (requiredStart > existingEnd + 1) {
1102
+ adjustedStartIndex = Math.min(startIndex, existingEnd + 1);
1103
+ adjustedEndIndex = Math.max(actualEndIndex, requiredEnd);
1104
+ } else if (requiredEnd < existingStart - 1) {
1105
+ adjustedStartIndex = Math.min(startIndex, requiredStart);
1106
+ adjustedEndIndex = Math.max(actualEndIndex, existingStart - 1);
1107
+ }
1108
+ }
1109
+ if (adjustedStartIndex > 0) {
1110
+ const prevIndex = adjustedStartIndex - 1;
1111
+ const prevId = (_b = idCache.get(prevIndex)) != null ? _b : getId(state, prevIndex);
1112
+ const prevPosition = (_c = positions.get(prevId)) != null ? _c : 0;
1090
1113
  if (hasColumns) {
1091
- const prevColumn = (_c = columns.get(prevId)) != null ? _c : 1;
1114
+ const prevColumn = (_d = columns.get(prevId)) != null ? _d : 1;
1092
1115
  currentRowTop = prevPosition;
1093
1116
  column = prevColumn % numColumns + 1;
1094
1117
  } else {
1095
- const prevSize = (_d = sizesKnown.get(prevId)) != null ? _d : getItemSize(state, prevId, prevIndex, data[prevIndex], useAverageSize);
1118
+ const prevSize = (_e = sizesKnown.get(prevId)) != null ? _e : getItemSize(state, prevId, prevIndex, data[prevIndex], useAverageSize);
1096
1119
  currentRowTop = prevPosition + prevSize;
1097
1120
  }
1098
1121
  }
1099
1122
  const needsIndexByKey = dataChanged || indexByKey.size === 0;
1100
- const dataLength = data.length;
1101
- const requiredRange = getRequiredRange(ctx, state);
1102
- const shouldOptimize = dataLength >= 500;
1103
- const optimizedEndIndex = shouldOptimize ? Math.min(dataLength - 1, requiredRange.end) : dataLength - 1;
1104
- const actualEndIndex = endIndex !== void 0 ? Math.min(endIndex, dataLength - 1) : optimizedEndIndex;
1105
- let actualEndReached = startIndex;
1106
- for (let i = startIndex; i < dataLength; i++) {
1107
- const id = (_e = idCache.get(i)) != null ? _e : getId(state, i);
1108
- const size = (_f = sizesKnown.get(id)) != null ? _f : getItemSize(state, id, i, data[i], useAverageSize);
1123
+ let actualEndReached = adjustedStartIndex;
1124
+ for (let i = adjustedStartIndex; i < dataLength; i++) {
1125
+ const id = (_f = idCache.get(i)) != null ? _f : getId(state, i);
1126
+ const size = (_g = sizesKnown.get(id)) != null ? _g : getItemSize(state, id, i, data[i], useAverageSize);
1109
1127
  if (__DEV__ && needsIndexByKey) {
1110
1128
  if (indexByKeyForChecking.has(id)) {
1111
1129
  console.error(
@@ -1133,7 +1151,7 @@ function updateItemPositions(ctx, state, dataChanged, startIndex = 0, endIndex)
1133
1151
  currentRowTop += size;
1134
1152
  }
1135
1153
  actualEndReached = i;
1136
- if (shouldOptimize && i >= actualEndIndex && (!((_g = state.scrollingTo) == null ? void 0 : _g.index) || i >= state.scrollingTo.index)) {
1154
+ if (shouldOptimize && i >= adjustedEndIndex && (!((_h = state.scrollingTo) == null ? void 0 : _h.index) || i >= state.scrollingTo.index)) {
1137
1155
  break;
1138
1156
  }
1139
1157
  }
@@ -1143,13 +1161,16 @@ function updateItemPositions(ctx, state, dataChanged, startIndex = 0, endIndex)
1143
1161
  if (dataChanged) {
1144
1162
  state.positionRange = {
1145
1163
  end: actualEndReached,
1146
- start: startIndex,
1164
+ start: adjustedStartIndex,
1147
1165
  valid: true
1148
1166
  };
1149
1167
  } else {
1150
1168
  state.positionRange = {
1151
1169
  end: Math.max(state.positionRange.valid ? state.positionRange.end : actualEndReached, actualEndReached),
1152
- start: Math.min(state.positionRange.valid ? state.positionRange.start : startIndex, startIndex),
1170
+ start: Math.min(
1171
+ state.positionRange.valid ? state.positionRange.start : adjustedStartIndex,
1172
+ adjustedStartIndex
1173
+ ),
1153
1174
  valid: true
1154
1175
  };
1155
1176
  }
@@ -1974,7 +1995,8 @@ function calculateItemsInView(ctx, state, params = {}) {
1974
1995
  let loopStart = startBufferedIdOrig ? indexByKey.get(startBufferedIdOrig) || 0 : 0;
1975
1996
  for (let i = loopStart; i >= 0; i--) {
1976
1997
  const id = (_a = idCache.get(i)) != null ? _a : getId(state, i);
1977
- const top = positions.get(id);
1998
+ const top = getPositionById(ctx, state, id);
1999
+ if (top === void 0) break;
1978
2000
  const size = (_b = sizes.get(id)) != null ? _b : getItemSize(state, id, i, data[i]);
1979
2001
  const bottom = top + size;
1980
2002
  if (bottom > scroll - scrollBuffer) {
@@ -2003,7 +2025,7 @@ function calculateItemsInView(ctx, state, params = {}) {
2003
2025
  for (let i = Math.max(0, loopStart); i < dataLength && (!foundEnd || i <= maxIndexRendered); i++) {
2004
2026
  const id = (_c = idCache.get(i)) != null ? _c : getId(state, i);
2005
2027
  const size = (_d = sizes.get(id)) != null ? _d : getItemSize(state, id, i, data[i]);
2006
- const top = positions.get(id);
2028
+ const top = getPositionById(ctx, state, id);
2007
2029
  if (!foundEnd) {
2008
2030
  if (startNoBuffer === null && top + size > scroll) {
2009
2031
  startNoBuffer = i;
package/index.mjs CHANGED
@@ -1045,7 +1045,7 @@ function ensurePositionCalculated(ctx, state, index) {
1045
1045
  updateItemPositions(ctx, state, false, newStart, newEnd);
1046
1046
  }
1047
1047
  function updateItemPositions(ctx, state, dataChanged, startIndex = 0, endIndex) {
1048
- var _a, _b, _c, _d, _e, _f, _g;
1048
+ var _a, _b, _c, _d, _e, _f, _g, _h;
1049
1049
  const {
1050
1050
  columns,
1051
1051
  indexByKey,
@@ -1062,29 +1062,47 @@ function updateItemPositions(ctx, state, dataChanged, startIndex = 0, endIndex)
1062
1062
  let column = 1;
1063
1063
  let maxSizeInRow = 0;
1064
1064
  const hasColumns = numColumns > 1;
1065
- if (startIndex > 0) {
1066
- const prevIndex = startIndex - 1;
1067
- const prevId = (_a = idCache.get(prevIndex)) != null ? _a : getId(state, prevIndex);
1068
- const prevPosition = (_b = positions.get(prevId)) != null ? _b : 0;
1065
+ const dataLength = data.length;
1066
+ const requiredRange = getRequiredRange(ctx, state);
1067
+ const shouldOptimize = dataLength >= 500;
1068
+ const optimizedEndIndex = shouldOptimize ? Math.min(dataLength - 1, requiredRange.end) : dataLength - 1;
1069
+ const actualEndIndex = endIndex !== void 0 ? Math.min(endIndex, dataLength - 1) : optimizedEndIndex;
1070
+ let adjustedStartIndex = startIndex;
1071
+ let adjustedEndIndex = actualEndIndex;
1072
+ if (((_a = state.positionRange) == null ? void 0 : _a.valid) && !dataChanged && shouldOptimize) {
1073
+ const existingStart = state.positionRange.start;
1074
+ const existingEnd = state.positionRange.end;
1075
+ const requiredStart = requiredRange.start;
1076
+ const requiredEnd = requiredRange.end;
1077
+ if (requiredStart <= existingEnd + 1 && requiredEnd >= existingStart - 1) {
1078
+ adjustedStartIndex = Math.min(startIndex, Math.min(existingStart, requiredStart));
1079
+ adjustedEndIndex = Math.max(actualEndIndex, Math.max(existingEnd, requiredEnd));
1080
+ } else if (requiredStart > existingEnd + 1) {
1081
+ adjustedStartIndex = Math.min(startIndex, existingEnd + 1);
1082
+ adjustedEndIndex = Math.max(actualEndIndex, requiredEnd);
1083
+ } else if (requiredEnd < existingStart - 1) {
1084
+ adjustedStartIndex = Math.min(startIndex, requiredStart);
1085
+ adjustedEndIndex = Math.max(actualEndIndex, existingStart - 1);
1086
+ }
1087
+ }
1088
+ if (adjustedStartIndex > 0) {
1089
+ const prevIndex = adjustedStartIndex - 1;
1090
+ const prevId = (_b = idCache.get(prevIndex)) != null ? _b : getId(state, prevIndex);
1091
+ const prevPosition = (_c = positions.get(prevId)) != null ? _c : 0;
1069
1092
  if (hasColumns) {
1070
- const prevColumn = (_c = columns.get(prevId)) != null ? _c : 1;
1093
+ const prevColumn = (_d = columns.get(prevId)) != null ? _d : 1;
1071
1094
  currentRowTop = prevPosition;
1072
1095
  column = prevColumn % numColumns + 1;
1073
1096
  } else {
1074
- const prevSize = (_d = sizesKnown.get(prevId)) != null ? _d : getItemSize(state, prevId, prevIndex, data[prevIndex], useAverageSize);
1097
+ const prevSize = (_e = sizesKnown.get(prevId)) != null ? _e : getItemSize(state, prevId, prevIndex, data[prevIndex], useAverageSize);
1075
1098
  currentRowTop = prevPosition + prevSize;
1076
1099
  }
1077
1100
  }
1078
1101
  const needsIndexByKey = dataChanged || indexByKey.size === 0;
1079
- const dataLength = data.length;
1080
- const requiredRange = getRequiredRange(ctx, state);
1081
- const shouldOptimize = dataLength >= 500;
1082
- const optimizedEndIndex = shouldOptimize ? Math.min(dataLength - 1, requiredRange.end) : dataLength - 1;
1083
- const actualEndIndex = endIndex !== void 0 ? Math.min(endIndex, dataLength - 1) : optimizedEndIndex;
1084
- let actualEndReached = startIndex;
1085
- for (let i = startIndex; i < dataLength; i++) {
1086
- const id = (_e = idCache.get(i)) != null ? _e : getId(state, i);
1087
- const size = (_f = sizesKnown.get(id)) != null ? _f : getItemSize(state, id, i, data[i], useAverageSize);
1102
+ let actualEndReached = adjustedStartIndex;
1103
+ for (let i = adjustedStartIndex; i < dataLength; i++) {
1104
+ const id = (_f = idCache.get(i)) != null ? _f : getId(state, i);
1105
+ const size = (_g = sizesKnown.get(id)) != null ? _g : getItemSize(state, id, i, data[i], useAverageSize);
1088
1106
  if (__DEV__ && needsIndexByKey) {
1089
1107
  if (indexByKeyForChecking.has(id)) {
1090
1108
  console.error(
@@ -1112,7 +1130,7 @@ function updateItemPositions(ctx, state, dataChanged, startIndex = 0, endIndex)
1112
1130
  currentRowTop += size;
1113
1131
  }
1114
1132
  actualEndReached = i;
1115
- if (shouldOptimize && i >= actualEndIndex && (!((_g = state.scrollingTo) == null ? void 0 : _g.index) || i >= state.scrollingTo.index)) {
1133
+ if (shouldOptimize && i >= adjustedEndIndex && (!((_h = state.scrollingTo) == null ? void 0 : _h.index) || i >= state.scrollingTo.index)) {
1116
1134
  break;
1117
1135
  }
1118
1136
  }
@@ -1122,13 +1140,16 @@ function updateItemPositions(ctx, state, dataChanged, startIndex = 0, endIndex)
1122
1140
  if (dataChanged) {
1123
1141
  state.positionRange = {
1124
1142
  end: actualEndReached,
1125
- start: startIndex,
1143
+ start: adjustedStartIndex,
1126
1144
  valid: true
1127
1145
  };
1128
1146
  } else {
1129
1147
  state.positionRange = {
1130
1148
  end: Math.max(state.positionRange.valid ? state.positionRange.end : actualEndReached, actualEndReached),
1131
- start: Math.min(state.positionRange.valid ? state.positionRange.start : startIndex, startIndex),
1149
+ start: Math.min(
1150
+ state.positionRange.valid ? state.positionRange.start : adjustedStartIndex,
1151
+ adjustedStartIndex
1152
+ ),
1132
1153
  valid: true
1133
1154
  };
1134
1155
  }
@@ -1953,7 +1974,8 @@ function calculateItemsInView(ctx, state, params = {}) {
1953
1974
  let loopStart = startBufferedIdOrig ? indexByKey.get(startBufferedIdOrig) || 0 : 0;
1954
1975
  for (let i = loopStart; i >= 0; i--) {
1955
1976
  const id = (_a = idCache.get(i)) != null ? _a : getId(state, i);
1956
- const top = positions.get(id);
1977
+ const top = getPositionById(ctx, state, id);
1978
+ if (top === void 0) break;
1957
1979
  const size = (_b = sizes.get(id)) != null ? _b : getItemSize(state, id, i, data[i]);
1958
1980
  const bottom = top + size;
1959
1981
  if (bottom > scroll - scrollBuffer) {
@@ -1982,7 +2004,7 @@ function calculateItemsInView(ctx, state, params = {}) {
1982
2004
  for (let i = Math.max(0, loopStart); i < dataLength && (!foundEnd || i <= maxIndexRendered); i++) {
1983
2005
  const id = (_c = idCache.get(i)) != null ? _c : getId(state, i);
1984
2006
  const size = (_d = sizes.get(id)) != null ? _d : getItemSize(state, id, i, data[i]);
1985
- const top = positions.get(id);
2007
+ const top = getPositionById(ctx, state, id);
1986
2008
  if (!foundEnd) {
1987
2009
  if (startNoBuffer === null && top + size > scroll) {
1988
2010
  startNoBuffer = i;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@legendapp/list",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "Legend List is a drop-in replacement for FlatList with much better performance and supporting dynamically sized items.",
5
5
  "sideEffects": false,
6
6
  "private": false,