From a3e38b0154d7cb53cc3e675421c6591bcb6c4b77 Mon Sep 17 00:00:00 2001 From: Dirk Holtwick Date: Sun, 6 Oct 2024 00:20:45 +0200 Subject: [PATCH 1/2] feat: better reload --- lib/basic/lazy-data.spec.ts | 30 ++--------------------- lib/basic/lazy-data.ts | 47 ++++++++++++++++++++++--------------- 2 files changed, 30 insertions(+), 47 deletions(-) diff --git a/lib/basic/lazy-data.spec.ts b/lib/basic/lazy-data.spec.ts index dc3f48f..3617619 100644 --- a/lib/basic/lazy-data.spec.ts +++ b/lib/basic/lazy-data.spec.ts @@ -61,21 +61,7 @@ describe('useLazyData', () => { margin: 2, }) setSize(20) - const r = setVisible(3, 2) - expect(r).toMatchInlineSnapshot(` - { - "allChunksToLoad": [ - 0, - 1, - 2, - 3, - ], - "fromChunk": 0, - "fromIndex": 1, - "toChunk": 3, - "toIndex": 7, - } - `) + setVisible(3, 2) await sleep(100) @@ -104,19 +90,7 @@ describe('useLazyData', () => { ] `) - const r2 = setVisible(4, 2) - expect(r2).toMatchInlineSnapshot(` - { - "allChunksToLoad": [ - 4, - ], - "fromChunk": 1, - "fromIndex": 2, - "toChunk": 4, - "toIndex": 8, - } - `) - + setVisible(4, 2) await sleep(100) expect(data).toMatchInlineSnapshot(` diff --git a/lib/basic/lazy-data.ts b/lib/basic/lazy-data.ts index 7baf13b..6be281b 100644 --- a/lib/basic/lazy-data.ts +++ b/lib/basic/lazy-data.ts @@ -25,47 +25,59 @@ export function useLazyData(opt: Config) { let iteration = 0 let dataSize = 0 const data = opt.data ?? reactive<(T | null)[]>([]) - const chunks: Record = {} + let chunks: Record = {} - function reload() { + let currentOffset = 0 + let currentLength = 0 + + function setup() { iteration++ arrayEmptyInPlace(data) const newData = createArray(dataSize) as (T | null)[] data.push(...newData as any) + chunks = {} } function setSize(size: number) { dataSize = size - reload() + setup() } if (size > 0) setSize(size) function setVisible(fromPos: number, length: number) { + currentOffset = fromPos + currentLength = length + const toPos = fromPos + length - // log.assert(fromPos <= toPos, 'fromPos must be less than or equal to toPos') const fromIndex = Math.max(0, fromPos - margin) const toIndex = Math.min(dataSize, toPos + margin) const fromChunk = Math.floor(fromIndex / chunkSize) const toChunk = Math.floor(toIndex / chunkSize) - const allChunksToLoad: number[] = [] - - let chunksToLoad: number[] = [] + let nextChunksToLoad: number[] = [] function flush() { - if (chunksToLoad.length === 0) + // Make a copy of the chunks to load + if (nextChunksToLoad.length === 0) return - const loading = [...chunksToLoad] - allChunksToLoad.push(...loading) - chunksToLoad = [] + const loading = [...nextChunksToLoad] + nextChunksToLoad = [] const fromIndex = loading[0] * chunkSize const itemCount = (loading[loading.length - 1] - loading[0] + 1) * chunkSize + const currentIteration = iteration + onFetch(fromIndex, itemCount).then((values) => { log('Loaded', fromIndex, itemCount, values) + + // Still the same context? + if (iteration !== currentIteration) + return + + // Apply the values loading.forEach(i => chunks[i] = ChunkStatus.Loaded) for (let i = 0; i < itemCount; i++) { data[fromIndex + i] = values[i] as any @@ -79,7 +91,7 @@ export function useLazyData(opt: Config) { const chunk = chunks[i] if (chunk == null) { chunks[i] = ChunkStatus.Loading - chunksToLoad.push(i) + nextChunksToLoad.push(i) } else { flush() @@ -87,14 +99,11 @@ export function useLazyData(opt: Config) { } flush() + } - return { - fromIndex, - toIndex, - fromChunk, - toChunk, - allChunksToLoad, - } + function reload() { + setup() + setVisible(currentOffset, currentLength) } return { From 824fddd596ba946f1cf0df95ee3f17362c2a90a4 Mon Sep 17 00:00:00 2001 From: Dirk Holtwick Date: Sun, 6 Oct 2024 00:21:46 +0200 Subject: [PATCH 2/2] 0.24.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 560ffd9..5f0e55a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "oui-kit", "type": "module", - "version": "0.24.0", + "version": "0.24.1", "author": { "email": "dirk.holtwick@gmail.com", "name": "Dirk Holtwick",