-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
195 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { createArray } from 'zeed' | ||
import { useLazyData } from './lazy-data' | ||
|
||
describe('useLazyData', () => { | ||
async function onFetch(offset: number, length: number) { | ||
return createArray(length).map((_, i) => offset + i) | ||
} | ||
|
||
it('should generate fake data', async () => { | ||
const result = await onFetch(5, 5) | ||
expect(result).toMatchInlineSnapshot(` | ||
[ | ||
5, | ||
6, | ||
7, | ||
8, | ||
9, | ||
] | ||
`) | ||
}) | ||
|
||
it('should initialize with size 0', () => { | ||
const { getSize } = useLazyData({ onFetch }) | ||
expect(getSize()).toBe(0) | ||
}) | ||
|
||
it('should set size correctly', () => { | ||
const { setSize, getSize, data } = useLazyData({ onFetch }) | ||
setSize(5) | ||
expect(getSize()).toBe(5) | ||
expect(data.length).toBe(5) | ||
expect(data).toMatchInlineSnapshot(` | ||
[ | ||
undefined, | ||
undefined, | ||
undefined, | ||
undefined, | ||
undefined, | ||
] | ||
`) | ||
}) | ||
|
||
it('should reload data correctly', () => { | ||
const { setSize, reload, data } = useLazyData({ onFetch }) | ||
setSize(3) | ||
reload() | ||
// Assuming the data is reactive and we can access it directly for testing purposes | ||
expect(data.length).toBe(3) | ||
expect(data.every(item => item == null)).toBe(true) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import type { LoggerInterface } from 'zeed' | ||
import { reactive } from 'vue' | ||
import { arrayEmptyInPlace, createArray, Logger } from 'zeed' | ||
|
||
const log: LoggerInterface = Logger('lazy-data') | ||
|
||
interface Config<T> { | ||
onFetch: (from: number, length: number) => Promise<T[]> | ||
chunkSize?: number | ||
margin?: number | ||
size?: number | ||
} | ||
|
||
enum ChunkStatus { | ||
Loading = 1, | ||
Loaded, | ||
Error, | ||
} | ||
|
||
export function useLazyData<T>(opt: Config<T>) { | ||
const { chunkSize = 10, margin = 5, onFetch, size = 0 } = opt | ||
|
||
let iteration = 0 | ||
let dataSize = 0 | ||
const data = reactive<(T | null)[]>([]) | ||
const chunks: Record<number, ChunkStatus | undefined> = {} | ||
|
||
function reload() { | ||
iteration++ | ||
arrayEmptyInPlace(data) | ||
const newData = createArray(dataSize) as (T | null)[] | ||
data.push(...newData as any) | ||
} | ||
|
||
function setSize(size: number) { | ||
dataSize = size | ||
reload() | ||
} | ||
|
||
if (size > 0) | ||
setSize(size) | ||
|
||
function setVisible(fromPos: number, toPos: number) { | ||
log.assert(fromPos <= toPos, 'fromPos must be less than or equal to toPos') | ||
const fromChunk = Math.floor(Math.max(0, fromPos - margin) / chunkSize) | ||
const toChunk = Math.floor((toPos + margin) / chunkSize) * chunkSize | ||
const iterationNow = iteration | ||
onFetch(fromChunk * chunkSize, ((toChunk - fromChunk) * chunkSize) + chunkSize).then((result) => { | ||
if (iterationNow !== iteration) | ||
return | ||
for (let i = fromChunk; i <= toChunk; i++) | ||
chunks[i] = ChunkStatus.Loaded | ||
|
||
// arraySetArrayInPlace(data, result, fromChunk * chunkSize) | ||
}) | ||
} | ||
|
||
return { | ||
setSize, | ||
getSize: () => dataSize, | ||
setVisible, | ||
reload, | ||
data, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "oui-kit", | ||
"type": "module", | ||
"version": "0.23.11", | ||
"version": "0.23.12", | ||
"author": { | ||
"email": "[email protected]", | ||
"name": "Dirk Holtwick", | ||
|
@@ -85,7 +85,6 @@ | |
"@vitejs/plugin-vue": "^5.1.4", | ||
"@vitest/browser": "^2.1.1", | ||
"eslint": "^9", | ||
"lightningcss": "^1.27.0", | ||
"stylus": "^0.63.0", | ||
"tsup": "^8.3.0", | ||
"typescript": "^5.6", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters