66 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-03-12 12:08:23 +08:00
import { defineStore } from 'pinia';
2023-04-02 01:01:56 +08:00
export const useDictStore = defineStore('dict', () => {
const dict = ref<Map<string, DictDataOption[]>>(new Map());
2023-04-02 01:01:56 +08:00
2023-04-03 00:05:09 +08:00
/**
*
* @param _key key
*/
const getDict = (_key: string): DictDataOption[] | null => {
if (!_key) {
2023-04-03 00:05:09 +08:00
return null;
}
return dict.value.get(_key) || null;
2023-04-03 00:05:09 +08:00
};
2023-04-02 01:01:56 +08:00
2023-04-03 00:05:09 +08:00
/**
*
* @param _key key
* @param _value value
*/
const setDict = (_key: string, _value: DictDataOption[]) => {
if (!_key) {
return false;
}
try {
dict.value.set(_key, _value);
return true;
} catch (e) {
console.error('Error in setDict:', e);
return false;
2023-04-03 00:05:09 +08:00
}
};
2023-04-02 01:01:56 +08:00
2023-04-03 00:05:09 +08:00
/**
*
* @param _key
*/
const removeDict = (_key: string): boolean => {
if (!_key) {
return false;
}
2023-04-03 00:05:09 +08:00
try {
return dict.value.delete(_key);
2023-04-03 00:05:09 +08:00
} catch (e) {
console.error('Error in removeDict:', e);
return false;
2023-04-03 00:05:09 +08:00
}
};
2023-04-02 01:01:56 +08:00
2023-04-03 00:05:09 +08:00
/**
*
*/
const cleanDict = (): void => {
dict.value.clear();
2023-04-03 00:05:09 +08:00
};
2023-04-02 01:01:56 +08:00
2023-04-03 00:05:09 +08:00
return {
dict,
getDict,
setDict,
removeDict,
cleanDict
};
2023-04-02 01:01:56 +08:00
});