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

immer

Package Overview
Dependencies
Maintainers
1
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 0.8.2 to 0.8.3

6

changelog.md

@@ -7,2 +7,8 @@ # Changelog

### 0.8.2
* Several general improvements: if an instances of an object are in the tree they will now be one instance in the resulting tree as well, also in the ES5 impl
* Always freeze data that is newly added to the draft
* Fixed [#75](https://github.com/mweststrate/immer/issues/75), don't use Symbols if not available.
### 0.8.1

@@ -9,0 +15,0 @@

334

dist/immer.js

@@ -5,74 +5,8 @@ 'use strict';

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
var PROXY_STATE = Symbol("immer-proxy-state"); // TODO: create per closure, to avoid sharing proxies between multiple immer version
var autoFreeze = true;
function isProxy(value) {
return !!value && !!value[PROXY_STATE];
}
function isProxyable(value) {
if (!value) return false;
if ((typeof value === "undefined" ? "undefined" : _typeof(value)) !== "object") return false;
if (Array.isArray(value)) return true;
var proto = Object.getPrototypeOf(value);
return proto === null || proto === Object.prototype;
}
function freeze(value) {
if (autoFreeze) {
Object.freeze(value);
}
return value;
}
/**
* Automatically freezes any state trees generated by immer.
* This protects against accidental modifications of the state tree outside of an immer function.
* This comes with a performance impact, so it is recommended to disable this option in production.
* It is by default enabled.
*
* @returns {void}
*/
function setAutoFreeze(enableAutoFreeze) {
autoFreeze = enableAutoFreeze;
}
function finalizeNonProxiedObject(parent, finalizer) {
// If finalize is called on an object that was not a proxy, it means that it is an object that was not there in the original
// tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well
if (!isProxyable(parent)) return;
if (Object.isFrozen(parent)) return;
var modified = false;
if (Array.isArray(parent)) {
for (var i = 0; i < parent.length; i++) {
var child = parent[i];
if (isProxy(child)) {
parent[i] = finalizer(child);
modified = true;
} else finalizeNonProxiedObject(child, finalizer);
}
} else {
for (var key in parent) {
var _child = parent[key];
if (isProxy(_child)) {
parent[key] = finalizer(_child);
modified = true;
} else finalizeNonProxiedObject(_child, finalizer);
}
}
if (modified) freeze(parent);
}
// @ts-check
var revocableProxies = null;
var proxies = null;
var objectTraps = {
get: get$1,
get: get,
has: function has(target, prop) {

@@ -85,6 +19,6 @@ return prop in source(target);

set: set$1,
set: set,
deleteProperty: deleteProperty,
getOwnPropertyDescriptor: getOwnPropertyDescriptor,
defineProperty: defineProperty$1,
defineProperty: defineProperty,
setPrototypeOf: function setPrototypeOf() {

@@ -96,14 +30,9 @@ throw new Error("Don't even try this...");

var arrayTraps = {};
var _loop = function _loop(key) {
each(objectTraps, function (key, fn) {
arrayTraps[key] = function () {
arguments[0] = arguments[0][0];
return objectTraps[key].apply(this, arguments);
return fn.apply(this, arguments);
};
};
});
for (var key in objectTraps) {
_loop(key);
}
function createState(parent, base) {

@@ -124,3 +53,3 @@ return {

function get$1(state, prop) {
function get(state, prop) {
if (prop === PROXY_STATE) return state;

@@ -139,3 +68,3 @@ if (state.modified) {

function set$1(state, prop, value) {
function set(state, prop, value) {
if (!state.modified) {

@@ -162,3 +91,3 @@ if (prop in state.base && Object.is(state.base[prop], value) || prop in state.proxies && state.proxies[prop] === value) return true;

function defineProperty$1() {
function defineProperty() {
throw new Error("Immer does currently not support defining properties on draft objects");

@@ -170,3 +99,4 @@ }

state.modified = true;
state.copy = Array.isArray(state.base) ? state.base.slice() : Object.assign({}, state.base); // TODO: eliminate those isArray checks?
state.copy = shallowCopy(state.base);
// copy the proxies over the base-copy
Object.assign(state.copy, state.proxies); // yup that works for arrays as well

@@ -180,34 +110,13 @@ if (state.parent) markChanged(state.parent);

var state = createState(parentState, base);
var proxy = void 0;
if (Array.isArray(base)) {
proxy = Proxy.revocable([state], arrayTraps);
} else {
proxy = Proxy.revocable(state, objectTraps);
}
revocableProxies.push(proxy);
var proxy = Array.isArray(base) ? Proxy.revocable([state], arrayTraps) : Proxy.revocable(state, objectTraps);
proxies.push(proxy);
return proxy.proxy;
}
// given a base object, returns it if unmodified, or return the changed cloned if modified
function finalize(base) {
if (isProxy(base)) {
var state = base[PROXY_STATE];
if (state.modified === true) {
if (state.finalized === true) return state.copy;
state.finalized = true;
if (Array.isArray(state.base)) return finalizeArray(state);
return finalizeObject(state);
} else return state.base;
} else if (base !== null && (typeof base === "undefined" ? "undefined" : _typeof(base)) === "object") {
finalizeNonProxiedObject(base, finalize);
}
return base;
}
function finalizeObject(state) {
var copy = state.copy;
var base = state.base;
for (var prop in copy) {
if (copy[prop] !== base[prop]) copy[prop] = finalize(copy[prop]);
}
each(copy, function (prop, value) {
if (value !== base[prop]) copy[prop] = finalize(value);
});
return freeze(copy);

@@ -219,5 +128,5 @@ }

var base = state.base;
for (var i = 0; i < copy.length; i++) {
if (copy[i] !== base[i]) copy[i] = finalize(copy[i]);
}
each(copy, function (i, value) {
if (value !== base[i]) copy[i] = finalize(value);
});
return freeze(copy);

@@ -227,4 +136,4 @@ }

function produceProxy(baseState, producer) {
var previousProxies = revocableProxies;
revocableProxies = [];
var previousProxies = proxies;
proxies = [];
try {

@@ -234,9 +143,7 @@ // create proxy for root

// execute the thunk
var maybeVoidReturn = producer(rootClone);
//values either than undefined will trigger warning;
!Object.is(maybeVoidReturn, undefined) && console.warn("Immer callback expects no return value. However " + (typeof maybeVoidReturn === "undefined" ? "undefined" : _typeof(maybeVoidReturn)) + " was returned");
verifyReturnValue(producer(rootClone));
// and finalize the modified proxy
var res = finalize(rootClone);
// revoke all proxies
revocableProxies.forEach(function (p) {
each(proxies, function (_, p) {
return p.revoke();

@@ -246,6 +153,12 @@ });

} finally {
revocableProxies = previousProxies;
proxies = previousProxies;
}
}
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
// @ts-check

@@ -265,3 +178,4 @@

finished: false,
finalizing: false
finalizing: false,
finalized: false
};

@@ -289,4 +203,4 @@ }

markChanged$1(state);
prepareCopy(state);
}
prepareCopy(state);
state.copy[prop] = value;

@@ -305,3 +219,3 @@ }

state.hasCopy = true;
state.copy = Array.isArray(state.base) ? state.base.slice() : Object.assign({}, state.base);
state.copy = shallowCopy(state.base);
}

@@ -311,4 +225,3 @@

function createProxy$1(parent, base) {
var proxy = void 0;
if (Array.isArray(base)) proxy = createArrayProxy(base);else proxy = createObjectProxy(base);
var proxy = Array.isArray(base) ? createArrayProxy(base) : createObjectProxy(base);
var state = createState$1(parent, proxy, base);

@@ -324,6 +237,6 @@ createHiddenProperty(proxy, PROXY_STATE, state);

enumerable: true,
get: function get$$1() {
get: function get() {
return _get(this[PROXY_STATE], prop);
},
set: function set$$1(value) {
set: function set(value) {
_set(this[PROXY_STATE], prop, value);

@@ -336,4 +249,4 @@ }

var proxy = Object.assign({}, base);
Object.keys(base).forEach(function (prop) {
return Object.defineProperty(proxy, prop, createPropertyProxy(prop));
each(base, function (prop) {
Object.defineProperty(proxy, prop, createPropertyProxy(prop));
});

@@ -345,5 +258,6 @@ return proxy;

var proxy = new Array(base.length);
for (var i = 0; i < base.length; i++) {
each(base, function (i) {
Object.defineProperty(proxy, "" + i, createPropertyProxy("" + i));
}return proxy;
});
return proxy;
}

@@ -382,22 +296,8 @@

function finalize$1(proxy) {
// TODO: almost litterally same as Proxy impl; let's reduce code duplication and rollup
if (isProxy(proxy)) {
var state = proxy[PROXY_STATE];
if (state.modified === true) {
if (Array.isArray(state.base)) return finalizeArray$1(proxy, state);
return finalizeObject$1(proxy, state);
} else return state.base;
} else if (proxy !== null && (typeof proxy === "undefined" ? "undefined" : _typeof(proxy)) === "object") {
finalizeNonProxiedObject(proxy, finalize$1);
}
return proxy;
}
function finalizeObject$1(proxy, state) {
var res = Object.assign({}, proxy);
var res = state.copy = shallowCopy(proxy);
var base = state.base;
for (var prop in res) {
if (proxy[prop] !== base[prop]) res[prop] = finalize$1(res[prop]);
}
each(res, function (prop, value) {
if (value !== base[prop]) res[prop] = finalize(value);
});
return freeze(res);

@@ -407,7 +307,7 @@ }

function finalizeArray$1(proxy, state) {
var res = proxy.slice();
var res = state.copy = shallowCopy(proxy);
var base = state.base;
for (var i = 0; i < res.length; i++) {
if (res[i] !== base[i]) res[i] = finalize$1(res[i]);
}
each(res, function (i, value) {
if (value !== base[i]) res[i] = finalize(value);
});
return freeze(res);

@@ -423,15 +323,16 @@ }

// execute the thunk
var maybeVoidReturn = producer(rootClone);
//values either than undefined will trigger warning;
!Object.is(maybeVoidReturn, undefined) && console.warn("Immer callback expects no return value. However " + (typeof maybeVoidReturn === "undefined" ? "undefined" : _typeof(maybeVoidReturn)) + " was returned");
verifyReturnValue(producer(rootClone));
// and finalize the modified proxy
for (var i = 0; i < states.length; i++) {
states[i].finalizing = true;
} // find and mark all changes (for parts not done yet)
each(states, function (_, state) {
state.finalizing = true;
});
// find and mark all changes (for parts not done yet)
// TODO: store states by depth, to be able guarantee processing leaves first
markChanges();
var res = finalize$1(rootClone);
var res = finalize(rootClone);
// make sure all proxies become unusable
for (var _i = 0; _i < states.length; _i++) {
states[_i].finished = true;
}return res;
each(states, function (_, state) {
state.finished = true;
});
return res;
} finally {

@@ -467,5 +368,108 @@ states = prevStates;

var PROXY_STATE = typeof Symbol !== "undefined" ? Symbol("immer-proxy-state") : "__$immer_state";
var autoFreeze = true;
var useProxies = typeof Proxy !== "undefined";
/**
* Automatically freezes any state trees generated by immer.
* This protects against accidental modifications of the state tree outside of an immer function.
* This comes with a performance impact, so it is recommended to disable this option in production.
* It is by default enabled.
*
* @returns {void}
*/
function setAutoFreeze(enableAutoFreeze) {
autoFreeze = enableAutoFreeze;
}
function setUseProxies(value) {
useProxies = value;
}
function getUseProxies() {
return useProxies;
}
function isProxy(value) {
return !!value && !!value[PROXY_STATE];
}
function isProxyable(value) {
if (!value) return false;
if ((typeof value === "undefined" ? "undefined" : _typeof(value)) !== "object") return false;
if (Array.isArray(value)) return true;
var proto = Object.getPrototypeOf(value);
return proto === null || proto === Object.prototype;
}
function freeze(value) {
if (autoFreeze) {
Object.freeze(value);
}
return value;
}
function shallowCopy(value) {
return Array.isArray(value) ? value.slice() : Object.assign({}, value); // TODO: eliminate those isArray checks?
}
function each(value, cb) {
if (Array.isArray(value)) {
for (var i = 0; i < value.length; i++) {
cb(i, value[i]);
}
} else {
for (var key in value) {
cb(key, value[key]);
}
}
}
// given a base object, returns it if unmodified, or return the changed cloned if modified
function finalize(base) {
if (isProxy(base)) {
var state = base[PROXY_STATE];
if (state.modified === true) {
if (state.finalized === true) return state.copy;
state.finalized = true;
if (Array.isArray(state.base)) return useProxies ? finalizeArray(state) : finalizeArray$1(base, state);
return useProxies ? finalizeObject(state) : finalizeObject$1(base, state);
} else return state.base;
} else if (base !== null && (typeof base === "undefined" ? "undefined" : _typeof(base)) === "object") {
finalizeNonProxiedObject(base);
}
return base;
}
function finalizeNonProxiedObject(parent) {
// If finalize is called on an object that was not a proxy, it means that it is an object that was not there in the original
// tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well
if (!isProxyable(parent)) return;
if (Object.isFrozen(parent)) return;
if (Array.isArray(parent)) {
for (var i = 0; i < parent.length; i++) {
var child = parent[i];
if (isProxy(child)) {
parent[i] = finalize(child);
} else finalizeNonProxiedObject(child);
}
} else {
for (var key in parent) {
var _child = parent[key];
if (isProxy(_child)) {
parent[key] = finalize(_child);
} else finalizeNonProxiedObject(_child);
}
}
// always freeze completely new data
freeze(parent);
}
function verifyReturnValue(value) {
// values either than undefined will trigger warning;
if (value !== undefined) console.warn("Immer callback expects no return value. However " + (typeof value === "undefined" ? "undefined" : _typeof(value)) + " was returned");
}
/**
* produce takes a state, and runs a function against it.

@@ -502,12 +506,8 @@ * That function can freely mutate the state, as it will create copies-on-write.

return useProxies ? produceProxy(baseState, producer) : produceEs5(baseState, producer);
return getUseProxies() ? produceProxy(baseState, producer) : produceEs5(baseState, producer);
}
function setUseProxies(value) {
useProxies = value;
}
exports['default'] = produce;
exports.setAutoFreeze = setAutoFreeze;
exports.setUseProxies = setUseProxies;
exports.setAutoFreeze = setAutoFreeze;
//# sourceMappingURL=immer.js.map

@@ -1,73 +0,7 @@

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
var PROXY_STATE = Symbol("immer-proxy-state"); // TODO: create per closure, to avoid sharing proxies between multiple immer version
var autoFreeze = true;
function isProxy(value) {
return !!value && !!value[PROXY_STATE];
}
function isProxyable(value) {
if (!value) return false;
if ((typeof value === "undefined" ? "undefined" : _typeof(value)) !== "object") return false;
if (Array.isArray(value)) return true;
var proto = Object.getPrototypeOf(value);
return proto === null || proto === Object.prototype;
}
function freeze(value) {
if (autoFreeze) {
Object.freeze(value);
}
return value;
}
/**
* Automatically freezes any state trees generated by immer.
* This protects against accidental modifications of the state tree outside of an immer function.
* This comes with a performance impact, so it is recommended to disable this option in production.
* It is by default enabled.
*
* @returns {void}
*/
function setAutoFreeze(enableAutoFreeze) {
autoFreeze = enableAutoFreeze;
}
function finalizeNonProxiedObject(parent, finalizer) {
// If finalize is called on an object that was not a proxy, it means that it is an object that was not there in the original
// tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well
if (!isProxyable(parent)) return;
if (Object.isFrozen(parent)) return;
var modified = false;
if (Array.isArray(parent)) {
for (var i = 0; i < parent.length; i++) {
var child = parent[i];
if (isProxy(child)) {
parent[i] = finalizer(child);
modified = true;
} else finalizeNonProxiedObject(child, finalizer);
}
} else {
for (var key in parent) {
var _child = parent[key];
if (isProxy(_child)) {
parent[key] = finalizer(_child);
modified = true;
} else finalizeNonProxiedObject(_child, finalizer);
}
}
if (modified) freeze(parent);
}
// @ts-check
var revocableProxies = null;
var proxies = null;
var objectTraps = {
get: get$1,
get: get,
has: function has(target, prop) {

@@ -80,6 +14,6 @@ return prop in source(target);

set: set$1,
set: set,
deleteProperty: deleteProperty,
getOwnPropertyDescriptor: getOwnPropertyDescriptor,
defineProperty: defineProperty$1,
defineProperty: defineProperty,
setPrototypeOf: function setPrototypeOf() {

@@ -91,14 +25,9 @@ throw new Error("Don't even try this...");

var arrayTraps = {};
var _loop = function _loop(key) {
each(objectTraps, function (key, fn) {
arrayTraps[key] = function () {
arguments[0] = arguments[0][0];
return objectTraps[key].apply(this, arguments);
return fn.apply(this, arguments);
};
};
});
for (var key in objectTraps) {
_loop(key);
}
function createState(parent, base) {

@@ -119,3 +48,3 @@ return {

function get$1(state, prop) {
function get(state, prop) {
if (prop === PROXY_STATE) return state;

@@ -134,3 +63,3 @@ if (state.modified) {

function set$1(state, prop, value) {
function set(state, prop, value) {
if (!state.modified) {

@@ -157,3 +86,3 @@ if (prop in state.base && Object.is(state.base[prop], value) || prop in state.proxies && state.proxies[prop] === value) return true;

function defineProperty$1() {
function defineProperty() {
throw new Error("Immer does currently not support defining properties on draft objects");

@@ -165,3 +94,4 @@ }

state.modified = true;
state.copy = Array.isArray(state.base) ? state.base.slice() : Object.assign({}, state.base); // TODO: eliminate those isArray checks?
state.copy = shallowCopy(state.base);
// copy the proxies over the base-copy
Object.assign(state.copy, state.proxies); // yup that works for arrays as well

@@ -175,34 +105,13 @@ if (state.parent) markChanged(state.parent);

var state = createState(parentState, base);
var proxy = void 0;
if (Array.isArray(base)) {
proxy = Proxy.revocable([state], arrayTraps);
} else {
proxy = Proxy.revocable(state, objectTraps);
}
revocableProxies.push(proxy);
var proxy = Array.isArray(base) ? Proxy.revocable([state], arrayTraps) : Proxy.revocable(state, objectTraps);
proxies.push(proxy);
return proxy.proxy;
}
// given a base object, returns it if unmodified, or return the changed cloned if modified
function finalize(base) {
if (isProxy(base)) {
var state = base[PROXY_STATE];
if (state.modified === true) {
if (state.finalized === true) return state.copy;
state.finalized = true;
if (Array.isArray(state.base)) return finalizeArray(state);
return finalizeObject(state);
} else return state.base;
} else if (base !== null && (typeof base === "undefined" ? "undefined" : _typeof(base)) === "object") {
finalizeNonProxiedObject(base, finalize);
}
return base;
}
function finalizeObject(state) {
var copy = state.copy;
var base = state.base;
for (var prop in copy) {
if (copy[prop] !== base[prop]) copy[prop] = finalize(copy[prop]);
}
each(copy, function (prop, value) {
if (value !== base[prop]) copy[prop] = finalize(value);
});
return freeze(copy);

@@ -214,5 +123,5 @@ }

var base = state.base;
for (var i = 0; i < copy.length; i++) {
if (copy[i] !== base[i]) copy[i] = finalize(copy[i]);
}
each(copy, function (i, value) {
if (value !== base[i]) copy[i] = finalize(value);
});
return freeze(copy);

@@ -222,4 +131,4 @@ }

function produceProxy(baseState, producer) {
var previousProxies = revocableProxies;
revocableProxies = [];
var previousProxies = proxies;
proxies = [];
try {

@@ -229,9 +138,7 @@ // create proxy for root

// execute the thunk
var maybeVoidReturn = producer(rootClone);
//values either than undefined will trigger warning;
!Object.is(maybeVoidReturn, undefined) && console.warn("Immer callback expects no return value. However " + (typeof maybeVoidReturn === "undefined" ? "undefined" : _typeof(maybeVoidReturn)) + " was returned");
verifyReturnValue(producer(rootClone));
// and finalize the modified proxy
var res = finalize(rootClone);
// revoke all proxies
revocableProxies.forEach(function (p) {
each(proxies, function (_, p) {
return p.revoke();

@@ -241,6 +148,12 @@ });

} finally {
revocableProxies = previousProxies;
proxies = previousProxies;
}
}
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
// @ts-check

@@ -260,3 +173,4 @@

finished: false,
finalizing: false
finalizing: false,
finalized: false
};

@@ -284,4 +198,4 @@ }

markChanged$1(state);
prepareCopy(state);
}
prepareCopy(state);
state.copy[prop] = value;

@@ -300,3 +214,3 @@ }

state.hasCopy = true;
state.copy = Array.isArray(state.base) ? state.base.slice() : Object.assign({}, state.base);
state.copy = shallowCopy(state.base);
}

@@ -306,4 +220,3 @@

function createProxy$1(parent, base) {
var proxy = void 0;
if (Array.isArray(base)) proxy = createArrayProxy(base);else proxy = createObjectProxy(base);
var proxy = Array.isArray(base) ? createArrayProxy(base) : createObjectProxy(base);
var state = createState$1(parent, proxy, base);

@@ -319,6 +232,6 @@ createHiddenProperty(proxy, PROXY_STATE, state);

enumerable: true,
get: function get$$1() {
get: function get() {
return _get(this[PROXY_STATE], prop);
},
set: function set$$1(value) {
set: function set(value) {
_set(this[PROXY_STATE], prop, value);

@@ -331,4 +244,4 @@ }

var proxy = Object.assign({}, base);
Object.keys(base).forEach(function (prop) {
return Object.defineProperty(proxy, prop, createPropertyProxy(prop));
each(base, function (prop) {
Object.defineProperty(proxy, prop, createPropertyProxy(prop));
});

@@ -340,5 +253,6 @@ return proxy;

var proxy = new Array(base.length);
for (var i = 0; i < base.length; i++) {
each(base, function (i) {
Object.defineProperty(proxy, "" + i, createPropertyProxy("" + i));
}return proxy;
});
return proxy;
}

@@ -377,22 +291,8 @@

function finalize$1(proxy) {
// TODO: almost litterally same as Proxy impl; let's reduce code duplication and rollup
if (isProxy(proxy)) {
var state = proxy[PROXY_STATE];
if (state.modified === true) {
if (Array.isArray(state.base)) return finalizeArray$1(proxy, state);
return finalizeObject$1(proxy, state);
} else return state.base;
} else if (proxy !== null && (typeof proxy === "undefined" ? "undefined" : _typeof(proxy)) === "object") {
finalizeNonProxiedObject(proxy, finalize$1);
}
return proxy;
}
function finalizeObject$1(proxy, state) {
var res = Object.assign({}, proxy);
var res = state.copy = shallowCopy(proxy);
var base = state.base;
for (var prop in res) {
if (proxy[prop] !== base[prop]) res[prop] = finalize$1(res[prop]);
}
each(res, function (prop, value) {
if (value !== base[prop]) res[prop] = finalize(value);
});
return freeze(res);

@@ -402,7 +302,7 @@ }

function finalizeArray$1(proxy, state) {
var res = proxy.slice();
var res = state.copy = shallowCopy(proxy);
var base = state.base;
for (var i = 0; i < res.length; i++) {
if (res[i] !== base[i]) res[i] = finalize$1(res[i]);
}
each(res, function (i, value) {
if (value !== base[i]) res[i] = finalize(value);
});
return freeze(res);

@@ -418,15 +318,16 @@ }

// execute the thunk
var maybeVoidReturn = producer(rootClone);
//values either than undefined will trigger warning;
!Object.is(maybeVoidReturn, undefined) && console.warn("Immer callback expects no return value. However " + (typeof maybeVoidReturn === "undefined" ? "undefined" : _typeof(maybeVoidReturn)) + " was returned");
verifyReturnValue(producer(rootClone));
// and finalize the modified proxy
for (var i = 0; i < states.length; i++) {
states[i].finalizing = true;
} // find and mark all changes (for parts not done yet)
each(states, function (_, state) {
state.finalizing = true;
});
// find and mark all changes (for parts not done yet)
// TODO: store states by depth, to be able guarantee processing leaves first
markChanges();
var res = finalize$1(rootClone);
var res = finalize(rootClone);
// make sure all proxies become unusable
for (var _i = 0; _i < states.length; _i++) {
states[_i].finished = true;
}return res;
each(states, function (_, state) {
state.finished = true;
});
return res;
} finally {

@@ -462,5 +363,108 @@ states = prevStates;

var PROXY_STATE = typeof Symbol !== "undefined" ? Symbol("immer-proxy-state") : "__$immer_state";
var autoFreeze = true;
var useProxies = typeof Proxy !== "undefined";
/**
* Automatically freezes any state trees generated by immer.
* This protects against accidental modifications of the state tree outside of an immer function.
* This comes with a performance impact, so it is recommended to disable this option in production.
* It is by default enabled.
*
* @returns {void}
*/
function setAutoFreeze(enableAutoFreeze) {
autoFreeze = enableAutoFreeze;
}
function setUseProxies(value) {
useProxies = value;
}
function getUseProxies() {
return useProxies;
}
function isProxy(value) {
return !!value && !!value[PROXY_STATE];
}
function isProxyable(value) {
if (!value) return false;
if ((typeof value === "undefined" ? "undefined" : _typeof(value)) !== "object") return false;
if (Array.isArray(value)) return true;
var proto = Object.getPrototypeOf(value);
return proto === null || proto === Object.prototype;
}
function freeze(value) {
if (autoFreeze) {
Object.freeze(value);
}
return value;
}
function shallowCopy(value) {
return Array.isArray(value) ? value.slice() : Object.assign({}, value); // TODO: eliminate those isArray checks?
}
function each(value, cb) {
if (Array.isArray(value)) {
for (var i = 0; i < value.length; i++) {
cb(i, value[i]);
}
} else {
for (var key in value) {
cb(key, value[key]);
}
}
}
// given a base object, returns it if unmodified, or return the changed cloned if modified
function finalize(base) {
if (isProxy(base)) {
var state = base[PROXY_STATE];
if (state.modified === true) {
if (state.finalized === true) return state.copy;
state.finalized = true;
if (Array.isArray(state.base)) return useProxies ? finalizeArray(state) : finalizeArray$1(base, state);
return useProxies ? finalizeObject(state) : finalizeObject$1(base, state);
} else return state.base;
} else if (base !== null && (typeof base === "undefined" ? "undefined" : _typeof(base)) === "object") {
finalizeNonProxiedObject(base);
}
return base;
}
function finalizeNonProxiedObject(parent) {
// If finalize is called on an object that was not a proxy, it means that it is an object that was not there in the original
// tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well
if (!isProxyable(parent)) return;
if (Object.isFrozen(parent)) return;
if (Array.isArray(parent)) {
for (var i = 0; i < parent.length; i++) {
var child = parent[i];
if (isProxy(child)) {
parent[i] = finalize(child);
} else finalizeNonProxiedObject(child);
}
} else {
for (var key in parent) {
var _child = parent[key];
if (isProxy(_child)) {
parent[key] = finalize(_child);
} else finalizeNonProxiedObject(_child);
}
}
// always freeze completely new data
freeze(parent);
}
function verifyReturnValue(value) {
// values either than undefined will trigger warning;
if (value !== undefined) console.warn("Immer callback expects no return value. However " + (typeof value === "undefined" ? "undefined" : _typeof(value)) + " was returned");
}
/**
* produce takes a state, and runs a function against it.

@@ -497,11 +501,7 @@ * That function can freely mutate the state, as it will create copies-on-write.

return useProxies ? produceProxy(baseState, producer) : produceEs5(baseState, producer);
return getUseProxies() ? produceProxy(baseState, producer) : produceEs5(baseState, producer);
}
function setUseProxies(value) {
useProxies = value;
}
export { setUseProxies, setAutoFreeze };
export { setAutoFreeze, setUseProxies };
export default produce;
//# sourceMappingURL=immer.module.js.map

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

var e,r;e=this,r=function(e){"use strict";var r="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},n=Symbol("immer-proxy-state"),t=!0;function o(e){return!!e&&!!e[n]}function i(e){if(!e)return!1;if("object"!==(void 0===e?"undefined":r(e)))return!1;if(Array.isArray(e))return!0;var n=Object.getPrototypeOf(e);return null===n||n===Object.prototype}function a(e){return t&&Object.freeze(e),e}function f(e,r){if(i(e)&&!Object.isFrozen(e)){var n=!1;if(Array.isArray(e))for(var t=0;t<e.length;t++){var u=e[t];o(u)?(e[t]=r(u),n=!0):f(u,r)}else for(var c in e){var s=e[c];o(s)?(e[c]=r(s),n=!0):f(s,r)}n&&a(e)}}var u=null,c={get:function(e,r){if(r===n)return e;if(e.modified){var t=e.copy[r];return!o(t)&&i(t)?e.copy[r]=b(e,t):t}if(r in e.proxies)return e.proxies[r];var a=e.base[r];return!o(a)&&i(a)?e.proxies[r]=b(e,a):a},has:function(e,r){return r in p(e)},ownKeys:function(e){return Reflect.ownKeys(p(e))},set:function(e,r,n){if(!e.modified){if(r in e.base&&Object.is(e.base[r],n)||r in e.proxies&&e.proxies[r]===n)return!0;l(e)}return e.copy[r]=n,!0},deleteProperty:function(e,r){return l(e),delete e.copy[r],!0},getOwnPropertyDescriptor:function(e,r){var n=e.modified?e.copy:r in e.proxies?e.proxies:e.base,t=Reflect.getOwnPropertyDescriptor(n,r);!t||Array.isArray(n)&&"length"===r||(t.configurable=!0);return t},defineProperty:function(){throw new Error("Immer does currently not support defining properties on draft objects")},setPrototypeOf:function(){throw new Error("Don't even try this...")}},s={},d=function(e){s[e]=function(){return arguments[0]=arguments[0][0],c[e].apply(this,arguments)}};for(var y in c)d(y);function p(e){return!0===e.modified?e.copy:e.base}function l(e){e.modified||(e.modified=!0,e.copy=Array.isArray(e.base)?e.base.slice():Object.assign({},e.base),Object.assign(e.copy,e.proxies),e.parent&&l(e.parent))}function b(e,r){var n={modified:!1,finalized:!1,parent:e,base:r,copy:void 0,proxies:{}},t=void 0;return t=Array.isArray(r)?Proxy.revocable([n],s):Proxy.revocable(n,c),u.push(t),t.proxy}function v(e){if(o(e)){var t=e[n];return!0===t.modified?!0===t.finalized?t.copy:(t.finalized=!0,Array.isArray(t.base)?function(e){for(var r=e.copy,n=e.base,t=0;t<r.length;t++)r[t]!==n[t]&&(r[t]=v(r[t]));return a(r)}(t):function(e){var r=e.copy,n=e.base;for(var t in r)r[t]!==n[t]&&(r[t]=v(r[t]));return a(r)}(t)):t.base}return null!==e&&"object"===(void 0===e?"undefined":r(e))&&f(e,v),e}var h={},m=null;function g(e){return e.hasCopy?e.copy:e.base}function j(e){e.modified||(e.modified=!0,e.parent&&j(e.parent))}function O(e){e.hasCopy||(e.hasCopy=!0,e.copy=Array.isArray(e.base)?e.base.slice():Object.assign({},e.base))}function w(e,r){var t,o,i=void 0;Array.isArray(r)?i=function(e){for(var r=new Array(e.length),n=0;n<e.length;n++)Object.defineProperty(r,""+n,x(""+n));return r}(r):(t=r,o=Object.assign({},t),Object.keys(t).forEach(function(e){return Object.defineProperty(o,e,x(e))}),i=o);var a,f,u,c={modified:!1,hasCopy:!1,parent:e,base:r,proxy:i,copy:void 0,finished:!1,finalizing:!1};return a=i,f=n,u=c,Object.defineProperty(a,f,{value:u,enumerable:!1,writable:!0}),m.push(c),i}function x(e){return h[e]||(h[e]={configurable:!0,enumerable:!0,get:function(){return function(e,r){A(e);var n=g(e)[r];return e.finalizing||o(n)||!i(n)?n:(O(e),e.copy[r]=w(e,n))}(this[n],e)},set:function(r){!function(e,r,n){if(A(e),!e.modified){if(Object.is(g(e)[r],n))return;j(e)}O(e),e.copy[r]=n}(this[n],e,r)}})}function A(e){if(!0===e.finished)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?")}function P(e){if(o(e)){var t=e[n];return!0===t.modified?Array.isArray(t.base)?function(e,r){for(var n=e.slice(),t=r.base,o=0;o<n.length;o++)n[o]!==t[o]&&(n[o]=P(n[o]));return a(n)}(e,t):function(e,r){var n=Object.assign({},e),t=r.base;for(var o in n)e[o]!==t[o]&&(n[o]=P(n[o]));return a(n)}(e,t):t.base}return null!==e&&"object"===(void 0===e?"undefined":r(e))&&f(e,P),e}function k(e,n){var t=m;m=[];try{var o=w(void 0,e),i=n(o);!Object.is(i,void 0)&&console.warn("Immer callback expects no return value. However "+(void 0===i?"undefined":r(i))+" was returned");for(var a=0;a<m.length;a++)m[a].finalizing=!0;!function(){for(var e=m.length-1;e>=0;e--){var n=m[e];!1===n.modified&&(Array.isArray(n.base)?(a=n).proxy.length!==a.base.length&&j(n):(t=n,o=Object.keys(t.base),i=Object.keys(t.proxy),function(e,n){if(Object.is(e,n))return!0;if("object"!==(void 0===e?"undefined":r(e))||null===e||"object"!==(void 0===n?"undefined":r(n))||null===n)return!1;var t=Object.keys(e),o=Object.keys(n);if(t.length!==o.length)return!1;for(var i=0;i<t.length;i++)if(!hasOwnProperty.call(n,t[i])||!Object.is(e[t[i]],n[t[i]]))return!1;return!0}(o,i)||j(n)))}var t,o,i,a}();for(var f=P(o),u=0;u<m.length;u++)m[u].finished=!0;return f}finally{m=t}}var z="undefined"!=typeof Proxy;e.default=function e(n,t){if(1===arguments.length){if("function"!=typeof n)throw new Error("if produce is called with 1 argument, the first argument should be a function");return function(){var r=arguments;return e(r[0],function(e){r[0]=e,n.apply(null,r)})}}if(2!==arguments.length)throw new Error("produce expects 1 or 2 arguments, got "+arguments.length);if(!i(n))throw new Error("the first argument to produce should be a plain object or array, got "+(void 0===n?"undefined":r(n)));if("function"!=typeof t)throw new Error("the second argument to produce should be a function");return z?function(e,n){var t=u;u=[];try{var o=b(void 0,e),i=n(o);!Object.is(i,void 0)&&console.warn("Immer callback expects no return value. However "+(void 0===i?"undefined":r(i))+" was returned");var a=v(o);return u.forEach(function(e){return e.revoke()}),a}finally{u=t}}(n,t):k(n,t)},e.setUseProxies=function(e){z=e},e.setAutoFreeze=function(e){t=e},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=null,n={get:function(e,r){if(r===h)return e;if(e.modified){var n=e.copy[r];return!j(n)&&w(n)?e.copy[r]=f(e,n):n}if(r in e.proxies)return e.proxies[r];var t=e.base[r];return!j(t)&&w(t)?e.proxies[r]=f(e,t):t},has:function(e,r){return r in o(e)},ownKeys:function(e){return Reflect.ownKeys(o(e))},set:function(e,r,n){if(!e.modified){if(r in e.base&&Object.is(e.base[r],n)||r in e.proxies&&e.proxies[r]===n)return!0;i(e)}return e.copy[r]=n,!0},deleteProperty:function(e,r){return i(e),delete e.copy[r],!0},getOwnPropertyDescriptor:function(e,r){var n=e.modified?e.copy:r in e.proxies?e.proxies:e.base,t=Reflect.getOwnPropertyDescriptor(n,r);!t||Array.isArray(n)&&"length"===r||(t.configurable=!0);return t},defineProperty:function(){throw new Error("Immer does currently not support defining properties on draft objects")},setPrototypeOf:function(){throw new Error("Don't even try this...")}},t={};function o(e){return!0===e.modified?e.copy:e.base}function i(e){e.modified||(e.modified=!0,e.copy=O(e.base),Object.assign(e.copy,e.proxies),e.parent&&i(e.parent))}function f(e,o){var i={modified:!1,finalized:!1,parent:e,base:o,copy:void 0,proxies:{}},f=Array.isArray(o)?Proxy.revocable([i],t):Proxy.revocable(i,n);return r.push(f),f.proxy}A(n,function(e,r){t[e]=function(){return arguments[0]=arguments[0][0],r.apply(this,arguments)}});var u="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},a={},c=null;function s(e){return e.hasCopy?e.copy:e.base}function y(e){e.modified||(e.modified=!0,e.parent&&y(e.parent))}function p(e){e.hasCopy||(e.hasCopy=!0,e.copy=O(e.base))}function d(e,r){var n,t,o,i,f,u,a,s=Array.isArray(r)?(o=r,i=new Array(o.length),A(o,function(e){Object.defineProperty(i,""+e,l(""+e))}),i):(n=r,t=Object.assign({},n),A(n,function(e){Object.defineProperty(t,e,l(e))}),t),y={modified:!1,hasCopy:!1,parent:e,base:r,proxy:s,copy:void 0,finished:!1,finalizing:!1,finalized:!1};return f=s,u=h,a=y,Object.defineProperty(f,u,{value:a,enumerable:!1,writable:!0}),c.push(y),s}function l(e){return a[e]||(a[e]={configurable:!0,enumerable:!0,get:function(){return function(e,r){b(e);var n=s(e)[r];return e.finalizing||j(n)||!w(n)?n:(p(e),e.copy[r]=d(e,n))}(this[h],e)},set:function(r){!function(e,r,n){if(b(e),!e.modified){if(Object.is(s(e)[r],n))return;y(e),p(e)}e.copy[r]=n}(this[h],e,r)}})}function b(e){if(!0===e.finished)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?")}function v(e,r){var n=c;c=[];try{var t=d(void 0,e);z(r(t)),A(c,function(e,r){r.finalizing=!0}),function(){for(var e=c.length-1;e>=0;e--){var r=c[e];!1===r.modified&&(Array.isArray(r.base)?(i=r).proxy.length!==i.base.length&&y(r):(n=r,t=Object.keys(n.base),o=Object.keys(n.proxy),function(e,r){if(Object.is(e,r))return!0;if("object"!==(void 0===e?"undefined":u(e))||null===e||"object"!==(void 0===r?"undefined":u(r))||null===r)return!1;var n=Object.keys(e),t=Object.keys(r);if(n.length!==t.length)return!1;for(var o=0;o<n.length;o++)if(!hasOwnProperty.call(r,n[o])||!Object.is(e[n[o]],r[n[o]]))return!1;return!0}(t,o)||y(r)))}var n,t,o,i}();var o=P(t);return A(c,function(e,r){r.finished=!0}),o}finally{c=n}}var h="undefined"!=typeof Symbol?Symbol("immer-proxy-state"):"__$immer_state",m=!0,g="undefined"!=typeof Proxy;function j(e){return!!e&&!!e[h]}function w(e){if(!e)return!1;if("object"!==(void 0===e?"undefined":u(e)))return!1;if(Array.isArray(e))return!0;var r=Object.getPrototypeOf(e);return null===r||r===Object.prototype}function x(e){return m&&Object.freeze(e),e}function O(e){return Array.isArray(e)?e.slice():Object.assign({},e)}function A(e,r){if(Array.isArray(e))for(var n=0;n<e.length;n++)r(n,e[n]);else for(var t in e)r(t,e[t])}function P(e){if(j(e)){var r=e[h];return!0===r.modified?!0===r.finalized?r.copy:(r.finalized=!0,Array.isArray(r.base)?g?(b=(l=r).copy,v=l.base,A(b,function(e,r){r!==v[e]&&(b[e]=P(r))}),x(b)):(s=e,p=(y=r).copy=O(s),d=y.base,A(p,function(e,r){r!==d[e]&&(p[e]=P(r))}),x(p)):g?(a=(f=r).copy,c=f.base,A(a,function(e,r){r!==c[e]&&(a[e]=P(r))}),x(a)):(n=e,o=(t=r).copy=O(n),i=t.base,A(o,function(e,r){r!==i[e]&&(o[e]=P(r))}),x(o))):r.base}var n,t,o,i,f,a,c,s,y,p,d,l,b,v;return null!==e&&"object"===(void 0===e?"undefined":u(e))&&function e(r){if(!w(r))return;if(Object.isFrozen(r))return;if(Array.isArray(r))for(var n=0;n<r.length;n++){var t=r[n];j(t)?r[n]=P(t):e(t)}else for(var o in r){var i=r[o];j(i)?r[o]=P(i):e(i)}x(r)}(e),e}function z(e){void 0!==e&&console.warn("Immer callback expects no return value. However "+(void 0===e?"undefined":u(e))+" was returned")}e.default=function e(n,t){if(1===arguments.length){if("function"!=typeof n)throw new Error("if produce is called with 1 argument, the first argument should be a function");return function(){var r=arguments;return e(r[0],function(e){r[0]=e,n.apply(null,r)})}}if(2!==arguments.length)throw new Error("produce expects 1 or 2 arguments, got "+arguments.length);if(!w(n))throw new Error("the first argument to produce should be a plain object or array, got "+(void 0===n?"undefined":u(n)));if("function"!=typeof t)throw new Error("the second argument to produce should be a function");return g?function(e,n){var t=r;r=[];try{var o=f(void 0,e);z(n(o));var i=P(o);return A(r,function(e,r){return r.revoke()}),i}finally{r=t}}(n,t):v(n,t)},e.setAutoFreeze=function(e){m=e},e.setUseProxies=function(e){g=e},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

@@ -38,1 +38,7 @@ /**

export function setAutoFreeze(autoFreeze: boolean): void
/**
* Manually override whether proxies should be used.
* By default done by using feature detection
*/
export function setUseProxies(useProxies: boolean): void
{
"name": "immer",
"version": "0.8.2",
"version": "0.8.3",
"description": "Create your next immutable state by mutating the current one",

@@ -13,7 +13,7 @@ "main": "dist/immer.js",

"test": "jest",
"test:perf": "yarn build && node --expose-gc node_modules/jest-cli/bin/jest.js --verbose --testRegex '__performance_tests__/.*'",
"test:perf": "yarn-or-npm build && node --expose-gc node_modules/jest-cli/bin/jest.js --verbose --testRegex '__performance_tests__/.*?js$'",
"coveralls": "jest --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",
"build": "rimraf dist/ && cross-env NODE_ENV=production rollup -c",
"prettier": "prettier \"*/**/*.js\" --ignore-path ./.prettierignore --write",
"prepublish": "yarn build"
"prepublish": "yarn-or-npm run build"
},

@@ -68,3 +68,4 @@ "pre-commit": [

"typescript": "^2.6.2",
"uglify-es": "^3.3.6"
"uglify-es": "^3.3.6",
"yarn-or-npm": "^2.0.4"
},

@@ -71,0 +72,0 @@ "jest": {

@@ -167,4 +167,5 @@ # Immer

case RECEIVE_PRODUCTS:
action.products.forEach(product => {
action.products.forEach(product => {
draft[product.id] = product
})
})

@@ -171,0 +172,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

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