|
|
|
|
@ -1,5 +1,4 @@
|
|
|
|
|
import { WatchEventType } from '@/config';
|
|
|
|
|
import _ from 'lodash';
|
|
|
|
|
import { useEffect, useRef } from 'react';
|
|
|
|
|
|
|
|
|
|
interface ChunkedCollection {
|
|
|
|
|
@ -19,6 +18,7 @@ export function useUpdateChunkedList(options: {
|
|
|
|
|
}) {
|
|
|
|
|
const cacheDataListRef = useRef<any[]>(options.dataList || []);
|
|
|
|
|
const timerRef = useRef<any>(null);
|
|
|
|
|
const countRef = useRef<number>(0);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
cacheDataListRef.current = [...(options.dataList || [])];
|
|
|
|
|
@ -30,74 +30,71 @@ export function useUpdateChunkedList(options: {
|
|
|
|
|
options.setDataList?.([...cacheDataListRef.current]);
|
|
|
|
|
}, 80);
|
|
|
|
|
};
|
|
|
|
|
const updateChunkedList = (
|
|
|
|
|
data: ChunkedCollection,
|
|
|
|
|
dataList?: { id: string | number }[]
|
|
|
|
|
) => {
|
|
|
|
|
const updateChunkedList = (data: ChunkedCollection) => {
|
|
|
|
|
console.log('updateChunkedList=====', {
|
|
|
|
|
ids: data?.ids,
|
|
|
|
|
type: data?.type,
|
|
|
|
|
collection: data?.collection.length,
|
|
|
|
|
dataList: _.map(dataList, (o: any) => o.id)
|
|
|
|
|
collection: data?.collection
|
|
|
|
|
});
|
|
|
|
|
let collections = data?.collection || [];
|
|
|
|
|
if (options?.computedID) {
|
|
|
|
|
collections = _.map(collections, (item: any) => {
|
|
|
|
|
collections = collections?.map((item: any) => {
|
|
|
|
|
item.id = options?.computedID?.(item);
|
|
|
|
|
return item;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (options?.filterFun) {
|
|
|
|
|
collections = _.filter(data?.collection, options?.filterFun);
|
|
|
|
|
collections = data?.collection?.filter(options?.filterFun);
|
|
|
|
|
}
|
|
|
|
|
if (options?.mapFun) {
|
|
|
|
|
collections = _.map(data?.collection, options?.mapFun);
|
|
|
|
|
collections = data?.collection?.map(options?.mapFun);
|
|
|
|
|
}
|
|
|
|
|
const ids = data?.ids || [];
|
|
|
|
|
// CREATE
|
|
|
|
|
if (data?.type === WatchEventType.CREATE) {
|
|
|
|
|
const newDataList: any[] = [];
|
|
|
|
|
_.each(collections, (item: any) => {
|
|
|
|
|
const updateIndex = _.findIndex(
|
|
|
|
|
cacheDataListRef.current,
|
|
|
|
|
const newDataList = collections.reduce((acc: any[], item: any) => {
|
|
|
|
|
const updateIndex = cacheDataListRef.current?.findIndex(
|
|
|
|
|
(sItem: any) => sItem.id === item.id
|
|
|
|
|
);
|
|
|
|
|
const updateItem = { ...item };
|
|
|
|
|
if (updateIndex === -1) {
|
|
|
|
|
const updateItem = _.cloneDeep(item);
|
|
|
|
|
newDataList.push(updateItem);
|
|
|
|
|
acc.push(updateItem);
|
|
|
|
|
} else {
|
|
|
|
|
cacheDataListRef.current[updateIndex] = updateItem;
|
|
|
|
|
}
|
|
|
|
|
console.log('create=========', updateIndex, collections);
|
|
|
|
|
});
|
|
|
|
|
cacheDataListRef.current = [...newDataList, ...cacheDataListRef.current];
|
|
|
|
|
// options.setDataList?.([...cacheDataListRef.current]);
|
|
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
|
}, []);
|
|
|
|
|
cacheDataListRef.current = [
|
|
|
|
|
...newDataList,
|
|
|
|
|
...cacheDataListRef.current
|
|
|
|
|
].slice(0, 10);
|
|
|
|
|
}
|
|
|
|
|
// DELETE
|
|
|
|
|
if (data?.type === WatchEventType.DELETE) {
|
|
|
|
|
cacheDataListRef.current = _.filter(
|
|
|
|
|
cacheDataListRef.current,
|
|
|
|
|
cacheDataListRef.current = cacheDataListRef.current?.filter(
|
|
|
|
|
(item: any) => {
|
|
|
|
|
return !_.includes(ids, item.id);
|
|
|
|
|
return !ids?.includes(item.id);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
options.setDataList?.([...cacheDataListRef.current]);
|
|
|
|
|
}
|
|
|
|
|
// UPDATE
|
|
|
|
|
if (data?.type === WatchEventType.UPDATE) {
|
|
|
|
|
_.each(collections, (item: any) => {
|
|
|
|
|
const updateIndex = _.findIndex(
|
|
|
|
|
cacheDataListRef.current,
|
|
|
|
|
collections?.forEach((item: any) => {
|
|
|
|
|
const updateIndex = cacheDataListRef.current?.findIndex(
|
|
|
|
|
(sItem: any) => sItem.id === item.id
|
|
|
|
|
);
|
|
|
|
|
const updateItem = { ...item };
|
|
|
|
|
if (updateIndex > -1) {
|
|
|
|
|
const updateItem = _.cloneDeep(item);
|
|
|
|
|
cacheDataListRef.current[updateIndex] = updateItem;
|
|
|
|
|
} else if (updateIndex === -1) {
|
|
|
|
|
const updateItem = _.cloneDeep(item);
|
|
|
|
|
cacheDataListRef.current.push(updateItem);
|
|
|
|
|
cacheDataListRef.current = [
|
|
|
|
|
updateItem,
|
|
|
|
|
...cacheDataListRef.current.slice(0, 9)
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
console.log('updateChunkedList=====update', cacheDataListRef.current);
|
|
|
|
|
// options.setDataList?.([...cacheDataListRef.current]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
debounceUpdateChunckedList();
|
|
|
|
|
|