|
@@ -1,216 +1,249 @@
|
|
|
-import { Ref, toRaw } from 'vue'
|
|
|
-import { deepIsRevise } from './diff'
|
|
|
+import { Ref, toRaw } from "vue";
|
|
|
+import { deepIsRevise } from "./diff";
|
|
|
|
|
|
export const storeSecurityPush = <T extends any>(items: T[], pushItem: T) => {
|
|
|
- const index = items.indexOf(pushItem)
|
|
|
+ const index = items.indexOf(pushItem);
|
|
|
if (!~index) {
|
|
|
- items.push(pushItem)
|
|
|
- return true
|
|
|
+ items.push(pushItem);
|
|
|
+ return true;
|
|
|
} else {
|
|
|
- return false
|
|
|
+ return false;
|
|
|
}
|
|
|
-}
|
|
|
+};
|
|
|
export const storeSecurityDelete = <T extends any>(items: T[], pushItem: T) => {
|
|
|
- const index = items.indexOf(pushItem)
|
|
|
+ const index = items.indexOf(pushItem);
|
|
|
if (~index) {
|
|
|
- items.splice(index, 1)
|
|
|
- return true
|
|
|
+ items.splice(index, 1);
|
|
|
+ return true;
|
|
|
} else {
|
|
|
- return false
|
|
|
+ return false;
|
|
|
}
|
|
|
-}
|
|
|
+};
|
|
|
|
|
|
-export function addStoreItem <T extends {id: any}>(
|
|
|
- items: Ref<T[]>,
|
|
|
- addAction: (item: T) => Promise<T>,
|
|
|
-): (item: T) => Promise<T>
|
|
|
-export function addStoreItem <T extends {id: any}, K extends {id: any} = T>(
|
|
|
- items: Ref<T[]>,
|
|
|
+export function addStoreItem<T extends { id: any }>(
|
|
|
+ items: Ref<T[]>,
|
|
|
+ addAction: (item: T) => Promise<T>
|
|
|
+): (item: T) => Promise<T>;
|
|
|
+export function addStoreItem<T extends { id: any }, K extends { id: any } = T>(
|
|
|
+ items: Ref<T[]>,
|
|
|
addAction: (item: K) => Promise<K>,
|
|
|
transform: (item: T) => Promise<K> | K
|
|
|
-): (item: T) => Promise<K>
|
|
|
-export function addStoreItem <T extends {id: any}, K extends {id: any} = T>(
|
|
|
- items: Ref<T[]>,
|
|
|
+): (item: T) => Promise<K>;
|
|
|
+export function addStoreItem<T extends { id: any }, K extends { id: any } = T>(
|
|
|
+ items: Ref<T[]>,
|
|
|
addAction: (item: K) => Promise<K>,
|
|
|
transform?: (item: T) => Promise<K> | K
|
|
|
) {
|
|
|
return async (item: T) => {
|
|
|
- let actionData: K
|
|
|
+ let actionData: K;
|
|
|
if (transform) {
|
|
|
- actionData = await transform(item)
|
|
|
+ actionData = await transform(item);
|
|
|
} else {
|
|
|
- actionData = item as unknown as K
|
|
|
+ actionData = item as unknown as K;
|
|
|
}
|
|
|
- const newItem = await addAction(actionData)
|
|
|
- const self = items.value.find(findItem => findItem.id === item.id)
|
|
|
+ const newItem = await addAction(actionData);
|
|
|
+ const self = items.value.find((findItem) => findItem.id === item.id);
|
|
|
if (self) {
|
|
|
- Object.assign(self, newItem)
|
|
|
+ Object.assign(self, newItem);
|
|
|
} else {
|
|
|
- storeSecurityPush(items.value, item)
|
|
|
+ storeSecurityPush(items.value, item);
|
|
|
}
|
|
|
- return newItem
|
|
|
- }
|
|
|
+ return newItem;
|
|
|
+ };
|
|
|
}
|
|
|
|
|
|
-export function updateStoreItem <T extends {id: any}>(
|
|
|
- items: Ref<T[]>,
|
|
|
- updateAction: (item: T, oldItem: T) => Promise<any>,
|
|
|
-): (item: T, oldItem: T) => Promise<void>
|
|
|
-export function updateStoreItem <T extends {id: any}, K extends {id: any} = T>(
|
|
|
- items: Ref<T[]>,
|
|
|
+export function updateStoreItem<T extends { id: any }>(
|
|
|
+ items: Ref<T[]>,
|
|
|
+ updateAction: (item: T, oldItem: T) => Promise<any>
|
|
|
+): (item: T, oldItem: T) => Promise<void>;
|
|
|
+export function updateStoreItem<
|
|
|
+ T extends { id: any },
|
|
|
+ K extends { id: any } = T
|
|
|
+>(
|
|
|
+ items: Ref<T[]>,
|
|
|
updateAction: (item: K, oldItem: T) => Promise<any>,
|
|
|
transform: (item: T) => Promise<K> | K
|
|
|
-): (item: T, oldItem: T) => Promise<void>
|
|
|
-export function updateStoreItem <T extends {id: any}, K extends {id: any} = T>(
|
|
|
- items: Ref<T[]>,
|
|
|
+): (item: T, oldItem: T) => Promise<void>;
|
|
|
+export function updateStoreItem<
|
|
|
+ T extends { id: any },
|
|
|
+ K extends { id: any } = T
|
|
|
+>(
|
|
|
+ items: Ref<T[]>,
|
|
|
updateAction: (item: K, oldItem: T) => Promise<any>,
|
|
|
transform?: (item: T) => Promise<K> | K
|
|
|
) {
|
|
|
return async (item: T, oldItem: T) => {
|
|
|
- let actionData: K
|
|
|
+ let actionData: K;
|
|
|
if (transform) {
|
|
|
- actionData = await transform(item)
|
|
|
+ actionData = await transform(item);
|
|
|
} else {
|
|
|
- actionData = item as unknown as K
|
|
|
+ actionData = item as unknown as K;
|
|
|
}
|
|
|
- await updateAction(actionData, oldItem)
|
|
|
- const storeItem = items.value.find(atom => atom.id === item.id)
|
|
|
+ await updateAction(actionData, oldItem);
|
|
|
+ const storeItem = items.value.find((atom) => atom.id === item.id);
|
|
|
if (storeItem) {
|
|
|
- Object.assign(storeItem, item)
|
|
|
+ Object.assign(storeItem, item);
|
|
|
}
|
|
|
- }
|
|
|
+ };
|
|
|
}
|
|
|
|
|
|
-export function deleteStoreItem <T extends {id: any}>(
|
|
|
- items: Ref<T[]>,
|
|
|
- deleteAction: (item: T) => Promise<any>,
|
|
|
-): (item: T) => Promise<void>
|
|
|
-export function deleteStoreItem <T extends {id: any}, K extends {id: any} = T>(
|
|
|
- items: Ref<T[]>,
|
|
|
+export function deleteStoreItem<T extends { id: any }>(
|
|
|
+ items: Ref<T[]>,
|
|
|
+ deleteAction: (item: T) => Promise<any>
|
|
|
+): (item: T) => Promise<void>;
|
|
|
+export function deleteStoreItem<
|
|
|
+ T extends { id: any },
|
|
|
+ K extends { id: any } = T
|
|
|
+>(
|
|
|
+ items: Ref<T[]>,
|
|
|
deleteAction: (item: K) => Promise<any>,
|
|
|
transform: (item: T) => Promise<K> | K
|
|
|
-): (item: T) => Promise<void>
|
|
|
-export function deleteStoreItem <T extends {id: any}, K extends {id: any} = T>(
|
|
|
- items: Ref<T[]>,
|
|
|
+): (item: T) => Promise<void>;
|
|
|
+export function deleteStoreItem<
|
|
|
+ T extends { id: any },
|
|
|
+ K extends { id: any } = T
|
|
|
+>(
|
|
|
+ items: Ref<T[]>,
|
|
|
deleteAction: (item: K) => Promise<any>,
|
|
|
transform?: (item: T) => Promise<K> | K
|
|
|
) {
|
|
|
return async (item: T) => {
|
|
|
- let actionData: K
|
|
|
+ let actionData: K;
|
|
|
if (transform) {
|
|
|
- actionData = await transform(item)
|
|
|
+ actionData = await transform(item);
|
|
|
} else {
|
|
|
- actionData = item as unknown as K
|
|
|
+ actionData = item as unknown as K;
|
|
|
}
|
|
|
- await deleteAction(actionData)
|
|
|
- storeSecurityDelete(items.value, item)
|
|
|
- }
|
|
|
+ await deleteAction(actionData);
|
|
|
+ storeSecurityDelete(items.value, item);
|
|
|
+ };
|
|
|
}
|
|
|
|
|
|
-export function fetchStoreItems <T extends {id: any}, K extends {id: any} = T>(
|
|
|
- items: Ref<T[]>,
|
|
|
- fetchAction: () => Promise<T[]>,
|
|
|
-): () => Promise<void>
|
|
|
-export function fetchStoreItems <T extends {id: any}, K extends {id: any} = T>(
|
|
|
- items: Ref<T[]>,
|
|
|
- fetchAction: () => Promise<T[]>,
|
|
|
- callback: (() => void) | null,
|
|
|
-): () => Promise<void>
|
|
|
-export function fetchStoreItems <T extends {id: any}, K extends {id: any} = T>(
|
|
|
- items: Ref<T[]>,
|
|
|
- fetchAction: () => Promise<K[]>,
|
|
|
+export function fetchStoreItems<
|
|
|
+ T extends { id: any },
|
|
|
+ K extends { id: any } = T
|
|
|
+>(items: Ref<T[]>, fetchAction: () => Promise<T[]>): () => Promise<void>;
|
|
|
+export function fetchStoreItems<
|
|
|
+ T extends { id: any },
|
|
|
+ K extends { id: any } = T
|
|
|
+>(
|
|
|
+ items: Ref<T[]>,
|
|
|
+ fetchAction: () => Promise<T[]>,
|
|
|
+ callback: (() => void) | null
|
|
|
+): () => Promise<void>;
|
|
|
+export function fetchStoreItems<
|
|
|
+ T extends { id: any },
|
|
|
+ K extends { id: any } = T
|
|
|
+>(
|
|
|
+ items: Ref<T[]>,
|
|
|
+ fetchAction: () => Promise<K[]>,
|
|
|
callback: (() => void) | null,
|
|
|
- transform: (items: K[]) => Promise<T[]> | T[],
|
|
|
-): () => Promise<void>
|
|
|
-export function fetchStoreItems <T extends {id: any}, K extends {id: any} = T>(
|
|
|
- items: Ref<T[]>,
|
|
|
- fetchAction: () => Promise<K[]>,
|
|
|
+ transform: (items: K[]) => Promise<T[]> | T[]
|
|
|
+): () => Promise<void>;
|
|
|
+export function fetchStoreItems<
|
|
|
+ T extends { id: any },
|
|
|
+ K extends { id: any } = T
|
|
|
+>(
|
|
|
+ items: Ref<T[]>,
|
|
|
+ fetchAction: () => Promise<K[]>,
|
|
|
callback?: (() => void) | null,
|
|
|
- transform?: (items: K[]) => Promise<T[]> | T[],
|
|
|
+ transform?: (items: K[]) => Promise<T[]> | T[]
|
|
|
) {
|
|
|
return async () => {
|
|
|
- const fetchItems = await fetchAction()
|
|
|
+ const fetchItems = await fetchAction();
|
|
|
|
|
|
- let actionData: T[]
|
|
|
+ let actionData: T[];
|
|
|
if (transform) {
|
|
|
- actionData = await transform(fetchItems)
|
|
|
+ actionData = await transform(fetchItems);
|
|
|
} else {
|
|
|
- actionData = fetchItems as unknown as T[]
|
|
|
+ actionData = fetchItems as unknown as T[];
|
|
|
}
|
|
|
- items.value = actionData
|
|
|
- callback && callback()
|
|
|
- }
|
|
|
+ items.value = actionData;
|
|
|
+ callback && callback();
|
|
|
+ };
|
|
|
}
|
|
|
|
|
|
-export const saveStoreItems = <T extends {id: any}>(
|
|
|
- newItems: Ref<T[]>,
|
|
|
- getOldItem: () => T[],
|
|
|
- actions: {
|
|
|
- add?: ReturnType<typeof addStoreItem<T>>,
|
|
|
- update?: ReturnType<typeof updateStoreItem<T>>,
|
|
|
- delete?: ReturnType<typeof deleteStoreItem<T>>,
|
|
|
- }
|
|
|
-) => () => {
|
|
|
- const oldItems = getOldItem()
|
|
|
- const {
|
|
|
- deleted,
|
|
|
- updated,
|
|
|
- added
|
|
|
- } = diffStoreItemsChange(newItems.value, oldItems)
|
|
|
+export const saveStoreItems =
|
|
|
+ <T extends { id: any }>(
|
|
|
+ newItems: Ref<T[]>,
|
|
|
+ getOldItem: () => T[],
|
|
|
+ actions: {
|
|
|
+ add?: ReturnType<typeof addStoreItem<T>>;
|
|
|
+ update?: ReturnType<typeof updateStoreItem<T>>;
|
|
|
+ delete?: ReturnType<typeof deleteStoreItem<T>>;
|
|
|
+ }
|
|
|
+ ) =>
|
|
|
+ () => {
|
|
|
+ const oldItems = getOldItem();
|
|
|
+ const { deleted, updated, added } = diffStoreItemsChange(
|
|
|
+ newItems.value,
|
|
|
+ oldItems
|
|
|
+ );
|
|
|
|
|
|
- console.log(deleted, updated, added, newItems.value.length, oldItems.length)
|
|
|
- const promiseAll: Promise<any>[] = []
|
|
|
-
|
|
|
- if (actions.delete) {
|
|
|
- for (const delItem of deleted) {
|
|
|
- promiseAll.push(actions.delete(delItem))
|
|
|
+ console.log(
|
|
|
+ deleted,
|
|
|
+ updated,
|
|
|
+ added,
|
|
|
+ newItems.value.length,
|
|
|
+ oldItems.length
|
|
|
+ );
|
|
|
+ const promiseAll: Promise<any>[] = [];
|
|
|
+
|
|
|
+ if (actions.delete) {
|
|
|
+ for (const delItem of deleted) {
|
|
|
+ promiseAll.push(actions.delete(delItem));
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- if (actions.update) {
|
|
|
- for (const [newItem, oldItem] of updated) {
|
|
|
- promiseAll.push(actions.update(newItem, oldItem))
|
|
|
+ if (actions.update) {
|
|
|
+ for (const [newItem, oldItem] of updated) {
|
|
|
+ promiseAll.push(actions.update(newItem, oldItem));
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- if (actions.add) {
|
|
|
- for (const addItem of added) {
|
|
|
- promiseAll.push(actions.add(addItem))
|
|
|
+ if (actions.add) {
|
|
|
+ for (const addItem of added) {
|
|
|
+ promiseAll.push(actions.add(addItem));
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- return Promise.all(promiseAll)
|
|
|
-}
|
|
|
+ return Promise.all(promiseAll);
|
|
|
+ };
|
|
|
|
|
|
-export const diffStoreItemsChange = <T extends Array<{ id: any }>>(newItems: T, oldItems: T) => {
|
|
|
- const addedItems = [] as unknown as T
|
|
|
- const deletedItems = [] as unknown as T
|
|
|
- const updateItems = [] as unknown as [T[number], T[number]][]
|
|
|
- newItems = toRaw(newItems)
|
|
|
+export const diffStoreItemsChange = <T extends Array<{ id: any }>>(
|
|
|
+ newItems: T,
|
|
|
+ oldItems: T
|
|
|
+) => {
|
|
|
+ const addedItems = [] as unknown as T;
|
|
|
+ const deletedItems = [] as unknown as T;
|
|
|
+ const updateItems = [] as unknown as [T[number], T[number]][];
|
|
|
+ newItems = toRaw(newItems);
|
|
|
for (const newItem of newItems) {
|
|
|
- const oldItem = oldItems.find(oldItem => newItem.id === oldItem.id)
|
|
|
+ const oldItem = oldItems.find((oldItem) => newItem.id === oldItem.id);
|
|
|
if (!oldItem) {
|
|
|
- storeSecurityPush(addedItems, newItem)
|
|
|
+ storeSecurityPush(addedItems, newItem);
|
|
|
} else if (deepIsRevise(oldItem, newItem)) {
|
|
|
- storeSecurityPush(updateItems, [newItem, oldItem] as any)
|
|
|
+ storeSecurityPush(updateItems, [newItem, oldItem] as any);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for (const oldItem of oldItems) {
|
|
|
- const newItem = newItems.find(newItem => newItem.id === oldItem.id)
|
|
|
+ const newItem = newItems.find((newItem) => newItem.id === oldItem.id);
|
|
|
if (!newItem) {
|
|
|
- storeSecurityPush(deletedItems, oldItem)
|
|
|
+ storeSecurityPush(deletedItems, oldItem);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
added: addedItems,
|
|
|
deleted: deletedItems,
|
|
|
- updated: updateItems
|
|
|
- }
|
|
|
-}
|
|
|
+ updated: updateItems,
|
|
|
+ };
|
|
|
+};
|
|
|
|
|
|
-export const recoverStoreItems = <T extends Array<{ id: any }>>(items: Ref<T>, getBackupItems: () => T) => () => {
|
|
|
- const backupItems = getBackupItems()
|
|
|
- items.value = backupItems.map(oldItem => {
|
|
|
- const model = items.value.find(item => item.id === oldItem.id)
|
|
|
- return model ? Object.assign(model, oldItem) : oldItem
|
|
|
- }) as T
|
|
|
-}
|
|
|
+export const recoverStoreItems =
|
|
|
+ <T extends Array<{ id: any }>>(items: Ref<T>, getBackupItems: () => T) =>
|
|
|
+ () => {
|
|
|
+ const backupItems = getBackupItems();
|
|
|
+ items.value = backupItems.map((oldItem) => {
|
|
|
+ const model = items.value.find((item) => item.id === oldItem.id);
|
|
|
+ return model ? Object.assign(model, oldItem) : oldItem;
|
|
|
+ }) as T;
|
|
|
+ };
|