v-dict 2.0.8 → 2.0.9

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.
@@ -207,33 +207,6 @@ function createDictManager(createDictManagerOptions = {}) {
207
207
  function getMapSnapshot() {
208
208
  return maps[code];
209
209
  }
210
- const createStateFromMap = (map = /* @__PURE__ */ new Map()) => {
211
- const newObj = /* @__PURE__ */ Object.create(null);
212
- const newList = [];
213
- mapToObj(map, {
214
- obj: newObj,
215
- itemTransformer
216
- });
217
- mapToList(map, {
218
- list: newList,
219
- itemTransformer
220
- });
221
- const newE = Object.fromEntries(
222
- Array.from(map.keys()).map((key) => {
223
- var _a;
224
- return [key, (_a = transformer == null ? void 0 : transformer(key)) != null ? _a : key];
225
- })
226
- );
227
- const getItem = (value) => {
228
- return value !== null && value !== void 0 ? newObj[value] : null;
229
- };
230
- return {
231
- map: newObj,
232
- list: newList,
233
- E: newE,
234
- getItem
235
- };
236
- };
237
210
  function useDict(useDictOptions = {}) {
238
211
  const mergedOptions = {
239
212
  clone: false,
@@ -242,76 +215,94 @@ function createDictManager(createDictManagerOptions = {}) {
242
215
  ...useDictOptions
243
216
  };
244
217
  const { clone, immediate, refresh } = mergedOptions;
245
- const clonedLoadPromiseRef = react.useRef(null);
246
218
  const globalMap = react.useSyncExternalStore(subscribeMap, getMapSnapshot);
247
219
  const [clonedMap, setClonedMap] = react.useState(/* @__PURE__ */ new Map());
248
- const loadPromise = !clone ? globalLoadPromise : clonedLoadPromiseRef.current;
249
- const map = !clone ? globalMap : clonedMap;
250
- const stateRef = react.useRef(createStateFromMap(map));
251
- const load = react.useCallback((options) => {
252
- const oldLoadPromise = loadPromise;
253
- if (!clone) {
254
- globalLoadPromise = createPromise();
255
- } else {
256
- clonedLoadPromiseRef.current = createPromise();
257
- }
258
- loadDict({ ...mergedOptions, ...options }).then((map2) => {
259
- var _a;
220
+ const createStateFromMap = (map = /* @__PURE__ */ new Map()) => {
221
+ const newObj = /* @__PURE__ */ Object.create(null);
222
+ const newList = [];
223
+ mapToObj(map, {
224
+ obj: newObj,
225
+ itemTransformer
226
+ });
227
+ mapToList(map, {
228
+ list: newList,
229
+ itemTransformer
230
+ });
231
+ const newE = Object.fromEntries(
232
+ Array.from(map.keys()).map((key) => {
233
+ var _a;
234
+ return [key, (_a = transformer == null ? void 0 : transformer(key)) != null ? _a : key];
235
+ })
236
+ );
237
+ const getItem = (value) => {
238
+ return value !== null && value !== void 0 ? newObj[value] : null;
239
+ };
240
+ return {
241
+ map: newObj,
242
+ list: newList,
243
+ E: newE,
244
+ getItem
245
+ };
246
+ };
247
+ const stateRef = react.useRef({
248
+ ...createStateFromMap(clonedMap),
249
+ clear: () => {
260
250
  if (!clone) {
261
- maps[code] = map2;
251
+ maps[code] = /* @__PURE__ */ new Map();
262
252
  emitChange(code);
263
- } else {
264
- setClonedMap(map2);
253
+ return;
265
254
  }
266
- stateRef.current = createStateFromMap(map2);
267
- oldLoadPromise == null ? void 0 : oldLoadPromise.resolve(void 0);
255
+ setClonedMap(/* @__PURE__ */ new Map());
256
+ },
257
+ loadPromise: !clone ? globalLoadPromise : createPromise(),
258
+ async load(options) {
259
+ const oldLoadPromise = stateRef.current.loadPromise;
260
+ stateRef.current.loadPromise = createPromise();
268
261
  if (!clone) {
269
- globalLoadPromise.resolve(void 0);
270
- } else {
271
- (_a = clonedLoadPromiseRef.current) == null ? void 0 : _a.resolve(void 0);
262
+ globalLoadPromise = stateRef.current.loadPromise;
272
263
  }
273
- });
274
- return !clone ? globalLoadPromise : clonedLoadPromiseRef.current;
275
- }, []);
276
- const clear2 = () => {
277
- if (!clone) {
278
- maps[code] = /* @__PURE__ */ new Map();
279
- emitChange(code);
280
- return;
264
+ loadDict({ ...mergedOptions, ...options }).then((map) => {
265
+ Object.assign(stateRef.current, createStateFromMap(map));
266
+ if (!clone) {
267
+ maps[code] = map;
268
+ emitChange(code);
269
+ } else {
270
+ setClonedMap(map);
271
+ }
272
+ oldLoadPromise == null ? void 0 : oldLoadPromise.resolve(void 0);
273
+ stateRef.current.loadPromise.resolve(void 0);
274
+ });
275
+ return stateRef.current.loadPromise;
281
276
  }
282
- setClonedMap(/* @__PURE__ */ new Map());
283
- };
277
+ });
278
+ if (!clone) {
279
+ Object.assign(stateRef.current, createStateFromMap(globalMap));
280
+ }
284
281
  react.useEffect(() => {
285
282
  if (remote && !immediate) {
286
283
  return;
287
284
  }
288
285
  if (clone) {
289
- load();
286
+ stateRef.current.load();
290
287
  } else {
291
288
  if (!init) {
292
289
  init = true;
293
- load();
290
+ stateRef.current.load();
294
291
  } else if (refresh) {
295
- load();
292
+ stateRef.current.load();
296
293
  }
297
294
  }
298
295
  }, []);
299
296
  const ctx = react.useMemo(() => {
300
- const value = {
301
- ...stateRef.current,
302
- loadPromise,
303
- load,
304
- clear: clear2
305
- };
306
297
  return {
307
- ...value,
298
+ ...stateRef.current,
308
299
  // @ts-ignore
309
- ...managerExtra == null ? void 0 : managerExtra(value),
300
+ ...managerExtra == null ? void 0 : managerExtra(stateRef.current),
310
301
  // @ts-ignore
311
- ...extra == null ? void 0 : extra(value),
312
- stateRef
302
+ ...extra == null ? void 0 : extra(stateRef.current),
303
+ ref: stateRef
313
304
  };
314
- }, [stateRef.current, loadPromise]);
305
+ }, [!clone ? globalMap : clonedMap]);
315
306
  return ctx;
316
307
  }
317
308
  useDict.extend = (extendCode2, extendOptions) => {
@@ -1,6 +1,6 @@
1
1
  import { warn, isFunction, cloneDeep, createPromise, toMap, merge, mapToObj, mapToList } from '../chunk-E7KZBQBQ.js';
2
2
  export { defineDictData } from '../chunk-E7KZBQBQ.js';
3
- import { useRef, useSyncExternalStore, useState, useCallback, useEffect, useMemo } from 'react';
3
+ import { useSyncExternalStore, useState, useRef, useEffect, useMemo } from 'react';
4
4
 
5
5
  function createDictManager(createDictManagerOptions = {}) {
6
6
  const {
@@ -78,33 +78,6 @@ function createDictManager(createDictManagerOptions = {}) {
78
78
  function getMapSnapshot() {
79
79
  return maps[code];
80
80
  }
81
- const createStateFromMap = (map = /* @__PURE__ */ new Map()) => {
82
- const newObj = /* @__PURE__ */ Object.create(null);
83
- const newList = [];
84
- mapToObj(map, {
85
- obj: newObj,
86
- itemTransformer
87
- });
88
- mapToList(map, {
89
- list: newList,
90
- itemTransformer
91
- });
92
- const newE = Object.fromEntries(
93
- Array.from(map.keys()).map((key) => {
94
- var _a;
95
- return [key, (_a = transformer == null ? void 0 : transformer(key)) != null ? _a : key];
96
- })
97
- );
98
- const getItem = (value) => {
99
- return value !== null && value !== void 0 ? newObj[value] : null;
100
- };
101
- return {
102
- map: newObj,
103
- list: newList,
104
- E: newE,
105
- getItem
106
- };
107
- };
108
81
  function useDict(useDictOptions = {}) {
109
82
  const mergedOptions = {
110
83
  clone: false,
@@ -113,76 +86,94 @@ function createDictManager(createDictManagerOptions = {}) {
113
86
  ...useDictOptions
114
87
  };
115
88
  const { clone, immediate, refresh } = mergedOptions;
116
- const clonedLoadPromiseRef = useRef(null);
117
89
  const globalMap = useSyncExternalStore(subscribeMap, getMapSnapshot);
118
90
  const [clonedMap, setClonedMap] = useState(/* @__PURE__ */ new Map());
119
- const loadPromise = !clone ? globalLoadPromise : clonedLoadPromiseRef.current;
120
- const map = !clone ? globalMap : clonedMap;
121
- const stateRef = useRef(createStateFromMap(map));
122
- const load = useCallback((options) => {
123
- const oldLoadPromise = loadPromise;
124
- if (!clone) {
125
- globalLoadPromise = createPromise();
126
- } else {
127
- clonedLoadPromiseRef.current = createPromise();
128
- }
129
- loadDict({ ...mergedOptions, ...options }).then((map2) => {
130
- var _a;
91
+ const createStateFromMap = (map = /* @__PURE__ */ new Map()) => {
92
+ const newObj = /* @__PURE__ */ Object.create(null);
93
+ const newList = [];
94
+ mapToObj(map, {
95
+ obj: newObj,
96
+ itemTransformer
97
+ });
98
+ mapToList(map, {
99
+ list: newList,
100
+ itemTransformer
101
+ });
102
+ const newE = Object.fromEntries(
103
+ Array.from(map.keys()).map((key) => {
104
+ var _a;
105
+ return [key, (_a = transformer == null ? void 0 : transformer(key)) != null ? _a : key];
106
+ })
107
+ );
108
+ const getItem = (value) => {
109
+ return value !== null && value !== void 0 ? newObj[value] : null;
110
+ };
111
+ return {
112
+ map: newObj,
113
+ list: newList,
114
+ E: newE,
115
+ getItem
116
+ };
117
+ };
118
+ const stateRef = useRef({
119
+ ...createStateFromMap(clonedMap),
120
+ clear: () => {
131
121
  if (!clone) {
132
- maps[code] = map2;
122
+ maps[code] = /* @__PURE__ */ new Map();
133
123
  emitChange(code);
134
- } else {
135
- setClonedMap(map2);
124
+ return;
136
125
  }
137
- stateRef.current = createStateFromMap(map2);
138
- oldLoadPromise == null ? void 0 : oldLoadPromise.resolve(void 0);
126
+ setClonedMap(/* @__PURE__ */ new Map());
127
+ },
128
+ loadPromise: !clone ? globalLoadPromise : createPromise(),
129
+ async load(options) {
130
+ const oldLoadPromise = stateRef.current.loadPromise;
131
+ stateRef.current.loadPromise = createPromise();
139
132
  if (!clone) {
140
- globalLoadPromise.resolve(void 0);
141
- } else {
142
- (_a = clonedLoadPromiseRef.current) == null ? void 0 : _a.resolve(void 0);
133
+ globalLoadPromise = stateRef.current.loadPromise;
143
134
  }
144
- });
145
- return !clone ? globalLoadPromise : clonedLoadPromiseRef.current;
146
- }, []);
147
- const clear2 = () => {
148
- if (!clone) {
149
- maps[code] = /* @__PURE__ */ new Map();
150
- emitChange(code);
151
- return;
135
+ loadDict({ ...mergedOptions, ...options }).then((map) => {
136
+ Object.assign(stateRef.current, createStateFromMap(map));
137
+ if (!clone) {
138
+ maps[code] = map;
139
+ emitChange(code);
140
+ } else {
141
+ setClonedMap(map);
142
+ }
143
+ oldLoadPromise == null ? void 0 : oldLoadPromise.resolve(void 0);
144
+ stateRef.current.loadPromise.resolve(void 0);
145
+ });
146
+ return stateRef.current.loadPromise;
152
147
  }
153
- setClonedMap(/* @__PURE__ */ new Map());
154
- };
148
+ });
149
+ if (!clone) {
150
+ Object.assign(stateRef.current, createStateFromMap(globalMap));
151
+ }
155
152
  useEffect(() => {
156
153
  if (remote && !immediate) {
157
154
  return;
158
155
  }
159
156
  if (clone) {
160
- load();
157
+ stateRef.current.load();
161
158
  } else {
162
159
  if (!init) {
163
160
  init = true;
164
- load();
161
+ stateRef.current.load();
165
162
  } else if (refresh) {
166
- load();
163
+ stateRef.current.load();
167
164
  }
168
165
  }
169
166
  }, []);
170
167
  const ctx = useMemo(() => {
171
- const value = {
172
- ...stateRef.current,
173
- loadPromise,
174
- load,
175
- clear: clear2
176
- };
177
168
  return {
178
- ...value,
169
+ ...stateRef.current,
179
170
  // @ts-ignore
180
- ...managerExtra == null ? void 0 : managerExtra(value),
171
+ ...managerExtra == null ? void 0 : managerExtra(stateRef.current),
181
172
  // @ts-ignore
182
- ...extra == null ? void 0 : extra(value),
183
- stateRef
173
+ ...extra == null ? void 0 : extra(stateRef.current),
174
+ ref: stateRef
184
175
  };
185
- }, [stateRef.current, loadPromise]);
176
+ }, [!clone ? globalMap : clonedMap]);
186
177
  return ctx;
187
178
  }
188
179
  useDict.extend = (extendCode2, extendOptions) => {
@@ -21,17 +21,8 @@ export type Dict<K extends PropertyKey = PropertyKey, I extends Recordable = Dic
21
21
  load: (options?: O) => LoadPromise;
22
22
  clear: () => void;
23
23
  getItem: (value?: I['value'] | Nil) => I | Nil;
24
- stateRef?: {
25
- current: {
26
- list: I[];
27
- E: {
28
- [X in K]: X;
29
- };
30
- map: {
31
- [X in K]: I;
32
- };
33
- getItem: (value?: I['value'] | Nil) => I | Nil;
34
- };
24
+ ref?: {
25
+ current: Omit<Dict<K, I, O>, 'ref'>;
35
26
  };
36
27
  };
37
28
  export type Fetch = (code: string, options: Recordable) => MaybePromise<DictItemRecord[]>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "v-dict",
3
- "version": "2.0.8",
3
+ "version": "2.0.9",
4
4
  "type": "module",
5
5
  "description": "Vue3 & React Dict Manager",
6
6
  "repository": {