useRuleChainCache.ts
1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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();