|
| 1 | +const createRecycleManager = () => { |
| 2 | + const recycles: { key: number; index: number; show: boolean }[] = []; |
| 3 | + |
| 4 | + const poolKeys: number[] = []; |
| 5 | + let poolStartIndex = 0; |
| 6 | + let poolEndIndex = 0; |
| 7 | + |
| 8 | + let lastItemCount = 0; |
| 9 | + let lastStart = 0; |
| 10 | + let lastEnd = 0; |
| 11 | + |
| 12 | + const update = (itemCount: number, start: number, end: number) => { |
| 13 | + if (lastItemCount === itemCount && lastStart === start && lastEnd === end) |
| 14 | + return recycles; |
| 15 | + |
| 16 | + const poolCount = poolKeys.length; |
| 17 | + |
| 18 | + // 计算新的 visible 区域 |
| 19 | + const newFirstPool = |
| 20 | + start <= poolEndIndex |
| 21 | + ? // 1. 开头一定在 [0, start] |
| 22 | + Math.max( |
| 23 | + 0, |
| 24 | + Math.min( |
| 25 | + start, |
| 26 | + // 2. 开头一定在 [poolStartIndex, poolEndIndex) 之间 |
| 27 | + Math.max( |
| 28 | + poolStartIndex, |
| 29 | + Math.min(poolEndIndex - 1, end - poolCount) |
| 30 | + ) |
| 31 | + ) |
| 32 | + ) |
| 33 | + : start; // poolEndIndex 如果比 start 小,则前部无法保留下来 |
| 34 | + |
| 35 | + const newLastPool = |
| 36 | + poolStartIndex <= end |
| 37 | + ? // 1. 结尾一定在 [end, itemCount] 之间 |
| 38 | + Math.max( |
| 39 | + end, |
| 40 | + Math.min( |
| 41 | + itemCount, |
| 42 | + // 2. 结尾一定在 (poolStartIndex, poolEndIndex] 之间 |
| 43 | + Math.max( |
| 44 | + poolStartIndex + 1, |
| 45 | + Math.min(poolEndIndex, newFirstPool + poolCount) |
| 46 | + ) |
| 47 | + ) |
| 48 | + ) |
| 49 | + : end; // end 如果比 poolStartIndex 小,则后部无法保留下来 |
| 50 | + |
| 51 | + if (poolCount === 0 || newLastPool - newFirstPool < poolCount) { |
| 52 | + // 无法复用,全都重新生成 |
| 53 | + const count = (recycles.length = poolKeys.length = end - start); |
| 54 | + for (let i = 0; i < count; i += 1) { |
| 55 | + poolKeys[i] = i; |
| 56 | + recycles[i] = { |
| 57 | + key: i, |
| 58 | + index: i + start, |
| 59 | + show: true, |
| 60 | + }; |
| 61 | + } |
| 62 | + poolStartIndex = start; |
| 63 | + poolEndIndex = end; |
| 64 | + lastItemCount = itemCount; |
| 65 | + lastStart = start; |
| 66 | + lastEnd = end; |
| 67 | + return recycles; |
| 68 | + } |
| 69 | + |
| 70 | + let usedPoolIndex = 0; |
| 71 | + let usedPoolOffset = 0; |
| 72 | + |
| 73 | + // 复用区域 |
| 74 | + let reuseStart = 0; |
| 75 | + let reuseEnd = 0; |
| 76 | + |
| 77 | + if (poolEndIndex < newFirstPool || newLastPool < poolStartIndex) { |
| 78 | + // 完全没有交集,随便复用 |
| 79 | + reuseStart = newFirstPool; |
| 80 | + reuseEnd = newFirstPool + poolCount; |
| 81 | + } else if (poolStartIndex < newFirstPool) { |
| 82 | + // 开头复用 |
| 83 | + usedPoolOffset = newFirstPool - poolStartIndex; |
| 84 | + reuseStart = newFirstPool; |
| 85 | + reuseEnd = newFirstPool + poolCount; |
| 86 | + } else if (newLastPool < poolEndIndex) { |
| 87 | + // 尾部复用 |
| 88 | + usedPoolOffset = poolCount - (poolEndIndex - newLastPool); |
| 89 | + reuseStart = newLastPool - poolCount; |
| 90 | + reuseEnd = newLastPool; |
| 91 | + } else if (newFirstPool <= poolStartIndex && poolEndIndex <= newLastPool) { |
| 92 | + // 新的 visible 是完全子集,直接复用 |
| 93 | + reuseStart = poolStartIndex; |
| 94 | + reuseEnd = poolEndIndex; |
| 95 | + } |
| 96 | + |
| 97 | + // 开头不可见区域 |
| 98 | + // 如果有不可见区域,则一定是来自上一次 visible 的复用 row |
| 99 | + for (let i = newFirstPool; i < start; i += 1, usedPoolIndex += 1) { |
| 100 | + const poolKey = poolKeys[(usedPoolOffset + usedPoolIndex) % poolCount]; |
| 101 | + const recycle = recycles[i - newFirstPool]; |
| 102 | + recycle.key = poolKey; |
| 103 | + recycle.index = i; |
| 104 | + recycle.show = false; |
| 105 | + } |
| 106 | + |
| 107 | + // 可见区域 |
| 108 | + for (let i = start, keyIndex = 0; i < end; i += 1) { |
| 109 | + let poolKey: number; |
| 110 | + if (reuseStart <= i && i < reuseEnd) { |
| 111 | + // 复用 row |
| 112 | + poolKey = poolKeys[(usedPoolOffset + usedPoolIndex) % poolCount]; |
| 113 | + usedPoolIndex += 1; |
| 114 | + } else { |
| 115 | + // 新建 row |
| 116 | + poolKey = poolCount + keyIndex; |
| 117 | + keyIndex += 1; |
| 118 | + } |
| 119 | + const recycleIndex = i - newFirstPool; |
| 120 | + if (recycleIndex < recycles.length) { |
| 121 | + const recycle = recycles[recycleIndex]; |
| 122 | + recycle.key = poolKey; |
| 123 | + recycle.index = i; |
| 124 | + recycle.show = true; |
| 125 | + } else { |
| 126 | + recycles.push({ |
| 127 | + key: poolKey, |
| 128 | + index: i, |
| 129 | + show: true, |
| 130 | + }); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // 末尾不可见区域 |
| 135 | + // 如果有不可见区域,则一定是来自上一次 visible 的复用 row |
| 136 | + for (let i = end; i < newLastPool; i += 1, usedPoolIndex += 1) { |
| 137 | + const poolKey = poolKeys[(usedPoolOffset + usedPoolIndex) % poolCount]; |
| 138 | + const recycle = recycles[i - newFirstPool]; |
| 139 | + recycle.key = poolKey; |
| 140 | + recycle.index = i; |
| 141 | + recycle.show = false; |
| 142 | + } |
| 143 | + |
| 144 | + // 更新 poolKeys |
| 145 | + for (let i = 0; i < recycles.length; i += 1) { |
| 146 | + poolKeys[i] = recycles[i].key; |
| 147 | + } |
| 148 | + recycles.sort((a, b) => a.key - b.key); |
| 149 | + poolStartIndex = newFirstPool; |
| 150 | + poolEndIndex = newLastPool; |
| 151 | + lastItemCount = itemCount; |
| 152 | + lastStart = start; |
| 153 | + lastEnd = end; |
| 154 | + |
| 155 | + return recycles; |
| 156 | + }; |
| 157 | + |
| 158 | + return update; |
| 159 | +}; |
| 160 | + |
| 161 | +export default createRecycleManager; |
0 commit comments