object-observer
Advanced tools
Comparing version 4.4.0 to 4.5.0
@@ -203,3 +203,3 @@ export { Observable, ObjectObserver }; | ||
return new TypedArrayOMeta({ target: item, ownKey: key, parent: parent }).proxy; | ||
} else if (item instanceof Date || item instanceof Blob || item instanceof Error) { | ||
} else if (item instanceof Date || item instanceof Error || (globalThis.Blob && item instanceof Blob)) { | ||
return item; | ||
@@ -659,3 +659,3 @@ } else { | ||
return new TypedArrayOMeta({ target: target, ownKey: null, parent: null, options: options }).proxy; | ||
} else if (target instanceof Date || target instanceof Blob || target instanceof Error) { | ||
} else if (target instanceof Date || target instanceof Error || (globalThis.Blob && target instanceof Blob)) { | ||
throw new Error(`${target} found to be one of a on-observable types`); | ||
@@ -662,0 +662,0 @@ } else { |
@@ -1,1 +0,1 @@ | ||
function t(){const t=this[d],e=t.target;let o,r,n;for(e.reverse(),o=0,r=e.length;o<r;o++)if((n=e[o])&&"object"==typeof n){const i=n[d];i&&(i.ownKey=o)}var s=[new O(h,[],void 0,void 0,this)];return j(t,s),this}function e(t){const e=this[d],o=e.target;let r,n,s;for(o.sort(t),r=0,n=o.length;r<n;r++)if((s=o[r])&&"object"==typeof s){const i=s[d];i&&(i.ownKey=r)}return t=[new O(a,[],void 0,void 0,this)],j(e,t),this}function o(t,n,s){const i=this[d],h=i.target,a=[],e=h.length,l=h.slice(0);if(n=void 0===n?0:n<0?Math.max(e+n,0):Math.min(n,e),s=void 0===s?e:s<0?Math.max(e+s,0):Math.min(s,e),n<e&&n<s){h.fill(t,n,s);let r;for(let t=n,e,o;t<s;t++)e=h[t],h[t]=m(e,t,i),t in l?(o=l[t],o&&"object"==typeof o&&(r=o[d],r&&(o=r.detach())),a.push(new O(v,[t],h[t],o,this))):a.push(new O(w,[t],h[t],void 0,this));j(i,a)}return this}function r(n,t,e){const s=this[d],i=s.target,o=i.length;n=n<0?Math.max(o+n,0):n,t=void 0===t?0:t<0?Math.max(o+t,0):Math.min(t,o),e=void 0===e?o:e<0?Math.max(o+e,0):Math.min(e,o);var h=Math.min(e-t,o-n);if(n<o&&n!==t&&0<h){const a=i.slice(0),l=[];i.copyWithin(n,t,e);for(let t=n,e,o,r;t<n+h;t++)e=i[t],e&&"object"==typeof e&&(e=m(e,t,s),i[t]=e),o=a[t],o&&"object"==typeof o&&(r=o[d],r&&(o=r.detach())),"object"!=typeof e&&e===o||l.push(new O(v,[t],e,o,this));j(s,l)}return this}const w="insert",v="update",g="delete",h="reverse",a="shuffle",d=Symbol.for("object-observer-meta-key-0"),n={async:1},s={observe:{value:function(e,t){if("function"!=typeof e)throw new Error(`observer MUST be a function, got '${e}'`);const o=this[d].observers;o.some(t=>t[0]===e)?console.warn("observer may be bound to an observable only once; will NOT rebind"):o.push([e,(t=>{if(!t||"object"!=typeof t)return null;const e={},o=[];for(var[r,n]of Object.entries(t))if("path"===r){if("string"!=typeof n||""===n)throw new Error('"path" option, if/when provided, MUST be a non-empty string');e[r]=n}else if("pathsOf"===r){if(t.path)throw new Error('"pathsOf" option MAY NOT be specified together with "path" option');if("string"!=typeof n)throw new Error('"pathsOf" option, if/when provided, MUST be a string (MAY be empty)');e[r]=t.pathsOf.split(".").filter(Boolean)}else if("pathsFrom"===r){if(t.path||t.pathsOf)throw new Error('"pathsFrom" option MAY NOT be specified together with "path"/"pathsOf" option/s');if("string"!=typeof n||""===n)throw new Error('"pathsFrom" option, if/when provided, MUST be a non-empty string');e[r]=n}else o.push(r);if(o.length)throw new Error(`'${o.join(", ")}' is/are not a valid observer option/s`);return e})(t)])}},unobserve:{value:function(){const o=this[d].observers;let r=o.length;if(r){let e=arguments.length;if(e)for(;e--;){let t=r;for(;t--;)o[t][0]===arguments[e]&&(o.splice(t,1),r--)}else o.splice(0)}}}},i=(t,e)=>{const o=Object.defineProperties({},s);o[d]=e;for(const r in t)o[r]=m(t[r],r,e);return o},l=(e,o)=>{var r=e.length;const n=Object.defineProperties(new Array(r),s);n[d]=o;for(let t=0;t<r;t++)n[t]=m(e[t],t,o);return n},f=(t,e)=>(Object.defineProperties(t,s),t[d]=e,t),c=(t,e)=>{if(!t)return e;let o=e;if(t.path){const r=t.path;o=e.filter(t=>t.path.join(".")===r)}else if(t.pathsOf){const n=t.pathsOf,s=n.join(".");o=e.filter(t=>(t.path.length===n.length+1||t.path.length===n.length&&(t.type===h||t.type===a))&&t.path.join(".").startsWith(s))}else if(t.pathsFrom){const i=t.pathsFrom;o=e.filter(t=>t.path.join(".").startsWith(i))}return o},p=(e,o)=>{try{e(o)}catch(t){console.error(`failed to notify listener ${e} with `+o,t)}},u=function(){var t,e,o=this.batches;this.batches=null;for([t,e]of o)p(t,e)},j=(t,e)=>{let o=t,r,n,s,i,h;var a=e.length;do{for(r=o.observers,h=r.length;h--;)if([n,s]=r[h],(i=c(s,e)).length)if(o.options.async){o.batches||(o.batches=[],queueMicrotask(u.bind(o)));let t;for(const l of o.batches)if(l[0]===n){t=l;break}t||(t=[n,[]],o.batches.push(t)),Array.prototype.push.apply(t[1],i)}else p(n,i);if(o.parent){const f=new Array(a);for(let t=0;t<a;t++)f[t]={...e[t]},f[t].path=[o.ownKey,...f[t].path];e=f,o=o.parent}else o=null}while(o)},m=(t,e,o)=>t&&"object"==typeof t?Array.isArray(t)?new A({target:t,ownKey:e,parent:o}).proxy:ArrayBuffer.isView(t)?new K({target:t,ownKey:e,parent:o}).proxy:t instanceof Date||t instanceof Blob||t instanceof Error?t:new M({target:t,ownKey:e,parent:o}).proxy:t,y={pop:function(){const t=this[d],e=t.target,o=e.length-1;let r=e.pop();if(r&&"object"==typeof r){const s=r[d];s&&(r=s.detach())}var n=[new O(g,[o],void 0,r,this)];return j(t,n),r},push:function(){const e=this[d],o=e.target,r=arguments.length,n=new Array(r),s=o.length;for(let t=0;t<r;t++)n[t]=m(arguments[t],s+t,e);var t=Reflect.apply(o.push,o,n);const i=[];for(let t=s,e=o.length;t<e;t++)i[t-s]=new O(w,[t],o[t],void 0,this);return j(e,i),t},shift:function(){const t=this[d],e=t.target;let o,r,n,s,i;for(o=e.shift(),o&&"object"==typeof o&&(i=o[d],i&&(o=i.detach())),r=0,n=e.length;r<n;r++)(s=e[r])&&"object"==typeof s&&(i=s[d],i&&(i.ownKey=r));var h=[new O(g,[0],void 0,o,this)];return j(t,h),o},unshift:function(){const e=this[d],r=e.target,o=arguments.length,n=new Array(o);for(let t=0;t<o;t++)n[t]=m(arguments[t],t,e);var t=Reflect.apply(r.unshift,r,n);for(let t=0,e=r.length,o;t<e;t++)if((o=r[t])&&"object"==typeof o){const h=o[d];h&&(h.ownKey=t)}var s=n.length;const i=new Array(s);for(let t=0;t<s;t++)i[t]=new O(w,[t],r[t],void 0,this);return j(e,i),t},reverse:t,sort:e,fill:o,copyWithin:r,splice:function(){const e=this[d],o=e.target,r=arguments.length,n=new Array(r),t=o.length;for(let t=0;t<r;t++)n[t]=m(arguments[t],t,e);const s=0===r?0:n[0]<0?t+n[0]:n[0],i=r<2?t-s:n[1],h=Math.max(r-2,0),a=Reflect.apply(o.splice,o,n),l=o.length;let f;for(let t=0,e;t<l;t++)(e=o[t])&&"object"==typeof e&&(f=e[d],f&&(f.ownKey=t));let c,p,u;for(c=0,p=a.length;c<p;c++)(u=a[c])&&"object"==typeof u&&(f=u[d],f&&(a[c]=f.detach()));const y=[];let b;for(b=0;b<i;b++)b<h?y.push(new O(v,[s+b],o[s+b],a[b],this)):y.push(new O(g,[s+b],void 0,a[b],this));for(;b<h;b++)y.push(new O(w,[s+b],o[s+b],void 0,this));return j(e,y),a}},b={reverse:t,sort:e,fill:o,copyWithin:r,set:function(t,e){const o=this[d],r=o.target,n=t.length,s=r.slice(0);e=e||0,r.set(t,e);const i=new Array(n);for(let t=e;t<n+e;t++)i[t-e]=new O(v,[t],r[t],s[t],this);j(o,i)}};class O{constructor(t,e,o,r,n){this.type=t,this.path=e,this.value=o,this.oldValue=r,this.object=n}}class x{constructor(t,e){var{target:o,parent:r,ownKey:n}=t;r&&void 0!==n?(this.parent=r,this.ownKey=n):(this.parent=null,this.ownKey=null);o=e(o,this);this.observers=[],this.revocable=Proxy.revocable(o,this),this.proxy=this.revocable.proxy,this.target=o,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 n));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(t,e,o){let r=t[e];if(o!==r){o=m(o,e,this);if(t[e]=o,r&&"object"==typeof r){const n=r[d];n&&(r=n.detach())}o=void 0===r?[new O(w,[e],o,void 0,this.proxy)]:[new O(v,[e],o,r,this.proxy)];j(this,o)}return!0}deleteProperty(t,e){let o=t[e];if(delete t[e],o&&"object"==typeof o){const r=o[d];r&&(o=r.detach())}e=[new O(g,[e],void 0,o,this.proxy)];return j(this,e),!0}}class M extends x{constructor(t){super(t,i)}}class A extends x{constructor(t){super(t,l)}get(t,e){return y[e]||t[e]}}class K extends x{constructor(t){super(t,f)}get(t,e){return b[e]||t[e]}}const E=Object.freeze({from:(t,e)=>{if(t&&"object"==typeof t){if(t[d])return t;if(Array.isArray(t))return new A({target:t,ownKey:null,parent:null,options:e}).proxy;if(ArrayBuffer.isView(t))return new K({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 M({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[d])}),S=Symbol("callback-key"),k=Symbol("targets-key");class T{constructor(t){this[S]=t,this[k]=new Set,Object.freeze(this)}observe(t,e){const o=E.from(t);return o.observe(this[S],e),this[k].add(o),o}unobserve(t){t.unobserve(this[S]),this[k].delete(t)}disconnect(){for(const t of this[k])t.unobserve(this[S]);this[k].clear()}}export{E as Observable,T as ObjectObserver}; | ||
function t(){const t=this[d],e=t.target;let o,r,n;for(e.reverse(),o=0,r=e.length;o<r;o++)if((n=e[o])&&"object"==typeof n){const i=n[d];i&&(i.ownKey=o)}var s=[new O(h,[],void 0,void 0,this)];return j(t,s),this}function e(t){const e=this[d],o=e.target;let r,n,s;for(o.sort(t),r=0,n=o.length;r<n;r++)if((s=o[r])&&"object"==typeof s){const i=s[d];i&&(i.ownKey=r)}return t=[new O(a,[],void 0,void 0,this)],j(e,t),this}function o(t,n,s){const i=this[d],h=i.target,a=[],e=h.length,l=h.slice(0);if(n=void 0===n?0:n<0?Math.max(e+n,0):Math.min(n,e),s=void 0===s?e:s<0?Math.max(e+s,0):Math.min(s,e),n<e&&n<s){h.fill(t,n,s);let r;for(let t=n,e,o;t<s;t++)e=h[t],h[t]=m(e,t,i),t in l?(o=l[t],o&&"object"==typeof o&&(r=o[d],r&&(o=r.detach())),a.push(new O(v,[t],h[t],o,this))):a.push(new O(w,[t],h[t],void 0,this));j(i,a)}return this}function r(n,t,e){const s=this[d],i=s.target,o=i.length;n=n<0?Math.max(o+n,0):n,t=void 0===t?0:t<0?Math.max(o+t,0):Math.min(t,o),e=void 0===e?o:e<0?Math.max(o+e,0):Math.min(e,o);var h=Math.min(e-t,o-n);if(n<o&&n!==t&&0<h){const a=i.slice(0),l=[];i.copyWithin(n,t,e);for(let t=n,e,o,r;t<n+h;t++)e=i[t],e&&"object"==typeof e&&(e=m(e,t,s),i[t]=e),o=a[t],o&&"object"==typeof o&&(r=o[d],r&&(o=r.detach())),"object"!=typeof e&&e===o||l.push(new O(v,[t],e,o,this));j(s,l)}return this}const w="insert",v="update",g="delete",h="reverse",a="shuffle",d=Symbol.for("object-observer-meta-key-0"),n={async:1},s={observe:{value:function(e,t){if("function"!=typeof e)throw new Error(`observer MUST be a function, got '${e}'`);const o=this[d].observers;o.some(t=>t[0]===e)?console.warn("observer may be bound to an observable only once; will NOT rebind"):o.push([e,(t=>{if(!t||"object"!=typeof t)return null;const e={},o=[];for(var[r,n]of Object.entries(t))if("path"===r){if("string"!=typeof n||""===n)throw new Error('"path" option, if/when provided, MUST be a non-empty string');e[r]=n}else if("pathsOf"===r){if(t.path)throw new Error('"pathsOf" option MAY NOT be specified together with "path" option');if("string"!=typeof n)throw new Error('"pathsOf" option, if/when provided, MUST be a string (MAY be empty)');e[r]=t.pathsOf.split(".").filter(Boolean)}else if("pathsFrom"===r){if(t.path||t.pathsOf)throw new Error('"pathsFrom" option MAY NOT be specified together with "path"/"pathsOf" option/s');if("string"!=typeof n||""===n)throw new Error('"pathsFrom" option, if/when provided, MUST be a non-empty string');e[r]=n}else o.push(r);if(o.length)throw new Error(`'${o.join(", ")}' is/are not a valid observer option/s`);return e})(t)])}},unobserve:{value:function(){const o=this[d].observers;let r=o.length;if(r){let e=arguments.length;if(e)for(;e--;){let t=r;for(;t--;)o[t][0]===arguments[e]&&(o.splice(t,1),r--)}else o.splice(0)}}}},i=(t,e)=>{const o=Object.defineProperties({},s);o[d]=e;for(const r in t)o[r]=m(t[r],r,e);return o},l=(e,o)=>{var r=e.length;const n=Object.defineProperties(new Array(r),s);n[d]=o;for(let t=0;t<r;t++)n[t]=m(e[t],t,o);return n},f=(t,e)=>(Object.defineProperties(t,s),t[d]=e,t),c=(t,e)=>{if(!t)return e;let o=e;if(t.path){const r=t.path;o=e.filter(t=>t.path.join(".")===r)}else if(t.pathsOf){const n=t.pathsOf,s=n.join(".");o=e.filter(t=>(t.path.length===n.length+1||t.path.length===n.length&&(t.type===h||t.type===a))&&t.path.join(".").startsWith(s))}else if(t.pathsFrom){const i=t.pathsFrom;o=e.filter(t=>t.path.join(".").startsWith(i))}return o},p=(e,o)=>{try{e(o)}catch(t){console.error(`failed to notify listener ${e} with `+o,t)}},b=function(){var t,e,o=this.batches;this.batches=null;for([t,e]of o)p(t,e)},j=(t,e)=>{let o=t,r,n,s,i,h;var a=e.length;do{for(r=o.observers,h=r.length;h--;)if([n,s]=r[h],(i=c(s,e)).length)if(o.options.async){o.batches||(o.batches=[],queueMicrotask(b.bind(o)));let t;for(const l of o.batches)if(l[0]===n){t=l;break}t||(t=[n,[]],o.batches.push(t)),Array.prototype.push.apply(t[1],i)}else p(n,i);if(o.parent){const f=new Array(a);for(let t=0;t<a;t++)f[t]={...e[t]},f[t].path=[o.ownKey,...f[t].path];e=f,o=o.parent}else o=null}while(o)},m=(t,e,o)=>t&&"object"==typeof t?Array.isArray(t)?new A({target:t,ownKey:e,parent:o}).proxy:ArrayBuffer.isView(t)?new K({target:t,ownKey:e,parent:o}).proxy:t instanceof Date||t instanceof Error||globalThis.Blob&&t instanceof Blob?t:new M({target:t,ownKey:e,parent:o}).proxy:t,u={pop:function(){const t=this[d],e=t.target,o=e.length-1;let r=e.pop();if(r&&"object"==typeof r){const s=r[d];s&&(r=s.detach())}var n=[new O(g,[o],void 0,r,this)];return j(t,n),r},push:function(){const e=this[d],o=e.target,r=arguments.length,n=new Array(r),s=o.length;for(let t=0;t<r;t++)n[t]=m(arguments[t],s+t,e);var t=Reflect.apply(o.push,o,n);const i=[];for(let t=s,e=o.length;t<e;t++)i[t-s]=new O(w,[t],o[t],void 0,this);return j(e,i),t},shift:function(){const t=this[d],e=t.target;let o,r,n,s,i;for(o=e.shift(),o&&"object"==typeof o&&(i=o[d],i&&(o=i.detach())),r=0,n=e.length;r<n;r++)(s=e[r])&&"object"==typeof s&&(i=s[d],i&&(i.ownKey=r));var h=[new O(g,[0],void 0,o,this)];return j(t,h),o},unshift:function(){const e=this[d],r=e.target,o=arguments.length,n=new Array(o);for(let t=0;t<o;t++)n[t]=m(arguments[t],t,e);var t=Reflect.apply(r.unshift,r,n);for(let t=0,e=r.length,o;t<e;t++)if((o=r[t])&&"object"==typeof o){const h=o[d];h&&(h.ownKey=t)}var s=n.length;const i=new Array(s);for(let t=0;t<s;t++)i[t]=new O(w,[t],r[t],void 0,this);return j(e,i),t},reverse:t,sort:e,fill:o,copyWithin:r,splice:function(){const e=this[d],o=e.target,r=arguments.length,n=new Array(r),t=o.length;for(let t=0;t<r;t++)n[t]=m(arguments[t],t,e);const s=0===r?0:n[0]<0?t+n[0]:n[0],i=r<2?t-s:n[1],h=Math.max(r-2,0),a=Reflect.apply(o.splice,o,n),l=o.length;let f;for(let t=0,e;t<l;t++)(e=o[t])&&"object"==typeof e&&(f=e[d],f&&(f.ownKey=t));let c,p,b;for(c=0,p=a.length;c<p;c++)(b=a[c])&&"object"==typeof b&&(f=b[d],f&&(a[c]=f.detach()));const u=[];let y;for(y=0;y<i;y++)y<h?u.push(new O(v,[s+y],o[s+y],a[y],this)):u.push(new O(g,[s+y],void 0,a[y],this));for(;y<h;y++)u.push(new O(w,[s+y],o[s+y],void 0,this));return j(e,u),a}},y={reverse:t,sort:e,fill:o,copyWithin:r,set:function(t,e){const o=this[d],r=o.target,n=t.length,s=r.slice(0);e=e||0,r.set(t,e);const i=new Array(n);for(let t=e;t<n+e;t++)i[t-e]=new O(v,[t],r[t],s[t],this);j(o,i)}};class O{constructor(t,e,o,r,n){this.type=t,this.path=e,this.value=o,this.oldValue=r,this.object=n}}class x{constructor(t,e){var{target:o,parent:r,ownKey:n}=t;r&&void 0!==n?(this.parent=r,this.ownKey=n):(this.parent=null,this.ownKey=null);o=e(o,this);this.observers=[],this.revocable=Proxy.revocable(o,this),this.proxy=this.revocable.proxy,this.target=o,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 n));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(t,e,o){let r=t[e];if(o!==r){o=m(o,e,this);if(t[e]=o,r&&"object"==typeof r){const n=r[d];n&&(r=n.detach())}o=void 0===r?[new O(w,[e],o,void 0,this.proxy)]:[new O(v,[e],o,r,this.proxy)];j(this,o)}return!0}deleteProperty(t,e){let o=t[e];if(delete t[e],o&&"object"==typeof o){const r=o[d];r&&(o=r.detach())}e=[new O(g,[e],void 0,o,this.proxy)];return j(this,e),!0}}class M extends x{constructor(t){super(t,i)}}class A extends x{constructor(t){super(t,l)}get(t,e){return u[e]||t[e]}}class K extends x{constructor(t){super(t,f)}get(t,e){return y[e]||t[e]}}const E=Object.freeze({from:(t,e)=>{if(t&&"object"==typeof t){if(t[d])return t;if(Array.isArray(t))return new A({target:t,ownKey:null,parent:null,options:e}).proxy;if(ArrayBuffer.isView(t))return new K({target:t,ownKey:null,parent:null,options:e}).proxy;if(t instanceof Date||t instanceof Error||globalThis.Blob&&t instanceof Blob)throw new Error(t+" found to be one of a on-observable types");return new M({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[d])}),T=Symbol("callback-key"),S=Symbol("targets-key");class k{constructor(t){this[T]=t,this[S]=new Set,Object.freeze(this)}observe(t,e){const o=E.from(t);return o.observe(this[T],e),this[S].add(o),o}unobserve(t){t.unobserve(this[T]),this[S].delete(t)}disconnect(){for(const t of this[S])t.unobserve(this[T]);this[S].clear()}}export{E as Observable,k as ObjectObserver}; |
@@ -203,3 +203,3 @@ export { Observable, ObjectObserver }; | ||
return new TypedArrayOMeta({ target: item, ownKey: key, parent: parent }).proxy; | ||
} else if (item instanceof Date || item instanceof Blob || item instanceof Error) { | ||
} else if (item instanceof Date || item instanceof Error || (globalThis.Blob && item instanceof Blob)) { | ||
return item; | ||
@@ -659,3 +659,3 @@ } else { | ||
return new TypedArrayOMeta({ target: target, ownKey: null, parent: null, options: options }).proxy; | ||
} else if (target instanceof Date || target instanceof Blob || target instanceof Error) { | ||
} else if (target instanceof Date || target instanceof Error || (globalThis.Blob && target instanceof Blob)) { | ||
throw new Error(`${target} found to be one of a on-observable types`); | ||
@@ -662,0 +662,0 @@ } else { |
@@ -1,1 +0,1 @@ | ||
function t(){const t=this[d],e=t.target;let o,r,n;for(e.reverse(),o=0,r=e.length;o<r;o++)if((n=e[o])&&"object"==typeof n){const i=n[d];i&&(i.ownKey=o)}var s=[new O(h,[],void 0,void 0,this)];return j(t,s),this}function e(t){const e=this[d],o=e.target;let r,n,s;for(o.sort(t),r=0,n=o.length;r<n;r++)if((s=o[r])&&"object"==typeof s){const i=s[d];i&&(i.ownKey=r)}return t=[new O(a,[],void 0,void 0,this)],j(e,t),this}function o(t,n,s){const i=this[d],h=i.target,a=[],e=h.length,l=h.slice(0);if(n=void 0===n?0:n<0?Math.max(e+n,0):Math.min(n,e),s=void 0===s?e:s<0?Math.max(e+s,0):Math.min(s,e),n<e&&n<s){h.fill(t,n,s);let r;for(let t=n,e,o;t<s;t++)e=h[t],h[t]=m(e,t,i),t in l?(o=l[t],o&&"object"==typeof o&&(r=o[d],r&&(o=r.detach())),a.push(new O(v,[t],h[t],o,this))):a.push(new O(w,[t],h[t],void 0,this));j(i,a)}return this}function r(n,t,e){const s=this[d],i=s.target,o=i.length;n=n<0?Math.max(o+n,0):n,t=void 0===t?0:t<0?Math.max(o+t,0):Math.min(t,o),e=void 0===e?o:e<0?Math.max(o+e,0):Math.min(e,o);var h=Math.min(e-t,o-n);if(n<o&&n!==t&&0<h){const a=i.slice(0),l=[];i.copyWithin(n,t,e);for(let t=n,e,o,r;t<n+h;t++)e=i[t],e&&"object"==typeof e&&(e=m(e,t,s),i[t]=e),o=a[t],o&&"object"==typeof o&&(r=o[d],r&&(o=r.detach())),"object"!=typeof e&&e===o||l.push(new O(v,[t],e,o,this));j(s,l)}return this}const w="insert",v="update",g="delete",h="reverse",a="shuffle",d=Symbol.for("object-observer-meta-key-0"),n={async:1},s={observe:{value:function(e,t){if("function"!=typeof e)throw new Error(`observer MUST be a function, got '${e}'`);const o=this[d].observers;o.some(t=>t[0]===e)?console.warn("observer may be bound to an observable only once; will NOT rebind"):o.push([e,(t=>{if(!t||"object"!=typeof t)return null;const e={},o=[];for(var[r,n]of Object.entries(t))if("path"===r){if("string"!=typeof n||""===n)throw new Error('"path" option, if/when provided, MUST be a non-empty string');e[r]=n}else if("pathsOf"===r){if(t.path)throw new Error('"pathsOf" option MAY NOT be specified together with "path" option');if("string"!=typeof n)throw new Error('"pathsOf" option, if/when provided, MUST be a string (MAY be empty)');e[r]=t.pathsOf.split(".").filter(Boolean)}else if("pathsFrom"===r){if(t.path||t.pathsOf)throw new Error('"pathsFrom" option MAY NOT be specified together with "path"/"pathsOf" option/s');if("string"!=typeof n||""===n)throw new Error('"pathsFrom" option, if/when provided, MUST be a non-empty string');e[r]=n}else o.push(r);if(o.length)throw new Error(`'${o.join(", ")}' is/are not a valid observer option/s`);return e})(t)])}},unobserve:{value:function(){const o=this[d].observers;let r=o.length;if(r){let e=arguments.length;if(e)for(;e--;){let t=r;for(;t--;)o[t][0]===arguments[e]&&(o.splice(t,1),r--)}else o.splice(0)}}}},i=(t,e)=>{const o=Object.defineProperties({},s);o[d]=e;for(const r in t)o[r]=m(t[r],r,e);return o},l=(e,o)=>{var r=e.length;const n=Object.defineProperties(new Array(r),s);n[d]=o;for(let t=0;t<r;t++)n[t]=m(e[t],t,o);return n},f=(t,e)=>(Object.defineProperties(t,s),t[d]=e,t),c=(t,e)=>{if(!t)return e;let o=e;if(t.path){const r=t.path;o=e.filter(t=>t.path.join(".")===r)}else if(t.pathsOf){const n=t.pathsOf,s=n.join(".");o=e.filter(t=>(t.path.length===n.length+1||t.path.length===n.length&&(t.type===h||t.type===a))&&t.path.join(".").startsWith(s))}else if(t.pathsFrom){const i=t.pathsFrom;o=e.filter(t=>t.path.join(".").startsWith(i))}return o},p=(e,o)=>{try{e(o)}catch(t){console.error(`failed to notify listener ${e} with `+o,t)}},u=function(){var t,e,o=this.batches;this.batches=null;for([t,e]of o)p(t,e)},j=(t,e)=>{let o=t,r,n,s,i,h;var a=e.length;do{for(r=o.observers,h=r.length;h--;)if([n,s]=r[h],(i=c(s,e)).length)if(o.options.async){o.batches||(o.batches=[],queueMicrotask(u.bind(o)));let t;for(const l of o.batches)if(l[0]===n){t=l;break}t||(t=[n,[]],o.batches.push(t)),Array.prototype.push.apply(t[1],i)}else p(n,i);if(o.parent){const f=new Array(a);for(let t=0;t<a;t++)f[t]={...e[t]},f[t].path=[o.ownKey,...f[t].path];e=f,o=o.parent}else o=null}while(o)},m=(t,e,o)=>t&&"object"==typeof t?Array.isArray(t)?new A({target:t,ownKey:e,parent:o}).proxy:ArrayBuffer.isView(t)?new K({target:t,ownKey:e,parent:o}).proxy:t instanceof Date||t instanceof Blob||t instanceof Error?t:new M({target:t,ownKey:e,parent:o}).proxy:t,y={pop:function(){const t=this[d],e=t.target,o=e.length-1;let r=e.pop();if(r&&"object"==typeof r){const s=r[d];s&&(r=s.detach())}var n=[new O(g,[o],void 0,r,this)];return j(t,n),r},push:function(){const e=this[d],o=e.target,r=arguments.length,n=new Array(r),s=o.length;for(let t=0;t<r;t++)n[t]=m(arguments[t],s+t,e);var t=Reflect.apply(o.push,o,n);const i=[];for(let t=s,e=o.length;t<e;t++)i[t-s]=new O(w,[t],o[t],void 0,this);return j(e,i),t},shift:function(){const t=this[d],e=t.target;let o,r,n,s,i;for(o=e.shift(),o&&"object"==typeof o&&(i=o[d],i&&(o=i.detach())),r=0,n=e.length;r<n;r++)(s=e[r])&&"object"==typeof s&&(i=s[d],i&&(i.ownKey=r));var h=[new O(g,[0],void 0,o,this)];return j(t,h),o},unshift:function(){const e=this[d],r=e.target,o=arguments.length,n=new Array(o);for(let t=0;t<o;t++)n[t]=m(arguments[t],t,e);var t=Reflect.apply(r.unshift,r,n);for(let t=0,e=r.length,o;t<e;t++)if((o=r[t])&&"object"==typeof o){const h=o[d];h&&(h.ownKey=t)}var s=n.length;const i=new Array(s);for(let t=0;t<s;t++)i[t]=new O(w,[t],r[t],void 0,this);return j(e,i),t},reverse:t,sort:e,fill:o,copyWithin:r,splice:function(){const e=this[d],o=e.target,r=arguments.length,n=new Array(r),t=o.length;for(let t=0;t<r;t++)n[t]=m(arguments[t],t,e);const s=0===r?0:n[0]<0?t+n[0]:n[0],i=r<2?t-s:n[1],h=Math.max(r-2,0),a=Reflect.apply(o.splice,o,n),l=o.length;let f;for(let t=0,e;t<l;t++)(e=o[t])&&"object"==typeof e&&(f=e[d],f&&(f.ownKey=t));let c,p,u;for(c=0,p=a.length;c<p;c++)(u=a[c])&&"object"==typeof u&&(f=u[d],f&&(a[c]=f.detach()));const y=[];let b;for(b=0;b<i;b++)b<h?y.push(new O(v,[s+b],o[s+b],a[b],this)):y.push(new O(g,[s+b],void 0,a[b],this));for(;b<h;b++)y.push(new O(w,[s+b],o[s+b],void 0,this));return j(e,y),a}},b={reverse:t,sort:e,fill:o,copyWithin:r,set:function(t,e){const o=this[d],r=o.target,n=t.length,s=r.slice(0);e=e||0,r.set(t,e);const i=new Array(n);for(let t=e;t<n+e;t++)i[t-e]=new O(v,[t],r[t],s[t],this);j(o,i)}};class O{constructor(t,e,o,r,n){this.type=t,this.path=e,this.value=o,this.oldValue=r,this.object=n}}class x{constructor(t,e){var{target:o,parent:r,ownKey:n}=t;r&&void 0!==n?(this.parent=r,this.ownKey=n):(this.parent=null,this.ownKey=null);o=e(o,this);this.observers=[],this.revocable=Proxy.revocable(o,this),this.proxy=this.revocable.proxy,this.target=o,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 n));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(t,e,o){let r=t[e];if(o!==r){o=m(o,e,this);if(t[e]=o,r&&"object"==typeof r){const n=r[d];n&&(r=n.detach())}o=void 0===r?[new O(w,[e],o,void 0,this.proxy)]:[new O(v,[e],o,r,this.proxy)];j(this,o)}return!0}deleteProperty(t,e){let o=t[e];if(delete t[e],o&&"object"==typeof o){const r=o[d];r&&(o=r.detach())}e=[new O(g,[e],void 0,o,this.proxy)];return j(this,e),!0}}class M extends x{constructor(t){super(t,i)}}class A extends x{constructor(t){super(t,l)}get(t,e){return y[e]||t[e]}}class K extends x{constructor(t){super(t,f)}get(t,e){return b[e]||t[e]}}const E=Object.freeze({from:(t,e)=>{if(t&&"object"==typeof t){if(t[d])return t;if(Array.isArray(t))return new A({target:t,ownKey:null,parent:null,options:e}).proxy;if(ArrayBuffer.isView(t))return new K({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 M({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[d])}),S=Symbol("callback-key"),k=Symbol("targets-key");class T{constructor(t){this[S]=t,this[k]=new Set,Object.freeze(this)}observe(t,e){const o=E.from(t);return o.observe(this[S],e),this[k].add(o),o}unobserve(t){t.unobserve(this[S]),this[k].delete(t)}disconnect(){for(const t of this[k])t.unobserve(this[S]);this[k].clear()}}export{E as Observable,T as ObjectObserver}; | ||
function t(){const t=this[d],e=t.target;let o,r,n;for(e.reverse(),o=0,r=e.length;o<r;o++)if((n=e[o])&&"object"==typeof n){const i=n[d];i&&(i.ownKey=o)}var s=[new O(h,[],void 0,void 0,this)];return j(t,s),this}function e(t){const e=this[d],o=e.target;let r,n,s;for(o.sort(t),r=0,n=o.length;r<n;r++)if((s=o[r])&&"object"==typeof s){const i=s[d];i&&(i.ownKey=r)}return t=[new O(a,[],void 0,void 0,this)],j(e,t),this}function o(t,n,s){const i=this[d],h=i.target,a=[],e=h.length,l=h.slice(0);if(n=void 0===n?0:n<0?Math.max(e+n,0):Math.min(n,e),s=void 0===s?e:s<0?Math.max(e+s,0):Math.min(s,e),n<e&&n<s){h.fill(t,n,s);let r;for(let t=n,e,o;t<s;t++)e=h[t],h[t]=m(e,t,i),t in l?(o=l[t],o&&"object"==typeof o&&(r=o[d],r&&(o=r.detach())),a.push(new O(v,[t],h[t],o,this))):a.push(new O(w,[t],h[t],void 0,this));j(i,a)}return this}function r(n,t,e){const s=this[d],i=s.target,o=i.length;n=n<0?Math.max(o+n,0):n,t=void 0===t?0:t<0?Math.max(o+t,0):Math.min(t,o),e=void 0===e?o:e<0?Math.max(o+e,0):Math.min(e,o);var h=Math.min(e-t,o-n);if(n<o&&n!==t&&0<h){const a=i.slice(0),l=[];i.copyWithin(n,t,e);for(let t=n,e,o,r;t<n+h;t++)e=i[t],e&&"object"==typeof e&&(e=m(e,t,s),i[t]=e),o=a[t],o&&"object"==typeof o&&(r=o[d],r&&(o=r.detach())),"object"!=typeof e&&e===o||l.push(new O(v,[t],e,o,this));j(s,l)}return this}const w="insert",v="update",g="delete",h="reverse",a="shuffle",d=Symbol.for("object-observer-meta-key-0"),n={async:1},s={observe:{value:function(e,t){if("function"!=typeof e)throw new Error(`observer MUST be a function, got '${e}'`);const o=this[d].observers;o.some(t=>t[0]===e)?console.warn("observer may be bound to an observable only once; will NOT rebind"):o.push([e,(t=>{if(!t||"object"!=typeof t)return null;const e={},o=[];for(var[r,n]of Object.entries(t))if("path"===r){if("string"!=typeof n||""===n)throw new Error('"path" option, if/when provided, MUST be a non-empty string');e[r]=n}else if("pathsOf"===r){if(t.path)throw new Error('"pathsOf" option MAY NOT be specified together with "path" option');if("string"!=typeof n)throw new Error('"pathsOf" option, if/when provided, MUST be a string (MAY be empty)');e[r]=t.pathsOf.split(".").filter(Boolean)}else if("pathsFrom"===r){if(t.path||t.pathsOf)throw new Error('"pathsFrom" option MAY NOT be specified together with "path"/"pathsOf" option/s');if("string"!=typeof n||""===n)throw new Error('"pathsFrom" option, if/when provided, MUST be a non-empty string');e[r]=n}else o.push(r);if(o.length)throw new Error(`'${o.join(", ")}' is/are not a valid observer option/s`);return e})(t)])}},unobserve:{value:function(){const o=this[d].observers;let r=o.length;if(r){let e=arguments.length;if(e)for(;e--;){let t=r;for(;t--;)o[t][0]===arguments[e]&&(o.splice(t,1),r--)}else o.splice(0)}}}},i=(t,e)=>{const o=Object.defineProperties({},s);o[d]=e;for(const r in t)o[r]=m(t[r],r,e);return o},l=(e,o)=>{var r=e.length;const n=Object.defineProperties(new Array(r),s);n[d]=o;for(let t=0;t<r;t++)n[t]=m(e[t],t,o);return n},f=(t,e)=>(Object.defineProperties(t,s),t[d]=e,t),c=(t,e)=>{if(!t)return e;let o=e;if(t.path){const r=t.path;o=e.filter(t=>t.path.join(".")===r)}else if(t.pathsOf){const n=t.pathsOf,s=n.join(".");o=e.filter(t=>(t.path.length===n.length+1||t.path.length===n.length&&(t.type===h||t.type===a))&&t.path.join(".").startsWith(s))}else if(t.pathsFrom){const i=t.pathsFrom;o=e.filter(t=>t.path.join(".").startsWith(i))}return o},p=(e,o)=>{try{e(o)}catch(t){console.error(`failed to notify listener ${e} with `+o,t)}},b=function(){var t,e,o=this.batches;this.batches=null;for([t,e]of o)p(t,e)},j=(t,e)=>{let o=t,r,n,s,i,h;var a=e.length;do{for(r=o.observers,h=r.length;h--;)if([n,s]=r[h],(i=c(s,e)).length)if(o.options.async){o.batches||(o.batches=[],queueMicrotask(b.bind(o)));let t;for(const l of o.batches)if(l[0]===n){t=l;break}t||(t=[n,[]],o.batches.push(t)),Array.prototype.push.apply(t[1],i)}else p(n,i);if(o.parent){const f=new Array(a);for(let t=0;t<a;t++)f[t]={...e[t]},f[t].path=[o.ownKey,...f[t].path];e=f,o=o.parent}else o=null}while(o)},m=(t,e,o)=>t&&"object"==typeof t?Array.isArray(t)?new A({target:t,ownKey:e,parent:o}).proxy:ArrayBuffer.isView(t)?new K({target:t,ownKey:e,parent:o}).proxy:t instanceof Date||t instanceof Error||globalThis.Blob&&t instanceof Blob?t:new M({target:t,ownKey:e,parent:o}).proxy:t,u={pop:function(){const t=this[d],e=t.target,o=e.length-1;let r=e.pop();if(r&&"object"==typeof r){const s=r[d];s&&(r=s.detach())}var n=[new O(g,[o],void 0,r,this)];return j(t,n),r},push:function(){const e=this[d],o=e.target,r=arguments.length,n=new Array(r),s=o.length;for(let t=0;t<r;t++)n[t]=m(arguments[t],s+t,e);var t=Reflect.apply(o.push,o,n);const i=[];for(let t=s,e=o.length;t<e;t++)i[t-s]=new O(w,[t],o[t],void 0,this);return j(e,i),t},shift:function(){const t=this[d],e=t.target;let o,r,n,s,i;for(o=e.shift(),o&&"object"==typeof o&&(i=o[d],i&&(o=i.detach())),r=0,n=e.length;r<n;r++)(s=e[r])&&"object"==typeof s&&(i=s[d],i&&(i.ownKey=r));var h=[new O(g,[0],void 0,o,this)];return j(t,h),o},unshift:function(){const e=this[d],r=e.target,o=arguments.length,n=new Array(o);for(let t=0;t<o;t++)n[t]=m(arguments[t],t,e);var t=Reflect.apply(r.unshift,r,n);for(let t=0,e=r.length,o;t<e;t++)if((o=r[t])&&"object"==typeof o){const h=o[d];h&&(h.ownKey=t)}var s=n.length;const i=new Array(s);for(let t=0;t<s;t++)i[t]=new O(w,[t],r[t],void 0,this);return j(e,i),t},reverse:t,sort:e,fill:o,copyWithin:r,splice:function(){const e=this[d],o=e.target,r=arguments.length,n=new Array(r),t=o.length;for(let t=0;t<r;t++)n[t]=m(arguments[t],t,e);const s=0===r?0:n[0]<0?t+n[0]:n[0],i=r<2?t-s:n[1],h=Math.max(r-2,0),a=Reflect.apply(o.splice,o,n),l=o.length;let f;for(let t=0,e;t<l;t++)(e=o[t])&&"object"==typeof e&&(f=e[d],f&&(f.ownKey=t));let c,p,b;for(c=0,p=a.length;c<p;c++)(b=a[c])&&"object"==typeof b&&(f=b[d],f&&(a[c]=f.detach()));const u=[];let y;for(y=0;y<i;y++)y<h?u.push(new O(v,[s+y],o[s+y],a[y],this)):u.push(new O(g,[s+y],void 0,a[y],this));for(;y<h;y++)u.push(new O(w,[s+y],o[s+y],void 0,this));return j(e,u),a}},y={reverse:t,sort:e,fill:o,copyWithin:r,set:function(t,e){const o=this[d],r=o.target,n=t.length,s=r.slice(0);e=e||0,r.set(t,e);const i=new Array(n);for(let t=e;t<n+e;t++)i[t-e]=new O(v,[t],r[t],s[t],this);j(o,i)}};class O{constructor(t,e,o,r,n){this.type=t,this.path=e,this.value=o,this.oldValue=r,this.object=n}}class x{constructor(t,e){var{target:o,parent:r,ownKey:n}=t;r&&void 0!==n?(this.parent=r,this.ownKey=n):(this.parent=null,this.ownKey=null);o=e(o,this);this.observers=[],this.revocable=Proxy.revocable(o,this),this.proxy=this.revocable.proxy,this.target=o,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 n));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(t,e,o){let r=t[e];if(o!==r){o=m(o,e,this);if(t[e]=o,r&&"object"==typeof r){const n=r[d];n&&(r=n.detach())}o=void 0===r?[new O(w,[e],o,void 0,this.proxy)]:[new O(v,[e],o,r,this.proxy)];j(this,o)}return!0}deleteProperty(t,e){let o=t[e];if(delete t[e],o&&"object"==typeof o){const r=o[d];r&&(o=r.detach())}e=[new O(g,[e],void 0,o,this.proxy)];return j(this,e),!0}}class M extends x{constructor(t){super(t,i)}}class A extends x{constructor(t){super(t,l)}get(t,e){return u[e]||t[e]}}class K extends x{constructor(t){super(t,f)}get(t,e){return y[e]||t[e]}}const E=Object.freeze({from:(t,e)=>{if(t&&"object"==typeof t){if(t[d])return t;if(Array.isArray(t))return new A({target:t,ownKey:null,parent:null,options:e}).proxy;if(ArrayBuffer.isView(t))return new K({target:t,ownKey:null,parent:null,options:e}).proxy;if(t instanceof Date||t instanceof Error||globalThis.Blob&&t instanceof Blob)throw new Error(t+" found to be one of a on-observable types");return new M({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[d])}),T=Symbol("callback-key"),S=Symbol("targets-key");class k{constructor(t){this[T]=t,this[S]=new Set,Object.freeze(this)}observe(t,e){const o=E.from(t);return o.observe(this[T],e),this[S].add(o),o}unobserve(t){t.unobserve(this[T]),this[S].delete(t)}disconnect(){for(const t of this[S])t.unobserve(this[T]);this[S].clear()}}export{E as Observable,k as ObjectObserver}; |
{ | ||
"name": "object-observer", | ||
"version": "4.4.0", | ||
"version": "4.5.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": [ |
@@ -30,3 +30,8 @@ [![npm](https://img.shields.io/npm/v/object-observer.svg?label=npm%20object-observer)](https://www.npmjs.com/package/object-observer) | ||
Supported: ![CHROME](docs/browser-icons/chrome.png)<sub>61+</sub> | ![FIREFOX](docs/browser-icons/firefox.png)<sub>60+</sub> | ![EDGE](docs/browser-icons/edge.png)<sub>16+</sub> | ![NODE JS](docs/browser-icons/nodejs.png) <sub>8.10.0+</sub> | ||
Supported: | ||
![CHROME](docs/browser-icons/chrome.png)<sub>71+</sub> | | ||
![FIREFOX](docs/browser-icons/firefox.png)<sub>65+</sub> | | ||
![EDGE](docs/browser-icons/edge-chromium.png)<sub>79+</sub> | | ||
![SAFARI](docs/browser-icons/safari-ios.png)<sub>12.1</sub> | | ||
![NODE JS](docs/browser-icons/nodejs.png) <sub>12.0.0+</sub> | ||
@@ -33,0 +38,0 @@ Performance report can be found [here](docs/performance-report.md). |
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
67806
251