object-observer
Advanced tools
Comparing version 5.1.6 to 5.1.7
@@ -49,11 +49,11 @@ export { Observable, ObjectObserver }; | ||
}, | ||
prepareObject = (source, oMeta) => { | ||
prepareObject = (source, oMeta, visited) => { | ||
const target = {}; | ||
target[oMetaKey] = oMeta; | ||
for (const key in source) { | ||
target[key] = getObservedOf(source[key], key, oMeta); | ||
target[key] = getObservedOf(source[key], key, oMeta, visited); | ||
} | ||
return target; | ||
}, | ||
prepareArray = (source, oMeta) => { | ||
prepareArray = (source, oMeta, visited) => { | ||
let l = source.length; | ||
@@ -63,3 +63,3 @@ const target = new Array(l); | ||
for (let i = 0; i < l; i++) { | ||
target[i] = getObservedOf(source[i], i, oMeta); | ||
target[i] = getObservedOf(source[i], i, oMeta, visited); | ||
} | ||
@@ -169,7 +169,9 @@ return target; | ||
}, | ||
getObservedOf = (item, key, parent) => { | ||
if (typeof item !== 'object' || item === null) { | ||
getObservedOf = (item, key, parent, visited) => { | ||
if (visited !== undefined && visited.has(item)) { | ||
return null; | ||
} else if (typeof item !== 'object' || item === null) { | ||
return item; | ||
} else if (Array.isArray(item)) { | ||
return new ArrayOMeta({ target: item, ownKey: key, parent: parent }).proxy; | ||
return new ArrayOMeta({ target: item, ownKey: key, parent: parent, visited }).proxy; | ||
} else if (ArrayBuffer.isView(item)) { | ||
@@ -180,3 +182,3 @@ return new TypedArrayOMeta({ target: item, ownKey: key, parent: parent }).proxy; | ||
} else { | ||
return new ObjectOMeta({ target: item, ownKey: key, parent: parent }).proxy; | ||
return new ObjectOMeta({ target: item, ownKey: key, parent: parent, visited }).proxy; | ||
} | ||
@@ -519,3 +521,3 @@ }, | ||
constructor(properties, cloningFunction) { | ||
const { target, parent, ownKey } = properties; | ||
const { target, parent, ownKey, visited = new Set() } = properties; | ||
if (parent && ownKey !== undefined) { | ||
@@ -528,3 +530,5 @@ this.parent = parent; | ||
} | ||
const targetClone = cloningFunction(target, this); | ||
visited.add(target); | ||
const targetClone = cloningFunction(target, this, visited); | ||
visited.delete(target); | ||
this.observers = []; | ||
@@ -531,0 +535,0 @@ this.revocable = Proxy.revocable(targetClone, this); |
@@ -1,2 +0,2 @@ | ||
export{v as Observable,te as ObjectObserver};const m="insert",x="update",S="delete",R="reverse",F="shuffle",c=Symbol.for("object-observer-meta-key-0"),Y={async:1},I=o=>{if(!o||typeof o!="object")return null;const t={},e=[];for(const[r,n]of Object.entries(o))if(r==="path"){if(typeof n!="string"||n==="")throw new Error('"path" option, if/when provided, MUST be a non-empty string');t[r]=n}else if(r==="pathsOf"){if(o.path)throw new Error('"pathsOf" option MAY NOT be specified together with "path" option');if(typeof n!="string")throw new Error('"pathsOf" option, if/when provided, MUST be a string (MAY be empty)');t[r]=o.pathsOf.split(".").filter(Boolean)}else if(r==="pathsFrom"){if(o.path||o.pathsOf)throw new Error('"pathsFrom" option MAY NOT be specified together with "path"/"pathsOf" option/s');if(typeof n!="string"||n==="")throw new Error('"pathsFrom" option, if/when provided, MUST be a non-empty string');t[r]=n}else e.push(r);if(e.length)throw new Error(`'${e.join(", ")}' is/are not a valid observer option/s`);return t},B=(o,t)=>{const e={};e[c]=t;for(const r in o)e[r]=g(o[r],r,t);return e},D=(o,t)=>{let e=o.length;const r=new Array(e);r[c]=t;for(let n=0;n<e;n++)r[n]=g(o[n],n,t);return r},z=(o,t)=>(o[c]=t,o),q=(o,t)=>{if(o===null)return t;let e=t;if(o.path){const r=o.path;e=t.filter(n=>n.path.join(".")===r)}else if(o.pathsOf){const r=o.pathsOf,n=r.join(".");e=t.filter(i=>(i.path.length===r.length+1||i.path.length===r.length&&(i.type===R||i.type===F))&&i.path.join(".").startsWith(n))}else if(o.pathsFrom){const r=o.pathsFrom;e=t.filter(n=>n.path.join(".").startsWith(r))}return e},P=(o,t)=>{try{o(t)}catch(e){console.error(`failed to notify listener ${o} with ${t}`,e)}},H=function(){const t=this.batches;this.batches=[];for(const[e,r]of t)P(e,r)},y=(o,t)=>{let e=o,r,n,i,l,h,s;const u=t.length;do{for(r=e.options.async,n=e.observers,s=n.length;s--;)if([i,l]=n[s],h=q(l,t),h.length)if(r){e.batches.length===0&&queueMicrotask(H.bind(e));let a;for(const p of e.batches)if(p[0]===i){a=p;break}a||(a=[i,[]],e.batches.push(a)),Array.prototype.push.apply(a[1],h)}else P(i,h);const f=e.parent;if(f){for(let a=0;a<u;a++){const p=t[a];t[a]=new b(p.type,[e.ownKey,...p.path],p.value,p.oldValue,p.object)}e=f}else e=null}while(e)},g=(o,t,e)=>typeof o!="object"||o===null?o:Array.isArray(o)?new $({target:o,ownKey:t,parent:e}).proxy:ArrayBuffer.isView(o)?new N({target:o,ownKey:t,parent:e}).proxy:o instanceof Date?o:new W({target:o,ownKey:t,parent:e}).proxy,G=function(){const t=this[c],e=t.target,r=e.length-1;let n=e.pop();if(n&&typeof n=="object"){const l=n[c];l&&(n=l.detach())}const i=[new b(S,[r],void 0,n,this)];return y(t,i),n},J=function(){const t=this[c],e=t.target,r=arguments.length,n=new Array(r),i=e.length;for(let s=0;s<r;s++)n[s]=g(arguments[s],i+s,t);const l=Reflect.apply(e.push,e,n),h=[];for(let s=i,u=e.length;s<u;s++)h[s-i]=new b(m,[s],e[s],void 0,this);return y(t,h),l},Q=function(){const t=this[c],e=t.target;let r,n,i,l,h;for(r=e.shift(),r&&typeof r=="object"&&(h=r[c],h&&(r=h.detach())),n=0,i=e.length;n<i;n++)l=e[n],l&&typeof l=="object"&&(h=l[c],h&&(h.ownKey=n));const s=[new b(S,[0],void 0,r,this)];return y(t,s),r},X=function(){const t=this[c],e=t.target,r=arguments.length,n=new Array(r);for(let s=0;s<r;s++)n[s]=g(arguments[s],s,t);const i=Reflect.apply(e.unshift,e,n);for(let s=0,u=e.length,f;s<u;s++)if(f=e[s],f&&typeof f=="object"){const a=f[c];a&&(a.ownKey=s)}const l=n.length,h=new Array(l);for(let s=0;s<l;s++)h[s]=new b(m,[s],e[s],void 0,this);return y(t,h),i},L=function(){const t=this[c],e=t.target;let r,n,i;for(e.reverse(),r=0,n=e.length;r<n;r++)if(i=e[r],i&&typeof i=="object"){const h=i[c];h&&(h.ownKey=r)}const l=[new b(R,[],void 0,void 0,this)];return y(t,l),this},C=function(t){const e=this[c],r=e.target;let n,i,l;for(r.sort(t),n=0,i=r.length;n<i;n++)if(l=r[n],l&&typeof l=="object"){const s=l[c];s&&(s.ownKey=n)}const h=[new b(F,[],void 0,void 0,this)];return y(e,h),this},V=function(t,e,r){const n=this[c],i=n.target,l=[],h=i.length,s=i.slice(0);if(e=e===void 0?0:e<0?Math.max(h+e,0):Math.min(e,h),r=r===void 0?h:r<0?Math.max(h+r,0):Math.min(r,h),e<h&&r>e){i.fill(t,e,r);let u;for(let f=e,a,p;f<r;f++)a=i[f],i[f]=g(a,f,n),f in s?(p=s[f],p&&typeof p=="object"&&(u=p[c],u&&(p=u.detach())),l.push(new b(x,[f],i[f],p,this))):l.push(new b(m,[f],i[f],void 0,this));y(n,l)}return this},U=function(t,e,r){const n=this[c],i=n.target,l=i.length;t=t<0?Math.max(l+t,0):t,e=e===void 0?0:e<0?Math.max(l+e,0):Math.min(e,l),r=r===void 0?l:r<0?Math.max(l+r,0):Math.min(r,l);const h=Math.min(r-e,l-t);if(t<l&&t!==e&&h>0){const s=i.slice(0),u=[];i.copyWithin(t,e,r);for(let f=t,a,p,O;f<t+h;f++)a=i[f],a&&typeof a=="object"&&(a=g(a,f,n),i[f]=a),p=s[f],p&&typeof p=="object"&&(O=p[c],O&&(p=O.detach())),!(typeof a!="object"&&a===p)&&u.push(new b(x,[f],a,p,this));y(n,u)}return this},Z=function(){const t=this[c],e=t.target,r=arguments.length,n=new Array(r),i=e.length;for(let w=0;w<r;w++)n[w]=g(arguments[w],w,t);const l=r===0?0:n[0]<0?i+n[0]:n[0],h=r<2?i-l:n[1],s=Math.max(r-2,0),u=Reflect.apply(e.splice,e,n),f=e.length;let a;for(let w=0,E;w<f;w++)E=e[w],E&&typeof E=="object"&&(a=E[c],a&&(a.ownKey=w));let p,O,M;for(p=0,O=u.length;p<O;p++)M=u[p],M&&typeof M=="object"&&(a=M[c],a&&(u[p]=a.detach()));const A=[];let d;for(d=0;d<h;d++)d<s?A.push(new b(x,[l+d],e[l+d],u[d],this)):A.push(new b(S,[l+d],void 0,u[d],this));for(;d<s;d++)A.push(new b(m,[l+d],e[l+d],void 0,this));return y(t,A),u},_=function(t,e){const r=this[c],n=r.target,i=t.length,l=n.slice(0);e=e||0,n.set(t,e);const h=new Array(i);for(let s=e;s<i+e;s++)h[s-e]=new b(x,[s],n[s],l[s],this);y(r,h)},k={pop:G,push:J,shift:Q,unshift:X,reverse:L,sort:C,fill:V,copyWithin:U,splice:Z},ee={reverse:L,sort:C,fill:V,copyWithin:U,set:_};class b{constructor(t,e,r,n,i){this.type=t,this.path=e,this.value=r,this.oldValue=n,this.object=i}}class T{constructor(t,e){const{target:r,parent:n,ownKey:i}=t;n&&i!==void 0?(this.parent=n,this.ownKey=i):(this.parent=null,this.ownKey=null);const l=e(r,this);this.observers=[],this.revocable=Proxy.revocable(l,this),this.proxy=this.revocable.proxy,this.target=l,this.options=this.processOptions(t.options),this.options.async&&(this.batches=[])}processOptions(t){if(t){if(typeof t!="object")throw new Error(`Observable options if/when provided, MAY only be an object, got '${t}'`);const e=Object.keys(t).filter(r=>!(r in Y));if(e.length)throw new Error(`'${e.join(", ")}' is/are not a valid Observable option/s`);return Object.assign({},t)}else return{}}detach(){return this.parent=null,this.target}set(t,e,r){let n=t[e];if(r!==n){const i=g(r,e,this);if(t[e]=i,n&&typeof n=="object"){const h=n[c];h&&(n=h.detach())}const l=n===void 0?[new b(m,[e],i,void 0,this.proxy)]:[new b(x,[e],i,n,this.proxy)];y(this,l)}return!0}deleteProperty(t,e){let r=t[e];if(delete t[e],r&&typeof r=="object"){const i=r[c];i&&(r=i.detach())}const n=[new b(S,[e],void 0,r,this.proxy)];return y(this,n),!0}}class W extends T{constructor(t){super(t,B)}}class $ extends T{constructor(t){super(t,D)}get(t,e){return k[e]||t[e]}}class N extends T{constructor(t){super(t,z)}get(t,e){return ee[e]||t[e]}}const v=Object.freeze({from:(o,t)=>{if(!o||typeof o!="object")throw new Error("observable MAY ONLY be created from a non-null object");if(o[c])return o;if(Array.isArray(o))return new $({target:o,ownKey:null,parent:null,options:t}).proxy;if(ArrayBuffer.isView(o))return new N({target:o,ownKey:null,parent:null,options:t}).proxy;if(o instanceof Date)throw new Error(`${o} found to be one of a non-observable types`);return new W({target:o,ownKey:null,parent:null,options:t}).proxy},isObservable:o=>!!(o&&o[c]),observe:(o,t,e)=>{if(!v.isObservable(o))throw new Error("invalid observable parameter");if(typeof t!="function")throw new Error(`observer MUST be a function, got '${t}'`);const r=o[c].observers;r.some(n=>n[0]===t)?console.warn("observer may be bound to an observable only once; will NOT rebind"):r.push([t,I(e)])},unobserve:(o,...t)=>{if(!v.isObservable(o))throw new Error("invalid observable parameter");const e=o[c].observers;let r=e.length;if(!!r){if(!t.length){e.splice(0);return}for(;r;)t.indexOf(e[--r][0])>=0&&e.splice(r,1)}}}),K=Symbol("observer-key"),j=Symbol("targets-key");class te{constructor(t){this[K]=t,this[j]=new Set,Object.freeze(this)}observe(t,e){const r=v.from(t);return v.observe(r,this[K],e),this[j].add(r),r}unobserve(t){v.unobserve(t,this[K]),this[j].delete(t)}disconnect(){for(const t of this[j])v.unobserve(t,this[K]);this[j].clear()}} | ||
export{v as Observable,te as ObjectObserver};const m="insert",x="update",S="delete",R="reverse",F="shuffle",c=Symbol.for("object-observer-meta-key-0"),Y={async:1},I=o=>{if(!o||typeof o!="object")return null;const t={},e=[];for(const[r,n]of Object.entries(o))if(r==="path"){if(typeof n!="string"||n==="")throw new Error('"path" option, if/when provided, MUST be a non-empty string');t[r]=n}else if(r==="pathsOf"){if(o.path)throw new Error('"pathsOf" option MAY NOT be specified together with "path" option');if(typeof n!="string")throw new Error('"pathsOf" option, if/when provided, MUST be a string (MAY be empty)');t[r]=o.pathsOf.split(".").filter(Boolean)}else if(r==="pathsFrom"){if(o.path||o.pathsOf)throw new Error('"pathsFrom" option MAY NOT be specified together with "path"/"pathsOf" option/s');if(typeof n!="string"||n==="")throw new Error('"pathsFrom" option, if/when provided, MUST be a non-empty string');t[r]=n}else e.push(r);if(e.length)throw new Error(`'${e.join(", ")}' is/are not a valid observer option/s`);return t},B=(o,t,e)=>{const r={};r[c]=t;for(const n in o)r[n]=g(o[n],n,t,e);return r},D=(o,t,e)=>{let r=o.length;const n=new Array(r);n[c]=t;for(let i=0;i<r;i++)n[i]=g(o[i],i,t,e);return n},z=(o,t)=>(o[c]=t,o),q=(o,t)=>{if(o===null)return t;let e=t;if(o.path){const r=o.path;e=t.filter(n=>n.path.join(".")===r)}else if(o.pathsOf){const r=o.pathsOf,n=r.join(".");e=t.filter(i=>(i.path.length===r.length+1||i.path.length===r.length&&(i.type===R||i.type===F))&&i.path.join(".").startsWith(n))}else if(o.pathsFrom){const r=o.pathsFrom;e=t.filter(n=>n.path.join(".").startsWith(r))}return e},P=(o,t)=>{try{o(t)}catch(e){console.error(`failed to notify listener ${o} with ${t}`,e)}},H=function(){const t=this.batches;this.batches=[];for(const[e,r]of t)P(e,r)},y=(o,t)=>{let e=o,r,n,i,l,h,s;const u=t.length;do{for(r=e.options.async,n=e.observers,s=n.length;s--;)if([i,l]=n[s],h=q(l,t),h.length)if(r){e.batches.length===0&&queueMicrotask(H.bind(e));let a;for(const p of e.batches)if(p[0]===i){a=p;break}a||(a=[i,[]],e.batches.push(a)),Array.prototype.push.apply(a[1],h)}else P(i,h);const f=e.parent;if(f){for(let a=0;a<u;a++){const p=t[a];t[a]=new b(p.type,[e.ownKey,...p.path],p.value,p.oldValue,p.object)}e=f}else e=null}while(e)},g=(o,t,e,r)=>r!==void 0&&r.has(o)?null:typeof o!="object"||o===null?o:Array.isArray(o)?new $({target:o,ownKey:t,parent:e,visited:r}).proxy:ArrayBuffer.isView(o)?new N({target:o,ownKey:t,parent:e}).proxy:o instanceof Date?o:new W({target:o,ownKey:t,parent:e,visited:r}).proxy,G=function(){const t=this[c],e=t.target,r=e.length-1;let n=e.pop();if(n&&typeof n=="object"){const l=n[c];l&&(n=l.detach())}const i=[new b(S,[r],void 0,n,this)];return y(t,i),n},J=function(){const t=this[c],e=t.target,r=arguments.length,n=new Array(r),i=e.length;for(let s=0;s<r;s++)n[s]=g(arguments[s],i+s,t);const l=Reflect.apply(e.push,e,n),h=[];for(let s=i,u=e.length;s<u;s++)h[s-i]=new b(m,[s],e[s],void 0,this);return y(t,h),l},Q=function(){const t=this[c],e=t.target;let r,n,i,l,h;for(r=e.shift(),r&&typeof r=="object"&&(h=r[c],h&&(r=h.detach())),n=0,i=e.length;n<i;n++)l=e[n],l&&typeof l=="object"&&(h=l[c],h&&(h.ownKey=n));const s=[new b(S,[0],void 0,r,this)];return y(t,s),r},X=function(){const t=this[c],e=t.target,r=arguments.length,n=new Array(r);for(let s=0;s<r;s++)n[s]=g(arguments[s],s,t);const i=Reflect.apply(e.unshift,e,n);for(let s=0,u=e.length,f;s<u;s++)if(f=e[s],f&&typeof f=="object"){const a=f[c];a&&(a.ownKey=s)}const l=n.length,h=new Array(l);for(let s=0;s<l;s++)h[s]=new b(m,[s],e[s],void 0,this);return y(t,h),i},L=function(){const t=this[c],e=t.target;let r,n,i;for(e.reverse(),r=0,n=e.length;r<n;r++)if(i=e[r],i&&typeof i=="object"){const h=i[c];h&&(h.ownKey=r)}const l=[new b(R,[],void 0,void 0,this)];return y(t,l),this},C=function(t){const e=this[c],r=e.target;let n,i,l;for(r.sort(t),n=0,i=r.length;n<i;n++)if(l=r[n],l&&typeof l=="object"){const s=l[c];s&&(s.ownKey=n)}const h=[new b(F,[],void 0,void 0,this)];return y(e,h),this},V=function(t,e,r){const n=this[c],i=n.target,l=[],h=i.length,s=i.slice(0);if(e=e===void 0?0:e<0?Math.max(h+e,0):Math.min(e,h),r=r===void 0?h:r<0?Math.max(h+r,0):Math.min(r,h),e<h&&r>e){i.fill(t,e,r);let u;for(let f=e,a,p;f<r;f++)a=i[f],i[f]=g(a,f,n),f in s?(p=s[f],p&&typeof p=="object"&&(u=p[c],u&&(p=u.detach())),l.push(new b(x,[f],i[f],p,this))):l.push(new b(m,[f],i[f],void 0,this));y(n,l)}return this},U=function(t,e,r){const n=this[c],i=n.target,l=i.length;t=t<0?Math.max(l+t,0):t,e=e===void 0?0:e<0?Math.max(l+e,0):Math.min(e,l),r=r===void 0?l:r<0?Math.max(l+r,0):Math.min(r,l);const h=Math.min(r-e,l-t);if(t<l&&t!==e&&h>0){const s=i.slice(0),u=[];i.copyWithin(t,e,r);for(let f=t,a,p,O;f<t+h;f++)a=i[f],a&&typeof a=="object"&&(a=g(a,f,n),i[f]=a),p=s[f],p&&typeof p=="object"&&(O=p[c],O&&(p=O.detach())),!(typeof a!="object"&&a===p)&&u.push(new b(x,[f],a,p,this));y(n,u)}return this},Z=function(){const t=this[c],e=t.target,r=arguments.length,n=new Array(r),i=e.length;for(let w=0;w<r;w++)n[w]=g(arguments[w],w,t);const l=r===0?0:n[0]<0?i+n[0]:n[0],h=r<2?i-l:n[1],s=Math.max(r-2,0),u=Reflect.apply(e.splice,e,n),f=e.length;let a;for(let w=0,E;w<f;w++)E=e[w],E&&typeof E=="object"&&(a=E[c],a&&(a.ownKey=w));let p,O,M;for(p=0,O=u.length;p<O;p++)M=u[p],M&&typeof M=="object"&&(a=M[c],a&&(u[p]=a.detach()));const A=[];let d;for(d=0;d<h;d++)d<s?A.push(new b(x,[l+d],e[l+d],u[d],this)):A.push(new b(S,[l+d],void 0,u[d],this));for(;d<s;d++)A.push(new b(m,[l+d],e[l+d],void 0,this));return y(t,A),u},_=function(t,e){const r=this[c],n=r.target,i=t.length,l=n.slice(0);e=e||0,n.set(t,e);const h=new Array(i);for(let s=e;s<i+e;s++)h[s-e]=new b(x,[s],n[s],l[s],this);y(r,h)},k={pop:G,push:J,shift:Q,unshift:X,reverse:L,sort:C,fill:V,copyWithin:U,splice:Z},ee={reverse:L,sort:C,fill:V,copyWithin:U,set:_};class b{constructor(t,e,r,n,i){this.type=t,this.path=e,this.value=r,this.oldValue=n,this.object=i}}class T{constructor(t,e){const{target:r,parent:n,ownKey:i,visited:l=new Set}=t;n&&i!==void 0?(this.parent=n,this.ownKey=i):(this.parent=null,this.ownKey=null),l.add(r);const h=e(r,this,l);l.delete(r),this.observers=[],this.revocable=Proxy.revocable(h,this),this.proxy=this.revocable.proxy,this.target=h,this.options=this.processOptions(t.options),this.options.async&&(this.batches=[])}processOptions(t){if(t){if(typeof t!="object")throw new Error(`Observable options if/when provided, MAY only be an object, got '${t}'`);const e=Object.keys(t).filter(r=>!(r in Y));if(e.length)throw new Error(`'${e.join(", ")}' is/are not a valid Observable option/s`);return Object.assign({},t)}else return{}}detach(){return this.parent=null,this.target}set(t,e,r){let n=t[e];if(r!==n){const i=g(r,e,this);if(t[e]=i,n&&typeof n=="object"){const h=n[c];h&&(n=h.detach())}const l=n===void 0?[new b(m,[e],i,void 0,this.proxy)]:[new b(x,[e],i,n,this.proxy)];y(this,l)}return!0}deleteProperty(t,e){let r=t[e];if(delete t[e],r&&typeof r=="object"){const i=r[c];i&&(r=i.detach())}const n=[new b(S,[e],void 0,r,this.proxy)];return y(this,n),!0}}class W extends T{constructor(t){super(t,B)}}class $ extends T{constructor(t){super(t,D)}get(t,e){return k[e]||t[e]}}class N extends T{constructor(t){super(t,z)}get(t,e){return ee[e]||t[e]}}const v=Object.freeze({from:(o,t)=>{if(!o||typeof o!="object")throw new Error("observable MAY ONLY be created from a non-null object");if(o[c])return o;if(Array.isArray(o))return new $({target:o,ownKey:null,parent:null,options:t}).proxy;if(ArrayBuffer.isView(o))return new N({target:o,ownKey:null,parent:null,options:t}).proxy;if(o instanceof Date)throw new Error(`${o} found to be one of a non-observable types`);return new W({target:o,ownKey:null,parent:null,options:t}).proxy},isObservable:o=>!!(o&&o[c]),observe:(o,t,e)=>{if(!v.isObservable(o))throw new Error("invalid observable parameter");if(typeof t!="function")throw new Error(`observer MUST be a function, got '${t}'`);const r=o[c].observers;r.some(n=>n[0]===t)?console.warn("observer may be bound to an observable only once; will NOT rebind"):r.push([t,I(e)])},unobserve:(o,...t)=>{if(!v.isObservable(o))throw new Error("invalid observable parameter");const e=o[c].observers;let r=e.length;if(r){if(!t.length){e.splice(0);return}for(;r;)t.indexOf(e[--r][0])>=0&&e.splice(r,1)}}}),K=Symbol("observer-key"),j=Symbol("targets-key");class te{constructor(t){this[K]=t,this[j]=new Set,Object.freeze(this)}observe(t,e){const r=v.from(t);return v.observe(r,this[K],e),this[j].add(r),r}unobserve(t){v.unobserve(t,this[K]),this[j].delete(t)}disconnect(){for(const t of this[j])v.unobserve(t,this[K]);this[j].clear()}} | ||
//# sourceMappingURL=object-observer.min.js.map |
@@ -49,11 +49,11 @@ export { Observable, ObjectObserver }; | ||
}, | ||
prepareObject = (source, oMeta) => { | ||
prepareObject = (source, oMeta, visited) => { | ||
const target = {}; | ||
target[oMetaKey] = oMeta; | ||
for (const key in source) { | ||
target[key] = getObservedOf(source[key], key, oMeta); | ||
target[key] = getObservedOf(source[key], key, oMeta, visited); | ||
} | ||
return target; | ||
}, | ||
prepareArray = (source, oMeta) => { | ||
prepareArray = (source, oMeta, visited) => { | ||
let l = source.length; | ||
@@ -63,3 +63,3 @@ const target = new Array(l); | ||
for (let i = 0; i < l; i++) { | ||
target[i] = getObservedOf(source[i], i, oMeta); | ||
target[i] = getObservedOf(source[i], i, oMeta, visited); | ||
} | ||
@@ -169,7 +169,9 @@ return target; | ||
}, | ||
getObservedOf = (item, key, parent) => { | ||
if (typeof item !== 'object' || item === null) { | ||
getObservedOf = (item, key, parent, visited) => { | ||
if (visited !== undefined && visited.has(item)) { | ||
return null; | ||
} else if (typeof item !== 'object' || item === null) { | ||
return item; | ||
} else if (Array.isArray(item)) { | ||
return new ArrayOMeta({ target: item, ownKey: key, parent: parent }).proxy; | ||
return new ArrayOMeta({ target: item, ownKey: key, parent: parent, visited }).proxy; | ||
} else if (ArrayBuffer.isView(item)) { | ||
@@ -180,3 +182,3 @@ return new TypedArrayOMeta({ target: item, ownKey: key, parent: parent }).proxy; | ||
} else { | ||
return new ObjectOMeta({ target: item, ownKey: key, parent: parent }).proxy; | ||
return new ObjectOMeta({ target: item, ownKey: key, parent: parent, visited }).proxy; | ||
} | ||
@@ -519,3 +521,3 @@ }, | ||
constructor(properties, cloningFunction) { | ||
const { target, parent, ownKey } = properties; | ||
const { target, parent, ownKey, visited = new Set() } = properties; | ||
if (parent && ownKey !== undefined) { | ||
@@ -528,3 +530,5 @@ this.parent = parent; | ||
} | ||
const targetClone = cloningFunction(target, this); | ||
visited.add(target); | ||
const targetClone = cloningFunction(target, this, visited); | ||
visited.delete(target); | ||
this.observers = []; | ||
@@ -531,0 +535,0 @@ this.revocable = Proxy.revocable(targetClone, this); |
@@ -1,2 +0,2 @@ | ||
export{v as Observable,te as ObjectObserver};const m="insert",x="update",S="delete",R="reverse",F="shuffle",c=Symbol.for("object-observer-meta-key-0"),Y={async:1},I=o=>{if(!o||typeof o!="object")return null;const t={},e=[];for(const[r,n]of Object.entries(o))if(r==="path"){if(typeof n!="string"||n==="")throw new Error('"path" option, if/when provided, MUST be a non-empty string');t[r]=n}else if(r==="pathsOf"){if(o.path)throw new Error('"pathsOf" option MAY NOT be specified together with "path" option');if(typeof n!="string")throw new Error('"pathsOf" option, if/when provided, MUST be a string (MAY be empty)');t[r]=o.pathsOf.split(".").filter(Boolean)}else if(r==="pathsFrom"){if(o.path||o.pathsOf)throw new Error('"pathsFrom" option MAY NOT be specified together with "path"/"pathsOf" option/s');if(typeof n!="string"||n==="")throw new Error('"pathsFrom" option, if/when provided, MUST be a non-empty string');t[r]=n}else e.push(r);if(e.length)throw new Error(`'${e.join(", ")}' is/are not a valid observer option/s`);return t},B=(o,t)=>{const e={};e[c]=t;for(const r in o)e[r]=g(o[r],r,t);return e},D=(o,t)=>{let e=o.length;const r=new Array(e);r[c]=t;for(let n=0;n<e;n++)r[n]=g(o[n],n,t);return r},z=(o,t)=>(o[c]=t,o),q=(o,t)=>{if(o===null)return t;let e=t;if(o.path){const r=o.path;e=t.filter(n=>n.path.join(".")===r)}else if(o.pathsOf){const r=o.pathsOf,n=r.join(".");e=t.filter(i=>(i.path.length===r.length+1||i.path.length===r.length&&(i.type===R||i.type===F))&&i.path.join(".").startsWith(n))}else if(o.pathsFrom){const r=o.pathsFrom;e=t.filter(n=>n.path.join(".").startsWith(r))}return e},P=(o,t)=>{try{o(t)}catch(e){console.error(`failed to notify listener ${o} with ${t}`,e)}},H=function(){const t=this.batches;this.batches=[];for(const[e,r]of t)P(e,r)},y=(o,t)=>{let e=o,r,n,i,l,h,s;const u=t.length;do{for(r=e.options.async,n=e.observers,s=n.length;s--;)if([i,l]=n[s],h=q(l,t),h.length)if(r){e.batches.length===0&&queueMicrotask(H.bind(e));let a;for(const p of e.batches)if(p[0]===i){a=p;break}a||(a=[i,[]],e.batches.push(a)),Array.prototype.push.apply(a[1],h)}else P(i,h);const f=e.parent;if(f){for(let a=0;a<u;a++){const p=t[a];t[a]=new b(p.type,[e.ownKey,...p.path],p.value,p.oldValue,p.object)}e=f}else e=null}while(e)},g=(o,t,e)=>typeof o!="object"||o===null?o:Array.isArray(o)?new $({target:o,ownKey:t,parent:e}).proxy:ArrayBuffer.isView(o)?new N({target:o,ownKey:t,parent:e}).proxy:o instanceof Date?o:new W({target:o,ownKey:t,parent:e}).proxy,G=function(){const t=this[c],e=t.target,r=e.length-1;let n=e.pop();if(n&&typeof n=="object"){const l=n[c];l&&(n=l.detach())}const i=[new b(S,[r],void 0,n,this)];return y(t,i),n},J=function(){const t=this[c],e=t.target,r=arguments.length,n=new Array(r),i=e.length;for(let s=0;s<r;s++)n[s]=g(arguments[s],i+s,t);const l=Reflect.apply(e.push,e,n),h=[];for(let s=i,u=e.length;s<u;s++)h[s-i]=new b(m,[s],e[s],void 0,this);return y(t,h),l},Q=function(){const t=this[c],e=t.target;let r,n,i,l,h;for(r=e.shift(),r&&typeof r=="object"&&(h=r[c],h&&(r=h.detach())),n=0,i=e.length;n<i;n++)l=e[n],l&&typeof l=="object"&&(h=l[c],h&&(h.ownKey=n));const s=[new b(S,[0],void 0,r,this)];return y(t,s),r},X=function(){const t=this[c],e=t.target,r=arguments.length,n=new Array(r);for(let s=0;s<r;s++)n[s]=g(arguments[s],s,t);const i=Reflect.apply(e.unshift,e,n);for(let s=0,u=e.length,f;s<u;s++)if(f=e[s],f&&typeof f=="object"){const a=f[c];a&&(a.ownKey=s)}const l=n.length,h=new Array(l);for(let s=0;s<l;s++)h[s]=new b(m,[s],e[s],void 0,this);return y(t,h),i},L=function(){const t=this[c],e=t.target;let r,n,i;for(e.reverse(),r=0,n=e.length;r<n;r++)if(i=e[r],i&&typeof i=="object"){const h=i[c];h&&(h.ownKey=r)}const l=[new b(R,[],void 0,void 0,this)];return y(t,l),this},C=function(t){const e=this[c],r=e.target;let n,i,l;for(r.sort(t),n=0,i=r.length;n<i;n++)if(l=r[n],l&&typeof l=="object"){const s=l[c];s&&(s.ownKey=n)}const h=[new b(F,[],void 0,void 0,this)];return y(e,h),this},V=function(t,e,r){const n=this[c],i=n.target,l=[],h=i.length,s=i.slice(0);if(e=e===void 0?0:e<0?Math.max(h+e,0):Math.min(e,h),r=r===void 0?h:r<0?Math.max(h+r,0):Math.min(r,h),e<h&&r>e){i.fill(t,e,r);let u;for(let f=e,a,p;f<r;f++)a=i[f],i[f]=g(a,f,n),f in s?(p=s[f],p&&typeof p=="object"&&(u=p[c],u&&(p=u.detach())),l.push(new b(x,[f],i[f],p,this))):l.push(new b(m,[f],i[f],void 0,this));y(n,l)}return this},U=function(t,e,r){const n=this[c],i=n.target,l=i.length;t=t<0?Math.max(l+t,0):t,e=e===void 0?0:e<0?Math.max(l+e,0):Math.min(e,l),r=r===void 0?l:r<0?Math.max(l+r,0):Math.min(r,l);const h=Math.min(r-e,l-t);if(t<l&&t!==e&&h>0){const s=i.slice(0),u=[];i.copyWithin(t,e,r);for(let f=t,a,p,O;f<t+h;f++)a=i[f],a&&typeof a=="object"&&(a=g(a,f,n),i[f]=a),p=s[f],p&&typeof p=="object"&&(O=p[c],O&&(p=O.detach())),!(typeof a!="object"&&a===p)&&u.push(new b(x,[f],a,p,this));y(n,u)}return this},Z=function(){const t=this[c],e=t.target,r=arguments.length,n=new Array(r),i=e.length;for(let w=0;w<r;w++)n[w]=g(arguments[w],w,t);const l=r===0?0:n[0]<0?i+n[0]:n[0],h=r<2?i-l:n[1],s=Math.max(r-2,0),u=Reflect.apply(e.splice,e,n),f=e.length;let a;for(let w=0,E;w<f;w++)E=e[w],E&&typeof E=="object"&&(a=E[c],a&&(a.ownKey=w));let p,O,M;for(p=0,O=u.length;p<O;p++)M=u[p],M&&typeof M=="object"&&(a=M[c],a&&(u[p]=a.detach()));const A=[];let d;for(d=0;d<h;d++)d<s?A.push(new b(x,[l+d],e[l+d],u[d],this)):A.push(new b(S,[l+d],void 0,u[d],this));for(;d<s;d++)A.push(new b(m,[l+d],e[l+d],void 0,this));return y(t,A),u},_=function(t,e){const r=this[c],n=r.target,i=t.length,l=n.slice(0);e=e||0,n.set(t,e);const h=new Array(i);for(let s=e;s<i+e;s++)h[s-e]=new b(x,[s],n[s],l[s],this);y(r,h)},k={pop:G,push:J,shift:Q,unshift:X,reverse:L,sort:C,fill:V,copyWithin:U,splice:Z},ee={reverse:L,sort:C,fill:V,copyWithin:U,set:_};class b{constructor(t,e,r,n,i){this.type=t,this.path=e,this.value=r,this.oldValue=n,this.object=i}}class T{constructor(t,e){const{target:r,parent:n,ownKey:i}=t;n&&i!==void 0?(this.parent=n,this.ownKey=i):(this.parent=null,this.ownKey=null);const l=e(r,this);this.observers=[],this.revocable=Proxy.revocable(l,this),this.proxy=this.revocable.proxy,this.target=l,this.options=this.processOptions(t.options),this.options.async&&(this.batches=[])}processOptions(t){if(t){if(typeof t!="object")throw new Error(`Observable options if/when provided, MAY only be an object, got '${t}'`);const e=Object.keys(t).filter(r=>!(r in Y));if(e.length)throw new Error(`'${e.join(", ")}' is/are not a valid Observable option/s`);return Object.assign({},t)}else return{}}detach(){return this.parent=null,this.target}set(t,e,r){let n=t[e];if(r!==n){const i=g(r,e,this);if(t[e]=i,n&&typeof n=="object"){const h=n[c];h&&(n=h.detach())}const l=n===void 0?[new b(m,[e],i,void 0,this.proxy)]:[new b(x,[e],i,n,this.proxy)];y(this,l)}return!0}deleteProperty(t,e){let r=t[e];if(delete t[e],r&&typeof r=="object"){const i=r[c];i&&(r=i.detach())}const n=[new b(S,[e],void 0,r,this.proxy)];return y(this,n),!0}}class W extends T{constructor(t){super(t,B)}}class $ extends T{constructor(t){super(t,D)}get(t,e){return k[e]||t[e]}}class N extends T{constructor(t){super(t,z)}get(t,e){return ee[e]||t[e]}}const v=Object.freeze({from:(o,t)=>{if(!o||typeof o!="object")throw new Error("observable MAY ONLY be created from a non-null object");if(o[c])return o;if(Array.isArray(o))return new $({target:o,ownKey:null,parent:null,options:t}).proxy;if(ArrayBuffer.isView(o))return new N({target:o,ownKey:null,parent:null,options:t}).proxy;if(o instanceof Date)throw new Error(`${o} found to be one of a non-observable types`);return new W({target:o,ownKey:null,parent:null,options:t}).proxy},isObservable:o=>!!(o&&o[c]),observe:(o,t,e)=>{if(!v.isObservable(o))throw new Error("invalid observable parameter");if(typeof t!="function")throw new Error(`observer MUST be a function, got '${t}'`);const r=o[c].observers;r.some(n=>n[0]===t)?console.warn("observer may be bound to an observable only once; will NOT rebind"):r.push([t,I(e)])},unobserve:(o,...t)=>{if(!v.isObservable(o))throw new Error("invalid observable parameter");const e=o[c].observers;let r=e.length;if(!!r){if(!t.length){e.splice(0);return}for(;r;)t.indexOf(e[--r][0])>=0&&e.splice(r,1)}}}),K=Symbol("observer-key"),j=Symbol("targets-key");class te{constructor(t){this[K]=t,this[j]=new Set,Object.freeze(this)}observe(t,e){const r=v.from(t);return v.observe(r,this[K],e),this[j].add(r),r}unobserve(t){v.unobserve(t,this[K]),this[j].delete(t)}disconnect(){for(const t of this[j])v.unobserve(t,this[K]);this[j].clear()}} | ||
export{v as Observable,te as ObjectObserver};const m="insert",x="update",S="delete",R="reverse",F="shuffle",c=Symbol.for("object-observer-meta-key-0"),Y={async:1},I=o=>{if(!o||typeof o!="object")return null;const t={},e=[];for(const[r,n]of Object.entries(o))if(r==="path"){if(typeof n!="string"||n==="")throw new Error('"path" option, if/when provided, MUST be a non-empty string');t[r]=n}else if(r==="pathsOf"){if(o.path)throw new Error('"pathsOf" option MAY NOT be specified together with "path" option');if(typeof n!="string")throw new Error('"pathsOf" option, if/when provided, MUST be a string (MAY be empty)');t[r]=o.pathsOf.split(".").filter(Boolean)}else if(r==="pathsFrom"){if(o.path||o.pathsOf)throw new Error('"pathsFrom" option MAY NOT be specified together with "path"/"pathsOf" option/s');if(typeof n!="string"||n==="")throw new Error('"pathsFrom" option, if/when provided, MUST be a non-empty string');t[r]=n}else e.push(r);if(e.length)throw new Error(`'${e.join(", ")}' is/are not a valid observer option/s`);return t},B=(o,t,e)=>{const r={};r[c]=t;for(const n in o)r[n]=g(o[n],n,t,e);return r},D=(o,t,e)=>{let r=o.length;const n=new Array(r);n[c]=t;for(let i=0;i<r;i++)n[i]=g(o[i],i,t,e);return n},z=(o,t)=>(o[c]=t,o),q=(o,t)=>{if(o===null)return t;let e=t;if(o.path){const r=o.path;e=t.filter(n=>n.path.join(".")===r)}else if(o.pathsOf){const r=o.pathsOf,n=r.join(".");e=t.filter(i=>(i.path.length===r.length+1||i.path.length===r.length&&(i.type===R||i.type===F))&&i.path.join(".").startsWith(n))}else if(o.pathsFrom){const r=o.pathsFrom;e=t.filter(n=>n.path.join(".").startsWith(r))}return e},P=(o,t)=>{try{o(t)}catch(e){console.error(`failed to notify listener ${o} with ${t}`,e)}},H=function(){const t=this.batches;this.batches=[];for(const[e,r]of t)P(e,r)},y=(o,t)=>{let e=o,r,n,i,l,h,s;const u=t.length;do{for(r=e.options.async,n=e.observers,s=n.length;s--;)if([i,l]=n[s],h=q(l,t),h.length)if(r){e.batches.length===0&&queueMicrotask(H.bind(e));let a;for(const p of e.batches)if(p[0]===i){a=p;break}a||(a=[i,[]],e.batches.push(a)),Array.prototype.push.apply(a[1],h)}else P(i,h);const f=e.parent;if(f){for(let a=0;a<u;a++){const p=t[a];t[a]=new b(p.type,[e.ownKey,...p.path],p.value,p.oldValue,p.object)}e=f}else e=null}while(e)},g=(o,t,e,r)=>r!==void 0&&r.has(o)?null:typeof o!="object"||o===null?o:Array.isArray(o)?new $({target:o,ownKey:t,parent:e,visited:r}).proxy:ArrayBuffer.isView(o)?new N({target:o,ownKey:t,parent:e}).proxy:o instanceof Date?o:new W({target:o,ownKey:t,parent:e,visited:r}).proxy,G=function(){const t=this[c],e=t.target,r=e.length-1;let n=e.pop();if(n&&typeof n=="object"){const l=n[c];l&&(n=l.detach())}const i=[new b(S,[r],void 0,n,this)];return y(t,i),n},J=function(){const t=this[c],e=t.target,r=arguments.length,n=new Array(r),i=e.length;for(let s=0;s<r;s++)n[s]=g(arguments[s],i+s,t);const l=Reflect.apply(e.push,e,n),h=[];for(let s=i,u=e.length;s<u;s++)h[s-i]=new b(m,[s],e[s],void 0,this);return y(t,h),l},Q=function(){const t=this[c],e=t.target;let r,n,i,l,h;for(r=e.shift(),r&&typeof r=="object"&&(h=r[c],h&&(r=h.detach())),n=0,i=e.length;n<i;n++)l=e[n],l&&typeof l=="object"&&(h=l[c],h&&(h.ownKey=n));const s=[new b(S,[0],void 0,r,this)];return y(t,s),r},X=function(){const t=this[c],e=t.target,r=arguments.length,n=new Array(r);for(let s=0;s<r;s++)n[s]=g(arguments[s],s,t);const i=Reflect.apply(e.unshift,e,n);for(let s=0,u=e.length,f;s<u;s++)if(f=e[s],f&&typeof f=="object"){const a=f[c];a&&(a.ownKey=s)}const l=n.length,h=new Array(l);for(let s=0;s<l;s++)h[s]=new b(m,[s],e[s],void 0,this);return y(t,h),i},L=function(){const t=this[c],e=t.target;let r,n,i;for(e.reverse(),r=0,n=e.length;r<n;r++)if(i=e[r],i&&typeof i=="object"){const h=i[c];h&&(h.ownKey=r)}const l=[new b(R,[],void 0,void 0,this)];return y(t,l),this},C=function(t){const e=this[c],r=e.target;let n,i,l;for(r.sort(t),n=0,i=r.length;n<i;n++)if(l=r[n],l&&typeof l=="object"){const s=l[c];s&&(s.ownKey=n)}const h=[new b(F,[],void 0,void 0,this)];return y(e,h),this},V=function(t,e,r){const n=this[c],i=n.target,l=[],h=i.length,s=i.slice(0);if(e=e===void 0?0:e<0?Math.max(h+e,0):Math.min(e,h),r=r===void 0?h:r<0?Math.max(h+r,0):Math.min(r,h),e<h&&r>e){i.fill(t,e,r);let u;for(let f=e,a,p;f<r;f++)a=i[f],i[f]=g(a,f,n),f in s?(p=s[f],p&&typeof p=="object"&&(u=p[c],u&&(p=u.detach())),l.push(new b(x,[f],i[f],p,this))):l.push(new b(m,[f],i[f],void 0,this));y(n,l)}return this},U=function(t,e,r){const n=this[c],i=n.target,l=i.length;t=t<0?Math.max(l+t,0):t,e=e===void 0?0:e<0?Math.max(l+e,0):Math.min(e,l),r=r===void 0?l:r<0?Math.max(l+r,0):Math.min(r,l);const h=Math.min(r-e,l-t);if(t<l&&t!==e&&h>0){const s=i.slice(0),u=[];i.copyWithin(t,e,r);for(let f=t,a,p,O;f<t+h;f++)a=i[f],a&&typeof a=="object"&&(a=g(a,f,n),i[f]=a),p=s[f],p&&typeof p=="object"&&(O=p[c],O&&(p=O.detach())),!(typeof a!="object"&&a===p)&&u.push(new b(x,[f],a,p,this));y(n,u)}return this},Z=function(){const t=this[c],e=t.target,r=arguments.length,n=new Array(r),i=e.length;for(let w=0;w<r;w++)n[w]=g(arguments[w],w,t);const l=r===0?0:n[0]<0?i+n[0]:n[0],h=r<2?i-l:n[1],s=Math.max(r-2,0),u=Reflect.apply(e.splice,e,n),f=e.length;let a;for(let w=0,E;w<f;w++)E=e[w],E&&typeof E=="object"&&(a=E[c],a&&(a.ownKey=w));let p,O,M;for(p=0,O=u.length;p<O;p++)M=u[p],M&&typeof M=="object"&&(a=M[c],a&&(u[p]=a.detach()));const A=[];let d;for(d=0;d<h;d++)d<s?A.push(new b(x,[l+d],e[l+d],u[d],this)):A.push(new b(S,[l+d],void 0,u[d],this));for(;d<s;d++)A.push(new b(m,[l+d],e[l+d],void 0,this));return y(t,A),u},_=function(t,e){const r=this[c],n=r.target,i=t.length,l=n.slice(0);e=e||0,n.set(t,e);const h=new Array(i);for(let s=e;s<i+e;s++)h[s-e]=new b(x,[s],n[s],l[s],this);y(r,h)},k={pop:G,push:J,shift:Q,unshift:X,reverse:L,sort:C,fill:V,copyWithin:U,splice:Z},ee={reverse:L,sort:C,fill:V,copyWithin:U,set:_};class b{constructor(t,e,r,n,i){this.type=t,this.path=e,this.value=r,this.oldValue=n,this.object=i}}class T{constructor(t,e){const{target:r,parent:n,ownKey:i,visited:l=new Set}=t;n&&i!==void 0?(this.parent=n,this.ownKey=i):(this.parent=null,this.ownKey=null),l.add(r);const h=e(r,this,l);l.delete(r),this.observers=[],this.revocable=Proxy.revocable(h,this),this.proxy=this.revocable.proxy,this.target=h,this.options=this.processOptions(t.options),this.options.async&&(this.batches=[])}processOptions(t){if(t){if(typeof t!="object")throw new Error(`Observable options if/when provided, MAY only be an object, got '${t}'`);const e=Object.keys(t).filter(r=>!(r in Y));if(e.length)throw new Error(`'${e.join(", ")}' is/are not a valid Observable option/s`);return Object.assign({},t)}else return{}}detach(){return this.parent=null,this.target}set(t,e,r){let n=t[e];if(r!==n){const i=g(r,e,this);if(t[e]=i,n&&typeof n=="object"){const h=n[c];h&&(n=h.detach())}const l=n===void 0?[new b(m,[e],i,void 0,this.proxy)]:[new b(x,[e],i,n,this.proxy)];y(this,l)}return!0}deleteProperty(t,e){let r=t[e];if(delete t[e],r&&typeof r=="object"){const i=r[c];i&&(r=i.detach())}const n=[new b(S,[e],void 0,r,this.proxy)];return y(this,n),!0}}class W extends T{constructor(t){super(t,B)}}class $ extends T{constructor(t){super(t,D)}get(t,e){return k[e]||t[e]}}class N extends T{constructor(t){super(t,z)}get(t,e){return ee[e]||t[e]}}const v=Object.freeze({from:(o,t)=>{if(!o||typeof o!="object")throw new Error("observable MAY ONLY be created from a non-null object");if(o[c])return o;if(Array.isArray(o))return new $({target:o,ownKey:null,parent:null,options:t}).proxy;if(ArrayBuffer.isView(o))return new N({target:o,ownKey:null,parent:null,options:t}).proxy;if(o instanceof Date)throw new Error(`${o} found to be one of a non-observable types`);return new W({target:o,ownKey:null,parent:null,options:t}).proxy},isObservable:o=>!!(o&&o[c]),observe:(o,t,e)=>{if(!v.isObservable(o))throw new Error("invalid observable parameter");if(typeof t!="function")throw new Error(`observer MUST be a function, got '${t}'`);const r=o[c].observers;r.some(n=>n[0]===t)?console.warn("observer may be bound to an observable only once; will NOT rebind"):r.push([t,I(e)])},unobserve:(o,...t)=>{if(!v.isObservable(o))throw new Error("invalid observable parameter");const e=o[c].observers;let r=e.length;if(r){if(!t.length){e.splice(0);return}for(;r;)t.indexOf(e[--r][0])>=0&&e.splice(r,1)}}}),K=Symbol("observer-key"),j=Symbol("targets-key");class te{constructor(t){this[K]=t,this[j]=new Set,Object.freeze(this)}observe(t,e){const r=v.from(t);return v.observe(r,this[K],e),this[j].add(r),r}unobserve(t){v.unobserve(t,this[K]),this[j].delete(t)}disconnect(){for(const t of this[j])v.unobserve(t,this[K]);this[j].clear()}} | ||
//# sourceMappingURL=object-observer.min.js.map |
{ | ||
"name": "object-observer", | ||
"version": "5.1.6", | ||
"version": "5.1.7", | ||
"description": "object-observer utility provides simple means to (deeply) observe specified object/array changes; implemented via native Proxy; changes delivered in a synchronous way", | ||
@@ -69,7 +69,7 @@ "keywords": [ | ||
"devDependencies": { | ||
"chai": "^4.3.6", | ||
"eslint": "^8.24.0", | ||
"chai": "4.3.6", | ||
"eslint": "^8.35.0", | ||
"just-test": "^3.0.4", | ||
"esbuild": "^0.15.9" | ||
"esbuild": "^0.17.10" | ||
} | ||
} |
@@ -19,2 +19,3 @@ [![npm](https://img.shields.io/npm/v/object-observer.svg?label=npm%20object-observer)](https://www.npmjs.com/package/object-observer) | ||
- original objects are __cloned__ while turned into `Observable`s | ||
- circular references are nullified in the clone | ||
- __array__ specifics: | ||
@@ -50,3 +51,5 @@ - generic object-like mutations supported | ||
Use regular `npm install object-observer --save-prod` to use the library from your local environment: | ||
Use regular `npm install object-observer --save-prod` to use the library from your local environment. | ||
__ES__ module: | ||
```js | ||
@@ -56,3 +59,3 @@ import { Observable } from 'object-observer'; | ||
Since version 5.1.0 a CJS porting is also available, using this syntax: | ||
__CJS__ flavor: | ||
```js | ||
@@ -63,3 +66,3 @@ const { Observable } = require('object-observer'); | ||
A __CDN__ deployment is also available (AWS driven), so one can import it directly: | ||
__CDN__ (most suggested, when possible): | ||
```js | ||
@@ -69,3 +72,3 @@ import { Observable } from 'https://libs.gullerya.com/object-observer/x.y.z/object-observer.min.js'; | ||
> Note: replace the `x.y.z` with the desired version, one of the listed in the [changelog](docs/changelog.md). | ||
> Replace the `x.y.z` with the desired version, one of the listed in the [changelog](docs/changelog.md). | ||
@@ -86,3 +89,3 @@ CDN features: | ||
Additionally, from version 4.2.0 there is also 'DOM-like' API flavor - constructable `ObjectObserver`. | ||
There is also a 'DOM-like' API flavor - constructable `ObjectObserver`. | ||
This API is resonating with DOM's `MutationObserver`, `ResizeObserver` etc from the syntax perspective. | ||
@@ -92,3 +95,3 @@ Under the hood it uses the same `Observable` mechanics. | ||
Starting from 4.3.x `object-observer` is cross-instance operable. | ||
`object-observer` is cross-instance operable. | ||
Observables created by different instances of the library will still be detected correctly as such and handled correctly by any of the instances. | ||
@@ -95,0 +98,0 @@ |
{ | ||
"dist/cdn/object-observer.js": "sha512-DDBqQO8ifWBD1SALi8Nd5Qz63kAWiXPreuH4oC3ZPQXiYGRN/6HFoJDVST2W7sEIDuCNgW2ZhoS0DqyDPrWVbQ==", | ||
"dist/cdn/object-observer.min.js": "sha512-7F59dQzPQ4pcrBVGJT3kc8KVT4KpdGawfeMeBd90ZTF2QUwP80vavH1VJR2rULXJ3Pr16Aq7G2L9V5wvAofu8w==", | ||
"dist/cdn/object-observer.min.js.map": "sha512-5p9R3m5DhHefGdaVzAut5vmoRiU0yHKHTdr6uRIPgSinkrEWVfINxUrghhXTSQhF9Rw5gDRJ47JHwh5LmnqCwg==" | ||
"dist/cdn/object-observer.js": "sha512-zgQjYoQ8cWub9yNbYWf2VmyhEHdP3azk4W5GDEKaaf0rHGcWmbZt0U15xhH9mU3Qw6u3fJ1AmtNLbll5Sxf3mw==", | ||
"dist/cdn/object-observer.min.js": "sha512-i5RB2EenTNVz0pg/1j99SX02lUP+wM4au/V0PxVciQ+EgSrAq73ZkywZXzptzZ4phQXwYYDWHAG8TuTgE0bsdg==", | ||
"dist/cdn/object-observer.min.js.map": "sha512-nsx9+MeTpSdx+3srFxLgH7/HOsUCwJmbHN23/FnUVqqhqSQPyQXeXefQjZ+MrHUQ5Ixuv02gz8W8/Dnc8wcK7w==" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
148062
2047
258