source: frontend/node_modules/immer/dist/immer.umd.development.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 61.2 KB
Line 
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3 typeof define === 'function' && define.amd ? define(['exports'], factory) :
4 (global = global || self, factory(global.immer = {}));
5}(this, (function (exports) { 'use strict';
6
7 var _ref;
8
9 // Should be no imports here!
10 // Some things that should be evaluated before all else...
11 // We only want to know if non-polyfilled symbols are available
12 var hasSymbol = typeof Symbol !== "undefined" && typeof
13 /*#__PURE__*/
14 Symbol("x") === "symbol";
15 var hasMap = typeof Map !== "undefined";
16 var hasSet = typeof Set !== "undefined";
17 var hasProxies = typeof Proxy !== "undefined" && typeof Proxy.revocable !== "undefined" && typeof Reflect !== "undefined";
18 /**
19 * The sentinel value returned by producers to replace the draft with undefined.
20 */
21
22 var NOTHING = hasSymbol ?
23 /*#__PURE__*/
24 Symbol.for("immer-nothing") : (_ref = {}, _ref["immer-nothing"] = true, _ref);
25 /**
26 * To let Immer treat your class instances as plain immutable objects
27 * (albeit with a custom prototype), you must define either an instance property
28 * or a static property on each of your custom classes.
29 *
30 * Otherwise, your class instance will never be drafted, which means it won't be
31 * safe to mutate in a produce callback.
32 */
33
34 var DRAFTABLE = hasSymbol ?
35 /*#__PURE__*/
36 Symbol.for("immer-draftable") : "__$immer_draftable";
37 var DRAFT_STATE = hasSymbol ?
38 /*#__PURE__*/
39 Symbol.for("immer-state") : "__$immer_state"; // Even a polyfilled Symbol might provide Symbol.iterator
40
41 var iteratorSymbol = typeof Symbol != "undefined" && Symbol.iterator || "@@iterator";
42
43 var errors = {
44 0: "Illegal state",
45 1: "Immer drafts cannot have computed properties",
46 2: "This object has been frozen and should not be mutated",
47 3: function _(data) {
48 return "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " + data;
49 },
50 4: "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.",
51 5: "Immer forbids circular references",
52 6: "The first or second argument to `produce` must be a function",
53 7: "The third argument to `produce` must be a function or undefined",
54 8: "First argument to `createDraft` must be a plain object, an array, or an immerable object",
55 9: "First argument to `finishDraft` must be a draft returned by `createDraft`",
56 10: "The given draft is already finalized",
57 11: "Object.defineProperty() cannot be used on an Immer draft",
58 12: "Object.setPrototypeOf() cannot be used on an Immer draft",
59 13: "Immer only supports deleting array indices",
60 14: "Immer only supports setting array indices and the 'length' property",
61 15: function _(path) {
62 return "Cannot apply patch, path doesn't resolve: " + path;
63 },
64 16: 'Sets cannot have "replace" patches.',
65 17: function _(op) {
66 return "Unsupported patch operation: " + op;
67 },
68 18: function _(plugin) {
69 return "The plugin for '" + plugin + "' has not been loaded into Immer. To enable the plugin, import and call `enable" + plugin + "()` when initializing your application.";
70 },
71 20: "Cannot use proxies if Proxy, Proxy.revocable or Reflect are not available",
72 21: function _(thing) {
73 return "produce can only be called on things that are draftable: plain objects, arrays, Map, Set or classes that are marked with '[immerable]: true'. Got '" + thing + "'";
74 },
75 22: function _(thing) {
76 return "'current' expects a draft, got: " + thing;
77 },
78 23: function _(thing) {
79 return "'original' expects a draft, got: " + thing;
80 },
81 24: "Patching reserved attributes like __proto__, prototype and constructor is not allowed"
82 };
83 function die(error) {
84 for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
85 args[_key - 1] = arguments[_key];
86 }
87
88 {
89 var e = errors[error];
90 var msg = !e ? "unknown error nr: " + error : typeof e === "function" ? e.apply(null, args) : e;
91 throw new Error("[Immer] " + msg);
92 }
93 }
94
95 /** Returns true if the given value is an Immer draft */
96
97 /*#__PURE__*/
98
99 function isDraft(value) {
100 return !!value && !!value[DRAFT_STATE];
101 }
102 /** Returns true if the given value can be drafted by Immer */
103
104 /*#__PURE__*/
105
106 function isDraftable(value) {
107 var _value$constructor;
108
109 if (!value) return false;
110 return isPlainObject(value) || Array.isArray(value) || !!value[DRAFTABLE] || !!((_value$constructor = value.constructor) === null || _value$constructor === void 0 ? void 0 : _value$constructor[DRAFTABLE]) || isMap(value) || isSet(value);
111 }
112 var objectCtorString =
113 /*#__PURE__*/
114 Object.prototype.constructor.toString();
115 /*#__PURE__*/
116
117 function isPlainObject(value) {
118 if (!value || typeof value !== "object") return false;
119 var proto = Object.getPrototypeOf(value);
120
121 if (proto === null) {
122 return true;
123 }
124
125 var Ctor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor;
126 if (Ctor === Object) return true;
127 return typeof Ctor == "function" && Function.toString.call(Ctor) === objectCtorString;
128 }
129 function original(value) {
130 if (!isDraft(value)) die(23, value);
131 return value[DRAFT_STATE].base_;
132 }
133 /*#__PURE__*/
134
135 var ownKeys = typeof Reflect !== "undefined" && Reflect.ownKeys ? Reflect.ownKeys : typeof Object.getOwnPropertySymbols !== "undefined" ? function (obj) {
136 return Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols(obj));
137 } :
138 /* istanbul ignore next */
139 Object.getOwnPropertyNames;
140 var getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors || function getOwnPropertyDescriptors(target) {
141 // Polyfill needed for Hermes and IE, see https://github.com/facebook/hermes/issues/274
142 var res = {};
143 ownKeys(target).forEach(function (key) {
144 res[key] = Object.getOwnPropertyDescriptor(target, key);
145 });
146 return res;
147 };
148 function each(obj, iter, enumerableOnly) {
149 if (enumerableOnly === void 0) {
150 enumerableOnly = false;
151 }
152
153 if (getArchtype(obj) === 0
154 /* Object */
155 ) {
156 (enumerableOnly ? Object.keys : ownKeys)(obj).forEach(function (key) {
157 if (!enumerableOnly || typeof key !== "symbol") iter(key, obj[key], obj);
158 });
159 } else {
160 obj.forEach(function (entry, index) {
161 return iter(index, entry, obj);
162 });
163 }
164 }
165 /*#__PURE__*/
166
167 function getArchtype(thing) {
168 /* istanbul ignore next */
169 var state = thing[DRAFT_STATE];
170 return state ? state.type_ > 3 ? state.type_ - 4 // cause Object and Array map back from 4 and 5
171 : state.type_ // others are the same
172 : Array.isArray(thing) ? 1
173 /* Array */
174 : isMap(thing) ? 2
175 /* Map */
176 : isSet(thing) ? 3
177 /* Set */
178 : 0
179 /* Object */
180 ;
181 }
182 /*#__PURE__*/
183
184 function has(thing, prop) {
185 return getArchtype(thing) === 2
186 /* Map */
187 ? thing.has(prop) : Object.prototype.hasOwnProperty.call(thing, prop);
188 }
189 /*#__PURE__*/
190
191 function get(thing, prop) {
192 // @ts-ignore
193 return getArchtype(thing) === 2
194 /* Map */
195 ? thing.get(prop) : thing[prop];
196 }
197 /*#__PURE__*/
198
199 function set(thing, propOrOldValue, value) {
200 var t = getArchtype(thing);
201 if (t === 2
202 /* Map */
203 ) thing.set(propOrOldValue, value);else if (t === 3
204 /* Set */
205 ) {
206 thing.add(value);
207 } else thing[propOrOldValue] = value;
208 }
209 /*#__PURE__*/
210
211 function is(x, y) {
212 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
213 if (x === y) {
214 return x !== 0 || 1 / x === 1 / y;
215 } else {
216 return x !== x && y !== y;
217 }
218 }
219 /*#__PURE__*/
220
221 function isMap(target) {
222 return hasMap && target instanceof Map;
223 }
224 /*#__PURE__*/
225
226 function isSet(target) {
227 return hasSet && target instanceof Set;
228 }
229 /*#__PURE__*/
230
231 function latest(state) {
232 return state.copy_ || state.base_;
233 }
234 /*#__PURE__*/
235
236 function shallowCopy(base) {
237 if (Array.isArray(base)) return Array.prototype.slice.call(base);
238 var descriptors = getOwnPropertyDescriptors(base);
239 delete descriptors[DRAFT_STATE];
240 var keys = ownKeys(descriptors);
241
242 for (var i = 0; i < keys.length; i++) {
243 var key = keys[i];
244 var desc = descriptors[key];
245
246 if (desc.writable === false) {
247 desc.writable = true;
248 desc.configurable = true;
249 } // like object.assign, we will read any _own_, get/set accessors. This helps in dealing
250 // with libraries that trap values, like mobx or vue
251 // unlike object.assign, non-enumerables will be copied as well
252
253
254 if (desc.get || desc.set) descriptors[key] = {
255 configurable: true,
256 writable: true,
257 enumerable: desc.enumerable,
258 value: base[key]
259 };
260 }
261
262 return Object.create(Object.getPrototypeOf(base), descriptors);
263 }
264 function freeze(obj, deep) {
265 if (deep === void 0) {
266 deep = false;
267 }
268
269 if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return obj;
270
271 if (getArchtype(obj) > 1
272 /* Map or Set */
273 ) {
274 obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections;
275 }
276
277 Object.freeze(obj);
278 if (deep) each(obj, function (key, value) {
279 return freeze(value, true);
280 }, true);
281 return obj;
282 }
283
284 function dontMutateFrozenCollections() {
285 die(2);
286 }
287
288 function isFrozen(obj) {
289 if (obj == null || typeof obj !== "object") return true; // See #600, IE dies on non-objects in Object.isFrozen
290
291 return Object.isFrozen(obj);
292 }
293
294 /** Plugin utilities */
295
296 var plugins = {};
297 function getPlugin(pluginKey) {
298 var plugin = plugins[pluginKey];
299
300 if (!plugin) {
301 die(18, pluginKey);
302 } // @ts-ignore
303
304
305 return plugin;
306 }
307 function loadPlugin(pluginKey, implementation) {
308 if (!plugins[pluginKey]) plugins[pluginKey] = implementation;
309 }
310
311 var currentScope;
312 function getCurrentScope() {
313 if ( !currentScope) die(0);
314 return currentScope;
315 }
316
317 function createScope(parent_, immer_) {
318 return {
319 drafts_: [],
320 parent_: parent_,
321 immer_: immer_,
322 // Whenever the modified draft contains a draft from another scope, we
323 // need to prevent auto-freezing so the unowned draft can be finalized.
324 canAutoFreeze_: true,
325 unfinalizedDrafts_: 0
326 };
327 }
328
329 function usePatchesInScope(scope, patchListener) {
330 if (patchListener) {
331 getPlugin("Patches"); // assert we have the plugin
332
333 scope.patches_ = [];
334 scope.inversePatches_ = [];
335 scope.patchListener_ = patchListener;
336 }
337 }
338 function revokeScope(scope) {
339 leaveScope(scope);
340 scope.drafts_.forEach(revokeDraft); // @ts-ignore
341
342 scope.drafts_ = null;
343 }
344 function leaveScope(scope) {
345 if (scope === currentScope) {
346 currentScope = scope.parent_;
347 }
348 }
349 function enterScope(immer) {
350 return currentScope = createScope(currentScope, immer);
351 }
352
353 function revokeDraft(draft) {
354 var state = draft[DRAFT_STATE];
355 if (state.type_ === 0
356 /* ProxyObject */
357 || state.type_ === 1
358 /* ProxyArray */
359 ) state.revoke_();else state.revoked_ = true;
360 }
361
362 function processResult(result, scope) {
363 scope.unfinalizedDrafts_ = scope.drafts_.length;
364 var baseDraft = scope.drafts_[0];
365 var isReplaced = result !== undefined && result !== baseDraft;
366 if (!scope.immer_.useProxies_) getPlugin("ES5").willFinalizeES5_(scope, result, isReplaced);
367
368 if (isReplaced) {
369 if (baseDraft[DRAFT_STATE].modified_) {
370 revokeScope(scope);
371 die(4);
372 }
373
374 if (isDraftable(result)) {
375 // Finalize the result in case it contains (or is) a subset of the draft.
376 result = finalize(scope, result);
377 if (!scope.parent_) maybeFreeze(scope, result);
378 }
379
380 if (scope.patches_) {
381 getPlugin("Patches").generateReplacementPatches_(baseDraft[DRAFT_STATE].base_, result, scope.patches_, scope.inversePatches_);
382 }
383 } else {
384 // Finalize the base draft.
385 result = finalize(scope, baseDraft, []);
386 }
387
388 revokeScope(scope);
389
390 if (scope.patches_) {
391 scope.patchListener_(scope.patches_, scope.inversePatches_);
392 }
393
394 return result !== NOTHING ? result : undefined;
395 }
396
397 function finalize(rootScope, value, path) {
398 // Don't recurse in tho recursive data structures
399 if (isFrozen(value)) return value;
400 var state = value[DRAFT_STATE]; // A plain object, might need freezing, might contain drafts
401
402 if (!state) {
403 each(value, function (key, childValue) {
404 return finalizeProperty(rootScope, state, value, key, childValue, path);
405 }, true // See #590, don't recurse into non-enumerable of non drafted objects
406 );
407 return value;
408 } // Never finalize drafts owned by another scope.
409
410
411 if (state.scope_ !== rootScope) return value; // Unmodified draft, return the (frozen) original
412
413 if (!state.modified_) {
414 maybeFreeze(rootScope, state.base_, true);
415 return state.base_;
416 } // Not finalized yet, let's do that now
417
418
419 if (!state.finalized_) {
420 state.finalized_ = true;
421 state.scope_.unfinalizedDrafts_--;
422 var result = // For ES5, create a good copy from the draft first, with added keys and without deleted keys.
423 state.type_ === 4
424 /* ES5Object */
425 || state.type_ === 5
426 /* ES5Array */
427 ? state.copy_ = shallowCopy(state.draft_) : state.copy_; // Finalize all children of the copy
428 // For sets we clone before iterating, otherwise we can get in endless loop due to modifying during iteration, see #628
429 // To preserve insertion order in all cases we then clear the set
430 // And we let finalizeProperty know it needs to re-add non-draft children back to the target
431
432 var resultEach = result;
433 var isSet = false;
434
435 if (state.type_ === 3
436 /* Set */
437 ) {
438 resultEach = new Set(result);
439 result.clear();
440 isSet = true;
441 }
442
443 each(resultEach, function (key, childValue) {
444 return finalizeProperty(rootScope, state, result, key, childValue, path, isSet);
445 }); // everything inside is frozen, we can freeze here
446
447 maybeFreeze(rootScope, result, false); // first time finalizing, let's create those patches
448
449 if (path && rootScope.patches_) {
450 getPlugin("Patches").generatePatches_(state, path, rootScope.patches_, rootScope.inversePatches_);
451 }
452 }
453
454 return state.copy_;
455 }
456
457 function finalizeProperty(rootScope, parentState, targetObject, prop, childValue, rootPath, targetIsSet) {
458 if ( childValue === targetObject) die(5);
459
460 if (isDraft(childValue)) {
461 var path = rootPath && parentState && parentState.type_ !== 3
462 /* Set */
463 && // Set objects are atomic since they have no keys.
464 !has(parentState.assigned_, prop) // Skip deep patches for assigned keys.
465 ? rootPath.concat(prop) : undefined; // Drafts owned by `scope` are finalized here.
466
467 var res = finalize(rootScope, childValue, path);
468 set(targetObject, prop, res); // Drafts from another scope must prevented to be frozen
469 // if we got a draft back from finalize, we're in a nested produce and shouldn't freeze
470
471 if (isDraft(res)) {
472 rootScope.canAutoFreeze_ = false;
473 } else return;
474 } else if (targetIsSet) {
475 targetObject.add(childValue);
476 } // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.
477
478
479 if (isDraftable(childValue) && !isFrozen(childValue)) {
480 if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) {
481 // optimization: if an object is not a draft, and we don't have to
482 // deepfreeze everything, and we are sure that no drafts are left in the remaining object
483 // cause we saw and finalized all drafts already; we can stop visiting the rest of the tree.
484 // This benefits especially adding large data tree's without further processing.
485 // See add-data.js perf test
486 return;
487 }
488
489 finalize(rootScope, childValue); // immer deep freezes plain objects, so if there is no parent state, we freeze as well
490
491 if (!parentState || !parentState.scope_.parent_) maybeFreeze(rootScope, childValue);
492 }
493 }
494
495 function maybeFreeze(scope, value, deep) {
496 if (deep === void 0) {
497 deep = false;
498 }
499
500 // we never freeze for a non-root scope; as it would prevent pruning for drafts inside wrapping objects
501 if (!scope.parent_ && scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {
502 freeze(value, deep);
503 }
504 }
505
506 /**
507 * Returns a new draft of the `base` object.
508 *
509 * The second argument is the parent draft-state (used internally).
510 */
511
512 function createProxyProxy(base, parent) {
513 var isArray = Array.isArray(base);
514 var state = {
515 type_: isArray ? 1
516 /* ProxyArray */
517 : 0
518 /* ProxyObject */
519 ,
520 // Track which produce call this is associated with.
521 scope_: parent ? parent.scope_ : getCurrentScope(),
522 // True for both shallow and deep changes.
523 modified_: false,
524 // Used during finalization.
525 finalized_: false,
526 // Track which properties have been assigned (true) or deleted (false).
527 assigned_: {},
528 // The parent draft state.
529 parent_: parent,
530 // The base state.
531 base_: base,
532 // The base proxy.
533 draft_: null,
534 // The base copy with any updated values.
535 copy_: null,
536 // Called by the `produce` function.
537 revoke_: null,
538 isManual_: false
539 }; // the traps must target something, a bit like the 'real' base.
540 // but also, we need to be able to determine from the target what the relevant state is
541 // (to avoid creating traps per instance to capture the state in closure,
542 // and to avoid creating weird hidden properties as well)
543 // So the trick is to use 'state' as the actual 'target'! (and make sure we intercept everything)
544 // Note that in the case of an array, we put the state in an array to have better Reflect defaults ootb
545
546 var target = state;
547 var traps = objectTraps;
548
549 if (isArray) {
550 target = [state];
551 traps = arrayTraps;
552 }
553
554 var _Proxy$revocable = Proxy.revocable(target, traps),
555 revoke = _Proxy$revocable.revoke,
556 proxy = _Proxy$revocable.proxy;
557
558 state.draft_ = proxy;
559 state.revoke_ = revoke;
560 return proxy;
561 }
562 /**
563 * Object drafts
564 */
565
566 var objectTraps = {
567 get: function get(state, prop) {
568 if (prop === DRAFT_STATE) return state;
569 var source = latest(state);
570
571 if (!has(source, prop)) {
572 // non-existing or non-own property...
573 return readPropFromProto(state, source, prop);
574 }
575
576 var value = source[prop];
577
578 if (state.finalized_ || !isDraftable(value)) {
579 return value;
580 } // Check for existing draft in modified state.
581 // Assigned values are never drafted. This catches any drafts we created, too.
582
583
584 if (value === peek(state.base_, prop)) {
585 prepareCopy(state);
586 return state.copy_[prop] = createProxy(state.scope_.immer_, value, state);
587 }
588
589 return value;
590 },
591 has: function has(state, prop) {
592 return prop in latest(state);
593 },
594 ownKeys: function ownKeys(state) {
595 return Reflect.ownKeys(latest(state));
596 },
597 set: function set(state, prop
598 /* strictly not, but helps TS */
599 , value) {
600 var desc = getDescriptorFromProto(latest(state), prop);
601
602 if (desc === null || desc === void 0 ? void 0 : desc.set) {
603 // special case: if this write is captured by a setter, we have
604 // to trigger it with the correct context
605 desc.set.call(state.draft_, value);
606 return true;
607 }
608
609 if (!state.modified_) {
610 // the last check is because we need to be able to distinguish setting a non-existing to undefined (which is a change)
611 // from setting an existing property with value undefined to undefined (which is not a change)
612 var current = peek(latest(state), prop); // special case, if we assigning the original value to a draft, we can ignore the assignment
613
614 var currentState = current === null || current === void 0 ? void 0 : current[DRAFT_STATE];
615
616 if (currentState && currentState.base_ === value) {
617 state.copy_[prop] = value;
618 state.assigned_[prop] = false;
619 return true;
620 }
621
622 if (is(value, current) && (value !== undefined || has(state.base_, prop))) return true;
623 prepareCopy(state);
624 markChanged(state);
625 }
626
627 if (state.copy_[prop] === value && ( // special case: handle new props with value 'undefined'
628 value !== undefined || prop in state.copy_) || // special case: NaN
629 Number.isNaN(value) && Number.isNaN(state.copy_[prop])) return true; // @ts-ignore
630
631 state.copy_[prop] = value;
632 state.assigned_[prop] = true;
633 return true;
634 },
635 deleteProperty: function deleteProperty(state, prop) {
636 // The `undefined` check is a fast path for pre-existing keys.
637 if (peek(state.base_, prop) !== undefined || prop in state.base_) {
638 state.assigned_[prop] = false;
639 prepareCopy(state);
640 markChanged(state);
641 } else {
642 // if an originally not assigned property was deleted
643 delete state.assigned_[prop];
644 } // @ts-ignore
645
646
647 if (state.copy_) delete state.copy_[prop];
648 return true;
649 },
650 // Note: We never coerce `desc.value` into an Immer draft, because we can't make
651 // the same guarantee in ES5 mode.
652 getOwnPropertyDescriptor: function getOwnPropertyDescriptor(state, prop) {
653 var owner = latest(state);
654 var desc = Reflect.getOwnPropertyDescriptor(owner, prop);
655 if (!desc) return desc;
656 return {
657 writable: true,
658 configurable: state.type_ !== 1
659 /* ProxyArray */
660 || prop !== "length",
661 enumerable: desc.enumerable,
662 value: owner[prop]
663 };
664 },
665 defineProperty: function defineProperty() {
666 die(11);
667 },
668 getPrototypeOf: function getPrototypeOf(state) {
669 return Object.getPrototypeOf(state.base_);
670 },
671 setPrototypeOf: function setPrototypeOf() {
672 die(12);
673 }
674 };
675 /**
676 * Array drafts
677 */
678
679 var arrayTraps = {};
680 each(objectTraps, function (key, fn) {
681 // @ts-ignore
682 arrayTraps[key] = function () {
683 arguments[0] = arguments[0][0];
684 return fn.apply(this, arguments);
685 };
686 });
687
688 arrayTraps.deleteProperty = function (state, prop) {
689 if ( isNaN(parseInt(prop))) die(13); // @ts-ignore
690
691 return arrayTraps.set.call(this, state, prop, undefined);
692 };
693
694 arrayTraps.set = function (state, prop, value) {
695 if ( prop !== "length" && isNaN(parseInt(prop))) die(14);
696 return objectTraps.set.call(this, state[0], prop, value, state[0]);
697 }; // Access a property without creating an Immer draft.
698
699
700 function peek(draft, prop) {
701 var state = draft[DRAFT_STATE];
702 var source = state ? latest(state) : draft;
703 return source[prop];
704 }
705
706 function readPropFromProto(state, source, prop) {
707 var _desc$get;
708
709 var desc = getDescriptorFromProto(source, prop);
710 return desc ? "value" in desc ? desc.value : // This is a very special case, if the prop is a getter defined by the
711 // prototype, we should invoke it with the draft as context!
712 (_desc$get = desc.get) === null || _desc$get === void 0 ? void 0 : _desc$get.call(state.draft_) : undefined;
713 }
714
715 function getDescriptorFromProto(source, prop) {
716 // 'in' checks proto!
717 if (!(prop in source)) return undefined;
718 var proto = Object.getPrototypeOf(source);
719
720 while (proto) {
721 var desc = Object.getOwnPropertyDescriptor(proto, prop);
722 if (desc) return desc;
723 proto = Object.getPrototypeOf(proto);
724 }
725
726 return undefined;
727 }
728
729 function markChanged(state) {
730 if (!state.modified_) {
731 state.modified_ = true;
732
733 if (state.parent_) {
734 markChanged(state.parent_);
735 }
736 }
737 }
738 function prepareCopy(state) {
739 if (!state.copy_) {
740 state.copy_ = shallowCopy(state.base_);
741 }
742 }
743
744 var Immer =
745 /*#__PURE__*/
746 function () {
747 function Immer(config) {
748 var _this = this;
749
750 this.useProxies_ = hasProxies;
751 this.autoFreeze_ = true;
752 /**
753 * The `produce` function takes a value and a "recipe function" (whose
754 * return value often depends on the base state). The recipe function is
755 * free to mutate its first argument however it wants. All mutations are
756 * only ever applied to a __copy__ of the base state.
757 *
758 * Pass only a function to create a "curried producer" which relieves you
759 * from passing the recipe function every time.
760 *
761 * Only plain objects and arrays are made mutable. All other objects are
762 * considered uncopyable.
763 *
764 * Note: This function is __bound__ to its `Immer` instance.
765 *
766 * @param {any} base - the initial state
767 * @param {Function} recipe - function that receives a proxy of the base state as first argument and which can be freely modified
768 * @param {Function} patchListener - optional function that will be called with all the patches produced here
769 * @returns {any} a new state, or the initial state if nothing was modified
770 */
771
772 this.produce = function (base, recipe, patchListener) {
773 // curried invocation
774 if (typeof base === "function" && typeof recipe !== "function") {
775 var defaultBase = recipe;
776 recipe = base;
777 var self = _this;
778 return function curriedProduce(base) {
779 var _this2 = this;
780
781 if (base === void 0) {
782 base = defaultBase;
783 }
784
785 for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
786 args[_key - 1] = arguments[_key];
787 }
788
789 return self.produce(base, function (draft) {
790 var _recipe;
791
792 return (_recipe = recipe).call.apply(_recipe, [_this2, draft].concat(args));
793 }); // prettier-ignore
794 };
795 }
796
797 if (typeof recipe !== "function") die(6);
798 if (patchListener !== undefined && typeof patchListener !== "function") die(7);
799 var result; // Only plain objects, arrays, and "immerable classes" are drafted.
800
801 if (isDraftable(base)) {
802 var scope = enterScope(_this);
803 var proxy = createProxy(_this, base, undefined);
804 var hasError = true;
805
806 try {
807 result = recipe(proxy);
808 hasError = false;
809 } finally {
810 // finally instead of catch + rethrow better preserves original stack
811 if (hasError) revokeScope(scope);else leaveScope(scope);
812 }
813
814 if (typeof Promise !== "undefined" && result instanceof Promise) {
815 return result.then(function (result) {
816 usePatchesInScope(scope, patchListener);
817 return processResult(result, scope);
818 }, function (error) {
819 revokeScope(scope);
820 throw error;
821 });
822 }
823
824 usePatchesInScope(scope, patchListener);
825 return processResult(result, scope);
826 } else if (!base || typeof base !== "object") {
827 result = recipe(base);
828 if (result === undefined) result = base;
829 if (result === NOTHING) result = undefined;
830 if (_this.autoFreeze_) freeze(result, true);
831
832 if (patchListener) {
833 var p = [];
834 var ip = [];
835 getPlugin("Patches").generateReplacementPatches_(base, result, p, ip);
836 patchListener(p, ip);
837 }
838
839 return result;
840 } else die(21, base);
841 };
842
843 this.produceWithPatches = function (base, recipe) {
844 // curried invocation
845 if (typeof base === "function") {
846 return function (state) {
847 for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
848 args[_key2 - 1] = arguments[_key2];
849 }
850
851 return _this.produceWithPatches(state, function (draft) {
852 return base.apply(void 0, [draft].concat(args));
853 });
854 };
855 }
856
857 var patches, inversePatches;
858
859 var result = _this.produce(base, recipe, function (p, ip) {
860 patches = p;
861 inversePatches = ip;
862 });
863
864 if (typeof Promise !== "undefined" && result instanceof Promise) {
865 return result.then(function (nextState) {
866 return [nextState, patches, inversePatches];
867 });
868 }
869
870 return [result, patches, inversePatches];
871 };
872
873 if (typeof (config === null || config === void 0 ? void 0 : config.useProxies) === "boolean") this.setUseProxies(config.useProxies);
874 if (typeof (config === null || config === void 0 ? void 0 : config.autoFreeze) === "boolean") this.setAutoFreeze(config.autoFreeze);
875 }
876
877 var _proto = Immer.prototype;
878
879 _proto.createDraft = function createDraft(base) {
880 if (!isDraftable(base)) die(8);
881 if (isDraft(base)) base = current(base);
882 var scope = enterScope(this);
883 var proxy = createProxy(this, base, undefined);
884 proxy[DRAFT_STATE].isManual_ = true;
885 leaveScope(scope);
886 return proxy;
887 };
888
889 _proto.finishDraft = function finishDraft(draft, patchListener) {
890 var state = draft && draft[DRAFT_STATE];
891
892 {
893 if (!state || !state.isManual_) die(9);
894 if (state.finalized_) die(10);
895 }
896
897 var scope = state.scope_;
898 usePatchesInScope(scope, patchListener);
899 return processResult(undefined, scope);
900 }
901 /**
902 * Pass true to automatically freeze all copies created by Immer.
903 *
904 * By default, auto-freezing is enabled.
905 */
906 ;
907
908 _proto.setAutoFreeze = function setAutoFreeze(value) {
909 this.autoFreeze_ = value;
910 }
911 /**
912 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is
913 * always faster than using ES5 proxies.
914 *
915 * By default, feature detection is used, so calling this is rarely necessary.
916 */
917 ;
918
919 _proto.setUseProxies = function setUseProxies(value) {
920 if (value && !hasProxies) {
921 die(20);
922 }
923
924 this.useProxies_ = value;
925 };
926
927 _proto.applyPatches = function applyPatches(base, patches) {
928 // If a patch replaces the entire state, take that replacement as base
929 // before applying patches
930 var i;
931
932 for (i = patches.length - 1; i >= 0; i--) {
933 var patch = patches[i];
934
935 if (patch.path.length === 0 && patch.op === "replace") {
936 base = patch.value;
937 break;
938 }
939 } // If there was a patch that replaced the entire state, start from the
940 // patch after that.
941
942
943 if (i > -1) {
944 patches = patches.slice(i + 1);
945 }
946
947 var applyPatchesImpl = getPlugin("Patches").applyPatches_;
948
949 if (isDraft(base)) {
950 // N.B: never hits if some patch a replacement, patches are never drafts
951 return applyPatchesImpl(base, patches);
952 } // Otherwise, produce a copy of the base state.
953
954
955 return this.produce(base, function (draft) {
956 return applyPatchesImpl(draft, patches);
957 });
958 };
959
960 return Immer;
961 }();
962 function createProxy(immer, value, parent) {
963 // precondition: createProxy should be guarded by isDraftable, so we know we can safely draft
964 var draft = isMap(value) ? getPlugin("MapSet").proxyMap_(value, parent) : isSet(value) ? getPlugin("MapSet").proxySet_(value, parent) : immer.useProxies_ ? createProxyProxy(value, parent) : getPlugin("ES5").createES5Proxy_(value, parent);
965 var scope = parent ? parent.scope_ : getCurrentScope();
966 scope.drafts_.push(draft);
967 return draft;
968 }
969
970 function current(value) {
971 if (!isDraft(value)) die(22, value);
972 return currentImpl(value);
973 }
974
975 function currentImpl(value) {
976 if (!isDraftable(value)) return value;
977 var state = value[DRAFT_STATE];
978 var copy;
979 var archType = getArchtype(value);
980
981 if (state) {
982 if (!state.modified_ && (state.type_ < 4 || !getPlugin("ES5").hasChanges_(state))) return state.base_; // Optimization: avoid generating new drafts during copying
983
984 state.finalized_ = true;
985 copy = copyHelper(value, archType);
986 state.finalized_ = false;
987 } else {
988 copy = copyHelper(value, archType);
989 }
990
991 each(copy, function (key, childValue) {
992 if (state && get(state.base_, key) === childValue) return; // no need to copy or search in something that didn't change
993
994 set(copy, key, currentImpl(childValue));
995 }); // In the future, we might consider freezing here, based on the current settings
996
997 return archType === 3
998 /* Set */
999 ? new Set(copy) : copy;
1000 }
1001
1002 function copyHelper(value, archType) {
1003 // creates a shallow copy, even if it is a map or set
1004 switch (archType) {
1005 case 2
1006 /* Map */
1007 :
1008 return new Map(value);
1009
1010 case 3
1011 /* Set */
1012 :
1013 // Set will be cloned as array temporarily, so that we can replace individual items
1014 return Array.from(value);
1015 }
1016
1017 return shallowCopy(value);
1018 }
1019
1020 function enableES5() {
1021 function willFinalizeES5_(scope, result, isReplaced) {
1022 if (!isReplaced) {
1023 if (scope.patches_) {
1024 markChangesRecursively(scope.drafts_[0]);
1025 } // This is faster when we don't care about which attributes changed.
1026
1027
1028 markChangesSweep(scope.drafts_);
1029 } // When a child draft is returned, look for changes.
1030 else if (isDraft(result) && result[DRAFT_STATE].scope_ === scope) {
1031 markChangesSweep(scope.drafts_);
1032 }
1033 }
1034
1035 function createES5Draft(isArray, base) {
1036 if (isArray) {
1037 var draft = new Array(base.length);
1038
1039 for (var i = 0; i < base.length; i++) {
1040 Object.defineProperty(draft, "" + i, proxyProperty(i, true));
1041 }
1042
1043 return draft;
1044 } else {
1045 var _descriptors = getOwnPropertyDescriptors(base);
1046
1047 delete _descriptors[DRAFT_STATE];
1048 var keys = ownKeys(_descriptors);
1049
1050 for (var _i = 0; _i < keys.length; _i++) {
1051 var key = keys[_i];
1052 _descriptors[key] = proxyProperty(key, isArray || !!_descriptors[key].enumerable);
1053 }
1054
1055 return Object.create(Object.getPrototypeOf(base), _descriptors);
1056 }
1057 }
1058
1059 function createES5Proxy_(base, parent) {
1060 var isArray = Array.isArray(base);
1061 var draft = createES5Draft(isArray, base);
1062 var state = {
1063 type_: isArray ? 5
1064 /* ES5Array */
1065 : 4
1066 /* ES5Object */
1067 ,
1068 scope_: parent ? parent.scope_ : getCurrentScope(),
1069 modified_: false,
1070 finalized_: false,
1071 assigned_: {},
1072 parent_: parent,
1073 // base is the object we are drafting
1074 base_: base,
1075 // draft is the draft object itself, that traps all reads and reads from either the base (if unmodified) or copy (if modified)
1076 draft_: draft,
1077 copy_: null,
1078 revoked_: false,
1079 isManual_: false
1080 };
1081 Object.defineProperty(draft, DRAFT_STATE, {
1082 value: state,
1083 // enumerable: false <- the default
1084 writable: true
1085 });
1086 return draft;
1087 } // property descriptors are recycled to make sure we don't create a get and set closure per property,
1088 // but share them all instead
1089
1090
1091 var descriptors = {};
1092
1093 function proxyProperty(prop, enumerable) {
1094 var desc = descriptors[prop];
1095
1096 if (desc) {
1097 desc.enumerable = enumerable;
1098 } else {
1099 descriptors[prop] = desc = {
1100 configurable: true,
1101 enumerable: enumerable,
1102 get: function get() {
1103 var state = this[DRAFT_STATE];
1104 assertUnrevoked(state); // @ts-ignore
1105
1106 return objectTraps.get(state, prop);
1107 },
1108 set: function set(value) {
1109 var state = this[DRAFT_STATE];
1110 assertUnrevoked(state); // @ts-ignore
1111
1112 objectTraps.set(state, prop, value);
1113 }
1114 };
1115 }
1116
1117 return desc;
1118 } // This looks expensive, but only proxies are visited, and only objects without known changes are scanned.
1119
1120
1121 function markChangesSweep(drafts) {
1122 // The natural order of drafts in the `scope` array is based on when they
1123 // were accessed. By processing drafts in reverse natural order, we have a
1124 // better chance of processing leaf nodes first. When a leaf node is known to
1125 // have changed, we can avoid any traversal of its ancestor nodes.
1126 for (var i = drafts.length - 1; i >= 0; i--) {
1127 var state = drafts[i][DRAFT_STATE];
1128
1129 if (!state.modified_) {
1130 switch (state.type_) {
1131 case 5
1132 /* ES5Array */
1133 :
1134 if (hasArrayChanges(state)) markChanged(state);
1135 break;
1136
1137 case 4
1138 /* ES5Object */
1139 :
1140 if (hasObjectChanges(state)) markChanged(state);
1141 break;
1142 }
1143 }
1144 }
1145 }
1146
1147 function markChangesRecursively(object) {
1148 if (!object || typeof object !== "object") return;
1149 var state = object[DRAFT_STATE];
1150 if (!state) return;
1151 var base_ = state.base_,
1152 draft_ = state.draft_,
1153 assigned_ = state.assigned_,
1154 type_ = state.type_;
1155
1156 if (type_ === 4
1157 /* ES5Object */
1158 ) {
1159 // Look for added keys.
1160 // probably there is a faster way to detect changes, as sweep + recurse seems to do some
1161 // unnecessary work.
1162 // also: probably we can store the information we detect here, to speed up tree finalization!
1163 each(draft_, function (key) {
1164 if (key === DRAFT_STATE) return; // The `undefined` check is a fast path for pre-existing keys.
1165
1166 if (base_[key] === undefined && !has(base_, key)) {
1167 assigned_[key] = true;
1168 markChanged(state);
1169 } else if (!assigned_[key]) {
1170 // Only untouched properties trigger recursion.
1171 markChangesRecursively(draft_[key]);
1172 }
1173 }); // Look for removed keys.
1174
1175 each(base_, function (key) {
1176 // The `undefined` check is a fast path for pre-existing keys.
1177 if (draft_[key] === undefined && !has(draft_, key)) {
1178 assigned_[key] = false;
1179 markChanged(state);
1180 }
1181 });
1182 } else if (type_ === 5
1183 /* ES5Array */
1184 ) {
1185 if (hasArrayChanges(state)) {
1186 markChanged(state);
1187 assigned_.length = true;
1188 }
1189
1190 if (draft_.length < base_.length) {
1191 for (var i = draft_.length; i < base_.length; i++) {
1192 assigned_[i] = false;
1193 }
1194 } else {
1195 for (var _i2 = base_.length; _i2 < draft_.length; _i2++) {
1196 assigned_[_i2] = true;
1197 }
1198 } // Minimum count is enough, the other parts has been processed.
1199
1200
1201 var min = Math.min(draft_.length, base_.length);
1202
1203 for (var _i3 = 0; _i3 < min; _i3++) {
1204 // Only untouched indices trigger recursion.
1205 if (!draft_.hasOwnProperty(_i3)) {
1206 assigned_[_i3] = true;
1207 }
1208
1209 if (assigned_[_i3] === undefined) markChangesRecursively(draft_[_i3]);
1210 }
1211 }
1212 }
1213
1214 function hasObjectChanges(state) {
1215 var base_ = state.base_,
1216 draft_ = state.draft_; // Search for added keys and changed keys. Start at the back, because
1217 // non-numeric keys are ordered by time of definition on the object.
1218
1219 var keys = ownKeys(draft_);
1220
1221 for (var i = keys.length - 1; i >= 0; i--) {
1222 var key = keys[i];
1223 if (key === DRAFT_STATE) continue;
1224 var baseValue = base_[key]; // The `undefined` check is a fast path for pre-existing keys.
1225
1226 if (baseValue === undefined && !has(base_, key)) {
1227 return true;
1228 } // Once a base key is deleted, future changes go undetected, because its
1229 // descriptor is erased. This branch detects any missed changes.
1230 else {
1231 var value = draft_[key];
1232
1233 var _state = value && value[DRAFT_STATE];
1234
1235 if (_state ? _state.base_ !== baseValue : !is(value, baseValue)) {
1236 return true;
1237 }
1238 }
1239 } // At this point, no keys were added or changed.
1240 // Compare key count to determine if keys were deleted.
1241
1242
1243 var baseIsDraft = !!base_[DRAFT_STATE];
1244 return keys.length !== ownKeys(base_).length + (baseIsDraft ? 0 : 1); // + 1 to correct for DRAFT_STATE
1245 }
1246
1247 function hasArrayChanges(state) {
1248 var draft_ = state.draft_;
1249 if (draft_.length !== state.base_.length) return true; // See #116
1250 // If we first shorten the length, our array interceptors will be removed.
1251 // If after that new items are added, result in the same original length,
1252 // those last items will have no intercepting property.
1253 // So if there is no own descriptor on the last position, we know that items were removed and added
1254 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check
1255 // the last one
1256 // last descriptor can be not a trap, if the array was extended
1257
1258 var descriptor = Object.getOwnPropertyDescriptor(draft_, draft_.length - 1); // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)
1259
1260 if (descriptor && !descriptor.get) return true; // if we miss a property, it has been deleted, so array probobaly changed
1261
1262 for (var i = 0; i < draft_.length; i++) {
1263 if (!draft_.hasOwnProperty(i)) return true;
1264 } // For all other cases, we don't have to compare, as they would have been picked up by the index setters
1265
1266
1267 return false;
1268 }
1269
1270 function hasChanges_(state) {
1271 return state.type_ === 4
1272 /* ES5Object */
1273 ? hasObjectChanges(state) : hasArrayChanges(state);
1274 }
1275
1276 function assertUnrevoked(state
1277 /*ES5State | MapState | SetState*/
1278 ) {
1279 if (state.revoked_) die(3, JSON.stringify(latest(state)));
1280 }
1281
1282 loadPlugin("ES5", {
1283 createES5Proxy_: createES5Proxy_,
1284 willFinalizeES5_: willFinalizeES5_,
1285 hasChanges_: hasChanges_
1286 });
1287 }
1288
1289 function enablePatches() {
1290 var REPLACE = "replace";
1291 var ADD = "add";
1292 var REMOVE = "remove";
1293
1294 function generatePatches_(state, basePath, patches, inversePatches) {
1295 switch (state.type_) {
1296 case 0
1297 /* ProxyObject */
1298 :
1299 case 4
1300 /* ES5Object */
1301 :
1302 case 2
1303 /* Map */
1304 :
1305 return generatePatchesFromAssigned(state, basePath, patches, inversePatches);
1306
1307 case 5
1308 /* ES5Array */
1309 :
1310 case 1
1311 /* ProxyArray */
1312 :
1313 return generateArrayPatches(state, basePath, patches, inversePatches);
1314
1315 case 3
1316 /* Set */
1317 :
1318 return generateSetPatches(state, basePath, patches, inversePatches);
1319 }
1320 }
1321
1322 function generateArrayPatches(state, basePath, patches, inversePatches) {
1323 var base_ = state.base_,
1324 assigned_ = state.assigned_;
1325 var copy_ = state.copy_; // Reduce complexity by ensuring `base` is never longer.
1326
1327 if (copy_.length < base_.length) {
1328 var _ref = [copy_, base_];
1329 base_ = _ref[0];
1330 copy_ = _ref[1];
1331 var _ref2 = [inversePatches, patches];
1332 patches = _ref2[0];
1333 inversePatches = _ref2[1];
1334 } // Process replaced indices.
1335
1336
1337 for (var i = 0; i < base_.length; i++) {
1338 if (assigned_[i] && copy_[i] !== base_[i]) {
1339 var path = basePath.concat([i]);
1340 patches.push({
1341 op: REPLACE,
1342 path: path,
1343 // Need to maybe clone it, as it can in fact be the original value
1344 // due to the base/copy inversion at the start of this function
1345 value: clonePatchValueIfNeeded(copy_[i])
1346 });
1347 inversePatches.push({
1348 op: REPLACE,
1349 path: path,
1350 value: clonePatchValueIfNeeded(base_[i])
1351 });
1352 }
1353 } // Process added indices.
1354
1355
1356 for (var _i = base_.length; _i < copy_.length; _i++) {
1357 var _path = basePath.concat([_i]);
1358
1359 patches.push({
1360 op: ADD,
1361 path: _path,
1362 // Need to maybe clone it, as it can in fact be the original value
1363 // due to the base/copy inversion at the start of this function
1364 value: clonePatchValueIfNeeded(copy_[_i])
1365 });
1366 }
1367
1368 if (base_.length < copy_.length) {
1369 inversePatches.push({
1370 op: REPLACE,
1371 path: basePath.concat(["length"]),
1372 value: base_.length
1373 });
1374 }
1375 } // This is used for both Map objects and normal objects.
1376
1377
1378 function generatePatchesFromAssigned(state, basePath, patches, inversePatches) {
1379 var base_ = state.base_,
1380 copy_ = state.copy_;
1381 each(state.assigned_, function (key, assignedValue) {
1382 var origValue = get(base_, key);
1383 var value = get(copy_, key);
1384 var op = !assignedValue ? REMOVE : has(base_, key) ? REPLACE : ADD;
1385 if (origValue === value && op === REPLACE) return;
1386 var path = basePath.concat(key);
1387 patches.push(op === REMOVE ? {
1388 op: op,
1389 path: path
1390 } : {
1391 op: op,
1392 path: path,
1393 value: value
1394 });
1395 inversePatches.push(op === ADD ? {
1396 op: REMOVE,
1397 path: path
1398 } : op === REMOVE ? {
1399 op: ADD,
1400 path: path,
1401 value: clonePatchValueIfNeeded(origValue)
1402 } : {
1403 op: REPLACE,
1404 path: path,
1405 value: clonePatchValueIfNeeded(origValue)
1406 });
1407 });
1408 }
1409
1410 function generateSetPatches(state, basePath, patches, inversePatches) {
1411 var base_ = state.base_,
1412 copy_ = state.copy_;
1413 var i = 0;
1414 base_.forEach(function (value) {
1415 if (!copy_.has(value)) {
1416 var path = basePath.concat([i]);
1417 patches.push({
1418 op: REMOVE,
1419 path: path,
1420 value: value
1421 });
1422 inversePatches.unshift({
1423 op: ADD,
1424 path: path,
1425 value: value
1426 });
1427 }
1428
1429 i++;
1430 });
1431 i = 0;
1432 copy_.forEach(function (value) {
1433 if (!base_.has(value)) {
1434 var path = basePath.concat([i]);
1435 patches.push({
1436 op: ADD,
1437 path: path,
1438 value: value
1439 });
1440 inversePatches.unshift({
1441 op: REMOVE,
1442 path: path,
1443 value: value
1444 });
1445 }
1446
1447 i++;
1448 });
1449 }
1450
1451 function generateReplacementPatches_(baseValue, replacement, patches, inversePatches) {
1452 patches.push({
1453 op: REPLACE,
1454 path: [],
1455 value: replacement === NOTHING ? undefined : replacement
1456 });
1457 inversePatches.push({
1458 op: REPLACE,
1459 path: [],
1460 value: baseValue
1461 });
1462 }
1463
1464 function applyPatches_(draft, patches) {
1465 patches.forEach(function (patch) {
1466 var path = patch.path,
1467 op = patch.op;
1468 var base = draft;
1469
1470 for (var i = 0; i < path.length - 1; i++) {
1471 var parentType = getArchtype(base);
1472 var p = path[i];
1473
1474 if (typeof p !== "string" && typeof p !== "number") {
1475 p = "" + p;
1476 } // See #738, avoid prototype pollution
1477
1478
1479 if ((parentType === 0
1480 /* Object */
1481 || parentType === 1
1482 /* Array */
1483 ) && (p === "__proto__" || p === "constructor")) die(24);
1484 if (typeof base === "function" && p === "prototype") die(24);
1485 base = get(base, p);
1486 if (typeof base !== "object") die(15, path.join("/"));
1487 }
1488
1489 var type = getArchtype(base);
1490 var value = deepClonePatchValue(patch.value); // used to clone patch to ensure original patch is not modified, see #411
1491
1492 var key = path[path.length - 1];
1493
1494 switch (op) {
1495 case REPLACE:
1496 switch (type) {
1497 case 2
1498 /* Map */
1499 :
1500 return base.set(key, value);
1501
1502 /* istanbul ignore next */
1503
1504 case 3
1505 /* Set */
1506 :
1507 die(16);
1508
1509 default:
1510 // if value is an object, then it's assigned by reference
1511 // in the following add or remove ops, the value field inside the patch will also be modifyed
1512 // so we use value from the cloned patch
1513 // @ts-ignore
1514 return base[key] = value;
1515 }
1516
1517 case ADD:
1518 switch (type) {
1519 case 1
1520 /* Array */
1521 :
1522 return key === "-" ? base.push(value) : base.splice(key, 0, value);
1523
1524 case 2
1525 /* Map */
1526 :
1527 return base.set(key, value);
1528
1529 case 3
1530 /* Set */
1531 :
1532 return base.add(value);
1533
1534 default:
1535 return base[key] = value;
1536 }
1537
1538 case REMOVE:
1539 switch (type) {
1540 case 1
1541 /* Array */
1542 :
1543 return base.splice(key, 1);
1544
1545 case 2
1546 /* Map */
1547 :
1548 return base.delete(key);
1549
1550 case 3
1551 /* Set */
1552 :
1553 return base.delete(patch.value);
1554
1555 default:
1556 return delete base[key];
1557 }
1558
1559 default:
1560 die(17, op);
1561 }
1562 });
1563 return draft;
1564 }
1565
1566 function deepClonePatchValue(obj) {
1567 if (!isDraftable(obj)) return obj;
1568 if (Array.isArray(obj)) return obj.map(deepClonePatchValue);
1569 if (isMap(obj)) return new Map(Array.from(obj.entries()).map(function (_ref3) {
1570 var k = _ref3[0],
1571 v = _ref3[1];
1572 return [k, deepClonePatchValue(v)];
1573 }));
1574 if (isSet(obj)) return new Set(Array.from(obj).map(deepClonePatchValue));
1575 var cloned = Object.create(Object.getPrototypeOf(obj));
1576
1577 for (var key in obj) {
1578 cloned[key] = deepClonePatchValue(obj[key]);
1579 }
1580
1581 if (has(obj, DRAFTABLE)) cloned[DRAFTABLE] = obj[DRAFTABLE];
1582 return cloned;
1583 }
1584
1585 function clonePatchValueIfNeeded(obj) {
1586 if (isDraft(obj)) {
1587 return deepClonePatchValue(obj);
1588 } else return obj;
1589 }
1590
1591 loadPlugin("Patches", {
1592 applyPatches_: applyPatches_,
1593 generatePatches_: generatePatches_,
1594 generateReplacementPatches_: generateReplacementPatches_
1595 });
1596 }
1597
1598 // types only!
1599 function enableMapSet() {
1600 /* istanbul ignore next */
1601 var _extendStatics = function extendStatics(d, b) {
1602 _extendStatics = Object.setPrototypeOf || {
1603 __proto__: []
1604 } instanceof Array && function (d, b) {
1605 d.__proto__ = b;
1606 } || function (d, b) {
1607 for (var p in b) {
1608 if (b.hasOwnProperty(p)) d[p] = b[p];
1609 }
1610 };
1611
1612 return _extendStatics(d, b);
1613 }; // Ugly hack to resolve #502 and inherit built in Map / Set
1614
1615
1616 function __extends(d, b) {
1617 _extendStatics(d, b);
1618
1619 function __() {
1620 this.constructor = d;
1621 }
1622
1623 d.prototype = ( // @ts-ignore
1624 __.prototype = b.prototype, new __());
1625 }
1626
1627 var DraftMap = function (_super) {
1628 __extends(DraftMap, _super); // Create class manually, cause #502
1629
1630
1631 function DraftMap(target, parent) {
1632 this[DRAFT_STATE] = {
1633 type_: 2
1634 /* Map */
1635 ,
1636 parent_: parent,
1637 scope_: parent ? parent.scope_ : getCurrentScope(),
1638 modified_: false,
1639 finalized_: false,
1640 copy_: undefined,
1641 assigned_: undefined,
1642 base_: target,
1643 draft_: this,
1644 isManual_: false,
1645 revoked_: false
1646 };
1647 return this;
1648 }
1649
1650 var p = DraftMap.prototype;
1651 Object.defineProperty(p, "size", {
1652 get: function get() {
1653 return latest(this[DRAFT_STATE]).size;
1654 } // enumerable: false,
1655 // configurable: true
1656
1657 });
1658
1659 p.has = function (key) {
1660 return latest(this[DRAFT_STATE]).has(key);
1661 };
1662
1663 p.set = function (key, value) {
1664 var state = this[DRAFT_STATE];
1665 assertUnrevoked(state);
1666
1667 if (!latest(state).has(key) || latest(state).get(key) !== value) {
1668 prepareMapCopy(state);
1669 markChanged(state);
1670 state.assigned_.set(key, true);
1671 state.copy_.set(key, value);
1672 state.assigned_.set(key, true);
1673 }
1674
1675 return this;
1676 };
1677
1678 p.delete = function (key) {
1679 if (!this.has(key)) {
1680 return false;
1681 }
1682
1683 var state = this[DRAFT_STATE];
1684 assertUnrevoked(state);
1685 prepareMapCopy(state);
1686 markChanged(state);
1687
1688 if (state.base_.has(key)) {
1689 state.assigned_.set(key, false);
1690 } else {
1691 state.assigned_.delete(key);
1692 }
1693
1694 state.copy_.delete(key);
1695 return true;
1696 };
1697
1698 p.clear = function () {
1699 var state = this[DRAFT_STATE];
1700 assertUnrevoked(state);
1701
1702 if (latest(state).size) {
1703 prepareMapCopy(state);
1704 markChanged(state);
1705 state.assigned_ = new Map();
1706 each(state.base_, function (key) {
1707 state.assigned_.set(key, false);
1708 });
1709 state.copy_.clear();
1710 }
1711 };
1712
1713 p.forEach = function (cb, thisArg) {
1714 var _this = this;
1715
1716 var state = this[DRAFT_STATE];
1717 latest(state).forEach(function (_value, key, _map) {
1718 cb.call(thisArg, _this.get(key), key, _this);
1719 });
1720 };
1721
1722 p.get = function (key) {
1723 var state = this[DRAFT_STATE];
1724 assertUnrevoked(state);
1725 var value = latest(state).get(key);
1726
1727 if (state.finalized_ || !isDraftable(value)) {
1728 return value;
1729 }
1730
1731 if (value !== state.base_.get(key)) {
1732 return value; // either already drafted or reassigned
1733 } // despite what it looks, this creates a draft only once, see above condition
1734
1735
1736 var draft = createProxy(state.scope_.immer_, value, state);
1737 prepareMapCopy(state);
1738 state.copy_.set(key, draft);
1739 return draft;
1740 };
1741
1742 p.keys = function () {
1743 return latest(this[DRAFT_STATE]).keys();
1744 };
1745
1746 p.values = function () {
1747 var _this2 = this,
1748 _ref;
1749
1750 var iterator = this.keys();
1751 return _ref = {}, _ref[iteratorSymbol] = function () {
1752 return _this2.values();
1753 }, _ref.next = function next() {
1754 var r = iterator.next();
1755 /* istanbul ignore next */
1756
1757 if (r.done) return r;
1758
1759 var value = _this2.get(r.value);
1760
1761 return {
1762 done: false,
1763 value: value
1764 };
1765 }, _ref;
1766 };
1767
1768 p.entries = function () {
1769 var _this3 = this,
1770 _ref2;
1771
1772 var iterator = this.keys();
1773 return _ref2 = {}, _ref2[iteratorSymbol] = function () {
1774 return _this3.entries();
1775 }, _ref2.next = function next() {
1776 var r = iterator.next();
1777 /* istanbul ignore next */
1778
1779 if (r.done) return r;
1780
1781 var value = _this3.get(r.value);
1782
1783 return {
1784 done: false,
1785 value: [r.value, value]
1786 };
1787 }, _ref2;
1788 };
1789
1790 p[iteratorSymbol] = function () {
1791 return this.entries();
1792 };
1793
1794 return DraftMap;
1795 }(Map);
1796
1797 function proxyMap_(target, parent) {
1798 // @ts-ignore
1799 return new DraftMap(target, parent);
1800 }
1801
1802 function prepareMapCopy(state) {
1803 if (!state.copy_) {
1804 state.assigned_ = new Map();
1805 state.copy_ = new Map(state.base_);
1806 }
1807 }
1808
1809 var DraftSet = function (_super) {
1810 __extends(DraftSet, _super); // Create class manually, cause #502
1811
1812
1813 function DraftSet(target, parent) {
1814 this[DRAFT_STATE] = {
1815 type_: 3
1816 /* Set */
1817 ,
1818 parent_: parent,
1819 scope_: parent ? parent.scope_ : getCurrentScope(),
1820 modified_: false,
1821 finalized_: false,
1822 copy_: undefined,
1823 base_: target,
1824 draft_: this,
1825 drafts_: new Map(),
1826 revoked_: false,
1827 isManual_: false
1828 };
1829 return this;
1830 }
1831
1832 var p = DraftSet.prototype;
1833 Object.defineProperty(p, "size", {
1834 get: function get() {
1835 return latest(this[DRAFT_STATE]).size;
1836 } // enumerable: true,
1837
1838 });
1839
1840 p.has = function (value) {
1841 var state = this[DRAFT_STATE];
1842 assertUnrevoked(state); // bit of trickery here, to be able to recognize both the value, and the draft of its value
1843
1844 if (!state.copy_) {
1845 return state.base_.has(value);
1846 }
1847
1848 if (state.copy_.has(value)) return true;
1849 if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value))) return true;
1850 return false;
1851 };
1852
1853 p.add = function (value) {
1854 var state = this[DRAFT_STATE];
1855 assertUnrevoked(state);
1856
1857 if (!this.has(value)) {
1858 prepareSetCopy(state);
1859 markChanged(state);
1860 state.copy_.add(value);
1861 }
1862
1863 return this;
1864 };
1865
1866 p.delete = function (value) {
1867 if (!this.has(value)) {
1868 return false;
1869 }
1870
1871 var state = this[DRAFT_STATE];
1872 assertUnrevoked(state);
1873 prepareSetCopy(state);
1874 markChanged(state);
1875 return state.copy_.delete(value) || (state.drafts_.has(value) ? state.copy_.delete(state.drafts_.get(value)) :
1876 /* istanbul ignore next */
1877 false);
1878 };
1879
1880 p.clear = function () {
1881 var state = this[DRAFT_STATE];
1882 assertUnrevoked(state);
1883
1884 if (latest(state).size) {
1885 prepareSetCopy(state);
1886 markChanged(state);
1887 state.copy_.clear();
1888 }
1889 };
1890
1891 p.values = function () {
1892 var state = this[DRAFT_STATE];
1893 assertUnrevoked(state);
1894 prepareSetCopy(state);
1895 return state.copy_.values();
1896 };
1897
1898 p.entries = function entries() {
1899 var state = this[DRAFT_STATE];
1900 assertUnrevoked(state);
1901 prepareSetCopy(state);
1902 return state.copy_.entries();
1903 };
1904
1905 p.keys = function () {
1906 return this.values();
1907 };
1908
1909 p[iteratorSymbol] = function () {
1910 return this.values();
1911 };
1912
1913 p.forEach = function forEach(cb, thisArg) {
1914 var iterator = this.values();
1915 var result = iterator.next();
1916
1917 while (!result.done) {
1918 cb.call(thisArg, result.value, result.value, this);
1919 result = iterator.next();
1920 }
1921 };
1922
1923 return DraftSet;
1924 }(Set);
1925
1926 function proxySet_(target, parent) {
1927 // @ts-ignore
1928 return new DraftSet(target, parent);
1929 }
1930
1931 function prepareSetCopy(state) {
1932 if (!state.copy_) {
1933 // create drafts for all entries to preserve insertion order
1934 state.copy_ = new Set();
1935 state.base_.forEach(function (value) {
1936 if (isDraftable(value)) {
1937 var draft = createProxy(state.scope_.immer_, value, state);
1938 state.drafts_.set(value, draft);
1939 state.copy_.add(draft);
1940 } else {
1941 state.copy_.add(value);
1942 }
1943 });
1944 }
1945 }
1946
1947 function assertUnrevoked(state
1948 /*ES5State | MapState | SetState*/
1949 ) {
1950 if (state.revoked_) die(3, JSON.stringify(latest(state)));
1951 }
1952
1953 loadPlugin("MapSet", {
1954 proxyMap_: proxyMap_,
1955 proxySet_: proxySet_
1956 });
1957 }
1958
1959 function enableAllPlugins() {
1960 enableES5();
1961 enableMapSet();
1962 enablePatches();
1963 }
1964
1965 var immer =
1966 /*#__PURE__*/
1967 new Immer();
1968 /**
1969 * The `produce` function takes a value and a "recipe function" (whose
1970 * return value often depends on the base state). The recipe function is
1971 * free to mutate its first argument however it wants. All mutations are
1972 * only ever applied to a __copy__ of the base state.
1973 *
1974 * Pass only a function to create a "curried producer" which relieves you
1975 * from passing the recipe function every time.
1976 *
1977 * Only plain objects and arrays are made mutable. All other objects are
1978 * considered uncopyable.
1979 *
1980 * Note: This function is __bound__ to its `Immer` instance.
1981 *
1982 * @param {any} base - the initial state
1983 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified
1984 * @param {Function} patchListener - optional function that will be called with all the patches produced here
1985 * @returns {any} a new state, or the initial state if nothing was modified
1986 */
1987
1988 var produce = immer.produce;
1989 /**
1990 * Like `produce`, but `produceWithPatches` always returns a tuple
1991 * [nextState, patches, inversePatches] (instead of just the next state)
1992 */
1993
1994 var produceWithPatches =
1995 /*#__PURE__*/
1996 immer.produceWithPatches.bind(immer);
1997 /**
1998 * Pass true to automatically freeze all copies created by Immer.
1999 *
2000 * Always freeze by default, even in production mode
2001 */
2002
2003 var setAutoFreeze =
2004 /*#__PURE__*/
2005 immer.setAutoFreeze.bind(immer);
2006 /**
2007 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is
2008 * always faster than using ES5 proxies.
2009 *
2010 * By default, feature detection is used, so calling this is rarely necessary.
2011 */
2012
2013 var setUseProxies =
2014 /*#__PURE__*/
2015 immer.setUseProxies.bind(immer);
2016 /**
2017 * Apply an array of Immer patches to the first argument.
2018 *
2019 * This function is a producer, which means copy-on-write is in effect.
2020 */
2021
2022 var applyPatches =
2023 /*#__PURE__*/
2024 immer.applyPatches.bind(immer);
2025 /**
2026 * Create an Immer draft from the given base state, which may be a draft itself.
2027 * The draft can be modified until you finalize it with the `finishDraft` function.
2028 */
2029
2030 var createDraft =
2031 /*#__PURE__*/
2032 immer.createDraft.bind(immer);
2033 /**
2034 * Finalize an Immer draft from a `createDraft` call, returning the base state
2035 * (if no changes were made) or a modified copy. The draft must *not* be
2036 * mutated afterwards.
2037 *
2038 * Pass a function as the 2nd argument to generate Immer patches based on the
2039 * changes that were made.
2040 */
2041
2042 var finishDraft =
2043 /*#__PURE__*/
2044 immer.finishDraft.bind(immer);
2045 /**
2046 * This function is actually a no-op, but can be used to cast an immutable type
2047 * to an draft type and make TypeScript happy
2048 *
2049 * @param value
2050 */
2051
2052 function castDraft(value) {
2053 return value;
2054 }
2055 /**
2056 * This function is actually a no-op, but can be used to cast a mutable type
2057 * to an immutable type and make TypeScript happy
2058 * @param value
2059 */
2060
2061 function castImmutable(value) {
2062 return value;
2063 }
2064
2065 exports.Immer = Immer;
2066 exports.applyPatches = applyPatches;
2067 exports.castDraft = castDraft;
2068 exports.castImmutable = castImmutable;
2069 exports.createDraft = createDraft;
2070 exports.current = current;
2071 exports.default = produce;
2072 exports.enableAllPlugins = enableAllPlugins;
2073 exports.enableES5 = enableES5;
2074 exports.enableMapSet = enableMapSet;
2075 exports.enablePatches = enablePatches;
2076 exports.finishDraft = finishDraft;
2077 exports.freeze = freeze;
2078 exports.immerable = DRAFTABLE;
2079 exports.isDraft = isDraft;
2080 exports.isDraftable = isDraftable;
2081 exports.nothing = NOTHING;
2082 exports.original = original;
2083 exports.produce = produce;
2084 exports.produceWithPatches = produceWithPatches;
2085 exports.setAutoFreeze = setAutoFreeze;
2086 exports.setUseProxies = setUseProxies;
2087
2088 Object.defineProperty(exports, '__esModule', { value: true });
2089
2090})));
2091//# sourceMappingURL=immer.umd.development.js.map
Note: See TracBrowser for help on using the repository browser.