Comparing version 0.5.0 to 0.6.0
# Changelog | ||
### 0.4.3 | ||
### 0.6.0 | ||
The default import now bundles both the es5 and proxy implementation as a convenient default. For more optimal bundle sizes, import `immer/proxy` or `immer/es5` | ||
### 0.5.0 | ||
* Make sure es5.js is transpiled | ||
@@ -6,0 +10,0 @@ |
"use strict"; | ||
// @ts-check | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
@@ -8,2 +12,5 @@ | ||
exports.default = produce; | ||
exports.setAutoFreeze = setAutoFreeze; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -273,6 +280,2 @@ | ||
autoFreeze = enableAutoFreeze; | ||
} | ||
createHiddenProperty(exports, "__esModule", true); | ||
module.exports.default = produce; | ||
module.exports.setAutoFreeze = setAutoFreeze; | ||
} |
@@ -5,2 +5,6 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.immer = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
@@ -10,2 +14,5 @@ | ||
exports.default = produce; | ||
exports.setAutoFreeze = setAutoFreeze; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -276,7 +283,3 @@ | ||
} | ||
createHiddenProperty(exports, "__esModule", true); | ||
module.exports.default = produce; | ||
module.exports.setAutoFreeze = setAutoFreeze; | ||
},{}]},{},[1])(1) | ||
}); |
"use strict"; | ||
// @ts-check | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
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; }; | ||
exports.default = produce; | ||
exports.setAutoFreeze = setAutoFreeze; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
if (typeof Proxy === "undefined") throw new Error("Immer requires `Proxy` to be available, but it seems to be not available on your platform. Consider requiring immer '\"immer/es5\"' instead."); | ||
const PROXY_STATE = Symbol("immer-proxy-state"); // TODO: create per closure, to avoid sharing proxies between multiple immer version | ||
let autoFreeze = true; | ||
var PROXY_STATE = Symbol("immer-proxy-state"); // TODO: create per closure, to avoid sharing proxies between multiple immer version | ||
var autoFreeze = true; | ||
const objectTraps = { | ||
get(target, prop) { | ||
var objectTraps = { | ||
get: function get(target, prop) { | ||
if (prop === PROXY_STATE) return target; | ||
return target.get(prop); | ||
}, | ||
has(target, prop) { | ||
has: function has(target, prop) { | ||
return prop in target.source; | ||
}, | ||
ownKeys(target) { | ||
ownKeys: function ownKeys(target) { | ||
return Reflect.ownKeys(target.source); | ||
}, | ||
set(target, prop, value) { | ||
set: function set(target, prop, value) { | ||
target.set(prop, value); | ||
return true; | ||
}, | ||
deleteProperty(target, prop) { | ||
deleteProperty: function deleteProperty(target, prop) { | ||
target.deleteProp(prop); | ||
return true; | ||
}, | ||
getOwnPropertyDescriptor(target, prop) { | ||
getOwnPropertyDescriptor: function getOwnPropertyDescriptor(target, prop) { | ||
return target.getOwnPropertyDescriptor(prop); | ||
}, | ||
defineProperty(target, property, descriptor) { | ||
defineProperty: function defineProperty(target, property, descriptor) { | ||
target.defineProperty(property, descriptor); | ||
return true; | ||
}, | ||
setPrototypeOf() { | ||
setPrototypeOf: function setPrototypeOf() { | ||
throw new Error("Don't even try this..."); | ||
@@ -40,29 +53,29 @@ } | ||
const arrayTraps = { | ||
get(target, prop) { | ||
var arrayTraps = { | ||
get: function get(target, prop) { | ||
if (prop === PROXY_STATE) return target[0]; | ||
return target[0].get(prop); | ||
}, | ||
has(target, prop) { | ||
has: function has(target, prop) { | ||
return prop in target[0].source; | ||
}, | ||
ownKeys(target) { | ||
ownKeys: function ownKeys(target) { | ||
return Reflect.ownKeys(target[0].source); | ||
}, | ||
set(target, prop, value) { | ||
set: function set(target, prop, value) { | ||
target[0].set(prop, value); | ||
return true; | ||
}, | ||
deleteProperty(target, prop) { | ||
deleteProperty: function deleteProperty(target, prop) { | ||
target[0].deleteProp(prop); | ||
return true; | ||
}, | ||
getOwnPropertyDescriptor(target, prop) { | ||
getOwnPropertyDescriptor: function getOwnPropertyDescriptor(target, prop) { | ||
return target[0].getOwnPropertyDescriptor(prop); | ||
}, | ||
defineProperty(target, property, descriptor) { | ||
defineProperty: function defineProperty(target, property, descriptor) { | ||
target[0].defineProperty(property, descriptor); | ||
return true; | ||
}, | ||
setPrototypeOf() { | ||
setPrototypeOf: function setPrototypeOf() { | ||
throw new Error("Don't even try this..."); | ||
@@ -85,7 +98,7 @@ } | ||
if (arguments.length === 1) { | ||
const producer = baseState; | ||
var _producer = baseState; | ||
// prettier-ignore | ||
if (typeof producer !== "function") throw new Error("if produce is called with 1 argument, the first argument should be a function"); | ||
if (typeof _producer !== "function") throw new Error("if produce is called with 1 argument, the first argument should be a function"); | ||
return function () { | ||
const args = arguments; | ||
var args = arguments; | ||
return produce(args[0], function (draft) { | ||
@@ -101,9 +114,11 @@ args[0] = draft; // blegh! | ||
if (arguments.length !== 2) throw new Error("produce expects 1 or 2 arguments, got " + arguments.length); | ||
if (!isProxyable(baseState)) throw new Error("the first argument to produce should be a plain object or array, got " + typeof baseState); | ||
if (!isProxyable(baseState)) throw new Error("the first argument to produce should be a plain object or array, got " + (typeof baseState === "undefined" ? "undefined" : _typeof(baseState))); | ||
if (typeof producer !== "function") throw new Error("the second argument to produce should be a function"); | ||
} | ||
const revocableProxies = []; | ||
var revocableProxies = []; | ||
class State { | ||
constructor(parent, base) { | ||
var State = function () { | ||
function State(parent, base) { | ||
_classCallCheck(this, State); | ||
this.modified = false; | ||
@@ -116,57 +131,70 @@ this.parent = parent; | ||
get source() { | ||
return this.modified === true ? this.copy : this.base; | ||
} | ||
get(prop) { | ||
if (this.modified) { | ||
const value = this.copy[prop]; | ||
if (!isProxy(value) && isProxyable(value)) return this.copy[prop] = createProxy(this, value); | ||
return value; | ||
} else { | ||
if (prop in this.proxies) return this.proxies[prop]; | ||
const value = this.base[prop]; | ||
if (!isProxy(value) && isProxyable(value)) return this.proxies[prop] = createProxy(this, value); | ||
return value; | ||
_createClass(State, [{ | ||
key: "get", | ||
value: function get(prop) { | ||
if (this.modified) { | ||
var value = this.copy[prop]; | ||
if (!isProxy(value) && isProxyable(value)) return this.copy[prop] = createProxy(this, value); | ||
return value; | ||
} else { | ||
if (prop in this.proxies) return this.proxies[prop]; | ||
var _value = this.base[prop]; | ||
if (!isProxy(_value) && isProxyable(_value)) return this.proxies[prop] = createProxy(this, _value); | ||
return _value; | ||
} | ||
} | ||
} | ||
set(prop, value) { | ||
if (!this.modified) { | ||
if (prop in this.base && this.base[prop] === value || prop in this.proxies && this.proxies[prop] === value) return; | ||
}, { | ||
key: "set", | ||
value: function set(prop, value) { | ||
if (!this.modified) { | ||
if (prop in this.base && this.base[prop] === value || prop in this.proxies && this.proxies[prop] === value) return; | ||
this.markChanged(); | ||
} | ||
this.copy[prop] = value; | ||
} | ||
}, { | ||
key: "deleteProp", | ||
value: function deleteProp(prop) { | ||
this.markChanged(); | ||
delete this.copy[prop]; | ||
} | ||
this.copy[prop] = value; | ||
} | ||
}, { | ||
key: "getOwnPropertyDescriptor", | ||
value: function getOwnPropertyDescriptor(prop) { | ||
var owner = this.modified ? this.copy : prop in this.proxies ? this.proxies : this.base; | ||
var descriptor = Reflect.getOwnPropertyDescriptor(owner, prop); | ||
if (descriptor && !(Array.isArray(owner) && prop === "length")) descriptor.configurable = true; | ||
return descriptor; | ||
} | ||
}, { | ||
key: "defineProperty", | ||
value: function defineProperty(property, descriptor) { | ||
throw new Error("Immer does currently not support defining properties on draft objects"); | ||
} | ||
}, { | ||
key: "markChanged", | ||
value: function markChanged() { | ||
if (!this.modified) { | ||
this.modified = true; | ||
this.copy = Array.isArray(this.base) ? this.base.slice() : Object.assign({}, this.base); // TODO: eliminate those isArray checks? | ||
Object.assign(this.copy, this.proxies); // yup that works for arrays as well | ||
if (this.parent) this.parent.markChanged(); | ||
} | ||
} | ||
}, { | ||
key: "source", | ||
get: function get() { | ||
return this.modified === true ? this.copy : this.base; | ||
} | ||
}]); | ||
deleteProp(prop) { | ||
this.markChanged(); | ||
delete this.copy[prop]; | ||
} | ||
return State; | ||
}(); | ||
getOwnPropertyDescriptor(prop) { | ||
const owner = this.modified ? this.copy : prop in this.proxies ? this.proxies : this.base; | ||
const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop); | ||
if (descriptor && !(Array.isArray(owner) && prop === "length")) descriptor.configurable = true; | ||
return descriptor; | ||
} | ||
// creates a proxy for plain objects / arrays | ||
defineProperty(property, descriptor) { | ||
throw new Error("Immer does currently not support defining properties on draft objects"); | ||
} | ||
markChanged() { | ||
if (!this.modified) { | ||
this.modified = true; | ||
this.copy = Array.isArray(this.base) ? this.base.slice() : Object.assign({}, this.base); // TODO: eliminate those isArray checks? | ||
Object.assign(this.copy, this.proxies); // yup that works for arrays as well | ||
if (this.parent) this.parent.markChanged(); | ||
} | ||
} | ||
} | ||
// creates a proxy for plain objects / arrays | ||
function createProxy(parentState, base) { | ||
const state = new State(parentState, base); | ||
let proxy; | ||
var state = new State(parentState, base); | ||
var proxy = void 0; | ||
if (Array.isArray(base)) { | ||
@@ -186,3 +214,3 @@ // Proxy should be created with an array to make it an array for JS | ||
if (isProxy(base)) { | ||
const state = base[PROXY_STATE]; | ||
var state = base[PROXY_STATE]; | ||
if (state.modified === true) { | ||
@@ -197,3 +225,3 @@ if (Array.isArray(state.base)) return finalizeArray(state); | ||
function finalizeObject(state) { | ||
const copy = state.copy; | ||
var copy = state.copy; | ||
Object.keys(copy).forEach(function (prop) { | ||
@@ -206,3 +234,3 @@ copy[prop] = finalize(copy[prop]); | ||
function finalizeArray(state) { | ||
const copy = state.copy; | ||
var copy = state.copy; | ||
copy.forEach(function (value, index) { | ||
@@ -215,9 +243,9 @@ copy[index] = finalize(copy[index]); | ||
// create proxy for root | ||
const rootClone = createProxy(undefined, baseState); | ||
var rootClone = createProxy(undefined, baseState); | ||
// execute the thunk | ||
const maybeVoidReturn = producer(rootClone); | ||
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} was returned`); | ||
!Object.is(maybeVoidReturn, undefined) && console.warn("Immer callback expects no return value. However " + (typeof maybeVoidReturn === "undefined" ? "undefined" : _typeof(maybeVoidReturn)) + " was returned"); | ||
// and finalize the modified proxy | ||
const res = finalize(rootClone); | ||
var res = finalize(rootClone); | ||
// revoke all proxies | ||
@@ -236,5 +264,5 @@ revocableProxies.forEach(function (p) { | ||
if (!value) return false; | ||
if (typeof value !== "object") return false; | ||
if ((typeof value === "undefined" ? "undefined" : _typeof(value)) !== "object") return false; | ||
if (Array.isArray(value)) return true; | ||
const proto = Object.getPrototypeOf(value); | ||
var proto = Object.getPrototypeOf(value); | ||
return proto === proto === null || Object.prototype; | ||
@@ -264,8 +292,2 @@ } | ||
autoFreeze = enableAutoFreeze; | ||
} | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
module.exports.default = produce; | ||
module.exports.setAutoFreeze = setAutoFreeze; | ||
} |
@@ -5,34 +5,47 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.immer = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
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; }; | ||
exports.default = produce; | ||
exports.setAutoFreeze = setAutoFreeze; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
if (typeof Proxy === "undefined") throw new Error("Immer requires `Proxy` to be available, but it seems to be not available on your platform. Consider requiring immer '\"immer/es5\"' instead."); | ||
const PROXY_STATE = Symbol("immer-proxy-state"); // TODO: create per closure, to avoid sharing proxies between multiple immer version | ||
let autoFreeze = true; | ||
var PROXY_STATE = Symbol("immer-proxy-state"); // TODO: create per closure, to avoid sharing proxies between multiple immer version | ||
var autoFreeze = true; | ||
const objectTraps = { | ||
get(target, prop) { | ||
var objectTraps = { | ||
get: function get(target, prop) { | ||
if (prop === PROXY_STATE) return target; | ||
return target.get(prop); | ||
}, | ||
has(target, prop) { | ||
has: function has(target, prop) { | ||
return prop in target.source; | ||
}, | ||
ownKeys(target) { | ||
ownKeys: function ownKeys(target) { | ||
return Reflect.ownKeys(target.source); | ||
}, | ||
set(target, prop, value) { | ||
set: function set(target, prop, value) { | ||
target.set(prop, value); | ||
return true; | ||
}, | ||
deleteProperty(target, prop) { | ||
deleteProperty: function deleteProperty(target, prop) { | ||
target.deleteProp(prop); | ||
return true; | ||
}, | ||
getOwnPropertyDescriptor(target, prop) { | ||
getOwnPropertyDescriptor: function getOwnPropertyDescriptor(target, prop) { | ||
return target.getOwnPropertyDescriptor(prop); | ||
}, | ||
defineProperty(target, property, descriptor) { | ||
defineProperty: function defineProperty(target, property, descriptor) { | ||
target.defineProperty(property, descriptor); | ||
return true; | ||
}, | ||
setPrototypeOf() { | ||
setPrototypeOf: function setPrototypeOf() { | ||
throw new Error("Don't even try this..."); | ||
@@ -42,29 +55,29 @@ } | ||
const arrayTraps = { | ||
get(target, prop) { | ||
var arrayTraps = { | ||
get: function get(target, prop) { | ||
if (prop === PROXY_STATE) return target[0]; | ||
return target[0].get(prop); | ||
}, | ||
has(target, prop) { | ||
has: function has(target, prop) { | ||
return prop in target[0].source; | ||
}, | ||
ownKeys(target) { | ||
ownKeys: function ownKeys(target) { | ||
return Reflect.ownKeys(target[0].source); | ||
}, | ||
set(target, prop, value) { | ||
set: function set(target, prop, value) { | ||
target[0].set(prop, value); | ||
return true; | ||
}, | ||
deleteProperty(target, prop) { | ||
deleteProperty: function deleteProperty(target, prop) { | ||
target[0].deleteProp(prop); | ||
return true; | ||
}, | ||
getOwnPropertyDescriptor(target, prop) { | ||
getOwnPropertyDescriptor: function getOwnPropertyDescriptor(target, prop) { | ||
return target[0].getOwnPropertyDescriptor(prop); | ||
}, | ||
defineProperty(target, property, descriptor) { | ||
defineProperty: function defineProperty(target, property, descriptor) { | ||
target[0].defineProperty(property, descriptor); | ||
return true; | ||
}, | ||
setPrototypeOf() { | ||
setPrototypeOf: function setPrototypeOf() { | ||
throw new Error("Don't even try this..."); | ||
@@ -87,7 +100,7 @@ } | ||
if (arguments.length === 1) { | ||
const producer = baseState; | ||
var _producer = baseState; | ||
// prettier-ignore | ||
if (typeof producer !== "function") throw new Error("if produce is called with 1 argument, the first argument should be a function"); | ||
if (typeof _producer !== "function") throw new Error("if produce is called with 1 argument, the first argument should be a function"); | ||
return function () { | ||
const args = arguments; | ||
var args = arguments; | ||
return produce(args[0], function (draft) { | ||
@@ -103,9 +116,11 @@ args[0] = draft; // blegh! | ||
if (arguments.length !== 2) throw new Error("produce expects 1 or 2 arguments, got " + arguments.length); | ||
if (!isProxyable(baseState)) throw new Error("the first argument to produce should be a plain object or array, got " + typeof baseState); | ||
if (!isProxyable(baseState)) throw new Error("the first argument to produce should be a plain object or array, got " + (typeof baseState === "undefined" ? "undefined" : _typeof(baseState))); | ||
if (typeof producer !== "function") throw new Error("the second argument to produce should be a function"); | ||
} | ||
const revocableProxies = []; | ||
var revocableProxies = []; | ||
class State { | ||
constructor(parent, base) { | ||
var State = function () { | ||
function State(parent, base) { | ||
_classCallCheck(this, State); | ||
this.modified = false; | ||
@@ -118,57 +133,70 @@ this.parent = parent; | ||
get source() { | ||
return this.modified === true ? this.copy : this.base; | ||
} | ||
get(prop) { | ||
if (this.modified) { | ||
const value = this.copy[prop]; | ||
if (!isProxy(value) && isProxyable(value)) return this.copy[prop] = createProxy(this, value); | ||
return value; | ||
} else { | ||
if (prop in this.proxies) return this.proxies[prop]; | ||
const value = this.base[prop]; | ||
if (!isProxy(value) && isProxyable(value)) return this.proxies[prop] = createProxy(this, value); | ||
return value; | ||
_createClass(State, [{ | ||
key: "get", | ||
value: function get(prop) { | ||
if (this.modified) { | ||
var value = this.copy[prop]; | ||
if (!isProxy(value) && isProxyable(value)) return this.copy[prop] = createProxy(this, value); | ||
return value; | ||
} else { | ||
if (prop in this.proxies) return this.proxies[prop]; | ||
var _value = this.base[prop]; | ||
if (!isProxy(_value) && isProxyable(_value)) return this.proxies[prop] = createProxy(this, _value); | ||
return _value; | ||
} | ||
} | ||
} | ||
set(prop, value) { | ||
if (!this.modified) { | ||
if (prop in this.base && this.base[prop] === value || prop in this.proxies && this.proxies[prop] === value) return; | ||
}, { | ||
key: "set", | ||
value: function set(prop, value) { | ||
if (!this.modified) { | ||
if (prop in this.base && this.base[prop] === value || prop in this.proxies && this.proxies[prop] === value) return; | ||
this.markChanged(); | ||
} | ||
this.copy[prop] = value; | ||
} | ||
}, { | ||
key: "deleteProp", | ||
value: function deleteProp(prop) { | ||
this.markChanged(); | ||
delete this.copy[prop]; | ||
} | ||
this.copy[prop] = value; | ||
} | ||
}, { | ||
key: "getOwnPropertyDescriptor", | ||
value: function getOwnPropertyDescriptor(prop) { | ||
var owner = this.modified ? this.copy : prop in this.proxies ? this.proxies : this.base; | ||
var descriptor = Reflect.getOwnPropertyDescriptor(owner, prop); | ||
if (descriptor && !(Array.isArray(owner) && prop === "length")) descriptor.configurable = true; | ||
return descriptor; | ||
} | ||
}, { | ||
key: "defineProperty", | ||
value: function defineProperty(property, descriptor) { | ||
throw new Error("Immer does currently not support defining properties on draft objects"); | ||
} | ||
}, { | ||
key: "markChanged", | ||
value: function markChanged() { | ||
if (!this.modified) { | ||
this.modified = true; | ||
this.copy = Array.isArray(this.base) ? this.base.slice() : Object.assign({}, this.base); // TODO: eliminate those isArray checks? | ||
Object.assign(this.copy, this.proxies); // yup that works for arrays as well | ||
if (this.parent) this.parent.markChanged(); | ||
} | ||
} | ||
}, { | ||
key: "source", | ||
get: function get() { | ||
return this.modified === true ? this.copy : this.base; | ||
} | ||
}]); | ||
deleteProp(prop) { | ||
this.markChanged(); | ||
delete this.copy[prop]; | ||
} | ||
return State; | ||
}(); | ||
getOwnPropertyDescriptor(prop) { | ||
const owner = this.modified ? this.copy : prop in this.proxies ? this.proxies : this.base; | ||
const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop); | ||
if (descriptor && !(Array.isArray(owner) && prop === "length")) descriptor.configurable = true; | ||
return descriptor; | ||
} | ||
// creates a proxy for plain objects / arrays | ||
defineProperty(property, descriptor) { | ||
throw new Error("Immer does currently not support defining properties on draft objects"); | ||
} | ||
markChanged() { | ||
if (!this.modified) { | ||
this.modified = true; | ||
this.copy = Array.isArray(this.base) ? this.base.slice() : Object.assign({}, this.base); // TODO: eliminate those isArray checks? | ||
Object.assign(this.copy, this.proxies); // yup that works for arrays as well | ||
if (this.parent) this.parent.markChanged(); | ||
} | ||
} | ||
} | ||
// creates a proxy for plain objects / arrays | ||
function createProxy(parentState, base) { | ||
const state = new State(parentState, base); | ||
let proxy; | ||
var state = new State(parentState, base); | ||
var proxy = void 0; | ||
if (Array.isArray(base)) { | ||
@@ -188,3 +216,3 @@ // Proxy should be created with an array to make it an array for JS | ||
if (isProxy(base)) { | ||
const state = base[PROXY_STATE]; | ||
var state = base[PROXY_STATE]; | ||
if (state.modified === true) { | ||
@@ -199,3 +227,3 @@ if (Array.isArray(state.base)) return finalizeArray(state); | ||
function finalizeObject(state) { | ||
const copy = state.copy; | ||
var copy = state.copy; | ||
Object.keys(copy).forEach(function (prop) { | ||
@@ -208,3 +236,3 @@ copy[prop] = finalize(copy[prop]); | ||
function finalizeArray(state) { | ||
const copy = state.copy; | ||
var copy = state.copy; | ||
copy.forEach(function (value, index) { | ||
@@ -217,9 +245,9 @@ copy[index] = finalize(copy[index]); | ||
// create proxy for root | ||
const rootClone = createProxy(undefined, baseState); | ||
var rootClone = createProxy(undefined, baseState); | ||
// execute the thunk | ||
const maybeVoidReturn = producer(rootClone); | ||
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} was returned`); | ||
!Object.is(maybeVoidReturn, undefined) && console.warn("Immer callback expects no return value. However " + (typeof maybeVoidReturn === "undefined" ? "undefined" : _typeof(maybeVoidReturn)) + " was returned"); | ||
// and finalize the modified proxy | ||
const res = finalize(rootClone); | ||
var res = finalize(rootClone); | ||
// revoke all proxies | ||
@@ -238,5 +266,5 @@ revocableProxies.forEach(function (p) { | ||
if (!value) return false; | ||
if (typeof value !== "object") return false; | ||
if ((typeof value === "undefined" ? "undefined" : _typeof(value)) !== "object") return false; | ||
if (Array.isArray(value)) return true; | ||
const proto = Object.getPrototypeOf(value); | ||
var proto = Object.getPrototypeOf(value); | ||
return proto === proto === null || Object.prototype; | ||
@@ -267,9 +295,3 @@ } | ||
} | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
module.exports.default = produce; | ||
module.exports.setAutoFreeze = setAutoFreeze; | ||
},{}]},{},[1])(1) | ||
}); |
{ | ||
"name": "immer", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Create your next immutable state by mutating the current one", | ||
"main": "dist/immer.js", | ||
"main": "dist/index.js", | ||
"types": "./index.d.ts", | ||
@@ -12,3 +12,3 @@ "scripts": { | ||
"build": "yarn build:babel && yarn build:umd", | ||
"build:babel": "cd src && babel -d ../dist immer.js && babel --no-babelrc --presets=es2015 -d ../dist es5.js", | ||
"build:babel": "cd src && babel immer.js -o ../proxy.js && babel --no-babelrc --presets=es2015 -d ../dist *.js", | ||
"build:umd": "browserify dist/immer.js -o dist/immer.umd.js -s immer && browserify dist/es5.js -o dist/es5.umd.js -s immer", | ||
@@ -38,2 +38,3 @@ "prettier": "prettier \"*/**/*.js\" --ignore-path ./.prettierignore --write", | ||
"es5.js", | ||
"proxy.js", | ||
"index.d.ts", | ||
@@ -40,0 +41,0 @@ "dist/" |
@@ -185,8 +185,12 @@ # Immer | ||
For example, Microsoft Internet Explorer or React Native on Android. | ||
In these cases, import the ES5 compatibile implementation first, which is a bit slower (see below) but semantically equivalent: | ||
In such cases Immer will fallback to an ES5 compatible implementation which works identical, but is a bit slower. | ||
However, if you want to slim down your bundle, you could only include the Proxy based implementation by importing from `immer/proxy`. | ||
```javascript | ||
import immer from "immer/es5" | ||
``` | ||
An overview of the available builds: | ||
Immer exposes its functionality in 3 different ways: | ||
* `import produce from "immer"`, the default, ships with both the Proxy and ES5 compatible implementation, and picks at runtime the best matching one. This is the most convenient, but adds the most to the bundle size (~2KB) | ||
* `import produce from "immer/proxy"`: This build is optimized for modern browser, which support Proxies and other modern language features, and doesn't polyfill things like `Symbol`. Use this if you are targetting a modern environment only | ||
* `import produce from "immer/es5"`: Only bundle the ES5 compatible implementation (which of course also works for modern browsers) | ||
## More reducer examples | ||
@@ -193,0 +197,0 @@ |
@@ -18,3 +18,3 @@ "use strict" | ||
*/ | ||
function produce(baseState, producer) { | ||
export default function produce(baseState, producer) { | ||
// curried invocation | ||
@@ -272,8 +272,4 @@ if (arguments.length === 1) { | ||
*/ | ||
function setAutoFreeze(enableAutoFreeze) { | ||
export function setAutoFreeze(enableAutoFreeze) { | ||
autoFreeze = enableAutoFreeze | ||
} | ||
createHiddenProperty(exports, "__esModule", true) | ||
module.exports.default = produce | ||
module.exports.setAutoFreeze = setAutoFreeze |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
84157
13
1535
295
0