Comparing version 2.0.0 to 2.1.0
@@ -622,44 +622,71 @@ 'use strict'; | ||
var minLength = Math.min(base.length, copy.length); | ||
// Reduce complexity by ensuring `base` is never longer. | ||
// Look for replaced indices. | ||
for (var i = 0; i < minLength; i++) { | ||
if (assigned[i] && base[i] !== copy[i]) { | ||
var path = basePath.concat(i); | ||
patches.push({ op: "replace", path: path, value: copy[i] }); | ||
inversePatches.push({ op: "replace", path: path, value: base[i] }); | ||
} | ||
if (copy.length < base.length) { | ||
var _ref = [copy, base]; | ||
base = _ref[0]; | ||
copy = _ref[1]; | ||
var _ref2 = [inversePatches, patches]; | ||
patches = _ref2[0]; | ||
inversePatches = _ref2[1]; | ||
} | ||
// Did the array expand? | ||
if (minLength < copy.length) { | ||
for (var _i = minLength; _i < copy.length; _i++) { | ||
var delta = copy.length - base.length; | ||
// Find the first replaced index. | ||
var start = 0; | ||
while (base[start] === copy[start] && start < base.length) { | ||
++start; | ||
} | ||
// Find the last replaced index. Search from the end to optimize splice patches. | ||
var end = base.length; | ||
while (end > start && base[end - 1] === copy[end + delta - 1]) { | ||
--end; | ||
} | ||
// Process replaced indices. | ||
for (var i = start; i < end; ++i) { | ||
if (assigned[i] && copy[i] !== base[i]) { | ||
var path = basePath.concat([i]); | ||
patches.push({ | ||
op: "add", | ||
path: basePath.concat(_i), | ||
value: copy[_i] | ||
op: "replace", | ||
path: path, | ||
value: copy[i] | ||
}); | ||
inversePatches.push({ | ||
op: "replace", | ||
path: path, | ||
value: base[i] | ||
}); | ||
} | ||
} | ||
var useRemove = end != base.length; | ||
var replaceCount = patches.length; | ||
// Process added indices. | ||
for (var _i = end + delta - 1; _i >= end; --_i) { | ||
var _path = basePath.concat([_i]); | ||
patches[replaceCount + _i - end] = { | ||
op: "add", | ||
path: _path, | ||
value: copy[_i] | ||
}; | ||
if (useRemove) { | ||
inversePatches.push({ | ||
op: "remove", | ||
path: _path | ||
}); | ||
} | ||
} | ||
// One "replace" patch reverses all non-splicing "add" patches. | ||
if (!useRemove) { | ||
inversePatches.push({ | ||
op: "replace", | ||
path: basePath.concat("length"), | ||
path: basePath.concat(["length"]), | ||
value: base.length | ||
}); | ||
} | ||
// ...or did it shrink? | ||
else if (minLength < base.length) { | ||
patches.push({ | ||
op: "replace", | ||
path: basePath.concat("length"), | ||
value: copy.length | ||
}); | ||
for (var _i2 = minLength; _i2 < base.length; _i2++) { | ||
inversePatches.push({ | ||
op: "add", | ||
path: basePath.concat(_i2), | ||
value: base[_i2] | ||
}); | ||
} | ||
} | ||
} | ||
@@ -691,4 +718,4 @@ | ||
var base = draft; | ||
for (var _i3 = 0; _i3 < path.length - 1; _i3++) { | ||
base = base[path[_i3]]; | ||
for (var _i2 = 0; _i2 < path.length - 1; _i2++) { | ||
base = base[path[_i2]]; | ||
if (!base || (typeof base === "undefined" ? "undefined" : _typeof(base)) !== "object") throw new Error("Cannot apply patch, path doesn't resolve: " + path.join("/")); // prettier-ignore | ||
@@ -699,10 +726,15 @@ } | ||
case "replace": | ||
case "add": | ||
// TODO: add support is not extensive, it does not support insertion or `-` atm! | ||
base[key] = patch.value; | ||
break; | ||
case "add": | ||
if (Array.isArray(base)) { | ||
// TODO: support "foo/-" paths for appending to an array | ||
base.splice(key, 0, patch.value); | ||
} else { | ||
base[key] = patch.value; | ||
} | ||
break; | ||
case "remove": | ||
if (Array.isArray(base)) { | ||
if (key !== base.length - 1) throw new Error("Only the last index of an array can be removed, index: " + key + ", length: " + base.length); // prettier-ignore | ||
base.length -= 1; | ||
base.splice(key, 1); | ||
} else { | ||
@@ -709,0 +741,0 @@ delete base[key]; |
@@ -618,44 +618,71 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
var minLength = Math.min(base.length, copy.length); | ||
// Reduce complexity by ensuring `base` is never longer. | ||
// Look for replaced indices. | ||
for (var i = 0; i < minLength; i++) { | ||
if (assigned[i] && base[i] !== copy[i]) { | ||
var path = basePath.concat(i); | ||
patches.push({ op: "replace", path: path, value: copy[i] }); | ||
inversePatches.push({ op: "replace", path: path, value: base[i] }); | ||
} | ||
if (copy.length < base.length) { | ||
var _ref = [copy, base]; | ||
base = _ref[0]; | ||
copy = _ref[1]; | ||
var _ref2 = [inversePatches, patches]; | ||
patches = _ref2[0]; | ||
inversePatches = _ref2[1]; | ||
} | ||
// Did the array expand? | ||
if (minLength < copy.length) { | ||
for (var _i = minLength; _i < copy.length; _i++) { | ||
var delta = copy.length - base.length; | ||
// Find the first replaced index. | ||
var start = 0; | ||
while (base[start] === copy[start] && start < base.length) { | ||
++start; | ||
} | ||
// Find the last replaced index. Search from the end to optimize splice patches. | ||
var end = base.length; | ||
while (end > start && base[end - 1] === copy[end + delta - 1]) { | ||
--end; | ||
} | ||
// Process replaced indices. | ||
for (var i = start; i < end; ++i) { | ||
if (assigned[i] && copy[i] !== base[i]) { | ||
var path = basePath.concat([i]); | ||
patches.push({ | ||
op: "add", | ||
path: basePath.concat(_i), | ||
value: copy[_i] | ||
op: "replace", | ||
path: path, | ||
value: copy[i] | ||
}); | ||
inversePatches.push({ | ||
op: "replace", | ||
path: path, | ||
value: base[i] | ||
}); | ||
} | ||
} | ||
var useRemove = end != base.length; | ||
var replaceCount = patches.length; | ||
// Process added indices. | ||
for (var _i = end + delta - 1; _i >= end; --_i) { | ||
var _path = basePath.concat([_i]); | ||
patches[replaceCount + _i - end] = { | ||
op: "add", | ||
path: _path, | ||
value: copy[_i] | ||
}; | ||
if (useRemove) { | ||
inversePatches.push({ | ||
op: "remove", | ||
path: _path | ||
}); | ||
} | ||
} | ||
// One "replace" patch reverses all non-splicing "add" patches. | ||
if (!useRemove) { | ||
inversePatches.push({ | ||
op: "replace", | ||
path: basePath.concat("length"), | ||
path: basePath.concat(["length"]), | ||
value: base.length | ||
}); | ||
} | ||
// ...or did it shrink? | ||
else if (minLength < base.length) { | ||
patches.push({ | ||
op: "replace", | ||
path: basePath.concat("length"), | ||
value: copy.length | ||
}); | ||
for (var _i2 = minLength; _i2 < base.length; _i2++) { | ||
inversePatches.push({ | ||
op: "add", | ||
path: basePath.concat(_i2), | ||
value: base[_i2] | ||
}); | ||
} | ||
} | ||
} | ||
@@ -687,4 +714,4 @@ | ||
var base = draft; | ||
for (var _i3 = 0; _i3 < path.length - 1; _i3++) { | ||
base = base[path[_i3]]; | ||
for (var _i2 = 0; _i2 < path.length - 1; _i2++) { | ||
base = base[path[_i2]]; | ||
if (!base || (typeof base === "undefined" ? "undefined" : _typeof(base)) !== "object") throw new Error("Cannot apply patch, path doesn't resolve: " + path.join("/")); // prettier-ignore | ||
@@ -695,10 +722,15 @@ } | ||
case "replace": | ||
case "add": | ||
// TODO: add support is not extensive, it does not support insertion or `-` atm! | ||
base[key] = patch.value; | ||
break; | ||
case "add": | ||
if (Array.isArray(base)) { | ||
// TODO: support "foo/-" paths for appending to an array | ||
base.splice(key, 0, patch.value); | ||
} else { | ||
base[key] = patch.value; | ||
} | ||
break; | ||
case "remove": | ||
if (Array.isArray(base)) { | ||
if (key !== base.length - 1) throw new Error("Only the last index of an array can be removed, index: " + key + ", length: " + base.length); // prettier-ignore | ||
base.length -= 1; | ||
base.splice(key, 1); | ||
} else { | ||
@@ -705,0 +737,0 @@ delete base[key]; |
@@ -1,2 +0,2 @@ | ||
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),s="undefined"!=typeof Symbol?Symbol("immer-draftable"):"__$immer_draftable",u="undefined"!=typeof Symbol?Symbol("immer-state"):"__$immer_state";function c(e){return!!e&&!!e[u]}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[s]||!!e.constructor[s])}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!==u){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}var m=function(){function e(r){i(this,e),this.drafts=[],this.parent=r,this.canAutoFreeze=!0,this.patches=null}return a(e,[{key:"usePatches",value:function(e){e&&(this.patches=[],this.inversePatches=[],this.patchListener=e)}},{key:"revoke",value:function(){this.leave(),this.drafts.forEach(w),this.drafts=null}},{key:"leave",value:function(){this===e.current&&(e.current=this.parent)}}]),e}();function w(e){e[u].revoke()}m.current=null,m.enter=function(){return this.current=new m(this.current)};var P={};function O(e,r){var t=Array.isArray(e),n=E(e);y(n,function(r){!function(e,r,t){var n=P[r];n?n.enumerable=t:P[r]=n={configurable:!0,enumerable:t,get:function(){return function(e,r){D(e);var t=z(e)[r];if(!e.finalizing&&t===e.base[r]&&l(t))return A(e),e.copy[r]=O(t,e);return t}(this[u],r)},set:function(e){!function(e,r,t){if(D(e),e.assigned[r]=!0,!e.modified){if(g(z(e)[r],t))return;k(e),A(e)}e.copy[r]=t}(this[u],r,e)}};Object.defineProperty(e,r,n)}(n,r,t||v(e,r))});var o,i,a,f=r?r.scope:m.current;return o=n,i=u,a={scope:f,modified:!1,finalizing:!1,finalized:!1,assigned:{},parent:r,base:e,draft:n,copy:null,revoke:j,revoked:!1},Object.defineProperty(o,i,{value:a,enumerable:!1,writable:!0}),f.drafts.push(n),n}function j(){this.revoked=!0}function z(e){return e.copy||e.base}function k(e){e.modified||(e.modified=!0,e.parent&&k(e.parent))}function A(e){e.copy||(e.copy=E(e.base))}function E(e){var r=e&&e[u];if(r){r.finalizing=!0;var t=h(r.draft,!0);return r.finalizing=!1,t}return h(e)}function D(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(z(e)))}function x(e){for(var r=e.length-1;r>=0;r--){var t=e[r][u];t.modified||(Array.isArray(t.base)?S(t)&&k(t):F(t)&&k(t))}}function F(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 S(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 R=Object.freeze({willFinalize:function(e,r,t){e.drafts.forEach(function(e){e[u].finalizing=!0}),t?c(r)&&r[u].scope===e&&x(e.drafts):(e.patches&&function e(r){if(r&&"object"===(void 0===r?"undefined":o(r))){var t=r[u];if(t){var n=t.base,i=t.draft,a=t.assigned;if(Array.isArray(r)){if(S(t)){if(k(t),a.length=!0,i.length<n.length)for(var f=i.length;f<n.length;f++)a[f]=!1;else for(var s=n.length;s<i.length;s++)a[s]=!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]||b(n,r)?a[r]||e(i[r]):(a[r]=!0,k(t))}),Object.keys(n).forEach(function(e){void 0!==i[e]||b(i,e)||(a[e]=!1,k(t))})}}}(e.drafts[0]),x(e.drafts))},createProxy:O});function I(e,r){var t=r?r.scope:m.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],_):Proxy.revocable(n,N),i=o.revoke,a=o.proxy;return n.draft=a,n.revoke=i,t.drafts.push(a),a}var N={get:function(e,r){if(r===u)return e;var t=e.drafts;if(!e.modified&&b(t,r))return t[r];var n=C(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]=I(n,e)},has:function(e,r){return r in C(e)},ownKeys:function(e){return Reflect.ownKeys(C(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=C(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")}},_={};function C(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(N,function(e,r){_[e]=function(){return arguments[0]=arguments[0][0],r.apply(this,arguments)}}),_.deleteProperty=function(e,r){if(isNaN(parseInt(r)))throw new Error("Immer only supports deleting array indices");return N.deleteProperty.call(this,e[0],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 N.set.call(this,e[0],r,t)};var U=Object.freeze({willFinalize:function(){},createProxy:I});function K(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),s=0;s<f;s++)if(a[s]&&o[s]!==i[s]){var u=r.concat(s);t.push({op:"replace",path:u,value:i[s]}),n.push({op:"replace",path:u,value:o[s]})}if(f<i.length){for(var c=f;c<i.length;c++)t.push({op:"add",path:r.concat(c),value:i[c]});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],s=i[e],u=a?e in o?"replace":"add":"remove";if(f!==s||"replace"!==u){var c=r.concat(e);t.push("remove"===u?{op:u,path:c}:{op:u,path:c,value:s}),n.push("add"===u?{op:"remove",path:c}:"remove"===u?{op:"add",path:c,value:f}:{op:"replace",path:c,value:f})}})}(e,r,t,n)}function M(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 s=i[i.length-1];switch(n.op){case"replace":case"add":a[s]=n.value;break;case"remove":if(Array.isArray(a)){if(s!==a.length-1)throw new Error("Only the last index of an array can be removed, index: "+s+", length: "+a.length);a.length-=1}else delete a[s];break;default:throw new Error("Unsupported patch operation: "+n.op)}}}return e}var L={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},$=function(){function e(r){i(this,e),p(this,L,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)){var a=m.enter(),s=this.createProxy(e),u=!0;try{i=r.call(s,s),u=!1}finally{u?a.revoke():a.leave()}return i instanceof Promise?i.then(function(e){return a.usePatches(t),n.processResult(e,a)},function(e){throw a.revoke(),e}):(a.usePatches(t),this.processResult(i,a))}return void 0===(i=r(e))?e:i!==f?i:void 0}},{key:"createDraft",value:function(e){if(!l(e))throw new Error("First argument to createDraft should be a plain object, an array, or an immerable object.");var r=m.enter(),t=this.createProxy(e);return r.leave(),t[u].customDraft=!0,t}},{key:"finishDraft",value:function(e,r){if(!c(e))throw new Error("First argument to finishDraft should be an object from createDraft.");var t=e[u];if(!t.customDraft)throw new Error("The draft provided was not created using `createDraft`");if(t.finalized)throw new Error("The draft provided was has already been finished");var n=t.scope;return n.usePatches(r),this.processResult(void 0,n)}},{key:"setAutoFreeze",value:function(e){this.autoFreeze=e}},{key:"setUseProxies",value:function(e){this.useProxies=e,p(this,e?U:R)}},{key:"applyPatches",value:function(e,r){return c(e)?M(e,r):this.produce(e,function(e){return M(e,r)})}},{key:"processResult",value:function(e,r){var t=r.drafts[0],n=void 0!==e&&e!==t;if(this.willFinalize(r,e,n),n){if(t[u].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.");l(e)&&(e=this.finalize(e,null,r)),r.patches&&(r.patches.push({op:"replace",path:[],value:e}),r.inversePatches.push({op:"replace",path:[],value:t[u].base}))}else e=this.finalize(t,[],r);return r.revoke(),r.patches&&r.patchListener(r.patches,r.inversePatches),e!==f?e:void 0}},{key:"finalize",value:function(e,r,t){var n=this,o=e[u];if(!o)return Object.isFrozen(e)?e:this.finalizeTree(e,null,t);if(o.scope!==t)return e;if(!o.modified)return o.base;if(!o.finalized){if(o.finalized=!0,this.finalizeTree(o.draft,r,t),this.onDelete)if(this.useProxies){var i=o.assigned;for(var a in i)i[a]||this.onDelete(o,a)}else{var f=o.base,s=o.copy;y(f,function(e){b(s,e)||n.onDelete(o,e)})}this.onCopy&&this.onCopy(o),this.autoFreeze&&t.canAutoFreeze&&Object.freeze(o.copy),r&&t.patches&&K(o,r,t.patches,t.inversePatches)}return o.copy}},{key:"finalizeTree",value:function(e,r,t){var n=this,o=e[u];o&&(this.useProxies||(o.finalizing=!0,o.copy=h(o.draft,!0),o.finalizing=!1),e=o.copy);var i=!!r&&!!t.patches;return y(e,function a(f,s,u){if(s===u)throw Error("Immer forbids circular references");var p=!!o&&u===e;if(c(s)){var d=p&&i&&!o.assigned[f]?r.concat(f):null;if(c(s=n.finalize(s,d,t))&&(t.canAutoFreeze=!1),Array.isArray(u)||v(u,f)?u[f]=s:Object.defineProperty(u,f,{value:s}),p&&s===o.base[f])return}else{if(p&&g(s,o.base[f]))return;l(s)&&!Object.isFrozen(s)&&y(s,a)}p&&n.onAssign&&n.onAssign(o,f,s)}),e}}]),e}(),J=new $,V=J.produce,q=J.setAutoFreeze.bind(J),B=J.setUseProxies.bind(J),G=J.applyPatches.bind(J),H=J.createDraft.bind(J),Q=J.finishDraft.bind(J);e.produce=V,e.default=V,e.setAutoFreeze=q,e.setUseProxies=B,e.applyPatches=G,e.createDraft=H,e.finishDraft=Q,e.Immer=$,e.original=function(e){if(e&&e[u])return e[u].base},e.isDraft=c,e.isDraftable=l,e.nothing=f,e.immerable=s,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={}); | ||
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),s="undefined"!=typeof Symbol?Symbol("immer-draftable"):"__$immer_draftable",u="undefined"!=typeof Symbol?Symbol("immer-state"):"__$immer_state";function c(e){return!!e&&!!e[u]}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[s]||!!e.constructor[s])}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!==u){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 m(e,r){return e===r?0!==e||1/e==1/r:e!=e&&r!=r}var g=function(){function e(r){i(this,e),this.drafts=[],this.parent=r,this.canAutoFreeze=!0,this.patches=null}return a(e,[{key:"usePatches",value:function(e){e&&(this.patches=[],this.inversePatches=[],this.patchListener=e)}},{key:"revoke",value:function(){this.leave(),this.drafts.forEach(w),this.drafts=null}},{key:"leave",value:function(){this===e.current&&(e.current=this.parent)}}]),e}();function w(e){e[u].revoke()}g.current=null,g.enter=function(){return this.current=new g(this.current)};var P={};function O(e,r){var t=Array.isArray(e),n=E(e);y(n,function(r){!function(e,r,t){var n=P[r];n?n.enumerable=t:P[r]=n={configurable:!0,enumerable:t,get:function(){return function(e,r){D(e);var t=z(e)[r];if(!e.finalizing&&t===e.base[r]&&l(t))return k(e),e.copy[r]=O(t,e);return t}(this[u],r)},set:function(e){!function(e,r,t){if(D(e),e.assigned[r]=!0,!e.modified){if(m(z(e)[r],t))return;A(e),k(e)}e.copy[r]=t}(this[u],r,e)}};Object.defineProperty(e,r,n)}(n,r,t||v(e,r))});var o,i,a,f=r?r.scope:g.current;return o=n,i=u,a={scope:f,modified:!1,finalizing:!1,finalized:!1,assigned:{},parent:r,base:e,draft:n,copy:null,revoke:j,revoked:!1},Object.defineProperty(o,i,{value:a,enumerable:!1,writable:!0}),f.drafts.push(n),n}function j(){this.revoked=!0}function z(e){return e.copy||e.base}function A(e){e.modified||(e.modified=!0,e.parent&&A(e.parent))}function k(e){e.copy||(e.copy=E(e.base))}function E(e){var r=e&&e[u];if(r){r.finalizing=!0;var t=h(r.draft,!0);return r.finalizing=!1,t}return h(e)}function D(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(z(e)))}function x(e){for(var r=e.length-1;r>=0;r--){var t=e[r][u];t.modified||(Array.isArray(t.base)?S(t)&&A(t):F(t)&&A(t))}}function F(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 S(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 R=Object.freeze({willFinalize:function(e,r,t){e.drafts.forEach(function(e){e[u].finalizing=!0}),t?c(r)&&r[u].scope===e&&x(e.drafts):(e.patches&&function e(r){if(r&&"object"===(void 0===r?"undefined":o(r))){var t=r[u];if(t){var n=t.base,i=t.draft,a=t.assigned;if(Array.isArray(r)){if(S(t)){if(A(t),a.length=!0,i.length<n.length)for(var f=i.length;f<n.length;f++)a[f]=!1;else for(var s=n.length;s<i.length;s++)a[s]=!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]||b(n,r)?a[r]||e(i[r]):(a[r]=!0,A(t))}),Object.keys(n).forEach(function(e){void 0!==i[e]||b(i,e)||(a[e]=!1,A(t))})}}}(e.drafts[0]),x(e.drafts))},createProxy:O});function I(e,r){var t=r?r.scope:g.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],_):Proxy.revocable(n,N),i=o.revoke,a=o.proxy;return n.draft=a,n.revoke=i,t.drafts.push(a),a}var N={get:function(e,r){if(r===u)return e;var t=e.drafts;if(!e.modified&&b(t,r))return t[r];var n=C(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]=I(n,e)},has:function(e,r){return r in C(e)},ownKeys:function(e){return Reflect.ownKeys(C(e))},set:function(e,r,t){if(!e.modified){var n=t?m(e.base[r],t)||t===e.drafts[r]:m(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=C(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")}},_={};function C(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(N,function(e,r){_[e]=function(){return arguments[0]=arguments[0][0],r.apply(this,arguments)}}),_.deleteProperty=function(e,r){if(isNaN(parseInt(r)))throw new Error("Immer only supports deleting array indices");return N.deleteProperty.call(this,e[0],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 N.set.call(this,e[0],r,t)};var U=Object.freeze({willFinalize:function(){},createProxy:I});function K(e,r,t,n){Array.isArray(e.base)?function(e,r,t,n){var o=e.base,i=e.copy,a=e.assigned;if(i.length<o.length){var f=[i,o];o=f[0],i=f[1];var s=[n,t];t=s[0],n=s[1]}var u=i.length-o.length,c=0;for(;o[c]===i[c]&&c<o.length;)++c;var l=o.length;for(;l>c&&o[l-1]===i[l+u-1];)--l;for(var p=c;p<l;++p)if(a[p]&&i[p]!==o[p]){var d=r.concat([p]);t.push({op:"replace",path:d,value:i[p]}),n.push({op:"replace",path:d,value:o[p]})}for(var h=l!=o.length,y=t.length,v=l+u-1;v>=l;--v){var b=r.concat([v]);t[y+v-l]={op:"add",path:b,value:i[v]},h&&n.push({op:"remove",path:b})}h||n.push({op:"replace",path:r.concat(["length"]),value:o.length})}(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],s=i[e],u=a?e in o?"replace":"add":"remove";if(f!==s||"replace"!==u){var c=r.concat(e);t.push("remove"===u?{op:u,path:c}:{op:u,path:c,value:s}),n.push("add"===u?{op:"remove",path:c}:"remove"===u?{op:"add",path:c,value:f}:{op:"replace",path:c,value:f})}})}(e,r,t,n)}function L(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 s=i[i.length-1];switch(n.op){case"replace":a[s]=n.value;break;case"add":Array.isArray(a)?a.splice(s,0,n.value):a[s]=n.value;break;case"remove":Array.isArray(a)?a.splice(s,1):delete a[s];break;default:throw new Error("Unsupported patch operation: "+n.op)}}}return e}var M={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},$=function(){function e(r){i(this,e),p(this,M,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)){var a=g.enter(),s=this.createProxy(e),u=!0;try{i=r.call(s,s),u=!1}finally{u?a.revoke():a.leave()}return i instanceof Promise?i.then(function(e){return a.usePatches(t),n.processResult(e,a)},function(e){throw a.revoke(),e}):(a.usePatches(t),this.processResult(i,a))}return void 0===(i=r(e))?e:i!==f?i:void 0}},{key:"createDraft",value:function(e){if(!l(e))throw new Error("First argument to createDraft should be a plain object, an array, or an immerable object.");var r=g.enter(),t=this.createProxy(e);return r.leave(),t[u].customDraft=!0,t}},{key:"finishDraft",value:function(e,r){if(!c(e))throw new Error("First argument to finishDraft should be an object from createDraft.");var t=e[u];if(!t.customDraft)throw new Error("The draft provided was not created using `createDraft`");if(t.finalized)throw new Error("The draft provided was has already been finished");var n=t.scope;return n.usePatches(r),this.processResult(void 0,n)}},{key:"setAutoFreeze",value:function(e){this.autoFreeze=e}},{key:"setUseProxies",value:function(e){this.useProxies=e,p(this,e?U:R)}},{key:"applyPatches",value:function(e,r){return c(e)?L(e,r):this.produce(e,function(e){return L(e,r)})}},{key:"processResult",value:function(e,r){var t=r.drafts[0],n=void 0!==e&&e!==t;if(this.willFinalize(r,e,n),n){if(t[u].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.");l(e)&&(e=this.finalize(e,null,r)),r.patches&&(r.patches.push({op:"replace",path:[],value:e}),r.inversePatches.push({op:"replace",path:[],value:t[u].base}))}else e=this.finalize(t,[],r);return r.revoke(),r.patches&&r.patchListener(r.patches,r.inversePatches),e!==f?e:void 0}},{key:"finalize",value:function(e,r,t){var n=this,o=e[u];if(!o)return Object.isFrozen(e)?e:this.finalizeTree(e,null,t);if(o.scope!==t)return e;if(!o.modified)return o.base;if(!o.finalized){if(o.finalized=!0,this.finalizeTree(o.draft,r,t),this.onDelete)if(this.useProxies){var i=o.assigned;for(var a in i)i[a]||this.onDelete(o,a)}else{var f=o.base,s=o.copy;y(f,function(e){b(s,e)||n.onDelete(o,e)})}this.onCopy&&this.onCopy(o),this.autoFreeze&&t.canAutoFreeze&&Object.freeze(o.copy),r&&t.patches&&K(o,r,t.patches,t.inversePatches)}return o.copy}},{key:"finalizeTree",value:function(e,r,t){var n=this,o=e[u];o&&(this.useProxies||(o.finalizing=!0,o.copy=h(o.draft,!0),o.finalizing=!1),e=o.copy);var i=!!r&&!!t.patches;return y(e,function a(f,s,u){if(s===u)throw Error("Immer forbids circular references");var p=!!o&&u===e;if(c(s)){var d=p&&i&&!o.assigned[f]?r.concat(f):null;if(c(s=n.finalize(s,d,t))&&(t.canAutoFreeze=!1),Array.isArray(u)||v(u,f)?u[f]=s:Object.defineProperty(u,f,{value:s}),p&&s===o.base[f])return}else{if(p&&m(s,o.base[f]))return;l(s)&&!Object.isFrozen(s)&&y(s,a)}p&&n.onAssign&&n.onAssign(o,f,s)}),e}}]),e}(),J=new $,V=J.produce,q=J.setAutoFreeze.bind(J),B=J.setUseProxies.bind(J),G=J.applyPatches.bind(J),H=J.createDraft.bind(J),Q=J.finishDraft.bind(J);e.produce=V,e.default=V,e.setAutoFreeze=q,e.setUseProxies=B,e.applyPatches=G,e.createDraft=H,e.finishDraft=Q,e.Immer=$,e.original=function(e){if(e&&e[u])return e[u].base},e.isDraft=c,e.isDraftable=l,e.nothing=f,e.immerable=s,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 |
{ | ||
"name": "immer", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Create your next immutable state by mutating the current one", | ||
@@ -5,0 +5,0 @@ "main": "dist/immer.js", |
@@ -351,3 +351,3 @@ <img src="images/immer-logo.png" height="200px" align="right"/> | ||
const loadedUser = await produce(user, async function(draft) { | ||
user.todos = await (await window.fetch("http://host/" + draft.name)).json() | ||
draft.todos = await (await window.fetch("http://host/" + draft.name)).json() | ||
}) | ||
@@ -642,2 +642,3 @@ ``` | ||
- [immer-reducer](https://github.com/epeli/immer-reducer) _Type-safe and terse Redux reducers with Typescript_ | ||
- [redux-ts-utils](https://github.com/knpwrs/redux-ts-utils) _Everything you need to create type-safe applications with Redux with a strong emphasis on simplicity_ | ||
- ... and [many more](https://www.npmjs.com/browse/depended/immer) | ||
@@ -644,0 +645,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
300369
2204
760