Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Socket
Sign inDemoInstall

immer

Package Overview
Dependencies
Maintainers
2
Versions
173
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

immer - npm Package Compare versions

Comparing version 2.1.5 to 3.0.0

132

dist/immer.d.ts

@@ -0,1 +1,8 @@

type Tail<T extends any[]> = ((...t: T) => any) extends ((
_: any,
...tail: infer TT
) => any)
? TT
: []
/** Object types that should never be mapped */

@@ -15,23 +22,13 @@ type AtomicObject =

type ArrayMethod = Exclude<keyof [], number>
type Indices<T> = Exclude<keyof T, ArrayMethod>
export type Draft<T> = T extends AtomicObject
? T
: T extends object
? {-readonly [K in keyof T]: Draft<T[K]>}
: T
export type DraftArray<T extends ReadonlyArray<any>> = Array<
{[P in Indices<T>]: Draft<T[P]>}[Indices<T>]
>
export type DraftTuple<T extends ReadonlyArray<any>> = {
[P in keyof T]: P extends Indices<T> ? Draft<T[P]> : never
}
export type Draft<T> = T extends never[]
/** Convert a mutable type into a readonly type */
export type Immutable<T> = T extends AtomicObject
? T
: T extends ReadonlyArray<any>
? T[number][] extends T
? DraftArray<T>
: DraftTuple<T>
: T extends AtomicObject
? T
: T extends object
? {-readonly [P in keyof T]: Draft<T[P]>}
? {readonly [K in keyof T]: Immutable<T[K]>}
: T

@@ -57,53 +54,56 @@

type ImmutableTuple<T extends ReadonlyArray<any>> = {
readonly [P in keyof T]: Immutable<T[P]>
}
/**
* The `produce` function takes a value and a "recipe function" (whose
* return value often depends on the base state). The recipe function is
* free to mutate its first argument however it wants. All mutations are
* only ever applied to a __copy__ of the base state.
*
* Pass only a function to create a "curried producer" which relieves you
* from passing the recipe function every time.
*
* Only plain objects and arrays are made mutable. All other objects are
* considered uncopyable.
*
* Note: This function is __bound__ to its `Immer` instance.
*
* @param {any} base - the initial state
* @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified
* @param {Function} patchListener - optional function that will be called with all the patches produced here
* @returns {any} a new state, or the initial state if nothing was modified
*/
export interface IProduce {
/** Curried producer */
<
Recipe extends (...args: any[]) => any,
Params extends any[] = Parameters<Recipe>,
T = Params[0]
>(
recipe: Recipe
): <Base extends Immutable<T>>(
base: Base,
...rest: Tail<Params>
) => Produced<Base, ReturnType<Recipe>>
// ^ by making the returned type generic, the actual type of the passed in object is preferred
// over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe
// Note: the type of S is the widened version of T, so it can have more props than T, but that is technically actually correct!
/** Convert a mutable type into a readonly type */
export type Immutable<T> = T extends object
? T extends AtomicObject
? T
: T extends ReadonlyArray<any>
? Array<T[number]> extends T
? {[P in keyof T]: ReadonlyArray<Immutable<T[number]>>}[keyof T]
: ImmutableTuple<T>
: {readonly [P in keyof T]: Immutable<T[P]>}
: T
/** Curried producer with initial state */
<
Recipe extends (...args: any[]) => any,
Params extends any[] = Parameters<Recipe>,
T = Params[0]
>(
recipe: Recipe,
initialState: Immutable<T>
): <Base extends Immutable<T>>(
base?: Base,
...rest: Tail<Params>
) => Produced<Base, ReturnType<Recipe>>
export interface IProduce {
/**
* The `produce` function takes a value and a "recipe function" (whose
* return value often depends on the base state). The recipe function is
* free to mutate its first argument however it wants. All mutations are
* only ever applied to a __copy__ of the base state.
*
* Pass only a function to create a "curried producer" which relieves you
* from passing the recipe function every time.
*
* Only plain objects and arrays are made mutable. All other objects are
* considered uncopyable.
*
* Note: This function is __bound__ to its `Immer` instance.
*
* @param {any} base - the initial state
* @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified
* @param {Function} patchListener - optional function that will be called with all the patches produced here
* @returns {any} a new state, or the initial state if nothing was modified
*/
<T = any, Return = void, D = Draft<T>>(
base: T,
recipe: (this: D, draft: D) => Return,
/** Normal producer */
<Base, D = Draft<Base>, Return = void>(
base: Base,
recipe: (draft: D) => Return,
listener?: PatchListener
): Produced<T, Return>
/** Curried producer with a default value */
<T = any, Rest extends any[] = [], Return = void, D = Draft<T>>(
recipe: (this: D, draft: D, ...rest: Rest) => Return,
defaultBase: T
): (base: Immutable<D> | undefined, ...rest: Rest) => Produced<D, Return>
/** Curried producer with no default value */
<T = any, Rest extends any[] = [], Return = void>(
recipe: (this: Draft<T>, draft: Draft<T>, ...rest: Rest) => Return
): (base: Immutable<T>, ...rest: Rest) => Produced<T, Return>
): Produced<Base, Return>
}

@@ -110,0 +110,0 @@

@@ -760,5 +760,6 @@ 'use strict';

var defaultBase = recipe;
recipe = base; // prettier-ignore
return function (base) {
recipe = base;
var self = this;
return function curriedProduce(base) {
var this$1 = this;
if ( base === void 0 ) base = defaultBase;

@@ -768,4 +769,4 @@ var args = [], len = arguments.length - 1;

return this$1.produce(base, function (draft) { return recipe.call.apply(recipe, [ draft, draft ].concat( args )); });
};
return self.produce(base, function (draft) { return recipe.call.apply(recipe, [ this$1, draft ].concat( args )); }); // prettier-ignore
};
} // prettier-ignore

