immer-peasy
Version:
Easy Peasy friendly modification of immer
3 lines (2 loc) • 12.1 kB
JavaScript
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((e=e||self).immer={})}(this,function(e){"use strict";var r,t="undefined"!=typeof Symbol?Symbol("immer-nothing"):((r={})["immer-nothing"]=!0,r),n="undefined"!=typeof Symbol&&Symbol.for?Symbol.for("immer-draftable"):"__$immer_draftable",o="undefined"!=typeof Symbol&&Symbol.for?Symbol.for("immer-state"):"__$immer_state";function i(e){return!!e&&!!e[o]}function a(e){if(!e||"object"!=typeof e)return!1;if(Array.isArray(e))return!0;var r=Object.getPrototypeOf(e);return!r||r===Object.prototype||(!!e[n]||!!e.constructor[n])}var s=Object.assign||function(e,r){for(var t in r)l(r,t)&&(e[t]=r[t]);return e},f="undefined"!=typeof Reflect&&Reflect.ownKeys?Reflect.ownKeys:void 0!==Object.getOwnPropertySymbols?function(e){return Object.getOwnPropertyNames(e).concat(Object.getOwnPropertySymbols(e))}:Object.getOwnPropertyNames;function c(e,r){if(void 0===r&&(r=!1),Array.isArray(e))return e.slice();var t=Object.create(Object.getPrototypeOf(e));return f(e).forEach(function(n){if(n!==o){var i=Object.getOwnPropertyDescriptor(e,n),a=i.value;i.get&&r&&(a=i.get.call(e)),i.enumerable?t[n]=a:r&&Object.defineProperty(t,n,{value:a,writable:!0,configurable:!0})}}),t}function u(e,r){if(Array.isArray(e))for(var t=0;t<e.length;t++)r(t,e[t],e);else f(e).forEach(function(t){return r(t,e[t],e)})}function p(e,r){var t=Object.getOwnPropertyDescriptor(e,r);return!!t&&t.enumerable}function l(e,r){return Object.prototype.hasOwnProperty.call(e,r)}function d(e,r){return e===r?0!==e||1/e==1/r:e!=e&&r!=r}var h=function(e){this.drafts=[],this.parent=e,this.canAutoFreeze=!0,this.patches=null};function y(e){e[o].revoke()}h.prototype.usePatches=function(e){e&&(this.patches=[],this.inversePatches=[],this.patchListener=e)},h.prototype.revoke=function(){this.leave(),this.drafts.forEach(y),this.drafts=null},h.prototype.leave=function(){this===h.current&&(h.current=this.parent)},h.current=null,h.enter=function(){return this.current=new h(this.current)};var v={};function b(e,r){var t=Array.isArray(e),n=z(e);u(n,function(r){!function(e,r,t){var n=v[r];n?n.enumerable=t:v[r]=n={configurable:!0,enumerable:t,get:function(){return function(e,r){j(e);var t=P(m(e),r);if(e.finalizing)return t;if(t===P(e.base,r)&&a(t))return O(e),e.copy[r]=b(t,e);return t}(this[o],r)},set:function(e){!function(e,r,t){if(j(e),e.assigned[r]=!0,!e.modified){if(d(t,P(m(e),r)))return;w(e),O(e)}e.copy[r]=t}(this[o],r,e)}};Object.defineProperty(e,r,n)}(n,r,t||p(e,r))});var i,s,f,c=r?r.scope:h.current;return i=n,s=o,f={scope:c,modified:!1,finalizing:!1,finalized:!1,assigned:{},parent:r,base:e,draft:n,copy:null,revoke:g,revoked:!1},Object.defineProperty(i,s,{value:f,enumerable:!1,writable:!0}),c.drafts.push(n),n}function g(){this.revoked=!0}function m(e){return e.copy||e.base}function P(e,r){var t=e[o];if(t&&!t.finalizing){t.finalizing=!0;var n=e[r];return t.finalizing=!1,n}return e[r]}function w(e){e.modified||(e.modified=!0,e.parent&&w(e.parent))}function O(e){e.copy||(e.copy=z(e.base))}function z(e){var r=e&&e[o];if(r){r.finalizing=!0;var t=c(r.draft,!0);return r.finalizing=!1,t}return c(e)}function j(e){if(!0===e.revoked)throw new Error("Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? "+JSON.stringify(m(e)))}function A(e){for(var r=e.length-1;r>=0;r--){var t=e[r][o];t.modified||(Array.isArray(t.base)?E(t)&&w(t):D(t)&&w(t))}}function D(e){for(var r=e.base,t=e.draft,n=Object.keys(t),i=n.length-1;i>=0;i--){var a=n[i],s=r[a];if(void 0===s&&!l(r,a))return!0;var f=t[a],c=f&&f[o];if(c?c.base!==s:!d(f,s))return!0}return n.length!==Object.keys(r).length}function E(e){var r=e.draft;if(r.length!==e.base.length)return!0;var t=Object.getOwnPropertyDescriptor(r,r.length-1);return!(!t||t.get)}var k=Object.freeze({willFinalize:function(e,r,t){e.drafts.forEach(function(e){e[o].finalizing=!0}),t?i(r)&&r[o].scope===e&&A(e.drafts):(e.patches&&function e(r){if(r&&"object"==typeof r){var t=r[o];if(t){var n=t.base,i=t.draft,a=t.assigned;if(Array.isArray(r)){if(E(t)){if(w(t),a.length=!0,i.length<n.length)for(var s=i.length;s<n.length;s++)a[s]=!1;else for(var f=n.length;f<i.length;f++)a[f]=!0;for(var c=0;c<i.length;c++)void 0===a[c]&&e(i[c])}}else Object.keys(i).forEach(function(r){void 0!==n[r]||l(n,r)?a[r]||e(i[r]):(a[r]=!0,w(t))}),Object.keys(n).forEach(function(e){void 0!==i[e]||l(i,e)||(a[e]=!1,w(t))})}}}(e.drafts[0]),A(e.drafts))},createProxy:b});function x(e,r){var t=r?r.scope:h.current,n={scope:t,modified:!1,finalized:!1,assigned:{},parent:r,base:e,draft:null,drafts:{},copy:null,revoke:null},o=Array.isArray(e)?Proxy.revocable([n],R):Proxy.revocable(n,F),i=o.revoke,a=o.proxy;return n.draft=a,n.revoke=i,t.drafts.push(a),a}var F={get:function(e,r){if(r===o)return e;var t=e.drafts;if(!e.modified&&l(t,r))return t[r];var n=S(e)[r];if(e.finalized||!a(n))return n;if(e.modified){if(n!==N(e.base,r))return n;t=e.copy}return t[r]=x(n,e)},has:function(e,r){return r in S(e)},ownKeys:function(e){return Reflect.ownKeys(S(e))},set:function(e,r,t){if(!e.modified){var n=N(e.base,r),o=t?d(n,t)||t===e.drafts[r]:d(n,t)&&r in e.base;if(o)return!0;_(e)}return e.assigned[r]=!0,e.copy[r]=t,!0},deleteProperty:function(e,r){(void 0!==N(e.base,r)||r in e.base)&&(e.assigned[r]=!1,_(e));e.copy&&delete e.copy[r];return!0},getOwnPropertyDescriptor:function(e,r){var t=S(e),n=Reflect.getOwnPropertyDescriptor(t,r);n&&(n.writable=!0,n.configurable=!Array.isArray(t)||"length"!==r);return n},defineProperty:function(){throw new Error("Object.defineProperty() cannot be used on an Immer draft")},getPrototypeOf:function(e){return Object.getPrototypeOf(e.base)},setPrototypeOf:function(){throw new Error("Object.setPrototypeOf() cannot be used on an Immer draft")}},R={};function S(e){return e.copy||e.base}function N(e,r){var t=e[o],n=Reflect.getOwnPropertyDescriptor(t?S(t):e,r);return n&&n.value}function _(e){e.modified||(e.modified=!0,e.copy=s(c(e.base),e.drafts),e.drafts=null,e.parent&&_(e.parent))}u(F,function(e,r){R[e]=function(){return arguments[0]=arguments[0][0],r.apply(this,arguments)}}),R.deleteProperty=function(e,r){if(isNaN(parseInt(r)))throw new Error("Immer only supports deleting array indices");return F.deleteProperty.call(this,e[0],r)},R.set=function(e,r,t){if("length"!==r&&isNaN(parseInt(r)))throw new Error("Immer only supports setting array indices and the 'length' property");return F.set.call(this,e[0],r,t)};var I=Object.freeze({willFinalize:function(){},createProxy:x});function T(e,r,t,n){Array.isArray(e.base)?function(e,r,t,n){var o,i,a=e.base,s=e.copy,f=e.assigned;s.length<a.length&&(a=(o=[s,a])[0],s=o[1],t=(i=[n,t])[0],n=i[1]);var c=s.length-a.length,u=0;for(;a[u]===s[u]&&u<a.length;)++u;var p=a.length;for(;p>u&&a[p-1]===s[p+c-1];)--p;for(var l=u;l<p;++l)if(f[l]&&s[l]!==a[l]){var d=r.concat([l]);t.push({op:"replace",path:d,value:s[l]}),n.push({op:"replace",path:d,value:a[l]})}for(var h=p!=a.length,y=t.length,v=p+c-1;v>=p;--v){var b=r.concat([v]);t[y+v-p]={op:"add",path:b,value:s[v]},h&&n.push({op:"remove",path:b})}h||n.push({op:"replace",path:r.concat(["length"]),value:a.length})}(e,r,t,n):function(e,r,t,n){var o=e.base,i=e.copy;u(e.assigned,function(e,a){var s=o[e],f=i[e],c=a?e in o?"replace":"add":"remove";if(s!==f||"replace"!==c){var u=r.concat(e);t.push("remove"===c?{op:c,path:u}:{op:c,path:u,value:f}),n.push("add"===c?{op:"remove",path:u}:"remove"===c?{op:"add",path:u,value:s}:{op:"replace",path:u,value:s})}})}(e,r,t,n)}function C(e,r){for(var t=0;t<r.length;t++){var n=r[t],o=n.path;if(0===o.length&&"replace"===n.op)e=n.value;else{for(var i=e,a=0;a<o.length-1;a++)if(!(i=i[o[a]])||"object"!=typeof i)throw new Error("Cannot apply patch, path doesn't resolve: "+o.join("/"));var s=o[o.length-1];switch(n.op){case"replace":i[s]=n.value;break;case"add":Array.isArray(i)?i.splice(s,0,n.value):i[s]=n.value;break;case"remove":Array.isArray(i)?i.splice(s,1):delete i[s];break;default:throw new Error("Unsupported patch operation: "+n.op)}}}return e}var U={useProxies:"undefined"!=typeof Proxy&&"undefined"!=typeof Reflect,autoFreeze:"undefined"!=typeof process?"production"!==process.env.NODE_ENV:"verifyMinified"===function(){}.name,onAssign:null,onDelete:null,onCopy:null},K=function(e){s(this,U,e),this.setUseProxies(this.useProxies),this.produce=this.produce.bind(this)};K.prototype.produce=function(e,r,n){var o,i=this;if("function"==typeof e&&"function"!=typeof r){var s=r;r=e;var f=this;return function(e){var t=this;void 0===e&&(e=s);for(var n=[],o=arguments.length-1;o-- >0;)n[o]=arguments[o+1];return f.produce(e,function(e){return r.call.apply(r,[t,e].concat(n))})}}if("function"!=typeof r)throw new Error("The first or second argument to `produce` must be a function");if(void 0!==n&&"function"!=typeof n)throw new Error("The third argument to `produce` must be a function or undefined");if(a(e)){var c=h.enter(),u=this.createProxy(e),p=!0;try{o=r(u),p=!1}finally{p?c.revoke():c.leave()}return o instanceof Promise?o.then(function(e){return c.usePatches(n),i.processResult(e,c)},function(e){throw c.revoke(),e}):(c.usePatches(n),this.processResult(o,c))}return void 0===(o=r(e))?e:o!==t?o:void 0},K.prototype.createDraft=function(e){if(!a(e))throw new Error("First argument to `createDraft` must be a plain object, an array, or an immerable object");var r=h.enter(),t=this.createProxy(e);return t[o].isManual=!0,r.leave(),t},K.prototype.finishDraft=function(e,r){var t=e&&e[o];if(!t||!t.isManual)throw new Error("First argument to `finishDraft` must be a draft returned by `createDraft`");if(t.finalized)throw new Error("The given draft is already finalized");var n=t.scope;return n.usePatches(r),this.processResult(void 0,n)},K.prototype.setAutoFreeze=function(e){this.autoFreeze=e},K.prototype.setUseProxies=function(e){this.useProxies=e,s(this,e?I:k)},K.prototype.applyPatches=function(e,r){return i(e)?C(e,r):this.produce(e,function(e){return C(e,r)})},K.prototype.processResult=function(e,r){var n=r.drafts[0],i=void 0!==e&&e!==n;if(this.willFinalize(r,e,i),i){if(n[o].modified)throw r.revoke(),new Error("An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.");a(e)&&(e=this.finalize(e,null,r)),r.patches&&(r.patches.push({op:"replace",path:[],value:e}),r.inversePatches.push({op:"replace",path:[],value:n[o].base}))}else e=this.finalize(n,[],r);return r.revoke(),r.patches&&r.patchListener(r.patches,r.inversePatches),e!==t?e:void 0},K.prototype.finalize=function(e,r,t){var n=this,i=e[o];if(!i)return Object.isFrozen(e)?e:this.finalizeTree(e,null,t);if(i.scope!==t)return e;if(!i.modified)return i.base;if(!i.finalized){if(i.finalized=!0,this.finalizeTree(i.draft,r,t),this.onDelete)if(this.useProxies){var a=i.assigned;for(var s in a)a[s]||this.onDelete(i,s)}else{var f=i.base,c=i.copy;u(f,function(e){l(c,e)||n.onDelete(i,e)})}this.onCopy&&this.onCopy(i),this.autoFreeze&&t.canAutoFreeze&&Object.freeze(i.copy),r&&t.patches&&T(i,r,t.patches,t.inversePatches)}return i.copy},K.prototype.finalizeTree=function(e,r,t){var n=this,s=e[o];s&&(this.useProxies||(s.copy=c(s.draft,!0)),e=s.copy);var f=!!r&&!!t.patches,l=function(o,c,h){if(c===h)throw Error("Immer forbids circular references");var y=!!s&&h===e;if(i(c)){var v=y&&f&&!s.assigned[o]?r.concat(o):null;if(i(c=n.finalize(c,v,t))&&(t.canAutoFreeze=!1),Array.isArray(h)||p(h,o)?h[o]=c:Object.defineProperty(h,o,{value:c}),y&&c===s.base[o])return}else{if(y&&d(c,s.base[o]))return;a(c)&&!Object.isFrozen(c)&&u(c,l)}y&&n.onAssign&&n.onAssign(s,o,c)};return u(e,l),e};var M=new K,L=M.produce,$=M.setAutoFreeze.bind(M),J=M.setUseProxies.bind(M),V=M.applyPatches.bind(M),q=M.createDraft.bind(M),B=M.finishDraft.bind(M);e.Immer=K,e.applyPatches=V,e.createDraft=q,e.default=L,e.finishDraft=B,e.immerable=n,e.isDraft=i,e.isDraftable=a,e.nothing=t,e.original=function(e){if(e&&e[o])return e[o].base},e.produce=L,e.setAutoFreeze=$,e.setUseProxies=J,Object.defineProperty(e,"__esModule",{value:!0})});
//# sourceMappingURL=immer.umd.js.map