-
Notifications
You must be signed in to change notification settings - Fork 7
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
Frank Jogeleit
committed
Dec 21, 2023
1 parent
b0b481b
commit 2f8eb92
Showing
31 changed files
with
519 additions
and
326 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
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import type { Ref, UnwrapRef } from "vue"; | ||
import type { UnwrapRefSimple } from "@vue/reactivity"; | ||
|
||
export const useInfinite = <T>(list: Ref<T[] | null>) => { | ||
const loaded = ref<T[]>([]) | ||
const index = ref(1) | ||
|
||
watch(list, (l) => { | ||
if (!list.value?.length) return | ||
|
||
loaded.value = (l || []).slice(0, 1) as UnwrapRefSimple<T>[] | ||
}, { immediate: true }) | ||
|
||
const load = ({ done }: any) => { | ||
const sum = (list.value || []).length | ||
if (!sum) { return done('ok') } | ||
|
||
if (sum === 1) { return done('empty') } | ||
|
||
const last = index.value | ||
const next = index.value + 2 > sum ? sum : index.value + 2 | ||
loaded.value = [...loaded.value, ...(list.value || []).slice(last, next)] as UnwrapRefSimple<T>[] | ||
|
||
index.value = next | ||
if (next === sum) { | ||
done('empty') | ||
} else { | ||
done('ok') | ||
} | ||
} | ||
|
||
|
||
return { load, loaded } | ||
} | ||
|
||
export const useInfiniteScroll = <T>(list: T[]) => { | ||
const loaded = ref<T[]>([]) | ||
const index = ref(1) | ||
|
||
watch(list, (l) => { | ||
if (!list.length) return | ||
|
||
loaded.value = (l || []).slice(0, 1) as UnwrapRefSimple<T>[] | ||
}, { immediate: true }) | ||
|
||
const load = ({ done }: any) => { | ||
const sum = (list || []).length | ||
if (!sum) { return done('ok') } | ||
|
||
if (sum === 1) { return done('empty') } | ||
|
||
const last = index.value | ||
const next = index.value + 2 > sum ? sum : index.value + 2 | ||
loaded.value = [...loaded.value, ...(list || []).slice(last, next)] as UnwrapRefSimple<T>[] | ||
|
||
index.value = next | ||
if (next === sum) { | ||
done('empty') | ||
} else { | ||
done('ok') | ||
} | ||
} | ||
|
||
|
||
return { load, loaded } | ||
} |
Oops, something went wrong.