@@ -791,3 +792,3 @@

try {
result = recipe.call(proxy, proxy);
result = recipe(proxy);
hasError = false;

@@ -794,0 +795,0 @@ } finally {

@@ -756,5 +756,6 @@ var obj;

var defaultBase = recipe;
recipe = base; // prettier-ignore
return function (base) {
recipe = base;
var self = this;
return function curriedProduce(base) {
var this$1 = this;
if ( base === void 0 ) base = defaultBase;

@@ -764,4 +765,4 @@ var args = [], len = arguments.length - 1;

return this$1.produce(base, function (draft) { return recipe.call.apply(recipe, [ draft, draft ].concat( args )); });
};
return self.produce(base, function (draft) { return recipe.call.apply(recipe, [ this$1, draft ].concat( args )); }); // prettier-ignore
};
} // prettier-ignore

@@ -787,3 +788,3 @@

try {
result = recipe.call(proxy, proxy);
result = recipe(proxy);
hasError = false;

@@ -790,0 +791,0 @@ } finally {

@@ -1,2 +0,2 @@

!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("immer-draftable"):"__$immer_draftable",o="undefined"!=typeof Symbol?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;if(i.get){if(!r)throw new Error("Immer drafts cannot have computed properties");a=i.get.call(e)}i.enumerable?t[n]=a: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){return Object.getOwnPropertyDescriptor(e,r).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=w(m(e),r);if(e.finalizing)return t;if(t===w(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,w(m(e),r)))return;P(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 w(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 P(e){e.modified||(e.modified=!0,e.parent&&P(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)?D(t)&&P(t):E(t)&&P(t))}}function E(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 D(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(D(t)){if(P(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,P(t))}),Object.keys(n).forEach(function(e){void 0!==i[e]||l(i,e)||(a[e]=!1,P(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=I(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 I(e)},ownKeys:function(e){return Reflect.ownKeys(I(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;S(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,S(e));e.copy&&delete e.copy[r];return!0},getOwnPropertyDescriptor:function(e,r){var t=I(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 I(e){return e.copy||e.base}function N(e,r){var t=e[o],n=Reflect.getOwnPropertyDescriptor(t?I(t):e,r);return n&&n.value}function S(e){e.modified||(e.modified=!0,e.copy=s(c(e.base),e.drafts),e.drafts=null,e.parent&&S(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 _=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;return r=e,function(e){void 0===e&&(e=s);for(var t=[],n=arguments.length-1;n-- >0;)t[n]=arguments[n+1];return i.produce(e,function(e){return r.call.apply(r,[e,e].concat(t))})}}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 f=h.enter(),c=this.createProxy(e),u=!0;try{o=r.call(c,c),u=!1}finally{u?f.revoke():f.leave()}return o instanceof Promise?o.then(function(e){return f.usePatches(n),i.processResult(e,f)},function(e){throw f.revoke(),e}):(f.usePatches(n),this.processResult(o,f))}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?_: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})});
!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("immer-draftable"):"__$immer_draftable",o="undefined"!=typeof Symbol?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;if(i.get){if(!r)throw new Error("Immer drafts cannot have computed properties");a=i.get.call(e)}i.enumerable?t[n]=a: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){return Object.getOwnPropertyDescriptor(e,r).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=w(m(e),r);if(e.finalizing)return t;if(t===w(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,w(m(e),r)))return;P(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 w(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 P(e){e.modified||(e.modified=!0,e.parent&&P(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)?D(t)&&P(t):E(t)&&P(t))}}function E(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 D(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(D(t)){if(P(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,P(t))}),Object.keys(n).forEach(function(e){void 0!==i[e]||l(i,e)||(a[e]=!1,P(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=I(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 I(e)},ownKeys:function(e){return Reflect.ownKeys(I(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;S(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,S(e));e.copy&&delete e.copy[r];return!0},getOwnPropertyDescriptor:function(e,r){var t=I(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 I(e){return e.copy||e.base}function N(e,r){var t=e[o],n=Reflect.getOwnPropertyDescriptor(t?I(t):e,r);return n&&n.value}function S(e){e.modified||(e.modified=!0,e.copy=s(c(e.base),e.drafts),e.drafts=null,e.parent&&S(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 _=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?_: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
{
"name": "immer",
"version": "2.1.5",
"version": "3.0.0",
"description": "Create your next immutable state by mutating the current one",

@@ -27,3 +27,3 @@ "main": "dist/immer.js",

"type": "git",
"url": "https://github.com/mweststrate/immer.git"
"url": "https://github.com/immerjs/immer.git"
},

@@ -38,5 +38,5 @@ "keywords": [

"bugs": {
"url": "https://github.com/mweststrate/immer/issues"
"url": "https://github.com/immerjs/immer/issues"
},
"homepage": "https://github.com/mweststrate/immer#readme",
"homepage": "https://github.com/immerjs/immer#readme",
"files": [

@@ -67,3 +67,3 @@ "dist",

"lodash.clonedeep": "^4.5.0",
"prettier": "^1.9.2",
"prettier": "1.17.0",
"pretty-quick": "^1.8.0",

@@ -73,3 +73,4 @@ "regenerator-runtime": "^0.11.1",

"seamless-immutable": "^7.1.3",
"typescript": "3.1.1",
"spec.ts": "^1.1.0",
"typescript": "3.4.3",
"yarn-or-npm": "^2.0.4"

@@ -76,0 +77,0 @@ },

@@ -5,8 +5,10 @@ <img src="images/immer-logo.png" height="200px" align="right"/>

[![npm](https://img.shields.io/npm/v/immer.svg)](https://www.npmjs.com/package/immer) [![size](http://img.badgesize.io/https://cdn.jsdelivr.net/npm/immer/dist/immer.umd.js?compression=gzip)](http://img.badgesize.io/https://cdn.jsdelivr.net/npm/immer/dist/immer.umd.js) [![install size](https://packagephobia.now.sh/badge?p=immer)](https://packagephobia.now.sh/result?p=immer) [![Build Status](https://travis-ci.org/mweststrate/immer.svg?branch=master)](https://travis-ci.org/mweststrate/immer) [![Coverage Status](https://coveralls.io/repos/github/mweststrate/immer/badge.svg?branch=master)](https://coveralls.io/github/mweststrate/immer?branch=master) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier) [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/michelweststrate)
[![npm](https://img.shields.io/npm/v/immer.svg)](https://www.npmjs.com/package/immer) [![size](http://img.badgesize.io/https://cdn.jsdelivr.net/npm/immer/dist/immer.umd.js?compression=gzip)](http://img.badgesize.io/https://cdn.jsdelivr.net/npm/immer/dist/immer.umd.js) [![Build Status](https://travis-ci.org/immerjs/immer.svg?branch=master)](https://travis-ci.org/immerjs/immer) [![Coverage Status](https://coveralls.io/repos/github/immerjs/immer/badge.svg?branch=master)](https://coveralls.io/github/immerjs/immer?branch=master) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier) [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/michelweststrate)
_Create the next immutable state tree by simply modifying the current tree_
### [Release notes](https://github.com/mweststrate/immer/releases)
Winner of the "breakthrough of the year" [open source award](https://osawards.com/react/) in 2019
### [Release notes](https://github.com/immerjs/immer/releases)
Did Immer make a difference to your project? Consider buying me a coffee!<br/><a href="https://www.buymeacoffee.com/mweststrate" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: auto !important;width: auto !important;" ></a>

@@ -510,3 +512,3 @@

By default `produce` tries to use proxies for optimal performance. However, on older JavaScript engines `Proxy` is not available. For example, when running Microsoft Internet Explorer or React Native on Android. In such cases, Immer will fallback to an ES5 compatible implementation which works identical, but is a bit slower.
By default `produce` tries to use proxies for optimal performance. However, on older JavaScript engines `Proxy` is not available. For example, when running Microsoft Internet Explorer or React Native (< v0.59) on Android. In such cases, Immer will fallback to an ES5 compatible implementation which works identical, but is a bit slower.

@@ -536,10 +538,10 @@ ## Importing immer

```js
import {immerable} from 'immer'
import {immerable} from "immer"
class Foo {
[immerable] = true // Option 1
[immerable] = true // Option 1
constructor() {
this[immerable] = true // Option 2
}
constructor() {
this[immerable] = true // Option 2
}
}

@@ -606,3 +608,3 @@

const newState = produce<State>(state, draft => {
const newState = produce(state, draft => {
// `x` can be modified here

@@ -617,27 +619,29 @@ draft.x++

**Note:** Immer v1.9+ supports Typescript v3.1+ only.
For curried reducers, the type is inferred from the first argument of recipe function, so make sure to type it. The `Draft` utility type can be used if the state argument type is immutable:
## Using `this`
```ts
import produce, {Draft} from "immer"
_Deprecated, this will probably be removed in a next major version, see [#308](https://github.com/mweststrate/immer/issues/308)_
interface State {
readonly x: number
}
The recipe will be always invoked with the `draft` as `this` context.
// `x` cannot be modified here
const state: State = {
x: 0
}
This means that the following constructions are also valid:
```javascript
const base = {counter: 0}
const next = produce(base, function() {
this.counter++
const increment = produce((draft: Draft<State>, inc: number) => {
// `x` can be modified here
draft.x += inc
})
console.log(next.counter) // 1
// OR
const increment = produce(function() {
this.counter++
})
console.log(increment(base).counter) // 1
const newState = increment(state, 2)
// `newState.x` cannot be modified here
```
**Note:** Immer v1.9+ supports TypeScript v3.1+ only.
**Note:** Immer v3.0+ supports TypeScript v3.4+ only.
# Pitfalls

@@ -648,3 +652,2 @@

1. Since Immer uses proxies, reading huge amounts of data from state comes with an overhead (especially in the ES5 implementation). If this ever becomes an issue (measure before you optimize!), do the current state analysis before entering the producer function or read from the `currentState` rather than the `draftState`. Also, realize that immer is opt-in everywhere, so it is perfectly fine to manually write super performance critical reducers, and use immer for all the normal ones. Also note that `original` can be used to get the original state of an object, which is cheaper to read.
1. Some debuggers (at least Node 6 is known) have trouble debugging when Proxies are in play. Node 8 is known to work correctly.
1. Always try to pull `produce` 'up', for example `for (let x of y) produce(base, d => d.push(x))` is exponentially slower than `produce(base, d => { for (let x of y) d.push(x)})`

@@ -741,2 +744,8 @@ 1. It is possible to return values from producers, except, it is not possible to return `undefined` that way, as it is indistinguishable from not updating the draft at all! If you want to replace the draft with `undefined`, just return `nothing` from the producer.

**Immer 2.\* -> 3.0**
In your producers, make sure you're not treating `this` as the draft. (see here: https://github.com/immerjs/immer/issues/308)
Upgrade to `typescript@^3.4` if you're a TypeScript user.
**Immer 1.\* -> 2.0**

@@ -746,2 +755,6 @@

**Immer 2.1 -> 2.2**
When using TypeScript, for curried reducers that are typed in the form `produce<Type>((arg) => { })`, rewrite this to `produce((arg: Type) => { })` or `produce((arg: Draft<Type>) => { })` for correct inference.
## FAQ

@@ -748,0 +761,0 @@

@@ -0,1 +1,8 @@

type Tail<T extends any[]> = ((...t: T) => any) extends ((
_: any,
...tail: infer TT
) => any)
? TT
: []
/** Object types that should never be mapped */

@@ -15,23 +22,13 @@ type AtomicObject =

type ArrayMethod = Exclude<keyof [], number>
type Indices<T> = Exclude<keyof T, ArrayMethod>
export type Draft<T> = T extends AtomicObject
? T
: T extends object
? {-readonly [K in keyof T]: Draft<T[K]>}
: T
export type DraftArray<T extends ReadonlyArray<any>> = Array<
{[P in Indices<T>]: Draft<T[P]>}[Indices<T>]
>
export type DraftTuple<T extends ReadonlyArray<any>> = {
[P in keyof T]: P extends Indices<T> ? Draft<T[P]> : never
}
export type Draft<T> = T extends never[]
/** Convert a mutable type into a readonly type */
export type Immutable<T> = T extends AtomicObject
? T
: T extends ReadonlyArray<any>
? T[number][] extends T
? DraftArray<T>
: DraftTuple<T>
: T extends AtomicObject
? T
: T extends object
? {-readonly [P in keyof T]: Draft<T[P]>}
? {readonly [K in keyof T]: Immutable<T[K]>}
: T

@@ -57,53 +54,56 @@

type ImmutableTuple<T extends ReadonlyArray<any>> = {
readonly [P in keyof T]: Immutable<T[P]>
}
/**
* The `produce` function takes a value and a "recipe function" (whose
* return value often depends on the base state). The recipe function is
* free to mutate its first argument however it wants. All mutations are
* only ever applied to a __copy__ of the base state.
*
* Pass only a function to create a "curried producer" which relieves you
* from passing the recipe function every time.
*
* Only plain objects and arrays are made mutable. All other objects are
* considered uncopyable.
*
* Note: This function is __bound__ to its `Immer` instance.
*
* @param {any} base - the initial state
* @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified
* @param {Function} patchListener - optional function that will be called with all the patches produced here
* @returns {any} a new state, or the initial state if nothing was modified
*/
export interface IProduce {
/** Curried producer */
<
Recipe extends (...args: any[]) => any,
Params extends any[] = Parameters<Recipe>,
T = Params[0]
>(
recipe: Recipe
): <Base extends Immutable<T>>(
base: Base,
...rest: Tail<Params>
) => Produced<Base, ReturnType<Recipe>>
// ^ by making the returned type generic, the actual type of the passed in object is preferred
// over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe
// Note: the type of S is the widened version of T, so it can have more props than T, but that is technically actually correct!
/** Convert a mutable type into a readonly type */
export type Immutable<T> = T extends object
? T extends AtomicObject
? T
: T extends ReadonlyArray<any>
? Array<T[number]> extends T
? {[P in keyof T]: ReadonlyArray<Immutable<T[number]>>}[keyof T]
: ImmutableTuple<T>
: {readonly [P in keyof T]: Immutable<T[P]>}
: T
/** Curried producer with initial state */
<
Recipe extends (...args: any[]) => any,
Params extends any[] = Parameters<Recipe>,
T = Params[0]
>(
recipe: Recipe,
initialState: Immutable<T>
): <Base extends Immutable<T>>(
base?: Base,
...rest: Tail<Params>
) => Produced<Base, ReturnType<Recipe>>
export interface IProduce {
/**
* The `produce` function takes a value and a "recipe function" (whose
* return value often depends on the base state). The recipe function is
* free to mutate its first argument however it wants. All mutations are
* only ever applied to a __copy__ of the base state.
*
* Pass only a function to create a "curried producer" which relieves you
* from passing the recipe function every time.
*
* Only plain objects and arrays are made mutable. All other objects are
* considered uncopyable.
*
* Note: This function is __bound__ to its `Immer` instance.
*
* @param {any} base - the initial state
* @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified
* @param {Function} patchListener - optional function that will be called with all the patches produced here
* @returns {any} a new state, or the initial state if nothing was modified
*/
<T = any, Return = void, D = Draft<T>>(
base: T,
recipe: (this: D, draft: D) => Return,
/** Normal producer */
<Base, D = Draft<Base>, Return = void>(
base: Base,
recipe: (draft: D) => Return,
listener?: PatchListener
): Produced<T, Return>
/** Curried producer with a default value */
<T = any, Rest extends any[] = [], Return = void, D = Draft<T>>(
recipe: (this: D, draft: D, ...rest: Rest) => Return,
defaultBase: T
): (base: Immutable<D> | undefined, ...rest: Rest) => Produced<D, Return>
/** Curried producer with no default value */
<T = any, Rest extends any[] = [], Return = void>(
recipe: (this: Draft<T>, draft: Draft<T>, ...rest: Rest) => Return
): (base: Immutable<T>, ...rest: Rest) => Produced<T, Return>
): Produced<Base, Return>
}

@@ -110,0 +110,0 @@

@@ -43,5 +43,6 @@ import * as legacyProxy from "./es5"

// prettier-ignore
return (base = defaultBase, ...args) =>
this.produce(base, draft => recipe.call(draft, draft, ...args))
const self = this
return function curriedProduce(base = defaultBase, ...args) {
return self.produce(base, draft => recipe.call(this, draft, ...args)) // prettier-ignore
}
}

@@ -67,3 +68,3 @@

try {
result = recipe.call(proxy, proxy)
result = recipe(proxy)
hasError = false

@@ -70,0 +71,0 @@ } finally {

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc