v-dict 1.0.8 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dict-manager.d.ts +1 -0
- package/dist/index.cjs +35 -20
- package/dist/index.js +35 -20
- package/dist/type.d.ts +2 -0
- package/dist/util.d.ts +1 -0
- package/package.json +1 -1
package/dist/dict-manager.d.ts
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -20,11 +20,11 @@ function createPromise(executor) {
|
|
|
20
20
|
promise.reject = reject;
|
|
21
21
|
return promise;
|
|
22
22
|
}
|
|
23
|
-
|
|
23
|
+
function clearObj(obj) {
|
|
24
24
|
for (const key of Object.keys(obj)) {
|
|
25
25
|
delete obj[key];
|
|
26
26
|
}
|
|
27
|
-
}
|
|
27
|
+
}
|
|
28
28
|
function mapToObj(map, obj = {}) {
|
|
29
29
|
clearObj(obj);
|
|
30
30
|
map.forEach((value, key) => {
|
|
@@ -63,6 +63,14 @@ var defineDictData = (data) => data;
|
|
|
63
63
|
function createDictManager(managerOptions = {}) {
|
|
64
64
|
const { fetch: managerFetch, extra: managerExtra } = managerOptions;
|
|
65
65
|
const maps = vue.reactive({});
|
|
66
|
+
function clear(code) {
|
|
67
|
+
if (code) {
|
|
68
|
+
const dictMap = maps[code];
|
|
69
|
+
dictMap && dictMap.clear();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
clearObj(maps);
|
|
73
|
+
}
|
|
66
74
|
const defineDict = (code, defineDictOptions) => {
|
|
67
75
|
var _a;
|
|
68
76
|
const {
|
|
@@ -71,26 +79,28 @@ function createDictManager(managerOptions = {}) {
|
|
|
71
79
|
fetch = managerFetch,
|
|
72
80
|
extra
|
|
73
81
|
} = (_a = lodashEs.isFunction(defineDictOptions) ? defineDictOptions() : defineDictOptions) != null ? _a : {};
|
|
82
|
+
let loaded = false;
|
|
74
83
|
const managerLoadPromise = vue.shallowRef(createPromise());
|
|
75
84
|
maps[code] = /* @__PURE__ */ new Map();
|
|
76
85
|
async function loadDict(options, mapRef) {
|
|
86
|
+
var _a2;
|
|
77
87
|
const dataMap = toMap(lodashEs.cloneDeep(data));
|
|
78
88
|
if (remote) {
|
|
79
|
-
await (fetch == null ? void 0 : fetch(code, options)
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}));
|
|
89
|
+
const res = (_a2 = await (fetch == null ? void 0 : fetch(code, options))) != null ? _a2 : [];
|
|
90
|
+
mapRef.value = toMap(res);
|
|
91
|
+
dataMap.forEach((value, key) => {
|
|
92
|
+
if (mapRef.value.has(key)) {
|
|
93
|
+
lodashEs.merge(mapRef.value.get(key), value);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
87
96
|
} else {
|
|
88
97
|
mapRef.value = dataMap;
|
|
89
98
|
}
|
|
99
|
+
loaded = true;
|
|
90
100
|
}
|
|
91
101
|
return (useDictOptions) => {
|
|
92
|
-
useDictOptions = lodashEs.merge({ clone: false, immediate: true }, useDictOptions);
|
|
93
|
-
const { clone, immediate } = useDictOptions;
|
|
102
|
+
useDictOptions = lodashEs.merge({ clone: false, immediate: true, refresh: false }, useDictOptions);
|
|
103
|
+
const { clone, immediate, refresh } = useDictOptions;
|
|
94
104
|
const loadPromise = !clone ? managerLoadPromise : vue.shallowRef(createPromise());
|
|
95
105
|
const mapRef = !clone ? vue.toRef(maps, code) : vue.ref(/* @__PURE__ */ new Map());
|
|
96
106
|
const objRef = vue.ref({});
|
|
@@ -110,25 +120,30 @@ function createDictManager(managerOptions = {}) {
|
|
|
110
120
|
}
|
|
111
121
|
return result;
|
|
112
122
|
});
|
|
113
|
-
if (remote) {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
123
|
+
if (!remote || immediate) {
|
|
124
|
+
if (!loaded || refresh) {
|
|
125
|
+
load();
|
|
126
|
+
}
|
|
117
127
|
}
|
|
118
128
|
function load(options) {
|
|
129
|
+
const oldLoadPromise = loadPromise.value;
|
|
119
130
|
loadPromise.value = createPromise();
|
|
120
131
|
loadDict(lodashEs.merge({}, useDictOptions, options), mapRef).then(() => {
|
|
121
|
-
|
|
122
|
-
|
|
132
|
+
oldLoadPromise.resolve();
|
|
133
|
+
loadPromise.value.resolve();
|
|
123
134
|
});
|
|
124
135
|
return loadPromise.value;
|
|
125
136
|
}
|
|
137
|
+
function _clear() {
|
|
138
|
+
mapRef.value.clear();
|
|
139
|
+
}
|
|
126
140
|
const ctx = {
|
|
127
141
|
map: objRef,
|
|
128
142
|
list: listRef,
|
|
129
143
|
E,
|
|
130
144
|
loadPromise,
|
|
131
|
-
load
|
|
145
|
+
load,
|
|
146
|
+
clear: _clear
|
|
132
147
|
};
|
|
133
148
|
const reactiveCtx = vue.reactive(ctx);
|
|
134
149
|
return vue.reactive({
|
|
@@ -138,7 +153,7 @@ function createDictManager(managerOptions = {}) {
|
|
|
138
153
|
});
|
|
139
154
|
};
|
|
140
155
|
};
|
|
141
|
-
return { defineDict };
|
|
156
|
+
return { defineDict, clear };
|
|
142
157
|
}
|
|
143
158
|
|
|
144
159
|
exports.createDictManager = createDictManager;
|
package/dist/index.js
CHANGED
|
@@ -18,11 +18,11 @@ function createPromise(executor) {
|
|
|
18
18
|
promise.reject = reject;
|
|
19
19
|
return promise;
|
|
20
20
|
}
|
|
21
|
-
|
|
21
|
+
function clearObj(obj) {
|
|
22
22
|
for (const key of Object.keys(obj)) {
|
|
23
23
|
delete obj[key];
|
|
24
24
|
}
|
|
25
|
-
}
|
|
25
|
+
}
|
|
26
26
|
function mapToObj(map, obj = {}) {
|
|
27
27
|
clearObj(obj);
|
|
28
28
|
map.forEach((value, key) => {
|
|
@@ -61,6 +61,14 @@ var defineDictData = (data) => data;
|
|
|
61
61
|
function createDictManager(managerOptions = {}) {
|
|
62
62
|
const { fetch: managerFetch, extra: managerExtra } = managerOptions;
|
|
63
63
|
const maps = reactive({});
|
|
64
|
+
function clear(code) {
|
|
65
|
+
if (code) {
|
|
66
|
+
const dictMap = maps[code];
|
|
67
|
+
dictMap && dictMap.clear();
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
clearObj(maps);
|
|
71
|
+
}
|
|
64
72
|
const defineDict = (code, defineDictOptions) => {
|
|
65
73
|
var _a;
|
|
66
74
|
const {
|
|
@@ -69,26 +77,28 @@ function createDictManager(managerOptions = {}) {
|
|
|
69
77
|
fetch = managerFetch,
|
|
70
78
|
extra
|
|
71
79
|
} = (_a = isFunction(defineDictOptions) ? defineDictOptions() : defineDictOptions) != null ? _a : {};
|
|
80
|
+
let loaded = false;
|
|
72
81
|
const managerLoadPromise = shallowRef(createPromise());
|
|
73
82
|
maps[code] = /* @__PURE__ */ new Map();
|
|
74
83
|
async function loadDict(options, mapRef) {
|
|
84
|
+
var _a2;
|
|
75
85
|
const dataMap = toMap(cloneDeep(data));
|
|
76
86
|
if (remote) {
|
|
77
|
-
await (fetch == null ? void 0 : fetch(code, options)
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}));
|
|
87
|
+
const res = (_a2 = await (fetch == null ? void 0 : fetch(code, options))) != null ? _a2 : [];
|
|
88
|
+
mapRef.value = toMap(res);
|
|
89
|
+
dataMap.forEach((value, key) => {
|
|
90
|
+
if (mapRef.value.has(key)) {
|
|
91
|
+
merge(mapRef.value.get(key), value);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
85
94
|
} else {
|
|
86
95
|
mapRef.value = dataMap;
|
|
87
96
|
}
|
|
97
|
+
loaded = true;
|
|
88
98
|
}
|
|
89
99
|
return (useDictOptions) => {
|
|
90
|
-
useDictOptions = merge({ clone: false, immediate: true }, useDictOptions);
|
|
91
|
-
const { clone, immediate } = useDictOptions;
|
|
100
|
+
useDictOptions = merge({ clone: false, immediate: true, refresh: false }, useDictOptions);
|
|
101
|
+
const { clone, immediate, refresh } = useDictOptions;
|
|
92
102
|
const loadPromise = !clone ? managerLoadPromise : shallowRef(createPromise());
|
|
93
103
|
const mapRef = !clone ? toRef(maps, code) : ref(/* @__PURE__ */ new Map());
|
|
94
104
|
const objRef = ref({});
|
|
@@ -108,25 +118,30 @@ function createDictManager(managerOptions = {}) {
|
|
|
108
118
|
}
|
|
109
119
|
return result;
|
|
110
120
|
});
|
|
111
|
-
if (remote) {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
121
|
+
if (!remote || immediate) {
|
|
122
|
+
if (!loaded || refresh) {
|
|
123
|
+
load();
|
|
124
|
+
}
|
|
115
125
|
}
|
|
116
126
|
function load(options) {
|
|
127
|
+
const oldLoadPromise = loadPromise.value;
|
|
117
128
|
loadPromise.value = createPromise();
|
|
118
129
|
loadDict(merge({}, useDictOptions, options), mapRef).then(() => {
|
|
119
|
-
|
|
120
|
-
|
|
130
|
+
oldLoadPromise.resolve();
|
|
131
|
+
loadPromise.value.resolve();
|
|
121
132
|
});
|
|
122
133
|
return loadPromise.value;
|
|
123
134
|
}
|
|
135
|
+
function _clear() {
|
|
136
|
+
mapRef.value.clear();
|
|
137
|
+
}
|
|
124
138
|
const ctx = {
|
|
125
139
|
map: objRef,
|
|
126
140
|
list: listRef,
|
|
127
141
|
E,
|
|
128
142
|
loadPromise,
|
|
129
|
-
load
|
|
143
|
+
load,
|
|
144
|
+
clear: _clear
|
|
130
145
|
};
|
|
131
146
|
const reactiveCtx = reactive(ctx);
|
|
132
147
|
return reactive({
|
|
@@ -136,7 +151,7 @@ function createDictManager(managerOptions = {}) {
|
|
|
136
151
|
});
|
|
137
152
|
};
|
|
138
153
|
};
|
|
139
|
-
return { defineDict };
|
|
154
|
+
return { defineDict, clear };
|
|
140
155
|
}
|
|
141
156
|
|
|
142
157
|
export { createDictManager, defineDictData };
|
package/dist/type.d.ts
CHANGED
|
@@ -24,6 +24,7 @@ export type ExtraGetter<D extends Dict<string> = Dict<string>> = (dict: D) => Re
|
|
|
24
24
|
export type UseDictOptions = {
|
|
25
25
|
clone?: boolean;
|
|
26
26
|
immediate?: boolean;
|
|
27
|
+
refresh?: boolean;
|
|
27
28
|
} & Recordable;
|
|
28
29
|
type ExtractFetchOptions<F extends Fetch> = Parameters<F>[1] extends infer T ? T extends undefined ? {} : T : never;
|
|
29
30
|
export interface DefineDict<Extra extends ExtraGetter> {
|
|
@@ -46,5 +47,6 @@ export type Dict<Key extends PropertyKey = PropertyKey, ExtraItem extends Record
|
|
|
46
47
|
} & Recordable<Simplify<DictItem & ExtraItem>>;
|
|
47
48
|
loadPromise: LoadPromise;
|
|
48
49
|
load: (options?: Options) => LoadPromise;
|
|
50
|
+
clear: () => void;
|
|
49
51
|
};
|
|
50
52
|
export {};
|
package/dist/util.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { WritableDeep } from 'type-fest';
|
|
2
2
|
import type { DictItemRecord, DictMap, Recordable } from './type';
|
|
3
|
+
export declare function clearObj(obj: Recordable): void;
|
|
3
4
|
export declare function mapToObj(map: DictMap, obj?: Recordable<DictItemRecord>): Recordable<DictItemRecord>;
|
|
4
5
|
export declare function objToMap(obj: Recordable<DictItemRecord>): DictMap;
|
|
5
6
|
export declare function mapToList(map: DictMap, list?: DictItemRecord[]): DictItemRecord[];
|