@yakocloud/state-vocab 1.0.4 → 2.0.1
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 +3 -0
- package/dist/state-vocab.cjs.js +10 -8
- package/dist/state-vocab.es.js +402 -362
- package/dist/types/constants.d.ts +2 -0
- package/dist/types/context.d.ts +11 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/main.d.ts +1 -0
- package/dist/types/setup.d.ts +1 -0
- package/dist/types/state.d.ts +22 -0
- package/dist/types/state.utils.d.ts +11 -0
- package/dist/types/utils.d.ts +7 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -275,6 +275,7 @@ function Dashboard() {
|
|
|
275
275
|
const [counter, setCounter, resetCounter] = storage.stats.counter.useState(0)
|
|
276
276
|
const [note, setNote] = storage.personal.note.useState('', {
|
|
277
277
|
delayedSet: 500,
|
|
278
|
+
bidirectional: true,
|
|
278
279
|
onSet: (v) => console.log('Saving note:', v),
|
|
279
280
|
})
|
|
280
281
|
|
|
@@ -323,6 +324,7 @@ createRoot(document.getElementById('root')!).render(
|
|
|
323
324
|
|---|---|---|
|
|
324
325
|
| `storage` | `Storage \| undefined` | `undefined` (in-memory) |
|
|
325
326
|
| `defaultValue` | `T \| undefined` | `undefined` |
|
|
327
|
+
| `bidirectional` | `true \| undefined` | `undefined` |
|
|
326
328
|
| `serialize` | `(v: T) => string` | `JSON.stringify` |
|
|
327
329
|
| `deserialize` | `(v: string) => T` | `JSON.parse` |
|
|
328
330
|
|
|
@@ -341,5 +343,6 @@ React context provider. Must be an ancestor of any component using `.useState()`
|
|
|
341
343
|
| `defaultValue` | `D \| (() => D) \| undefined` | Local default, overrides `defineState` default |
|
|
342
344
|
| `options.delayedSet` | `number \| undefined` | Debounce delay for `onSet` in ms |
|
|
343
345
|
| `options.onSet` | `(next: D, prev: D) => void \| undefined` | Callback after state change |
|
|
346
|
+
| `options.bidirectional` | `true \| undefined` | bidirectional state update to sync tabs |
|
|
344
347
|
|
|
345
348
|
Returns `[value, setValue, resetValue]`.
|
package/dist/state-vocab.cjs.js
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
2
|
-
|
|
3
|
-
`),O=n.stack.
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const j=require("react"),Ve=Symbol("state-def"),K=Symbol("state-path");function Er(o,a,u){if(!a)return o;const s=a.split(".");let c=o;for(const y of s)if(c!==null&&typeof c=="object"&&y in c)c=c[y];else return u;return c===void 0?u:c}function mr(o,a,u){const s=a.replace(/\[(\d+)\]/g,".$1").split(".");let c=o;for(let y=0;y<s.length-1;y++){const h=s[y],p=s[y+1];(c[h]===void 0||c[h]===null)&&(c[h]=/^\d+$/.test(p)?[]:{}),c=c[h]}return c[s[s.length-1]]=u,o}function Rr(o,a=0){let u;return function(...s){u!==void 0&&clearTimeout(u),u=setTimeout(()=>{u=void 0,o.apply(this,s)},a)}}function _r(o,a,u=[]){return j.useMemo(()=>Rr(o,a),u)}function Sr(o){const a=JSON.stringify(o,null,2).split(`
|
|
2
|
+
`),u=[],s=[];for(const c of a){const y=c.match(/^(\s*)"([^"]+)"(\s*:\s*)(.+)$/);if(y){const[,h,p,g,b]=y;u.push(`${h}%c"${p}"%c${g}%c${b}`),s.push("color: #9cdcfe; font-weight: bold","color: #cccccc","color: #ce9178")}else u.push(`%c${c}`),s.push("color: #cccccc")}console.log(u.join(`
|
|
3
|
+
`),...s,o)}var z={exports:{}},Y={};var xe;function Tr(){if(xe)return Y;xe=1;var o=j,a=Symbol.for("react.element"),u=Symbol.for("react.fragment"),s=Object.prototype.hasOwnProperty,c=o.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,y={key:!0,ref:!0,__self:!0,__source:!0};function h(p,g,b){var f,_={},C=null,w=null;b!==void 0&&(C=""+b),g.key!==void 0&&(C=""+g.key),g.ref!==void 0&&(w=g.ref);for(f in g)s.call(g,f)&&!y.hasOwnProperty(f)&&(_[f]=g[f]);if(p&&p.defaultProps)for(f in g=p.defaultProps,g)_[f]===void 0&&(_[f]=g[f]);return{$$typeof:a,type:p,key:C,ref:w,props:_,_owner:c.current}}return Y.Fragment=u,Y.jsx=h,Y.jsxs=h,Y}var L={};var je;function wr(){return je||(je=1,process.env.NODE_ENV!=="production"&&(function(){var o=j,a=Symbol.for("react.element"),u=Symbol.for("react.portal"),s=Symbol.for("react.fragment"),c=Symbol.for("react.strict_mode"),y=Symbol.for("react.profiler"),h=Symbol.for("react.provider"),p=Symbol.for("react.context"),g=Symbol.for("react.forward_ref"),b=Symbol.for("react.suspense"),f=Symbol.for("react.suspense_list"),_=Symbol.for("react.memo"),C=Symbol.for("react.lazy"),w=Symbol.for("react.offscreen"),O=Symbol.iterator,R="@@iterator";function k(e){if(e===null||typeof e!="object")return null;var r=O&&e[O]||e[R];return typeof r=="function"?r:null}var D=o.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;function S(e){{for(var r=arguments.length,t=new Array(r>1?r-1:0),n=1;n<r;n++)t[n-1]=arguments[n];We("error",e,t)}}function We(e,r,t){{var n=D.ReactDebugCurrentFrame,d=n.getStackAddendum();d!==""&&(r+="%s",t=t.concat([d]));var v=t.map(function(l){return String(l)});v.unshift("Warning: "+r),Function.prototype.apply.call(console[e],console,v)}}var Me=!1,Ye=!1,Le=!1,Ne=!1,Ue=!1,ae;ae=Symbol.for("react.module.reference");function Je(e){return!!(typeof e=="string"||typeof e=="function"||e===s||e===y||Ue||e===c||e===b||e===f||Ne||e===w||Me||Ye||Le||typeof e=="object"&&e!==null&&(e.$$typeof===C||e.$$typeof===_||e.$$typeof===h||e.$$typeof===p||e.$$typeof===g||e.$$typeof===ae||e.getModuleId!==void 0))}function qe(e,r,t){var n=e.displayName;if(n)return n;var d=r.displayName||r.name||"";return d!==""?t+"("+d+")":t}function oe(e){return e.displayName||"Context"}function A(e){if(e==null)return null;if(typeof e.tag=="number"&&S("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),typeof e=="function")return e.displayName||e.name||null;if(typeof e=="string")return e;switch(e){case s:return"Fragment";case u:return"Portal";case y:return"Profiler";case c:return"StrictMode";case b:return"Suspense";case f:return"SuspenseList"}if(typeof e=="object")switch(e.$$typeof){case p:var r=e;return oe(r)+".Consumer";case h:var t=e;return oe(t._context)+".Provider";case g:return qe(e,e.render,"ForwardRef");case _:var n=e.displayName||null;return n!==null?n:A(e.type)||"Memo";case C:{var d=e,v=d._payload,l=d._init;try{return A(l(v))}catch{return null}}}return null}var F=Object.assign,W=0,ie,ue,se,ce,le,fe,de;function ve(){}ve.__reactDisabledLog=!0;function ze(){{if(W===0){ie=console.log,ue=console.info,se=console.warn,ce=console.error,le=console.group,fe=console.groupCollapsed,de=console.groupEnd;var e={configurable:!0,enumerable:!0,value:ve,writable:!0};Object.defineProperties(console,{info:e,log:e,warn:e,error:e,group:e,groupCollapsed:e,groupEnd:e})}W++}}function Be(){{if(W--,W===0){var e={configurable:!0,enumerable:!0,writable:!0};Object.defineProperties(console,{log:F({},e,{value:ie}),info:F({},e,{value:ue}),warn:F({},e,{value:se}),error:F({},e,{value:ce}),group:F({},e,{value:le}),groupCollapsed:F({},e,{value:fe}),groupEnd:F({},e,{value:de})})}W<0&&S("disabledDepth fell below zero. This is a bug in React. Please file an issue.")}}var G=D.ReactCurrentDispatcher,X;function N(e,r,t){{if(X===void 0)try{throw Error()}catch(d){var n=d.stack.trim().match(/\n( *(at )?)/);X=n&&n[1]||""}return`
|
|
4
|
+
`+X+e}}var H=!1,U;{var Ke=typeof WeakMap=="function"?WeakMap:Map;U=new Ke}function pe(e,r){if(!e||H)return"";{var t=U.get(e);if(t!==void 0)return t}var n;H=!0;var d=Error.prepareStackTrace;Error.prepareStackTrace=void 0;var v;v=G.current,G.current=null,ze();try{if(r){var l=function(){throw Error()};if(Object.defineProperty(l.prototype,"props",{set:function(){throw Error()}}),typeof Reflect=="object"&&Reflect.construct){try{Reflect.construct(l,[])}catch(P){n=P}Reflect.construct(e,[],l)}else{try{l.call()}catch(P){n=P}e.call(l.prototype)}}else{try{throw Error()}catch(P){n=P}e()}}catch(P){if(P&&n&&typeof P.stack=="string"){for(var i=P.stack.split(`
|
|
5
|
+
`),T=n.stack.split(`
|
|
6
|
+
`),E=i.length-1,m=T.length-1;E>=1&&m>=0&&i[E]!==T[m];)m--;for(;E>=1&&m>=0;E--,m--)if(i[E]!==T[m]){if(E!==1||m!==1)do if(E--,m--,m<0||i[E]!==T[m]){var x=`
|
|
7
|
+
`+i[E].replace(" at new "," at ");return e.displayName&&x.includes("<anonymous>")&&(x=x.replace("<anonymous>",e.displayName)),typeof e=="function"&&U.set(e,x),x}while(E>=1&&m>=0);break}}}finally{H=!1,G.current=v,Be(),Error.prepareStackTrace=d}var $=e?e.displayName||e.name:"",V=$?N($):"";return typeof e=="function"&&U.set(e,V),V}function Ge(e,r,t){return pe(e,!1)}function Xe(e){var r=e.prototype;return!!(r&&r.isReactComponent)}function J(e,r,t){if(e==null)return"";if(typeof e=="function")return pe(e,Xe(e));if(typeof e=="string")return N(e);switch(e){case b:return N("Suspense");case f:return N("SuspenseList")}if(typeof e=="object")switch(e.$$typeof){case g:return Ge(e.render);case _:return J(e.type,r,t);case C:{var n=e,d=n._payload,v=n._init;try{return J(v(d),r,t)}catch{}}}return""}var M=Object.prototype.hasOwnProperty,ge={},ye=D.ReactDebugCurrentFrame;function q(e){if(e){var r=e._owner,t=J(e.type,e._source,r?r.type:null);ye.setExtraStackFrame(t)}else ye.setExtraStackFrame(null)}function He(e,r,t,n,d){{var v=Function.call.bind(M);for(var l in e)if(v(e,l)){var i=void 0;try{if(typeof e[l]!="function"){var T=Error((n||"React class")+": "+t+" type `"+l+"` is invalid; it must be a function, usually from the `prop-types` package, but received `"+typeof e[l]+"`.This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`.");throw T.name="Invariant Violation",T}i=e[l](r,l,n,t,null,"SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED")}catch(E){i=E}i&&!(i instanceof Error)&&(q(d),S("%s: type specification of %s `%s` is invalid; the type checker function must return `null` or an `Error` but returned a %s. You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument).",n||"React class",t,l,typeof i),q(null)),i instanceof Error&&!(i.message in ge)&&(ge[i.message]=!0,q(d),S("Failed %s type: %s",t,i.message),q(null))}}}var Ze=Array.isArray;function Z(e){return Ze(e)}function Qe(e){{var r=typeof Symbol=="function"&&Symbol.toStringTag,t=r&&e[Symbol.toStringTag]||e.constructor.name||"Object";return t}}function er(e){try{return be(e),!1}catch{return!0}}function be(e){return""+e}function he(e){if(er(e))return S("The provided key is an unsupported type %s. This value must be coerced to a string before before using it here.",Qe(e)),be(e)}var Ee=D.ReactCurrentOwner,rr={key:!0,ref:!0,__self:!0,__source:!0},me,Re;function tr(e){if(M.call(e,"ref")){var r=Object.getOwnPropertyDescriptor(e,"ref").get;if(r&&r.isReactWarning)return!1}return e.ref!==void 0}function nr(e){if(M.call(e,"key")){var r=Object.getOwnPropertyDescriptor(e,"key").get;if(r&&r.isReactWarning)return!1}return e.key!==void 0}function ar(e,r){typeof e.ref=="string"&&Ee.current}function or(e,r){{var t=function(){me||(me=!0,S("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)",r))};t.isReactWarning=!0,Object.defineProperty(e,"key",{get:t,configurable:!0})}}function ir(e,r){{var t=function(){Re||(Re=!0,S("%s: `ref` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)",r))};t.isReactWarning=!0,Object.defineProperty(e,"ref",{get:t,configurable:!0})}}var ur=function(e,r,t,n,d,v,l){var i={$$typeof:a,type:e,key:r,ref:t,props:l,_owner:v};return i._store={},Object.defineProperty(i._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:!1}),Object.defineProperty(i,"_self",{configurable:!1,enumerable:!1,writable:!1,value:n}),Object.defineProperty(i,"_source",{configurable:!1,enumerable:!1,writable:!1,value:d}),Object.freeze&&(Object.freeze(i.props),Object.freeze(i)),i};function sr(e,r,t,n,d){{var v,l={},i=null,T=null;t!==void 0&&(he(t),i=""+t),nr(r)&&(he(r.key),i=""+r.key),tr(r)&&(T=r.ref,ar(r,d));for(v in r)M.call(r,v)&&!rr.hasOwnProperty(v)&&(l[v]=r[v]);if(e&&e.defaultProps){var E=e.defaultProps;for(v in E)l[v]===void 0&&(l[v]=E[v])}if(i||T){var m=typeof e=="function"?e.displayName||e.name||"Unknown":e;i&&or(l,m),T&&ir(l,m)}return ur(e,i,T,d,n,Ee.current,l)}}var Q=D.ReactCurrentOwner,_e=D.ReactDebugCurrentFrame;function I(e){if(e){var r=e._owner,t=J(e.type,e._source,r?r.type:null);_e.setExtraStackFrame(t)}else _e.setExtraStackFrame(null)}var ee;ee=!1;function re(e){return typeof e=="object"&&e!==null&&e.$$typeof===a}function Se(){{if(Q.current){var e=A(Q.current.type);if(e)return`
|
|
6
8
|
|
|
7
|
-
Check the render method of \``+e+"`."}return""}}function
|
|
9
|
+
Check the render method of \``+e+"`."}return""}}function cr(e){return""}var Te={};function lr(e){{var r=Se();if(!r){var t=typeof e=="string"?e:e.displayName||e.name;t&&(r=`
|
|
8
10
|
|
|
9
|
-
Check the top-level render call using <`+t+">.")}return r}}function
|
|
11
|
+
Check the top-level render call using <`+t+">.")}return r}}function we(e,r){{if(!e._store||e._store.validated||e.key!=null)return;e._store.validated=!0;var t=lr(r);if(Te[t])return;Te[t]=!0;var n="";e&&e._owner&&e._owner!==Q.current&&(n=" It was passed a child from "+A(e._owner.type)+"."),I(e),S('Each child in a list should have a unique "key" prop.%s%s See https://reactjs.org/link/warning-keys for more information.',t,n),I(null)}}function Pe(e,r){{if(typeof e!="object")return;if(Z(e))for(var t=0;t<e.length;t++){var n=e[t];re(n)&&we(n,r)}else if(re(e))e._store&&(e._store.validated=!0);else if(e){var d=k(e);if(typeof d=="function"&&d!==e.entries)for(var v=d.call(e),l;!(l=v.next()).done;)re(l.value)&&we(l.value,r)}}}function fr(e){{var r=e.type;if(r==null||typeof r=="string")return;var t;if(typeof r=="function")t=r.propTypes;else if(typeof r=="object"&&(r.$$typeof===g||r.$$typeof===_))t=r.propTypes;else return;if(t){var n=A(r);He(t,e.props,"prop",n,e)}else if(r.PropTypes!==void 0&&!ee){ee=!0;var d=A(r);S("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?",d||"Unknown")}typeof r.getDefaultProps=="function"&&!r.getDefaultProps.isReactClassApproved&&S("getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead.")}}function dr(e){{for(var r=Object.keys(e.props),t=0;t<r.length;t++){var n=r[t];if(n!=="children"&&n!=="key"){I(e),S("Invalid prop `%s` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.",n),I(null);break}}e.ref!==null&&(I(e),S("Invalid attribute `ref` supplied to `React.Fragment`."),I(null))}}var Ce={};function Oe(e,r,t,n,d,v){{var l=Je(e);if(!l){var i="";(e===void 0||typeof e=="object"&&e!==null&&Object.keys(e).length===0)&&(i+=" You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.");var T=cr();T?i+=T:i+=Se();var E;e===null?E="null":Z(e)?E="array":e!==void 0&&e.$$typeof===a?(E="<"+(A(e.type)||"Unknown")+" />",i=" Did you accidentally export a JSX literal instead of a component?"):E=typeof e,S("React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s",E,i)}var m=sr(e,r,t,d,v);if(m==null)return m;if(l){var x=r.children;if(x!==void 0)if(n)if(Z(x)){for(var $=0;$<x.length;$++)Pe(x[$],e);Object.freeze&&Object.freeze(x)}else S("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else Pe(x,e)}if(M.call(r,"key")){var V=A(e),P=Object.keys(r).filter(function(hr){return hr!=="key"}),te=P.length>0?"{key: someKey, "+P.join(": ..., ")+": ...}":"{key: someKey}";if(!Ce[V+te]){var br=P.length>0?"{"+P.join(": ..., ")+": ...}":"{}";S(`A props object containing a "key" prop is being spread into JSX:
|
|
10
12
|
let props = %s;
|
|
11
13
|
<%s {...props} />
|
|
12
14
|
React keys must be passed directly to JSX without using spread:
|
|
13
15
|
let props = %s;
|
|
14
|
-
<%s key={someKey} {...props} />`,
|
|
16
|
+
<%s key={someKey} {...props} />`,te,V,br,V),Ce[V+te]=!0}}return e===s?dr(m):fr(m),m}}function vr(e,r,t){return Oe(e,r,t,!0)}function pr(e,r,t){return Oe(e,r,t,!1)}var gr=pr,yr=vr;L.Fragment=s,L.jsx=gr,L.jsxs=yr})()),L}var ke;function Pr(){return ke||(ke=1,process.env.NODE_ENV==="production"?z.exports=Tr():z.exports=wr()),z.exports}var Cr=Pr();const Ie=j.createContext({stateVocab:{},setStateVocab:()=>{}});function Or(){return j.useContext(Ie)}const xr=o=>{const{children:a,verbose:u}=o,[s,c]=j.useState({});return u&&Sr(s),Cr.jsx(Ie.Provider,{value:{stateVocab:s,setStateVocab:c},children:a})},B=(o,a)=>u=>{const s={...u};return mr(s,o,a),s},ne=(o,a)=>typeof o=="function"?o():o??a,De=o=>{const{serialized:a,defaultValue:u,superDefaultValue:s,deserialize:c}=o;if(a===null){const y=ne(u,s);return typeof y>"u"?void 0:y}return c(a)};function jr(o={}){const{storage:a,serialize:u=JSON.stringify,deserialize:s=JSON.parse}=o,c=o.defaultValue,y=o.bidirectional;return{[Ve]:!0,[K]:"",useState(h,p){p??={};const g=_r(p.onSet??(()=>{}),p.delayedSet,[]),b=Or(),f=this[K],_=j.useMemo(()=>{if(!a)return;const O=a.getItem(f);return De({serialized:O,defaultValue:h,superDefaultValue:c,deserialize:s})},[f]),C=j.useMemo(()=>Er(b.stateVocab,f,_??ne(h,c)),[b.stateVocab,_,f]);j.useEffect(()=>{!a||typeof _>"u"||b.setStateVocab(B(f,_))},[_]),j.useEffect(()=>{if(!p.bidirectional&&!y)return;const O=R=>{if(R.key!==f)return;const k=R.newValue,D=De({serialized:k,defaultValue:h,superDefaultValue:c,deserialize:s});b.setStateVocab(B(f,D)),g(D,w.current),w.current=D};return window.addEventListener("storage",O),()=>window.removeEventListener("storage",O)},[f,p.bidirectional,y]);const w=j.useRef(C);return[C,function(R){const k=typeof R=="function"?R(w.current):R;b.setStateVocab(B(f,k)),g(k,w.current),a&&a.setItem(f,u(k)),w.current=k},function(){const R=ne(h,c);if(typeof R>"u"){a?.removeItem(f);return}b.setStateVocab(B(f,R)),g(R,w.current),w.current=R,a&&a.setItem(f,u(R))}]},toString(){return this[K]}}}const Ae=new WeakMap,Fe=new WeakMap;function $e(o,a=""){let u=Ae.get(o);u||(u=new Map,Ae.set(o,u));const s=u.get(a);if(s)return s;const c=new Proxy(o,{get(y,h){const p=y[h],g=a?`${a}.${String(h)}`:String(h);if(p&&typeof p=="object"&&Ve in p){const b=p;let f=Fe.get(b);f||(f=new Map,Fe.set(b,f));const _=f.get(g);if(_)return _;const C=Reflect.ownKeys(b).filter(R=>typeof b[R]=="function"),w=Object.fromEntries(C.map(R=>[R,(...k)=>b[R].call({...b,[K]:g},...k)])),O={...b,...w};return f.set(g,O),O}return p&&typeof p=="object"?$e(p,g):p}});return u.set(a,c),c}function kr(o){return $e(o)}exports.StorageProvider=xr;exports.defineState=jr;exports.setupStorage=kr;
|