object-observer
Advanced tools
Comparing version 4.2.4 to 4.3.0
@@ -9,3 +9,3 @@ export { Observable, ObjectObserver }; | ||
SHUFFLE = 'shuffle', | ||
oMetaKey = Symbol('observable-meta-key'), | ||
oMetaKey = Symbol.for('object-observer-meta-key-0'), | ||
validObservableOptionKeys = { async: 1 }, | ||
@@ -12,0 +12,0 @@ processObserveOptions = options => { |
@@ -1,1 +0,1 @@ | ||
export{A as Observable,S as ObjectObserver};const t="insert",e="update",o="delete",n=Symbol("observable-meta-key"),r={async:1},s={[n]:{value:null},observe:{value:function(t,e){if("function"!=typeof t)throw new Error(`observer MUST be a function, got '${t}'`);const o=this[n].observers;o.some(e=>e[0]===t)?console.warn("observer may be bound to an observable only once; will NOT rebind"):o.push([t,(t=>{if(!t||"object"!=typeof t)return null;const e={},o=[];for(const[n,r]of Object.entries(t))if("path"===n){if("string"!=typeof r||""===r)throw new Error('"path" option, if/when provided, MUST be a non-empty string');e[n]=r}else if("pathsOf"===n){if(t.path)throw new Error('"pathsOf" option MAY NOT be specified together with "path" option');if("string"!=typeof r)throw new Error('"pathsOf" option, if/when provided, MUST be a string (MAY be empty)');e[n]=t.pathsOf.split(".").filter(Boolean)}else if("pathsFrom"===n){if(t.path||t.pathsOf)throw new Error('"pathsFrom" option MAY NOT be specified together with "path"/"pathsOf" option/s');if("string"!=typeof r||""===r)throw new Error('"pathsFrom" option, if/when provided, MUST be a non-empty string');e[n]=r}else o.push(n);if(o.length)throw new Error(`'${o.join(", ")}' is/are not a valid observer option/s`);return e})(e)])}},unobserve:{value:function(){const t=this[n].observers;let e=t.length;if(e){let o=arguments.length;if(o)for(;o--;){let n=e;for(;n--;)t[n][0]===arguments[o]&&(t.splice(n,1),e--)}else t.splice(0)}}}},i=(t,e)=>{s[n].value=e;const o=Object.defineProperties({},s);for(const[n,r]of Object.entries(t))o[n]=u(r,n,e);return o},h=(t,e)=>{let o=0,r=t.length;s[n].value=e;const i=Object.defineProperties(new Array(r),s);for(;o<r;o++)i[o]=u(t[o],o,e);return i},a=(t,e)=>(s[n].value=e,Object.defineProperties(t,s),t),l=(t,e)=>{let o=e;if(t)if(t.path){const n=t.path;o=e.filter(t=>t.path.join(".")===n)}else if(t.pathsOf){const n=t.pathsOf;o=e.filter(t=>t.path.length===n.length+1||t.path.length===n.length&&("reverse"===t.type||"shuffle"===t.type))}else if(t.pathsFrom){const n=t.pathsFrom;o=e.filter(t=>t.path.join(".").startsWith(n))}return o},c=(t,e)=>{try{t(e)}catch(o){console.error(`failed to notify listener ${t} with ${e}`,o)}},f=function(){const t=this.batches;this.batches=null;for(const[e,o]of t)c(e,o)},p=(t,e)=>{let o,n,r,s,i,h,a,p=t;const u=e.length;do{for(i=(o=p.observers).length;i--;)if([n,r]=o[i],(s=l(r,e)).length)if(p.options.async){let t;p.batches||(p.batches=[],queueMicrotask(f.bind(p)));for(const e of p.batches)if(e[0]===n){t=e;break}t||(t=[n,[]],p.batches.push(t)),Array.prototype.push.apply(t[1],s)}else c(n,s);let t;if(p.parent){t=new Array(u);for(let o=0;o<u;o++)a=e[o],h=[p.ownKey,...a.path],t[o]={type:a.type,path:h,value:a.value,oldValue:a.oldValue,object:a.object};e=t,p=p.parent}else p=null}while(p)},u=(t,e,o)=>t&&"object"==typeof t?Array.isArray(t)?new x({target:t,ownKey:e,parent:o}).proxy:ArrayBuffer.isView(t)?new M({target:t,ownKey:e,parent:o}).proxy:t instanceof Date||t instanceof Blob||t instanceof Error?t:new O({target:t,ownKey:e,parent:o}).proxy:t,y=function(){const t=this[n],e=t.target;let o,r,s;for(e.reverse(),o=0,r=e.length;o<r;o++)if((s=e[o])&&"object"==typeof s){const t=s[n];t&&(t.ownKey=o)}const i=[new j("reverse",[],void 0,void 0,this)];return p(t,i),this},b=function(t){const e=this[n],o=e.target;let r,s,i;for(o.sort(t),r=0,s=o.length;r<s;r++)if((i=o[r])&&"object"==typeof i){const t=i[n];t&&(t.ownKey=r)}const h=[new j("shuffle",[],void 0,void 0,this)];return p(e,h),this},w=function(o,r,s){const i=this[n],h=i.target,a=[],l=h.length,c=h.slice(0);if(r=void 0===r?0:r<0?Math.max(l+r,0):Math.min(r,l),s=void 0===s?l:s<0?Math.max(l+s,0):Math.min(s,l),r<l&&s>r){let l;h.fill(o,r,s);for(let o,f,p=r;p<s;p++)o=h[p],h[p]=u(o,p,i),p in c?((f=c[p])&&"object"==typeof f&&(l=f[n])&&(f=l.detach()),a.push(new j(e,[p],h[p],f,this))):a.push(new j(t,[p],h[p],void 0,this));p(i,a)}return this},d=function(t,o,r){const s=this[n],i=s.target,h=i.length;t=t<0?Math.max(h+t,0):t,o=void 0===o?0:o<0?Math.max(h+o,0):Math.min(o,h),r=void 0===r?h:r<0?Math.max(h+r,0):Math.min(r,h);const a=Math.min(r-o,h-t);if(t<h&&t!==o&&a>0){const h=i.slice(0),l=[];i.copyWithin(t,o,r);for(let o,r,c,f=t;f<t+a;f++)(o=i[f])&&"object"==typeof o&&(o=u(o,f,s),i[f]=o),(r=h[f])&&"object"==typeof r&&(c=r[n])&&(r=c.detach()),"object"!=typeof o&&o===r||l.push(new j(e,[f],o,r,this));p(s,l)}return this},v={pop:function(){const t=this[n],e=t.target,r=e.length-1;let s=e.pop();if(s&&"object"==typeof s){const t=s[n];t&&(s=t.detach())}const i=[new j(o,[r],void 0,s,this)];return p(t,i),s},push:function(){const e=this[n],o=e.target,r=arguments.length,s=new Array(r),i=o.length;for(let t=0;t<r;t++)s[t]=u(arguments[t],i+t,e);const h=Reflect.apply(o.push,o,s),a=[];for(let e=i,n=o.length;e<n;e++)a[e-i]=new j(t,[e],o[e],void 0,this);return p(e,a),h},shift:function(){const t=this[n],e=t.target;let r,s,i,h,a;for((r=e.shift())&&"object"==typeof r&&(a=r[n])&&(r=a.detach()),s=0,i=e.length;s<i;s++)(h=e[s])&&"object"==typeof h&&(a=h[n])&&(a.ownKey=s);const l=[new j(o,[0],void 0,r,this)];return p(t,l),r},unshift:function(){const e=this[n],o=e.target,r=arguments.length,s=new Array(r);for(let t=0;t<r;t++)s[t]=u(arguments[t],t,e);const i=Reflect.apply(o.unshift,o,s);for(let t,e=0,r=o.length;e<r;e++)if((t=o[e])&&"object"==typeof t){const o=t[n];o&&(o.ownKey=e)}const h=s.length,a=new Array(h);for(let e=0;e<h;e++)a[e]=new j(t,[e],o[e],void 0,this);return p(e,a),i},reverse:y,sort:b,fill:w,copyWithin:d,splice:function(){const r=this[n],s=r.target,i=arguments.length,h=new Array(i),a=s.length;for(let t=0;t<i;t++)h[t]=u(arguments[t],t,r);const l=0===i?0:h[0]<0?a+h[0]:h[0],c=i<2?a-l:h[1],f=Math.max(i-2,0),y=Reflect.apply(s.splice,s,h),b=s.length;let w,d,v,g;for(let t,e=0;e<b;e++)(t=s[e])&&"object"==typeof t&&(w=t[n])&&(w.ownKey=e);for(d=0,v=y.length;d<v;d++)(g=y[d])&&"object"==typeof g&&(w=g[n])&&(y[d]=w.detach());const m=[];let O;for(O=0;O<c;O++)O<f?m.push(new j(e,[l+O],s[l+O],y[O],this)):m.push(new j(o,[l+O],void 0,y[O],this));for(;O<f;O++)m.push(new j(t,[l+O],s[l+O],void 0,this));return p(r,m),y}},g={reverse:y,sort:b,fill:w,copyWithin:d,set:function(t,o){const r=this[n],s=r.target,i=t.length,h=s.slice(0);o=o||0,s.set(t,o);const a=new Array(i);for(let t=o;t<i+o;t++)a[t-o]=new j(e,[t],s[t],h[t],this);p(r,a)}};class j{constructor(t,e,o,n,r){this.type=t,this.path=e,this.value=o,this.oldValue=n,this.object=r}}class m{constructor(t,e){const{target:o,parent:n,ownKey:r}=t;n&&void 0!==r?(this.parent=n,this.ownKey=r):(this.parent=null,this.ownKey=null);const s=e(o,this);this.observers=[],this.revocable=Proxy.revocable(s,this),this.proxy=this.revocable.proxy,this.target=s,this.options=this.processOptions(t.options)}processOptions(t){if(t){if("object"!=typeof t)throw new Error(`Observable options if/when provided, MAY only be an object, got '${t}'`);const e=Object.keys(t).filter(t=>!(t in r));if(e.length)throw new Error(`'${e.join(", ")}' is/are not a valid Observable option/s`);return Object.assign({},t)}return{}}detach(){return this.parent=null,this.target}set(o,r,s){let i=o[r];if(s!==i){const h=u(s,r,this);if(o[r]=h,i&&"object"==typeof i){const t=i[n];t&&(i=t.detach())}const a=void 0===i?[new j(t,[r],h,void 0,this.proxy)]:[new j(e,[r],h,i,this.proxy)];p(this,a)}return!0}deleteProperty(t,e){let r=t[e];if(delete t[e],r&&"object"==typeof r){const t=r[n];t&&(r=t.detach())}const s=[new j(o,[e],void 0,r,this.proxy)];return p(this,s),!0}}class O extends m{constructor(t){super(t,i)}}class x extends m{constructor(t){super(t,h)}get(t,e){return v[e]||t[e]}}class M extends m{constructor(t){super(t,a)}get(t,e){return g[e]||t[e]}}const A=Object.freeze({from:(t,e)=>{if(t&&"object"==typeof t){if(t[n])return t;if(Array.isArray(t))return new x({target:t,ownKey:null,parent:null,options:e}).proxy;if(ArrayBuffer.isView(t))return new M({target:t,ownKey:null,parent:null,options:e}).proxy;if(t instanceof Date||t instanceof Blob||t instanceof Error)throw new Error(`${t} found to be one of a on-observable types`);return new O({target:t,ownKey:null,parent:null,options:e}).proxy}throw new Error("observable MAY ONLY be created from a non-null object")},isObservable:t=>!(!t||!t[n])}),K=Symbol("callback-key"),E=Symbol("targets-key");class S{constructor(t){this[K]=t,this[E]=new Set,Object.freeze(this)}observe(t,e){const o=A.from(t);return o.observe(this[K],e),this[E].add(o),o}unobserve(t){t.unobserve(this[K]),this[E].delete(t)}disconnect(){for(const t of this[E])t.unobserve(this[K]);this[E].clear()}} | ||
export{A as Observable,S as ObjectObserver};const t="insert",e="update",o="delete",n=Symbol.for("object-observer-meta-key-0"),r={async:1},s={[n]:{value:null},observe:{value:function(t,e){if("function"!=typeof t)throw new Error(`observer MUST be a function, got '${t}'`);const o=this[n].observers;o.some(e=>e[0]===t)?console.warn("observer may be bound to an observable only once; will NOT rebind"):o.push([t,(t=>{if(!t||"object"!=typeof t)return null;const e={},o=[];for(const[n,r]of Object.entries(t))if("path"===n){if("string"!=typeof r||""===r)throw new Error('"path" option, if/when provided, MUST be a non-empty string');e[n]=r}else if("pathsOf"===n){if(t.path)throw new Error('"pathsOf" option MAY NOT be specified together with "path" option');if("string"!=typeof r)throw new Error('"pathsOf" option, if/when provided, MUST be a string (MAY be empty)');e[n]=t.pathsOf.split(".").filter(Boolean)}else if("pathsFrom"===n){if(t.path||t.pathsOf)throw new Error('"pathsFrom" option MAY NOT be specified together with "path"/"pathsOf" option/s');if("string"!=typeof r||""===r)throw new Error('"pathsFrom" option, if/when provided, MUST be a non-empty string');e[n]=r}else o.push(n);if(o.length)throw new Error(`'${o.join(", ")}' is/are not a valid observer option/s`);return e})(e)])}},unobserve:{value:function(){const t=this[n].observers;let e=t.length;if(e){let o=arguments.length;if(o)for(;o--;){let n=e;for(;n--;)t[n][0]===arguments[o]&&(t.splice(n,1),e--)}else t.splice(0)}}}},i=(t,e)=>{s[n].value=e;const o=Object.defineProperties({},s);for(const[n,r]of Object.entries(t))o[n]=u(r,n,e);return o},h=(t,e)=>{let o=0,r=t.length;s[n].value=e;const i=Object.defineProperties(new Array(r),s);for(;o<r;o++)i[o]=u(t[o],o,e);return i},a=(t,e)=>(s[n].value=e,Object.defineProperties(t,s),t),l=(t,e)=>{let o=e;if(t)if(t.path){const n=t.path;o=e.filter(t=>t.path.join(".")===n)}else if(t.pathsOf){const n=t.pathsOf;o=e.filter(t=>t.path.length===n.length+1||t.path.length===n.length&&("reverse"===t.type||"shuffle"===t.type))}else if(t.pathsFrom){const n=t.pathsFrom;o=e.filter(t=>t.path.join(".").startsWith(n))}return o},c=(t,e)=>{try{t(e)}catch(o){console.error(`failed to notify listener ${t} with ${e}`,o)}},f=function(){const t=this.batches;this.batches=null;for(const[e,o]of t)c(e,o)},p=(t,e)=>{let o,n,r,s,i,h,a,p=t;const u=e.length;do{for(i=(o=p.observers).length;i--;)if([n,r]=o[i],(s=l(r,e)).length)if(p.options.async){let t;p.batches||(p.batches=[],queueMicrotask(f.bind(p)));for(const e of p.batches)if(e[0]===n){t=e;break}t||(t=[n,[]],p.batches.push(t)),Array.prototype.push.apply(t[1],s)}else c(n,s);let t;if(p.parent){t=new Array(u);for(let o=0;o<u;o++)a=e[o],h=[p.ownKey,...a.path],t[o]={type:a.type,path:h,value:a.value,oldValue:a.oldValue,object:a.object};e=t,p=p.parent}else p=null}while(p)},u=(t,e,o)=>t&&"object"==typeof t?Array.isArray(t)?new x({target:t,ownKey:e,parent:o}).proxy:ArrayBuffer.isView(t)?new M({target:t,ownKey:e,parent:o}).proxy:t instanceof Date||t instanceof Blob||t instanceof Error?t:new O({target:t,ownKey:e,parent:o}).proxy:t,y=function(){const t=this[n],e=t.target;let o,r,s;for(e.reverse(),o=0,r=e.length;o<r;o++)if((s=e[o])&&"object"==typeof s){const t=s[n];t&&(t.ownKey=o)}const i=[new j("reverse",[],void 0,void 0,this)];return p(t,i),this},b=function(t){const e=this[n],o=e.target;let r,s,i;for(o.sort(t),r=0,s=o.length;r<s;r++)if((i=o[r])&&"object"==typeof i){const t=i[n];t&&(t.ownKey=r)}const h=[new j("shuffle",[],void 0,void 0,this)];return p(e,h),this},w=function(o,r,s){const i=this[n],h=i.target,a=[],l=h.length,c=h.slice(0);if(r=void 0===r?0:r<0?Math.max(l+r,0):Math.min(r,l),s=void 0===s?l:s<0?Math.max(l+s,0):Math.min(s,l),r<l&&s>r){let l;h.fill(o,r,s);for(let o,f,p=r;p<s;p++)o=h[p],h[p]=u(o,p,i),p in c?((f=c[p])&&"object"==typeof f&&(l=f[n])&&(f=l.detach()),a.push(new j(e,[p],h[p],f,this))):a.push(new j(t,[p],h[p],void 0,this));p(i,a)}return this},d=function(t,o,r){const s=this[n],i=s.target,h=i.length;t=t<0?Math.max(h+t,0):t,o=void 0===o?0:o<0?Math.max(h+o,0):Math.min(o,h),r=void 0===r?h:r<0?Math.max(h+r,0):Math.min(r,h);const a=Math.min(r-o,h-t);if(t<h&&t!==o&&a>0){const h=i.slice(0),l=[];i.copyWithin(t,o,r);for(let o,r,c,f=t;f<t+a;f++)(o=i[f])&&"object"==typeof o&&(o=u(o,f,s),i[f]=o),(r=h[f])&&"object"==typeof r&&(c=r[n])&&(r=c.detach()),"object"!=typeof o&&o===r||l.push(new j(e,[f],o,r,this));p(s,l)}return this},v={pop:function(){const t=this[n],e=t.target,r=e.length-1;let s=e.pop();if(s&&"object"==typeof s){const t=s[n];t&&(s=t.detach())}const i=[new j(o,[r],void 0,s,this)];return p(t,i),s},push:function(){const e=this[n],o=e.target,r=arguments.length,s=new Array(r),i=o.length;for(let t=0;t<r;t++)s[t]=u(arguments[t],i+t,e);const h=Reflect.apply(o.push,o,s),a=[];for(let e=i,n=o.length;e<n;e++)a[e-i]=new j(t,[e],o[e],void 0,this);return p(e,a),h},shift:function(){const t=this[n],e=t.target;let r,s,i,h,a;for((r=e.shift())&&"object"==typeof r&&(a=r[n])&&(r=a.detach()),s=0,i=e.length;s<i;s++)(h=e[s])&&"object"==typeof h&&(a=h[n])&&(a.ownKey=s);const l=[new j(o,[0],void 0,r,this)];return p(t,l),r},unshift:function(){const e=this[n],o=e.target,r=arguments.length,s=new Array(r);for(let t=0;t<r;t++)s[t]=u(arguments[t],t,e);const i=Reflect.apply(o.unshift,o,s);for(let t,e=0,r=o.length;e<r;e++)if((t=o[e])&&"object"==typeof t){const o=t[n];o&&(o.ownKey=e)}const h=s.length,a=new Array(h);for(let e=0;e<h;e++)a[e]=new j(t,[e],o[e],void 0,this);return p(e,a),i},reverse:y,sort:b,fill:w,copyWithin:d,splice:function(){const r=this[n],s=r.target,i=arguments.length,h=new Array(i),a=s.length;for(let t=0;t<i;t++)h[t]=u(arguments[t],t,r);const l=0===i?0:h[0]<0?a+h[0]:h[0],c=i<2?a-l:h[1],f=Math.max(i-2,0),y=Reflect.apply(s.splice,s,h),b=s.length;let w,d,v,g;for(let t,e=0;e<b;e++)(t=s[e])&&"object"==typeof t&&(w=t[n])&&(w.ownKey=e);for(d=0,v=y.length;d<v;d++)(g=y[d])&&"object"==typeof g&&(w=g[n])&&(y[d]=w.detach());const m=[];let O;for(O=0;O<c;O++)O<f?m.push(new j(e,[l+O],s[l+O],y[O],this)):m.push(new j(o,[l+O],void 0,y[O],this));for(;O<f;O++)m.push(new j(t,[l+O],s[l+O],void 0,this));return p(r,m),y}},g={reverse:y,sort:b,fill:w,copyWithin:d,set:function(t,o){const r=this[n],s=r.target,i=t.length,h=s.slice(0);o=o||0,s.set(t,o);const a=new Array(i);for(let t=o;t<i+o;t++)a[t-o]=new j(e,[t],s[t],h[t],this);p(r,a)}};class j{constructor(t,e,o,n,r){this.type=t,this.path=e,this.value=o,this.oldValue=n,this.object=r}}class m{constructor(t,e){const{target:o,parent:n,ownKey:r}=t;n&&void 0!==r?(this.parent=n,this.ownKey=r):(this.parent=null,this.ownKey=null);const s=e(o,this);this.observers=[],this.revocable=Proxy.revocable(s,this),this.proxy=this.revocable.proxy,this.target=s,this.options=this.processOptions(t.options)}processOptions(t){if(t){if("object"!=typeof t)throw new Error(`Observable options if/when provided, MAY only be an object, got '${t}'`);const e=Object.keys(t).filter(t=>!(t in r));if(e.length)throw new Error(`'${e.join(", ")}' is/are not a valid Observable option/s`);return Object.assign({},t)}return{}}detach(){return this.parent=null,this.target}set(o,r,s){let i=o[r];if(s!==i){const h=u(s,r,this);if(o[r]=h,i&&"object"==typeof i){const t=i[n];t&&(i=t.detach())}const a=void 0===i?[new j(t,[r],h,void 0,this.proxy)]:[new j(e,[r],h,i,this.proxy)];p(this,a)}return!0}deleteProperty(t,e){let r=t[e];if(delete t[e],r&&"object"==typeof r){const t=r[n];t&&(r=t.detach())}const s=[new j(o,[e],void 0,r,this.proxy)];return p(this,s),!0}}class O extends m{constructor(t){super(t,i)}}class x extends m{constructor(t){super(t,h)}get(t,e){return v[e]||t[e]}}class M extends m{constructor(t){super(t,a)}get(t,e){return g[e]||t[e]}}const A=Object.freeze({from:(t,e)=>{if(t&&"object"==typeof t){if(t[n])return t;if(Array.isArray(t))return new x({target:t,ownKey:null,parent:null,options:e}).proxy;if(ArrayBuffer.isView(t))return new M({target:t,ownKey:null,parent:null,options:e}).proxy;if(t instanceof Date||t instanceof Blob||t instanceof Error)throw new Error(`${t} found to be one of a on-observable types`);return new O({target:t,ownKey:null,parent:null,options:e}).proxy}throw new Error("observable MAY ONLY be created from a non-null object")},isObservable:t=>!(!t||!t[n])}),K=Symbol("callback-key"),E=Symbol("targets-key");class S{constructor(t){this[K]=t,this[E]=new Set,Object.freeze(this)}observe(t,e){const o=A.from(t);return o.observe(this[K],e),this[E].add(o),o}unobserve(t){t.unobserve(this[K]),this[E].delete(t)}disconnect(){for(const t of this[E])t.unobserve(this[K]);this[E].clear()}} |
@@ -9,3 +9,3 @@ export { Observable, ObjectObserver }; | ||
SHUFFLE = 'shuffle', | ||
oMetaKey = Symbol('observable-meta-key'), | ||
oMetaKey = Symbol.for('object-observer-meta-key-0'), | ||
validObservableOptionKeys = { async: 1 }, | ||
@@ -12,0 +12,0 @@ processObserveOptions = options => { |
@@ -1,1 +0,1 @@ | ||
export{A as Observable,S as ObjectObserver};const t="insert",e="update",o="delete",n=Symbol("observable-meta-key"),r={async:1},s={[n]:{value:null},observe:{value:function(t,e){if("function"!=typeof t)throw new Error(`observer MUST be a function, got '${t}'`);const o=this[n].observers;o.some(e=>e[0]===t)?console.warn("observer may be bound to an observable only once; will NOT rebind"):o.push([t,(t=>{if(!t||"object"!=typeof t)return null;const e={},o=[];for(const[n,r]of Object.entries(t))if("path"===n){if("string"!=typeof r||""===r)throw new Error('"path" option, if/when provided, MUST be a non-empty string');e[n]=r}else if("pathsOf"===n){if(t.path)throw new Error('"pathsOf" option MAY NOT be specified together with "path" option');if("string"!=typeof r)throw new Error('"pathsOf" option, if/when provided, MUST be a string (MAY be empty)');e[n]=t.pathsOf.split(".").filter(Boolean)}else if("pathsFrom"===n){if(t.path||t.pathsOf)throw new Error('"pathsFrom" option MAY NOT be specified together with "path"/"pathsOf" option/s');if("string"!=typeof r||""===r)throw new Error('"pathsFrom" option, if/when provided, MUST be a non-empty string');e[n]=r}else o.push(n);if(o.length)throw new Error(`'${o.join(", ")}' is/are not a valid observer option/s`);return e})(e)])}},unobserve:{value:function(){const t=this[n].observers;let e=t.length;if(e){let o=arguments.length;if(o)for(;o--;){let n=e;for(;n--;)t[n][0]===arguments[o]&&(t.splice(n,1),e--)}else t.splice(0)}}}},i=(t,e)=>{s[n].value=e;const o=Object.defineProperties({},s);for(const[n,r]of Object.entries(t))o[n]=u(r,n,e);return o},h=(t,e)=>{let o=0,r=t.length;s[n].value=e;const i=Object.defineProperties(new Array(r),s);for(;o<r;o++)i[o]=u(t[o],o,e);return i},a=(t,e)=>(s[n].value=e,Object.defineProperties(t,s),t),l=(t,e)=>{let o=e;if(t)if(t.path){const n=t.path;o=e.filter(t=>t.path.join(".")===n)}else if(t.pathsOf){const n=t.pathsOf;o=e.filter(t=>t.path.length===n.length+1||t.path.length===n.length&&("reverse"===t.type||"shuffle"===t.type))}else if(t.pathsFrom){const n=t.pathsFrom;o=e.filter(t=>t.path.join(".").startsWith(n))}return o},c=(t,e)=>{try{t(e)}catch(o){console.error(`failed to notify listener ${t} with ${e}`,o)}},f=function(){const t=this.batches;this.batches=null;for(const[e,o]of t)c(e,o)},p=(t,e)=>{let o,n,r,s,i,h,a,p=t;const u=e.length;do{for(i=(o=p.observers).length;i--;)if([n,r]=o[i],(s=l(r,e)).length)if(p.options.async){let t;p.batches||(p.batches=[],queueMicrotask(f.bind(p)));for(const e of p.batches)if(e[0]===n){t=e;break}t||(t=[n,[]],p.batches.push(t)),Array.prototype.push.apply(t[1],s)}else c(n,s);let t;if(p.parent){t=new Array(u);for(let o=0;o<u;o++)a=e[o],h=[p.ownKey,...a.path],t[o]={type:a.type,path:h,value:a.value,oldValue:a.oldValue,object:a.object};e=t,p=p.parent}else p=null}while(p)},u=(t,e,o)=>t&&"object"==typeof t?Array.isArray(t)?new x({target:t,ownKey:e,parent:o}).proxy:ArrayBuffer.isView(t)?new M({target:t,ownKey:e,parent:o}).proxy:t instanceof Date||t instanceof Blob||t instanceof Error?t:new O({target:t,ownKey:e,parent:o}).proxy:t,y=function(){const t=this[n],e=t.target;let o,r,s;for(e.reverse(),o=0,r=e.length;o<r;o++)if((s=e[o])&&"object"==typeof s){const t=s[n];t&&(t.ownKey=o)}const i=[new j("reverse",[],void 0,void 0,this)];return p(t,i),this},b=function(t){const e=this[n],o=e.target;let r,s,i;for(o.sort(t),r=0,s=o.length;r<s;r++)if((i=o[r])&&"object"==typeof i){const t=i[n];t&&(t.ownKey=r)}const h=[new j("shuffle",[],void 0,void 0,this)];return p(e,h),this},w=function(o,r,s){const i=this[n],h=i.target,a=[],l=h.length,c=h.slice(0);if(r=void 0===r?0:r<0?Math.max(l+r,0):Math.min(r,l),s=void 0===s?l:s<0?Math.max(l+s,0):Math.min(s,l),r<l&&s>r){let l;h.fill(o,r,s);for(let o,f,p=r;p<s;p++)o=h[p],h[p]=u(o,p,i),p in c?((f=c[p])&&"object"==typeof f&&(l=f[n])&&(f=l.detach()),a.push(new j(e,[p],h[p],f,this))):a.push(new j(t,[p],h[p],void 0,this));p(i,a)}return this},d=function(t,o,r){const s=this[n],i=s.target,h=i.length;t=t<0?Math.max(h+t,0):t,o=void 0===o?0:o<0?Math.max(h+o,0):Math.min(o,h),r=void 0===r?h:r<0?Math.max(h+r,0):Math.min(r,h);const a=Math.min(r-o,h-t);if(t<h&&t!==o&&a>0){const h=i.slice(0),l=[];i.copyWithin(t,o,r);for(let o,r,c,f=t;f<t+a;f++)(o=i[f])&&"object"==typeof o&&(o=u(o,f,s),i[f]=o),(r=h[f])&&"object"==typeof r&&(c=r[n])&&(r=c.detach()),"object"!=typeof o&&o===r||l.push(new j(e,[f],o,r,this));p(s,l)}return this},v={pop:function(){const t=this[n],e=t.target,r=e.length-1;let s=e.pop();if(s&&"object"==typeof s){const t=s[n];t&&(s=t.detach())}const i=[new j(o,[r],void 0,s,this)];return p(t,i),s},push:function(){const e=this[n],o=e.target,r=arguments.length,s=new Array(r),i=o.length;for(let t=0;t<r;t++)s[t]=u(arguments[t],i+t,e);const h=Reflect.apply(o.push,o,s),a=[];for(let e=i,n=o.length;e<n;e++)a[e-i]=new j(t,[e],o[e],void 0,this);return p(e,a),h},shift:function(){const t=this[n],e=t.target;let r,s,i,h,a;for((r=e.shift())&&"object"==typeof r&&(a=r[n])&&(r=a.detach()),s=0,i=e.length;s<i;s++)(h=e[s])&&"object"==typeof h&&(a=h[n])&&(a.ownKey=s);const l=[new j(o,[0],void 0,r,this)];return p(t,l),r},unshift:function(){const e=this[n],o=e.target,r=arguments.length,s=new Array(r);for(let t=0;t<r;t++)s[t]=u(arguments[t],t,e);const i=Reflect.apply(o.unshift,o,s);for(let t,e=0,r=o.length;e<r;e++)if((t=o[e])&&"object"==typeof t){const o=t[n];o&&(o.ownKey=e)}const h=s.length,a=new Array(h);for(let e=0;e<h;e++)a[e]=new j(t,[e],o[e],void 0,this);return p(e,a),i},reverse:y,sort:b,fill:w,copyWithin:d,splice:function(){const r=this[n],s=r.target,i=arguments.length,h=new Array(i),a=s.length;for(let t=0;t<i;t++)h[t]=u(arguments[t],t,r);const l=0===i?0:h[0]<0?a+h[0]:h[0],c=i<2?a-l:h[1],f=Math.max(i-2,0),y=Reflect.apply(s.splice,s,h),b=s.length;let w,d,v,g;for(let t,e=0;e<b;e++)(t=s[e])&&"object"==typeof t&&(w=t[n])&&(w.ownKey=e);for(d=0,v=y.length;d<v;d++)(g=y[d])&&"object"==typeof g&&(w=g[n])&&(y[d]=w.detach());const m=[];let O;for(O=0;O<c;O++)O<f?m.push(new j(e,[l+O],s[l+O],y[O],this)):m.push(new j(o,[l+O],void 0,y[O],this));for(;O<f;O++)m.push(new j(t,[l+O],s[l+O],void 0,this));return p(r,m),y}},g={reverse:y,sort:b,fill:w,copyWithin:d,set:function(t,o){const r=this[n],s=r.target,i=t.length,h=s.slice(0);o=o||0,s.set(t,o);const a=new Array(i);for(let t=o;t<i+o;t++)a[t-o]=new j(e,[t],s[t],h[t],this);p(r,a)}};class j{constructor(t,e,o,n,r){this.type=t,this.path=e,this.value=o,this.oldValue=n,this.object=r}}class m{constructor(t,e){const{target:o,parent:n,ownKey:r}=t;n&&void 0!==r?(this.parent=n,this.ownKey=r):(this.parent=null,this.ownKey=null);const s=e(o,this);this.observers=[],this.revocable=Proxy.revocable(s,this),this.proxy=this.revocable.proxy,this.target=s,this.options=this.processOptions(t.options)}processOptions(t){if(t){if("object"!=typeof t)throw new Error(`Observable options if/when provided, MAY only be an object, got '${t}'`);const e=Object.keys(t).filter(t=>!(t in r));if(e.length)throw new Error(`'${e.join(", ")}' is/are not a valid Observable option/s`);return Object.assign({},t)}return{}}detach(){return this.parent=null,this.target}set(o,r,s){let i=o[r];if(s!==i){const h=u(s,r,this);if(o[r]=h,i&&"object"==typeof i){const t=i[n];t&&(i=t.detach())}const a=void 0===i?[new j(t,[r],h,void 0,this.proxy)]:[new j(e,[r],h,i,this.proxy)];p(this,a)}return!0}deleteProperty(t,e){let r=t[e];if(delete t[e],r&&"object"==typeof r){const t=r[n];t&&(r=t.detach())}const s=[new j(o,[e],void 0,r,this.proxy)];return p(this,s),!0}}class O extends m{constructor(t){super(t,i)}}class x extends m{constructor(t){super(t,h)}get(t,e){return v[e]||t[e]}}class M extends m{constructor(t){super(t,a)}get(t,e){return g[e]||t[e]}}const A=Object.freeze({from:(t,e)=>{if(t&&"object"==typeof t){if(t[n])return t;if(Array.isArray(t))return new x({target:t,ownKey:null,parent:null,options:e}).proxy;if(ArrayBuffer.isView(t))return new M({target:t,ownKey:null,parent:null,options:e}).proxy;if(t instanceof Date||t instanceof Blob||t instanceof Error)throw new Error(`${t} found to be one of a on-observable types`);return new O({target:t,ownKey:null,parent:null,options:e}).proxy}throw new Error("observable MAY ONLY be created from a non-null object")},isObservable:t=>!(!t||!t[n])}),K=Symbol("callback-key"),E=Symbol("targets-key");class S{constructor(t){this[K]=t,this[E]=new Set,Object.freeze(this)}observe(t,e){const o=A.from(t);return o.observe(this[K],e),this[E].add(o),o}unobserve(t){t.unobserve(this[K]),this[E].delete(t)}disconnect(){for(const t of this[E])t.unobserve(this[K]);this[E].clear()}} | ||
export{A as Observable,S as ObjectObserver};const t="insert",e="update",o="delete",n=Symbol.for("object-observer-meta-key-0"),r={async:1},s={[n]:{value:null},observe:{value:function(t,e){if("function"!=typeof t)throw new Error(`observer MUST be a function, got '${t}'`);const o=this[n].observers;o.some(e=>e[0]===t)?console.warn("observer may be bound to an observable only once; will NOT rebind"):o.push([t,(t=>{if(!t||"object"!=typeof t)return null;const e={},o=[];for(const[n,r]of Object.entries(t))if("path"===n){if("string"!=typeof r||""===r)throw new Error('"path" option, if/when provided, MUST be a non-empty string');e[n]=r}else if("pathsOf"===n){if(t.path)throw new Error('"pathsOf" option MAY NOT be specified together with "path" option');if("string"!=typeof r)throw new Error('"pathsOf" option, if/when provided, MUST be a string (MAY be empty)');e[n]=t.pathsOf.split(".").filter(Boolean)}else if("pathsFrom"===n){if(t.path||t.pathsOf)throw new Error('"pathsFrom" option MAY NOT be specified together with "path"/"pathsOf" option/s');if("string"!=typeof r||""===r)throw new Error('"pathsFrom" option, if/when provided, MUST be a non-empty string');e[n]=r}else o.push(n);if(o.length)throw new Error(`'${o.join(", ")}' is/are not a valid observer option/s`);return e})(e)])}},unobserve:{value:function(){const t=this[n].observers;let e=t.length;if(e){let o=arguments.length;if(o)for(;o--;){let n=e;for(;n--;)t[n][0]===arguments[o]&&(t.splice(n,1),e--)}else t.splice(0)}}}},i=(t,e)=>{s[n].value=e;const o=Object.defineProperties({},s);for(const[n,r]of Object.entries(t))o[n]=u(r,n,e);return o},h=(t,e)=>{let o=0,r=t.length;s[n].value=e;const i=Object.defineProperties(new Array(r),s);for(;o<r;o++)i[o]=u(t[o],o,e);return i},a=(t,e)=>(s[n].value=e,Object.defineProperties(t,s),t),l=(t,e)=>{let o=e;if(t)if(t.path){const n=t.path;o=e.filter(t=>t.path.join(".")===n)}else if(t.pathsOf){const n=t.pathsOf;o=e.filter(t=>t.path.length===n.length+1||t.path.length===n.length&&("reverse"===t.type||"shuffle"===t.type))}else if(t.pathsFrom){const n=t.pathsFrom;o=e.filter(t=>t.path.join(".").startsWith(n))}return o},c=(t,e)=>{try{t(e)}catch(o){console.error(`failed to notify listener ${t} with ${e}`,o)}},f=function(){const t=this.batches;this.batches=null;for(const[e,o]of t)c(e,o)},p=(t,e)=>{let o,n,r,s,i,h,a,p=t;const u=e.length;do{for(i=(o=p.observers).length;i--;)if([n,r]=o[i],(s=l(r,e)).length)if(p.options.async){let t;p.batches||(p.batches=[],queueMicrotask(f.bind(p)));for(const e of p.batches)if(e[0]===n){t=e;break}t||(t=[n,[]],p.batches.push(t)),Array.prototype.push.apply(t[1],s)}else c(n,s);let t;if(p.parent){t=new Array(u);for(let o=0;o<u;o++)a=e[o],h=[p.ownKey,...a.path],t[o]={type:a.type,path:h,value:a.value,oldValue:a.oldValue,object:a.object};e=t,p=p.parent}else p=null}while(p)},u=(t,e,o)=>t&&"object"==typeof t?Array.isArray(t)?new x({target:t,ownKey:e,parent:o}).proxy:ArrayBuffer.isView(t)?new M({target:t,ownKey:e,parent:o}).proxy:t instanceof Date||t instanceof Blob||t instanceof Error?t:new O({target:t,ownKey:e,parent:o}).proxy:t,y=function(){const t=this[n],e=t.target;let o,r,s;for(e.reverse(),o=0,r=e.length;o<r;o++)if((s=e[o])&&"object"==typeof s){const t=s[n];t&&(t.ownKey=o)}const i=[new j("reverse",[],void 0,void 0,this)];return p(t,i),this},b=function(t){const e=this[n],o=e.target;let r,s,i;for(o.sort(t),r=0,s=o.length;r<s;r++)if((i=o[r])&&"object"==typeof i){const t=i[n];t&&(t.ownKey=r)}const h=[new j("shuffle",[],void 0,void 0,this)];return p(e,h),this},w=function(o,r,s){const i=this[n],h=i.target,a=[],l=h.length,c=h.slice(0);if(r=void 0===r?0:r<0?Math.max(l+r,0):Math.min(r,l),s=void 0===s?l:s<0?Math.max(l+s,0):Math.min(s,l),r<l&&s>r){let l;h.fill(o,r,s);for(let o,f,p=r;p<s;p++)o=h[p],h[p]=u(o,p,i),p in c?((f=c[p])&&"object"==typeof f&&(l=f[n])&&(f=l.detach()),a.push(new j(e,[p],h[p],f,this))):a.push(new j(t,[p],h[p],void 0,this));p(i,a)}return this},d=function(t,o,r){const s=this[n],i=s.target,h=i.length;t=t<0?Math.max(h+t,0):t,o=void 0===o?0:o<0?Math.max(h+o,0):Math.min(o,h),r=void 0===r?h:r<0?Math.max(h+r,0):Math.min(r,h);const a=Math.min(r-o,h-t);if(t<h&&t!==o&&a>0){const h=i.slice(0),l=[];i.copyWithin(t,o,r);for(let o,r,c,f=t;f<t+a;f++)(o=i[f])&&"object"==typeof o&&(o=u(o,f,s),i[f]=o),(r=h[f])&&"object"==typeof r&&(c=r[n])&&(r=c.detach()),"object"!=typeof o&&o===r||l.push(new j(e,[f],o,r,this));p(s,l)}return this},v={pop:function(){const t=this[n],e=t.target,r=e.length-1;let s=e.pop();if(s&&"object"==typeof s){const t=s[n];t&&(s=t.detach())}const i=[new j(o,[r],void 0,s,this)];return p(t,i),s},push:function(){const e=this[n],o=e.target,r=arguments.length,s=new Array(r),i=o.length;for(let t=0;t<r;t++)s[t]=u(arguments[t],i+t,e);const h=Reflect.apply(o.push,o,s),a=[];for(let e=i,n=o.length;e<n;e++)a[e-i]=new j(t,[e],o[e],void 0,this);return p(e,a),h},shift:function(){const t=this[n],e=t.target;let r,s,i,h,a;for((r=e.shift())&&"object"==typeof r&&(a=r[n])&&(r=a.detach()),s=0,i=e.length;s<i;s++)(h=e[s])&&"object"==typeof h&&(a=h[n])&&(a.ownKey=s);const l=[new j(o,[0],void 0,r,this)];return p(t,l),r},unshift:function(){const e=this[n],o=e.target,r=arguments.length,s=new Array(r);for(let t=0;t<r;t++)s[t]=u(arguments[t],t,e);const i=Reflect.apply(o.unshift,o,s);for(let t,e=0,r=o.length;e<r;e++)if((t=o[e])&&"object"==typeof t){const o=t[n];o&&(o.ownKey=e)}const h=s.length,a=new Array(h);for(let e=0;e<h;e++)a[e]=new j(t,[e],o[e],void 0,this);return p(e,a),i},reverse:y,sort:b,fill:w,copyWithin:d,splice:function(){const r=this[n],s=r.target,i=arguments.length,h=new Array(i),a=s.length;for(let t=0;t<i;t++)h[t]=u(arguments[t],t,r);const l=0===i?0:h[0]<0?a+h[0]:h[0],c=i<2?a-l:h[1],f=Math.max(i-2,0),y=Reflect.apply(s.splice,s,h),b=s.length;let w,d,v,g;for(let t,e=0;e<b;e++)(t=s[e])&&"object"==typeof t&&(w=t[n])&&(w.ownKey=e);for(d=0,v=y.length;d<v;d++)(g=y[d])&&"object"==typeof g&&(w=g[n])&&(y[d]=w.detach());const m=[];let O;for(O=0;O<c;O++)O<f?m.push(new j(e,[l+O],s[l+O],y[O],this)):m.push(new j(o,[l+O],void 0,y[O],this));for(;O<f;O++)m.push(new j(t,[l+O],s[l+O],void 0,this));return p(r,m),y}},g={reverse:y,sort:b,fill:w,copyWithin:d,set:function(t,o){const r=this[n],s=r.target,i=t.length,h=s.slice(0);o=o||0,s.set(t,o);const a=new Array(i);for(let t=o;t<i+o;t++)a[t-o]=new j(e,[t],s[t],h[t],this);p(r,a)}};class j{constructor(t,e,o,n,r){this.type=t,this.path=e,this.value=o,this.oldValue=n,this.object=r}}class m{constructor(t,e){const{target:o,parent:n,ownKey:r}=t;n&&void 0!==r?(this.parent=n,this.ownKey=r):(this.parent=null,this.ownKey=null);const s=e(o,this);this.observers=[],this.revocable=Proxy.revocable(s,this),this.proxy=this.revocable.proxy,this.target=s,this.options=this.processOptions(t.options)}processOptions(t){if(t){if("object"!=typeof t)throw new Error(`Observable options if/when provided, MAY only be an object, got '${t}'`);const e=Object.keys(t).filter(t=>!(t in r));if(e.length)throw new Error(`'${e.join(", ")}' is/are not a valid Observable option/s`);return Object.assign({},t)}return{}}detach(){return this.parent=null,this.target}set(o,r,s){let i=o[r];if(s!==i){const h=u(s,r,this);if(o[r]=h,i&&"object"==typeof i){const t=i[n];t&&(i=t.detach())}const a=void 0===i?[new j(t,[r],h,void 0,this.proxy)]:[new j(e,[r],h,i,this.proxy)];p(this,a)}return!0}deleteProperty(t,e){let r=t[e];if(delete t[e],r&&"object"==typeof r){const t=r[n];t&&(r=t.detach())}const s=[new j(o,[e],void 0,r,this.proxy)];return p(this,s),!0}}class O extends m{constructor(t){super(t,i)}}class x extends m{constructor(t){super(t,h)}get(t,e){return v[e]||t[e]}}class M extends m{constructor(t){super(t,a)}get(t,e){return g[e]||t[e]}}const A=Object.freeze({from:(t,e)=>{if(t&&"object"==typeof t){if(t[n])return t;if(Array.isArray(t))return new x({target:t,ownKey:null,parent:null,options:e}).proxy;if(ArrayBuffer.isView(t))return new M({target:t,ownKey:null,parent:null,options:e}).proxy;if(t instanceof Date||t instanceof Blob||t instanceof Error)throw new Error(`${t} found to be one of a on-observable types`);return new O({target:t,ownKey:null,parent:null,options:e}).proxy}throw new Error("observable MAY ONLY be created from a non-null object")},isObservable:t=>!(!t||!t[n])}),K=Symbol("callback-key"),E=Symbol("targets-key");class S{constructor(t){this[K]=t,this[E]=new Set,Object.freeze(this)}observe(t,e){const o=A.from(t);return o.observe(this[K],e),this[E].add(o),o}unobserve(t){t.unobserve(this[K]),this[E].delete(t)}disconnect(){for(const t of this[E])t.unobserve(this[K]);this[E].clear()}} |
{ | ||
"name": "object-observer", | ||
"version": "4.2.4", | ||
"version": "4.3.0", | ||
"description": "object-observer utility provides simple means to (deeply) observe specified object/array changes; implemented via native Proxy; changes delivered in a synchronous way", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -13,3 +13,2 @@ [![npm](https://img.shields.io/npm/v/object-observer.svg?label=npm%20object-observer)](https://www.npmjs.com/package/object-observer) | ||
Main aspects and features: | ||
- implemented via native __Proxy__ (revokable) | ||
@@ -72,3 +71,4 @@ - observation is 'deep', yielding changes from a __sub-graphs__ too | ||
Additionally, from version 4.2.0 there is also 'DOM-like' API flavor, a one that is resonating with DOM's `MutationObserver`, `ResizeObserver` etc from the syntax perspective. | ||
Additionally, from version 4.2.0 there is also 'DOM-like' API flavor - constructable `ObjectObserver`. | ||
This API is resonating with DOM's `MutationObserver`, `ResizeObserver` etc from the syntax perspective. | ||
Under the hood it uses the same `Observable` mechanics. | ||
@@ -79,2 +79,5 @@ Read docs about this API flavor [here](docs/dom-like-api.md). | ||
Starting from 4.3.x `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. | ||
## Security | ||
@@ -81,0 +84,0 @@ |
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
67841
246