UNPKG

@qtpy/state-management-observable

Version:

1. [Основная идея и архитектура](#основная-идея-и-архитектура) 1.1. [Прозрачная реактивность](#прозрачная-реактивность) 1.2. [Поддержка типизированных строковых ключей](#поддержка-типизированных-строковых-ключей) 1.3. [Подписки с «гранул

2 lines (1 loc) 6.63 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function E(t){const e=t.match(/\((.*?)\)/);return e?e[1].split(/\s*,\s*/).filter(Boolean):[]}function N(t,e){const r=e.toString().trim(),n=E(r),i=r.match(/^\s*(?:\([^)]*\)|[a-zA-Z_$][\w$]*)\s*=>\s*([\s\S]+)$/);if(!i)throw new Error("Invalid function format");let c=i[1].trim();c.startsWith("(")&&c.endsWith(")")&&(c=c.slice(1,-1).trim());let s=c.replace(/\s+/g,"");const a=/\[([^\[\]]+),([^\[\]]+)\]/;for(;a.test(s);)s=s.replace(/\[([^\[\]]+),([^\[\]]+)\]/g,"[$1][$2]");if(n.length<2){let l=s.replace(/\[['"]([^[\]]+)['"]\]/g,".$1").replace(/\[([^[\]]+)\]/g,".$1");return l.startsWith(".")&&(l=l.slice(1)),l}const o=n[1],p=o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),M=new RegExp(`${p}\\(\\s*([^\\)]+)\\s*\\)`),A=new RegExp(`${p}\\(\\s*([^\\)]+)\\s*\\)`,"g");if(!M.test(s)){let l=s.replace(/\[['"]([^[\]]+)['"]\]/g,".$1").replace(/\[([^[\]]+)\]/g,".$1");return l.startsWith(".")&&(l=l.slice(1)),l}const P=[];let W;for(;(W=A.exec(s))!==null;)P.push(W[1]);const _=[];let D=0;function z(l){const u=P[D];return _.push([`${o}(${u})`,l]),D++,l}try{e(t,z)}catch{}const x={};for(const[l,u]of _){const g=l.slice(o.length+1,-1);x[g]=u}let d=s.replace(A,(l,u)=>{const g=x[u];return g===void 0?"undefined":String(g)});d=d.replace(/\[['"]([^[\]]+)['"]\]/g,(l,u)=>`.${u}`).replace(/\[([^[\]]+)\]/g,(l,u)=>`.${u}`);const S=d.replace(/\.\./g,".");return S.startsWith(".")?S.slice(1):S}function H(t){if(!(typeof t=="string")&&!(typeof t=="function")){const n=t===null?"null":typeof t,i=typeof t=="object"?JSON.stringify(Object.keys(t)):String(t);throw new Error(`[store] Ожидался путь (строка или функция-доступ), получено: ${n} (${i}).`)}}function R(t,e){let r;typeof e=="string"?r=e:r=N(t,e);let n="$";if(typeof e!="string"){const s=e.toString().trim();n=E(s)[0]||"$"}const i=`${n}.`,c=r.indexOf(i);return c>=0?r.slice(c+i.length):r}function T(t,e){return Array.isArray(t)?t.map(r=>T(r,e)).filter(r=>r!=="").join("."):typeof t=="function"?R(e,t):t==null?"":String(t)}function j(t,e){if(t===e)return!0;if(t==null||e==null)return t===e;if(typeof t!=typeof e)return!1;if(t instanceof Date&&e instanceof Date)return t.getTime()===e.getTime();if(typeof t!="object")return!1;const r=Object.keys(t),n=Object.keys(e);if(r.length!==n.length)return!1;for(let i=0;i<r.length;i++){const c=r[i];if(!Object.prototype.hasOwnProperty.call(e,c)||t[c]!==e[c])return!1}return!0}function O(t){return t.split(".").map(e=>/^\d+$/.test(e)?Number(e):e)}function B(t,e=[]){switch(t){case"push":case"pop":case"splice":case"shift":case"unshift":case"sort":case"reverse":return!e.length||e.includes(t)}return!1}function f(t,e){return t===void 0?!1:e==null?void 0:e()}function y(t,e,r,n,i){typeof e=="object"&&e!==null&&t?f(e,()=>t.set(e,r)):i&&n.set(i,r)}function m(t,e,r,n){if(typeof e=="object"&&e!==null)return f(e,()=>t.get(e));if(n)return r==null?void 0:r.get(n)}function q(t,e,r,n){typeof e=="object"&&e!==null?f(e,()=>t.delete(e)):n&&r.delete(n)}function G(t){try{if(t==null||typeof t!="object")return String(t);const e=JSON.stringify(t);let r=5381;for(let n=0;n<e.length;n++){const i=e.charCodeAt(n);r=((r<<5)+r^i)>>>0}return(r>>>0).toString(16)}catch{return!1}}function U(){return`${Math.floor(performance.now()).toString(36)}-${Math.random().toString(36).slice(2,6)}`}const k=()=>typeof window>"u";function F(t,e="ssrStoreId_default"){const r=t;let n=Promise.resolve();const i=r.asyncUpdate;function c(s){return(...a)=>{const o=n.then(()=>s(...a));return n=o.catch(()=>{}),o}}return r.updateSSR=c(i),r.updateSSR.quiet=c(i.quiet),r.getIsSSR=()=>k(),r.getSSRStoreId=()=>e,r.snapshot=async()=>{try{return await n,t.getRawStore()}catch(s){throw console.error("Failed to create snapshot:",s),s}},r.getSerializedStore=async s=>{const a=await r.snapshot(),o=JSON.stringify(a);return{window:`window['${e}'] = ${o};`,scriptTag:`<script id="${e}" type="application/json">${o}<\/script>`,serializedData:o}[s]},r.hydrate=()=>{if(!r.getIsSSR()&&window[e]){const s=document.getElementById(e);r.setRawStore(window[e]),delete window[e],s==null||s.remove()}},r.hydrateWithDocument=(s=0,a)=>{r.getIsSSR()||(window.onload=()=>{setTimeout(()=>{r.hydrate(),a==null||a()},s)})},r}function J(t,e){const r=O(e);let n=t;for(const i of r){if(n==null||typeof n!="object")return!1;n=n[i]}return!0}function V(t,e){if(!e)return t;const r=O(e);let n=t;for(const i of r){if(n==null)return;n=n[i]}return n}const v={array:["push","pop","shift","unshift","splice","sort","reverse","fill","copyWithin"],object:["assign","defineProperty","defineProperties"]};class w extends Array{constructor(...e){super(...e)}}function C(t,e){var r;return!Array.isArray(t)||(r=m(e,t))!=null&&r._isWrapped||(v.array.forEach(n=>{w.prototype[n]=function(...i){return f(this,()=>{y(e,this,{_prevSignature:"CHANGED_METHOD",_mutated:!0,_isWrapped:!0})}),Array.prototype[n].apply(this,i)}}),Object.setPrototypeOf(t,w.prototype),y(e,t,{_isWrapped:!0})),t}class h extends Object{constructor(...e){super(...e)}}function I(t,e){var r;return typeof t!="object"||t===null||(r=m(e,t))!=null&&r._isWrapped||(v.object.forEach(n=>{n in h.prototype||(h.prototype[n]=function(...i){return f(this,()=>{y(e,this,{_prevSignature:"CHANGED_METHOD",_mutated:!0,_isWrapped:!0})}),Object[n](this,...i)})}),Object.setPrototypeOf(t,h.prototype),y(e,t,{_isWrapped:!0})),t}function Z(t,e,r,n){let i=t;return Array.isArray(t)?i=C(t,n):t&&typeof t=="object"&&(i=I(t,n)),i!==t&&e&&r!=null&&(e[r]=i),i}function*$(t,e=!0,r=new WeakSet,n="",i=null,c=null){if(!(t==null||t==null||typeof t!="object"||r.has(t)))if(r.add(t),yield{node:t,path:n,parent:i,key:c},Array.isArray(t))for(let s=t.length-1;s>=0;s--){const a=n?`${n}.${s}`:String(s);(e||t[s]&&typeof t[s]=="object")&&(yield{node:t[s],path:a,parent:t,key:s},t[s]&&typeof t[s]=="object"&&(yield*$(t[s],e,r,a,t,s)))}else{const s=Object.keys(t);for(let a=s.length-1;a>=0;a--){const o=s[a],p=n?`${n}.${o}`:o;(e||t[o]&&typeof t[o]=="object")&&(yield{node:t[o],path:p,parent:t,key:o},t[o]&&typeof t[o]=="object"&&(yield*$(t[o],e,r,p,t,o)))}}}exports.WrapArray=w;exports.WrapObject=h;exports.calculateSnapshotHash=G;exports.deleteMetaData=q;exports.getByPath=V;exports.getCheckIsSSR=k;exports.getMetaData=m;exports.getRandomId=U;exports.getStringOfObject=N;exports.getStringPath=R;exports.isArrayMethod=B;exports.isPathValid=J;exports.iterateObjectTree=$;exports.normalizeCacheKey=T;exports.setMetaData=y;exports.shallowEqual=j;exports.splitPath=O;exports.ssrStore=F;exports.validatePath=H;exports.withMetaSupport=f;exports.wrapArrayMethods=C;exports.wrapNode=Z;exports.wrapObjectMethods=I;