zustand-kit 1.0.1 → 1.0.2

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/README.md CHANGED
@@ -108,7 +108,7 @@ function Settings() {
108
108
 
109
109
  ### Redux DevTools 集成
110
110
 
111
- 在开发环境下,所有全局状态会自动集成 Redux DevTools,便于调试:
111
+ 在开发环境下,所有全局状态会自动集成到统一的 Redux DevTools 视图中,便于调试:
112
112
 
113
113
  ```tsx
114
114
  import { useGlobalState } from 'zustand-kit';
@@ -127,7 +127,7 @@ const [debugData, setDebugData] = useGlobalState('debug', {}, {
127
127
  });
128
128
  ```
129
129
 
130
- Redux DevTools 中,每个状态会以 `GlobalState:{key}` 命名显示。
130
+ **注意:** 所有全局状态会聚合到一个名为 `GlobalStates (All)` 的 DevTools 实例中,每个状态以其 key 作为属性显示,方便统一查看和调试所有状态。
131
131
 
132
132
  ### 选择器模式(性能优化)
133
133
 
@@ -242,9 +242,12 @@ resetGlobalState('counter');
242
242
  - `options?: UseGlobalStateOptions` - 可选配置
243
243
  - `storage?: 'localStorage' | 'sessionStorage' | 'none'` - 持久化类型(默认 'none')
244
244
  - `storageKey?: string` - 存储键前缀(默认 'global-state')
245
+ - `enableDevtools?: boolean` - 是否启用 Redux DevTools(开发环境默认 true,生产环境默认 false)
245
246
 
246
247
  **返回:** `[state, setState, resetState]`
247
248
 
249
+ **注意:** 对于对象类型的状态,`setState` 支持部分更新。例如:`setUser({ name: 'Jane' })` 只会更新 `name` 字段,其他字段保持不变。
250
+
248
251
  ### `useGlobalSelector<T, R>(key, selector, equalityMode?)`
249
252
 
250
253
  使用选择器订阅状态的特定部分。支持自动检测返回值类型并选择合适的比较模式。
@@ -252,11 +255,12 @@ resetGlobalState('counter');
252
255
  **参数:**
253
256
  - `key: string` - 状态键
254
257
  - `selector: (state: T) => R` - 选择器函数
255
- - `equalityMode?: 'shallow'` - 可选的比较模式
258
+ - `equalityMode?: 'shallow' | false` - 可选的比较模式
256
259
  - `undefined` (默认):自动检测返回值类型
257
260
  - 基本类型:使用 `Object.is`
258
261
  - 对象/数组:使用浅比较
259
262
  - `'shallow'`:强制使用浅比较
263
+ - `false`:强制使用 `Object.is` 比较(即使对象类型)
260
264
 
261
265
  **返回:** 选择的值
262
266
 
package/dist/index.cjs.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } }var E=Object.defineProperty;var v=Object.getOwnPropertySymbols;var B=Object.prototype.hasOwnProperty,$=Object.prototype.propertyIsEnumerable;var w=(t,e,o)=>e in t?E(t,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[e]=o,g=(t,e)=>{for(var o in e||(e={}))B.call(e,o)&&w(t,o,e[o]);if(v)for(var o of v(e))$.call(e,o)&&w(t,o,e[o]);return t};var _react = require('react'); var U = _interopRequireWildcard(_react);var _zustand = require('zustand');var _middleware = require('zustand/middleware');var _shallow = require('zustand/react/shallow');var n=new Map;function J(){n.clear()}function _(t,e,o){let{storage:r="none",storageKey:i="global-state",enableDevtools:a=process.env.NODE_ENV!=="production"}=o||{};if(!n.has(t)){let u=typeof e=="object"&&e!==null&&!Array.isArray(e),p=l=>({value:e,setValue:S=>{l(typeof S=="function"?T=>({value:S(T.value)}):u&&typeof S=="object"&&S!==null?T=>({value:g(g({},T.value),S)}):{value:S})},reset:()=>l({value:e})}),c;if(r!=="none"&&a){let l=r==="localStorage"?localStorage:sessionStorage;c=_zustand.create.call(void 0, )(_middleware.devtools.call(void 0, _middleware.persist.call(void 0, p,{name:`${i}-${t}`,storage:_middleware.createJSONStorage.call(void 0, ()=>l)}),{name:`GlobalState:${t}`}))}else if(r!=="none"){let l=r==="localStorage"?localStorage:sessionStorage;c=_zustand.create.call(void 0, )(_middleware.persist.call(void 0, p,{name:`${i}-${t}`,storage:_middleware.createJSONStorage.call(void 0, ()=>l)}))}else a?c=_zustand.create.call(void 0, )(_middleware.devtools.call(void 0, p,{name:`GlobalState:${t}`})):c=_zustand.create.call(void 0, p);n.set(t,c)}let s=n.get(t),b=s(u=>u.value),A=_react.useMemo.call(void 0, ()=>u=>s.getState().setValue(u),[s]),x=_react.useMemo.call(void 0, ()=>()=>s.getState().reset(),[s]);return[b,A,x]}function M(t,e,o){let r=n.get(t);if(!r)throw new Error(`Global state with key "${t}" not found. Initialize it with useGlobalState first.`);let i=_react.useRef.call(void 0, e);_react.useEffect.call(void 0, ()=>{i.current=e});let a=_react.useCallback.call(void 0, s=>i.current(s.value),[]);if(o===void 0){let s=a(r.getState());return s!==null&&typeof s=="object"&&!U.isValidElement(s)?r(_shallow.useShallow.call(void 0, a)):r(a)}return r(o==="shallow"?_shallow.useShallow.call(void 0, a):a)}function k(t){let e=n.get(t);if(!e)throw new Error(`Global state with key "${t}" not found. Initialize it with useGlobalState first.`);return _react.useMemo.call(void 0, ()=>o=>e.getState().setValue(o),[e])}function F(t){let e=n.get(t);return e==null?void 0:e.getState().value}function H(t,e){let o=n.get(t);if(!o){process.env.NODE_ENV!=="production"&&console.warn(`Global state with key "${t}" not found. Initialize it with useGlobalState first.`);return}o.getState().setValue(e)}function L(t,e){let o=n.get(t);if(!o)return process.env.NODE_ENV!=="production"&&console.warn(`Global state with key "${t}" not found. Initialize it with useGlobalState first.`),()=>{};let r=o.getState().value;return o.subscribe(i=>{let a=i.value;Object.is(a,r)||(e(a,r),r=a)})}function Q(t){let e=n.get(t);if(!e){process.env.NODE_ENV!=="production"&&console.warn(`Global state with key "${t}" not found. Initialize it with useGlobalState first.`);return}e.getState().reset()}var W=_;exports.__clearAllStates__ = J; exports.default = W; exports.getGlobalState = F; exports.resetGlobalState = Q; exports.setGlobalState = H; exports.subscribeGlobalState = L; exports.useGlobalSelector = M; exports.useGlobalSetter = k; exports.useGlobalState = _;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } }var x=Object.defineProperty,E=Object.defineProperties;var N=Object.getOwnPropertyDescriptors;var h=Object.getOwnPropertySymbols;var O=Object.prototype.hasOwnProperty,D=Object.prototype.propertyIsEnumerable;var G=(t,e,o)=>e in t?x(t,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[e]=o,T=(t,e)=>{for(var o in e||(e={}))O.call(e,o)&&G(t,o,e[o]);if(h)for(var o of h(e))D.call(e,o)&&G(t,o,e[o]);return t},A=(t,e)=>E(t,N(e));var _react = require('react'); var m = _interopRequireWildcard(_react);var _zustand = require('zustand');var _middleware = require('zustand/middleware');var _shallow = require('zustand/react/shallow');var a=new Map,i=null;function C(){return i||(i=_zustand.create.call(void 0, )(_middleware.devtools.call(void 0, ()=>({}),{name:"GlobalStates (All)"}))),i}function p(t,e){if(i){let o=i.getState();i.setState(A(T({},o),{[t]:e}))}}function X(){a.clear(),i=null}function k(t,e,o){let{storage:n="none",storageKey:u="global-state",enableDevtools:r=process.env.NODE_ENV!=="production"}=o||{};if(!a.has(t)){let c=typeof e=="object"&&e!==null&&!Array.isArray(e),V=(S,K)=>({value:e,setValue:l=>{typeof l=="function"?S(d=>{let f=l(d.value);return r&&p(t,f),{value:f}}):c&&typeof l=="object"&&l!==null?S(d=>{let f=T(T({},d.value),l);return r&&p(t,f),{value:f}}):(S({value:l}),r&&p(t,l))},reset:()=>{S({value:e}),r&&p(t,e)}}),g;if(n!=="none"){let S=n==="localStorage"?localStorage:sessionStorage;g=_zustand.create.call(void 0, )(_middleware.persist.call(void 0, V,{name:`${u}-${t}`,storage:_middleware.createJSONStorage.call(void 0, ()=>S)}))}else g=_zustand.create.call(void 0, V);a.set(t,g),r&&(C(),p(t,e))}let s=a.get(t),b=s(c=>c.value),B=_react.useMemo.call(void 0, ()=>c=>s.getState().setValue(c),[s]),R=_react.useMemo.call(void 0, ()=>()=>s.getState().reset(),[s]);return[b,B,R]}function Y(t,e,o){let n=a.get(t);if(!n)throw new Error(`Global state with key "${t}" not found. Initialize it with useGlobalState first.`);let u=_react.useRef.call(void 0, e);_react.useEffect.call(void 0, ()=>{u.current=e});let r=_react.useCallback.call(void 0, s=>u.current(s.value),[]);if(o===void 0){let s=r(n.getState());return s!==null&&typeof s=="object"&&!m.isValidElement(s)?n(_shallow.useShallow.call(void 0, r)):n(r)}return n(o==="shallow"?_shallow.useShallow.call(void 0, r):r)}function Z(t){let e=a.get(t);if(!e)throw new Error(`Global state with key "${t}" not found. Initialize it with useGlobalState first.`);return _react.useMemo.call(void 0, ()=>o=>e.getState().setValue(o),[e])}function q(t){let e=a.get(t);return e==null?void 0:e.getState().value}function ee(t,e){let o=a.get(t);if(!o){process.env.NODE_ENV!=="production"&&console.warn(`Global state with key "${t}" not found. Initialize it with useGlobalState first.`);return}o.getState().setValue(e)}function te(t,e){let o=a.get(t);if(!o)return process.env.NODE_ENV!=="production"&&console.warn(`Global state with key "${t}" not found. Initialize it with useGlobalState first.`),()=>{};let n=o.getState().value;return o.subscribe(u=>{let r=u.value;Object.is(r,n)||(e(r,n),n=r)})}function oe(t){let e=a.get(t);if(!e){process.env.NODE_ENV!=="production"&&console.warn(`Global state with key "${t}" not found. Initialize it with useGlobalState first.`);return}e.getState().reset()}var re=k;exports.__clearAllStates__ = X; exports.default = re; exports.getGlobalState = q; exports.resetGlobalState = oe; exports.setGlobalState = ee; exports.subscribeGlobalState = te; exports.useGlobalSelector = Y; exports.useGlobalSetter = Z; exports.useGlobalState = k;
package/dist/index.esm.js CHANGED
@@ -1 +1 @@
1
- var E=Object.defineProperty;var v=Object.getOwnPropertySymbols;var B=Object.prototype.hasOwnProperty,$=Object.prototype.propertyIsEnumerable;var w=(t,e,o)=>e in t?E(t,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[e]=o,g=(t,e)=>{for(var o in e||(e={}))B.call(e,o)&&w(t,o,e[o]);if(v)for(var o of v(e))$.call(e,o)&&w(t,o,e[o]);return t};import*as U from"react";import{create as f}from"zustand";import{persist as V,createJSONStorage as G,devtools as h}from"zustand/middleware";import{useMemo as d,useRef as R,useCallback as N,useEffect as O}from"react";import{useShallow as m}from"zustand/react/shallow";var n=new Map;function J(){n.clear()}function _(t,e,o){let{storage:r="none",storageKey:i="global-state",enableDevtools:a=process.env.NODE_ENV!=="production"}=o||{};if(!n.has(t)){let u=typeof e=="object"&&e!==null&&!Array.isArray(e),p=l=>({value:e,setValue:S=>{l(typeof S=="function"?T=>({value:S(T.value)}):u&&typeof S=="object"&&S!==null?T=>({value:g(g({},T.value),S)}):{value:S})},reset:()=>l({value:e})}),c;if(r!=="none"&&a){let l=r==="localStorage"?localStorage:sessionStorage;c=f()(h(V(p,{name:`${i}-${t}`,storage:G(()=>l)}),{name:`GlobalState:${t}`}))}else if(r!=="none"){let l=r==="localStorage"?localStorage:sessionStorage;c=f()(V(p,{name:`${i}-${t}`,storage:G(()=>l)}))}else a?c=f()(h(p,{name:`GlobalState:${t}`})):c=f(p);n.set(t,c)}let s=n.get(t),b=s(u=>u.value),A=d(()=>u=>s.getState().setValue(u),[s]),x=d(()=>()=>s.getState().reset(),[s]);return[b,A,x]}function M(t,e,o){let r=n.get(t);if(!r)throw new Error(`Global state with key "${t}" not found. Initialize it with useGlobalState first.`);let i=R(e);O(()=>{i.current=e});let a=N(s=>i.current(s.value),[]);if(o===void 0){let s=a(r.getState());return s!==null&&typeof s=="object"&&!U.isValidElement(s)?r(m(a)):r(a)}return r(o==="shallow"?m(a):a)}function k(t){let e=n.get(t);if(!e)throw new Error(`Global state with key "${t}" not found. Initialize it with useGlobalState first.`);return d(()=>o=>e.getState().setValue(o),[e])}function F(t){let e=n.get(t);return e==null?void 0:e.getState().value}function H(t,e){let o=n.get(t);if(!o){process.env.NODE_ENV!=="production"&&console.warn(`Global state with key "${t}" not found. Initialize it with useGlobalState first.`);return}o.getState().setValue(e)}function L(t,e){let o=n.get(t);if(!o)return process.env.NODE_ENV!=="production"&&console.warn(`Global state with key "${t}" not found. Initialize it with useGlobalState first.`),()=>{};let r=o.getState().value;return o.subscribe(i=>{let a=i.value;Object.is(a,r)||(e(a,r),r=a)})}function Q(t){let e=n.get(t);if(!e){process.env.NODE_ENV!=="production"&&console.warn(`Global state with key "${t}" not found. Initialize it with useGlobalState first.`);return}e.getState().reset()}var W=_;export{J as __clearAllStates__,W as default,F as getGlobalState,Q as resetGlobalState,H as setGlobalState,L as subscribeGlobalState,M as useGlobalSelector,k as useGlobalSetter,_ as useGlobalState};
1
+ var x=Object.defineProperty,E=Object.defineProperties;var N=Object.getOwnPropertyDescriptors;var h=Object.getOwnPropertySymbols;var O=Object.prototype.hasOwnProperty,D=Object.prototype.propertyIsEnumerable;var G=(t,e,o)=>e in t?x(t,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[e]=o,T=(t,e)=>{for(var o in e||(e={}))O.call(e,o)&&G(t,o,e[o]);if(h)for(var o of h(e))D.call(e,o)&&G(t,o,e[o]);return t},A=(t,e)=>E(t,N(e));import*as m from"react";import{create as v}from"zustand";import{persist as _,createJSONStorage as $,devtools as I}from"zustand/middleware";import{useMemo as w,useRef as j,useCallback as y,useEffect as z}from"react";import{useShallow as U}from"zustand/react/shallow";var a=new Map,i=null;function C(){return i||(i=v()(I(()=>({}),{name:"GlobalStates (All)"}))),i}function p(t,e){if(i){let o=i.getState();i.setState(A(T({},o),{[t]:e}))}}function X(){a.clear(),i=null}function k(t,e,o){let{storage:n="none",storageKey:u="global-state",enableDevtools:r=process.env.NODE_ENV!=="production"}=o||{};if(!a.has(t)){let c=typeof e=="object"&&e!==null&&!Array.isArray(e),V=(S,K)=>({value:e,setValue:l=>{typeof l=="function"?S(d=>{let f=l(d.value);return r&&p(t,f),{value:f}}):c&&typeof l=="object"&&l!==null?S(d=>{let f=T(T({},d.value),l);return r&&p(t,f),{value:f}}):(S({value:l}),r&&p(t,l))},reset:()=>{S({value:e}),r&&p(t,e)}}),g;if(n!=="none"){let S=n==="localStorage"?localStorage:sessionStorage;g=v()(_(V,{name:`${u}-${t}`,storage:$(()=>S)}))}else g=v(V);a.set(t,g),r&&(C(),p(t,e))}let s=a.get(t),b=s(c=>c.value),B=w(()=>c=>s.getState().setValue(c),[s]),R=w(()=>()=>s.getState().reset(),[s]);return[b,B,R]}function Y(t,e,o){let n=a.get(t);if(!n)throw new Error(`Global state with key "${t}" not found. Initialize it with useGlobalState first.`);let u=j(e);z(()=>{u.current=e});let r=y(s=>u.current(s.value),[]);if(o===void 0){let s=r(n.getState());return s!==null&&typeof s=="object"&&!m.isValidElement(s)?n(U(r)):n(r)}return n(o==="shallow"?U(r):r)}function Z(t){let e=a.get(t);if(!e)throw new Error(`Global state with key "${t}" not found. Initialize it with useGlobalState first.`);return w(()=>o=>e.getState().setValue(o),[e])}function q(t){let e=a.get(t);return e==null?void 0:e.getState().value}function ee(t,e){let o=a.get(t);if(!o){process.env.NODE_ENV!=="production"&&console.warn(`Global state with key "${t}" not found. Initialize it with useGlobalState first.`);return}o.getState().setValue(e)}function te(t,e){let o=a.get(t);if(!o)return process.env.NODE_ENV!=="production"&&console.warn(`Global state with key "${t}" not found. Initialize it with useGlobalState first.`),()=>{};let n=o.getState().value;return o.subscribe(u=>{let r=u.value;Object.is(r,n)||(e(r,n),n=r)})}function oe(t){let e=a.get(t);if(!e){process.env.NODE_ENV!=="production"&&console.warn(`Global state with key "${t}" not found. Initialize it with useGlobalState first.`);return}e.getState().reset()}var re=k;export{X as __clearAllStates__,re as default,q as getGlobalState,oe as resetGlobalState,ee as setGlobalState,te as subscribeGlobalState,Y as useGlobalSelector,Z as useGlobalSetter,k as useGlobalState};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zustand-kit",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "⚡️ A minimalist React global state management library based on Zustand with persistence, TypeScript and fine-grained subscriptions",
5
5
  "keywords": [
6
6
  "zustand",
package/types/index.d.ts CHANGED
@@ -26,7 +26,7 @@ export interface UseGlobalStateOptions {
26
26
  */
27
27
  storageKey?: string;
28
28
  /**
29
- * Enable Redux DevTools integration
29
+ * Enable Redux DevTools integration (aggregated view only)
30
30
  * @default true in development, false in production
31
31
  */
32
32
  enableDevtools?: boolean;
@@ -66,12 +66,12 @@ export interface UseGlobalStateOptions {
66
66
  * storageKey: 'my-app'
67
67
  * });
68
68
  *
69
- * // Disable DevTools in development
69
+ * // Disable aggregated DevTools in development
70
70
  * const [privateData, setPrivateData] = useGlobalState('private', {}, {
71
71
  * enableDevtools: false
72
72
  * });
73
73
  *
74
- * // Force enable DevTools in production (not recommended)
74
+ * // Force enable aggregated DevTools in production (not recommended)
75
75
  * const [debugData, setDebugData] = useGlobalState('debug', {}, {
76
76
  * enableDevtools: true
77
77
  * });