UNPKG

immer

Version:

Create your next immutable state by mutating the current one

3 lines (2 loc) 11.3 kB
var e,r;e=this,r=function(e){"use strict";var r,t,n,o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},i=function(e,r){if(!(e instanceof r))throw new TypeError("Cannot call a class as a function")},a=function(){function e(e,r){for(var t=0;t<r.length;t++){var n=r[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(r,t,n){return t&&e(r.prototype,t),n&&e(r,n),r}}(),f="undefined"!=typeof Symbol?Symbol("immer-nothing"):(n=!0,(t="immer-nothing")in(r={})?Object.defineProperty(r,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):r[t]=n,r),u="undefined"!=typeof Symbol?Symbol("immer-draftable"):"__$immer_draftable",c="undefined"!=typeof Symbol?Symbol("immer-state"):"__$immer_state";function s(e){return!!e&&!!e[c]}function l(e){if(!e||"object"!==(void 0===e?"undefined":o(e)))return!1;if(Array.isArray(e))return!0;var r=Object.getPrototypeOf(e);return!r||r===Object.prototype||(!!e[u]||!!e.constructor[u])}var p=Object.assign||function(e,r){for(var t in r)b(r,t)&&(e[t]=r[t]);return e},d="undefined"!=typeof Reflect&&Reflect.ownKeys?Reflect.ownKeys:void 0!==Object.getOwnPropertySymbols?function(e){return Object.getOwnPropertyNames(e).concat(Object.getOwnPropertySymbols(e))}:Object.getOwnPropertyNames;function h(e){var r=arguments.length>1&&void 0!==arguments[1]&&arguments[1];if(Array.isArray(e))return e.slice();var t=Object.create(Object.getPrototypeOf(e));return d(e).forEach(function(n){if(n!==c){var o=Object.getOwnPropertyDescriptor(e,n);if(o.get){if(!r)throw new Error("Immer drafts cannot have computed properties");o.value=o.get.call(e)}o.enumerable?t[n]=o.value:Object.defineProperty(t,n,{value:o.value,writable:!0,configurable:!0})}}),t}function y(e,r){if(Array.isArray(e))for(var t=0;t<e.length;t++)r(t,e[t],e);else d(e).forEach(function(t){return r(t,e[t],e)})}function v(e,r){return Object.getOwnPropertyDescriptor(e,r).enumerable}function b(e,r){return Object.prototype.hasOwnProperty.call(e,r)}function g(e,r){return e===r?0!==e||1/e==1/r:e!=e&&r!=r}function m(e,r,t,n){Array.isArray(e.base)?function(e,r,t,n){for(var o=e.base,i=e.copy,a=e.assigned,f=Math.min(o.length,i.length),u=0;u<f;u++)if(a[u]&&o[u]!==i[u]){var c=r.concat(u);t.push({op:"replace",path:c,value:i[u]}),n.push({op:"replace",path:c,value:o[u]})}if(f<i.length){for(var s=f;s<i.length;s++)t.push({op:"add",path:r.concat(s),value:i[s]});n.push({op:"replace",path:r.concat("length"),value:o.length})}else if(f<o.length){t.push({op:"replace",path:r.concat("length"),value:i.length});for(var l=f;l<o.length;l++)n.push({op:"add",path:r.concat(l),value:o[l]})}}(e,r,t,n):function(e,r,t,n){var o=e.base,i=e.copy;y(e.assigned,function(e,a){var f=o[e],u=i[e],c=a?e in o?"replace":"add":"remove";if(f!==u||"replace"!==c){var s=r.concat(e);t.push("remove"===c?{op:c,path:s}:{op:c,path:s,value:u}),n.push("add"===c?{op:"remove",path:s}:"remove"===c?{op:"add",path:s,value:f}:{op:"replace",path:s,value:f})}})}(e,r,t,n)}var w={},O=[],P=function(){return O[O.length-1]};function j(e,r){var t=Array.isArray(e),n=x(e);y(n,function(r){!function(e,r,t){var n=w[r];n?n.enumerable=t:w[r]=n={configurable:!0,enumerable:t,get:function(){return function(e,r){S(e);var t=A(e)[r];if(!e.finalizing&&t===e.base[r]&&l(t))return k(e),e.copy[r]=j(t,e);return t}(this[c],r)},set:function(e){!function(e,r,t){if(S(e),e.assigned[r]=!0,!e.modified){if(g(A(e)[r],t))return;E(e),k(e)}e.copy[r]=t}(this[c],r,e)}};Object.defineProperty(e,r,n)}(n,r,t||v(e,r))});var o,i,a,f={scope:r?r.scope:P(),modified:!1,finalizing:!1,finalized:!1,assigned:{},parent:r,base:e,draft:n,copy:null,revoke:z,revoked:!1};return o=n,i=c,a=f,Object.defineProperty(o,i,{value:a,enumerable:!1,writable:!0}),f.scope.push(f),n}function z(){this.revoked=!0}function A(e){return e.copy||e.base}function E(e){e.modified||(e.modified=!0,e.parent&&E(e.parent))}function k(e){e.copy||(e.copy=x(e.base))}function x(e){var r=e&&e[c];if(r){r.finalizing=!0;var t=h(r.draft,!0);return r.finalizing=!1,t}return h(e)}function S(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(A(e)))}function D(e){for(var r=e.base,t=e.draft,n=Object.keys(t),o=n.length-1;o>=0;o--)if(void 0===r[n[o]]&&!b(r,n[o]))return!0;return n.length!==Object.keys(r).length}function F(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 I=Object.freeze({scopes:O,currentScope:P,willFinalize:function(e,r,t){var n=P();n.forEach(function(e){return e.finalizing=!0}),void 0!==e&&e!==r||(t&&function e(r){if(r&&"object"===(void 0===r?"undefined":o(r))){var t=r[c];if(t){var n=t.base,i=t.draft,a=t.assigned;if(Array.isArray(r)){if(F(t)){if(E(t),a.length=!0,i.length<n.length)for(var f=i.length;f<n.length;f++)a[f]=!1;else for(var u=n.length;u<i.length;u++)a[u]=!0;for(var s=0;s<i.length;s++)void 0===a[s]&&e(i[s])}}else Object.keys(i).forEach(function(r){void 0!==n[r]||b(n,r)?a[r]||e(i[r]):(a[r]=!0,E(t))}),Object.keys(n).forEach(function(e){void 0!==i[e]||b(i,e)||(a[e]=!1,E(t))})}}}(r),function(e){for(var r=e.length-1;r>=0;r--){var t=e[r];!1===t.modified&&(Array.isArray(t.base)?F(t)&&E(t):D(t)&&E(t))}}(n))},createDraft:j}),N=[],_=function(){return N[N.length-1]};function C(e,r){var t={scope:r?r.scope:_(),modified:!1,finalized:!1,assigned:{},parent:r,base:e,draft:null,drafts:{},copy:null,revoke:null},n=Array.isArray(e)?Proxy.revocable([t],U):Proxy.revocable(t,R),o=n.revoke,i=n.proxy;return t.draft=i,t.revoke=o,t.scope.push(t),i}var R={get:function(e,r){if(r===c)return e;var t=e.drafts;if(!e.modified&&b(t,r))return t[r];var n=K(e)[r];if(e.finalized||!l(n))return n;if(e.modified){if(n!==e.base[r])return n;t=e.copy}return t[r]=C(n,e)},has:function(e,r){return r in K(e)},ownKeys:function(e){return Reflect.ownKeys(K(e))},set:function(e,r,t){if(!e.modified){var n=t?g(e.base[r],t)||t===e.drafts[r]:g(e.base[r],t)&&r in e.base;if(n)return!0;T(e)}return e.assigned[r]=!0,e.copy[r]=t,!0},deleteProperty:function(e,r){(void 0!==e.base[r]||r in e.base)&&(e.assigned[r]=!1,T(e));e.copy&&delete e.copy[r];return!0},getOwnPropertyDescriptor:function(e,r){var t=K(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")}},U={};function K(e){return e.copy||e.base}function T(e){e.modified||(e.modified=!0,e.copy=p(h(e.base),e.drafts),e.drafts=null,e.parent&&T(e.parent))}y(R,function(e,r){U[e]=function(){return arguments[0]=arguments[0][0],r.apply(this,arguments)}}),U.deleteProperty=function(e,r){if(isNaN(parseInt(r)))throw new Error("Immer only supports deleting array indices");return R.deleteProperty.call(this,e[0],r)},U.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 R.set.call(this,e[0],r,t)};var M=Object.freeze({scopes:N,currentScope:_,willFinalize:function(){},createDraft:C});var $={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},J=function(){function e(r){i(this,e),p(this,$,r),this.setUseProxies(this.useProxies),this.produce=this.produce.bind(this)}return a(e,[{key:"produce",value:function(e,r,t){var n=this;if("function"==typeof e&&"function"!=typeof r){var o=r;return r=e,function(){for(var e=arguments.length,t=Array(e>1?e-1:0),i=1;i<e;i++)t[i-1]=arguments[i];var a=arguments.length>0&&void 0!==arguments[0]?arguments[0]:o;return n.produce(a,function(e){var n;return(n=r).call.apply(n,[e,e].concat(t))})}}if("function"!=typeof r)throw new Error("if first argument is not a function, the second argument to produce should be a function");if(void 0!==t&&"function"!=typeof t)throw new Error("the third argument of a producer should not be set or a function");var i=void 0;if(l(e)){this.scopes.push([]);var a=this.createDraft(e);try{i=r.call(a,a),this.willFinalize(i,a,!!t);var u=t&&[],s=t&&[];if(void 0===i||i===a)i=this.finalize(a,[],u,s);else{if(a[c].modified)throw new Error("An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.");l(i)&&(i=this.finalize(i)),t&&(u.push({op:"replace",path:[],value:i}),s.push({op:"replace",path:[],value:e}))}}finally{this.currentScope().forEach(function(e){return e.revoke()}),this.scopes.pop()}t&&t(u,s)}else if(void 0===(i=r(e)))return e;return i===f?void 0:i}},{key:"setAutoFreeze",value:function(e){this.autoFreeze=e}},{key:"setUseProxies",value:function(e){this.useProxies=e,p(this,e?M:I)}},{key:"finalize",value:function(e,r,t,n){var o=this,i=e[c];if(!i)return Object.isFrozen(e)?e:this.finalizeTree(e);if(i.scope!==this.currentScope())return e;if(!i.modified)return i.base;if(!i.finalized){if(i.finalized=!0,this.finalizeTree(i.draft,r,t,n),this.onDelete)if(this.useProxies){var a=i.assigned;for(var f in a)a[f]||this.onDelete(i,f)}else{var u=i.base,s=i.copy;y(u,function(e){b(s,e)||o.onDelete(i,e)})}this.onCopy&&this.onCopy(i),this.autoFreeze&&1===this.scopes.length&&Object.freeze(i.copy),t&&m(i,r,t,n)}return i.copy}},{key:"finalizeTree",value:function(e,r,t,n){var o=this,i=e[c];i&&(this.useProxies||(i.finalizing=!0,i.copy=h(i.draft,!0),i.finalizing=!1),e=i.copy);var a=this.onAssign;return y(e,function f(u,c,p){if(c===p)throw Error("Immer forbids circular references");var d=!!i&&p===e;if(s(c)){if(c=t&&d&&!i.assigned[u]?o.finalize(c,r.concat(u),t,n):o.finalize(c),Array.isArray(p)||v(p,u)?p[u]=c:Object.defineProperty(p,u,{value:c}),d&&c===i.base[u])return}else{if(d&&g(c,i.base[u]))return;l(c)&&!Object.isFrozen(c)&&y(c,f)}d&&a&&a(i,u,c)}),e}}]),e}(),V=new J,q=V.produce,B=q(function(e,r){for(var t=0;t<r.length;t++){var n=r[t],i=n.path;if(0===i.length&&"replace"===n.op)e=n.value;else{for(var a=e,f=0;f<i.length-1;f++)if(!(a=a[i[f]])||"object"!==(void 0===a?"undefined":o(a)))throw new Error("Cannot apply patch, path doesn't resolve: "+i.join("/"));var u=i[i.length-1];switch(n.op){case"replace":case"add":a[u]=n.value;break;case"remove":if(Array.isArray(a)){if(u!==a.length-1)throw new Error("Only the last index of an array can be removed, index: "+u+", length: "+a.length);a.length-=1}else delete a[u];break;default:throw new Error("Unsupported patch operation: "+n.op)}}}return e});e.produce=q,e.default=q,e.setAutoFreeze=function(e){return V.setAutoFreeze(e)},e.setUseProxies=function(e){return V.setUseProxies(e)},e.applyPatches=B,e.Immer=J,e.original=function(e){if(e&&e[c])return e[c].base},e.isDraft=s,e.isDraftable=l,e.nothing=f,e.immerable=u,Object.defineProperty(e,"__esModule",{value:!0})},"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r(e.immer={}); //# sourceMappingURL=immer.umd.js.map