useRuleChainCache.ts 1.52 KB
import { Edge, Node } from '@vue-flow/core';
import {
  RULE_NODE_KEY,
  RULE_CHAIN_KEY,
  RULE_NODE_LOCAL_CACHE_KEY,
  RULE_CHAIN_IMPORT_LOCAL_CACHE_KEY,
} from '/@/enums/cacheEnum';
import { createLocalStorage } from '/@/utils/cache';
import { RuleChainDetail, RuleChainType } from '../types/ruleNode';

const ruleNodeStorage = createLocalStorage({ prefixKey: RULE_NODE_LOCAL_CACHE_KEY });
const ruleChainStorage = createLocalStorage({ prefixKey: RULE_CHAIN_IMPORT_LOCAL_CACHE_KEY });

interface RuleNodeCacheType {
  nodes?: Node[];
  edges?: Edge[];
  originX?: number;
  originY?: number;
}

export interface RuleChainCacheType {
  ruleChain: Partial<RuleChainDetail>;
  metadata: RuleChainType;
}

export const setRuleNodeCache = ({
  nodes = [],
  edges = [],
  originX,
  originY,
}: RuleNodeCacheType) => {
  ruleNodeStorage.set(RULE_NODE_KEY, {
    nodes,
    edges,
    originX,
    originY,
  });
};

export const getRuleNodeCache = (): RuleNodeCacheType => ruleNodeStorage.get(RULE_NODE_KEY);

export const checkHasCacheRuleNode = () => !!getRuleNodeCache();

export const getRuleChainImportCache = (): RuleChainCacheType =>
  ruleChainStorage.get(RULE_CHAIN_KEY) || {};

export const setRuleChainImportCache = (value: RuleChainCacheType) =>
  ruleChainStorage.set(RULE_CHAIN_KEY, value);

export const clearRuleChainImportCache = () => ruleChainStorage.remove(RULE_CHAIN_KEY);

function initRuleNodeStorage() {
  const value = ruleNodeStorage.get(RULE_NODE_KEY);
  value && ruleNodeStorage.set(RULE_NODE_KEY, value);
}

initRuleNodeStorage();