useUpdateQueue.ts 834 Bytes
import { nextTick } from 'vue';
import { PublicFormInstaceType, DataSourceType } from '../../index.type';
import { buildUUID } from '/@/utils/uuid';

export const useUpdateQueue = (props: { dataSource: DataSourceType[] }) => {
  const needUpdateQueue: string[] = [];

  const triggerUpdate = (uuid: string, instance: PublicFormInstaceType) => {
    const index = needUpdateQueue.findIndex((item) => item === uuid);
    if (~index) {
      const value = props.dataSource.find((item) => item.uuid === uuid);
      nextTick(() => instance?.setFormValues(value || {}));
      needUpdateQueue.splice(index, 1);
    }
  };

  const trackUpdate = (uuid = buildUUID()) => {
    needUpdateQueue.push(uuid);
    return uuid;
  };

  const clear = () => {
    needUpdateQueue.length = 0;
  };

  return { trackUpdate, triggerUpdate, clear };
};