@slan-health/taro-vueuse
Version:
taro vue版本hooks
123 lines (122 loc) • 3.81 kB
JavaScript
import { shallowRef, computed, ref } from "vue";
import useDebouncePlugin from "./core/plugins/useDebouncePlugin.js";
import useErrorRetryPlugin from "./core/plugins/useErrorRetryPlugin.js";
import useReadyPlugin from "./core/plugins/useReadyPlugin.js";
import useRefreshDepsPlugin from "./core/plugins/useRefreshDepsPlugin.js";
import useThrottlePlugin from "./core/plugins/useThrottlePlugin.js";
import useQuery from "./core/useQuery.js";
import { isFunction, omit, warning, shallowCopy } from "./core/utils/index.js";
function useLoadMore(service, options) {
const { isNoMore, dataListKey = "list", ...restOptions } = options ?? {};
const data = shallowRef();
const dataList = computed(() => data.value[dataListKey] || []);
const loadingMore = ref(false);
const isTriggerByLoadMore = ref(false);
const count = ref(0);
const { runAsync, run, cancel: _cancel, ...rest } = useQuery(
async (lastData) => {
const currentCount = count.value;
const currentData = await service(lastData);
if (currentCount === count.value) {
if (lastData) {
data.value = {
...currentData,
[dataListKey]: [...lastData[dataListKey], ...currentData[dataListKey]]
};
} else {
data.value = currentData;
}
}
return currentData;
},
{
...restOptions,
defaultParams: [],
refreshDepsAction: () => {
if (restOptions == null ? void 0 : restOptions.refreshDepsAction) {
restOptions.refreshDepsAction();
} else {
refresh();
}
},
onError: (error) => {
var _a;
(_a = restOptions == null ? void 0 : restOptions.onError) == null ? void 0 : _a.call(restOptions, error);
},
onSuccess: (data2) => {
var _a;
(_a = restOptions == null ? void 0 : restOptions.onSuccess) == null ? void 0 : _a.call(restOptions, data2);
},
onBefore: () => {
var _a;
count.value += 1;
if (isTriggerByLoadMore.value) {
isTriggerByLoadMore.value = false;
loadingMore.value = true;
}
(_a = restOptions == null ? void 0 : restOptions.onBefore) == null ? void 0 : _a.call(restOptions);
},
onAfter: () => {
var _a;
loadingMore.value = false;
isTriggerByLoadMore.value = false;
(_a = restOptions == null ? void 0 : restOptions.onAfter) == null ? void 0 : _a.call(restOptions);
}
},
[
useErrorRetryPlugin,
useDebouncePlugin,
useThrottlePlugin,
useRefreshDepsPlugin,
useReadyPlugin
]
);
const noMore = computed(() => {
return isNoMore && isFunction(isNoMore) ? isNoMore(data.value) : false;
});
const loadMore = () => {
loadMoreAsync().catch(() => {
});
};
const loadMoreAsync = () => {
if (noMore.value) {
return Promise.reject(
warning(
"No more data. You need to ignore this error by checking if `noMore` is false before calling `loadMoreAsync`",
true
)
);
}
isTriggerByLoadMore.value = true;
return runAsync(data.value);
};
const refresh = () => run();
const refreshAsync = () => runAsync();
const cancel = () => {
count.value += 1;
_cancel();
loadingMore.value = false;
};
const mutate = (x) => {
const mutateData = isFunction(x) ? x(data.value) : x;
const _mutateData = shallowCopy(mutateData);
data.value = _mutateData;
};
return {
data,
dataList,
loadingMore,
noMore,
cancel,
mutate,
refresh,
refreshAsync,
loadMore,
loadMoreAsync,
...omit(rest, ["refresh", "refreshAsync", "mutate", "params", "data"])
};
}
export {
useLoadMore as default
};
//# sourceMappingURL=useLoadMore.js.map