@vve/immer 9.0.0-alpha.3 → 9.0.0-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,999 +0,0 @@
1
- var obj;
2
- var NOTHING = typeof Symbol !== "undefined" ? Symbol("immer-nothing") : ( obj = {}, obj["immer-nothing"] = true, obj );
3
- var DRAFTABLE = typeof Symbol !== "undefined" && Symbol.for ? Symbol.for("immer-draftable") : "__$immer_draftable";
4
- var DRAFT_STATE = typeof Symbol !== "undefined" && Symbol.for ? Symbol.for("immer-state") : "__$immer_state";
5
- function isDraft(value) {
6
- return !!value && !!value[DRAFT_STATE];
7
- }
8
- function isDraftable(value) {
9
- if (!value || typeof value !== "object") { return false; }
10
- if (Array.isArray(value)) { return true; }
11
- var proto = Object.getPrototypeOf(value);
12
- if (!proto || proto === Object.prototype) { return true; }
13
- return !!value[DRAFTABLE] || !!value.constructor[DRAFTABLE];
14
- }
15
- function original(value) {
16
- if (value && value[DRAFT_STATE]) {
17
- return value[DRAFT_STATE].base;
18
- }
19
- // otherwise return undefined
20
- }
21
-
22
- var assign = Object.assign || function assign(target, value) {
23
- for (var key in value) {
24
- if (has(value, key)) {
25
- target[key] = value[key];
26
- }
27
- }
28
- return target;
29
- };
30
- var ownKeys = typeof Reflect !== "undefined" && Reflect.ownKeys ? Reflect.ownKeys : typeof Object.getOwnPropertySymbols !== "undefined" ? function (obj) { return Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols(obj)); } : Object.getOwnPropertyNames;
31
- function shallowCopy(base, invokeGetters) {
32
- if ( invokeGetters === void 0 ) invokeGetters = false;
33
-
34
- if (Array.isArray(base)) { return base.slice(); }
35
- var clone = Object.create(Object.getPrototypeOf(base));
36
- ownKeys(base).forEach(function (key) {
37
- if (key === DRAFT_STATE) {
38
- return; // Never copy over draft state.
39
- }
40
-
41
- var desc = Object.getOwnPropertyDescriptor(base, key);
42
- var value = desc.value;
43
- if (desc.get) {
44
- if (!invokeGetters) {
45
- throw new Error("Immer drafts cannot have computed properties");
46
- }
47
- value = desc.get.call(base);
48
- }
49
- if (desc.enumerable) {
50
- clone[key] = value;
51
- } else {
52
- Object.defineProperty(clone, key, {
53
- value: value,
54
- writable: true,
55
- configurable: true
56
- });
57
- }
58
- });
59
- return clone;
60
- }
61
- function each(value, cb) {
62
- if (Array.isArray(value)) {
63
- for (var i = 0; i < value.length; i++) { cb(i, value[i], value); }
64
- } else {
65
- ownKeys(value).forEach(function (key) { return cb(key, value[key], value); });
66
- }
67
- }
68
- function isEnumerable(base, prop) {
69
- var desc = Object.getOwnPropertyDescriptor(base, prop);
70
- return !!desc && desc.enumerable;
71
- }
72
- function has(thing, prop) {
73
- return Object.prototype.hasOwnProperty.call(thing, prop);
74
- }
75
- function is(x, y) {
76
- // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
77
- if (x === y) {
78
- return x !== 0 || 1 / x === 1 / y;
79
- } else {
80
- return x !== x && y !== y;
81
- }
82
- }
83
-
84
- /** Each scope represents a `produce` call. */
85
- var ImmerScope = function ImmerScope(parent) {
86
- this.drafts = [];
87
- this.parent = parent;
88
-
89
- // Whenever the modified draft contains a draft from another scope, we
90
- // need to prevent auto-freezing so the unowned draft can be finalized.
91
- this.canAutoFreeze = true;
92
-
93
- // To avoid prototype lookups:
94
- this.patches = null;
95
- };
96
- ImmerScope.prototype.usePatches = function usePatches (patchListener) {
97
- if (patchListener) {
98
- this.patches = [];
99
- this.inversePatches = [];
100
- this.patchListener = patchListener;
101
- }
102
- };
103
- ImmerScope.prototype.revoke = function revoke$1$1 (keepAlive) {
104
- this.leave();
105
- keepAlive || this.drafts.forEach(revoke$1);
106
- this.drafts = null; // Make draft-related methods throw.
107
- };
108
-
109
- ImmerScope.prototype.leave = function leave () {
110
- if (this === ImmerScope.current) {
111
- ImmerScope.current = this.parent;
112
- }
113
- };
114
- ImmerScope.current = null;
115
- ImmerScope.enter = function () {
116
- return this.current = new ImmerScope(this.current);
117
- };
118
- function revoke$1(draft) {
119
- draft[DRAFT_STATE].revoke();
120
- }
121
-
122
- // property descriptors are recycled to make sure we don't create a get and set closure per property,
123
- // but share them all instead
124
- var descriptors = {};
125
- function willFinalize$1(scope, result, isReplaced) {
126
- scope.drafts.forEach(function (draft) {
127
- draft[DRAFT_STATE].finalizing = true;
128
- });
129
- if (!isReplaced) {
130
- if (scope.patches) {
131
- markChangesRecursively(scope.drafts[0]);
132
- }
133
- // This is faster when we don't care about which attributes changed.
134
- markChangesSweep(scope.drafts);
135
- }
136
- // When a child draft is returned, look for changes.
137
- else if (isDraft(result) && result[DRAFT_STATE].scope === scope) {
138
- markChangesSweep(scope.drafts);
139
- }
140
- }
141
- function createProxy$1(base, parent) {
142
- var isArray = Array.isArray(base);
143
- var draft = clonePotentialDraft(base);
144
- each(draft, function (prop) {
145
- proxyProperty(draft, prop, isArray || isEnumerable(base, prop));
146
- });
147
-
148
- // See "proxy.js" for property documentation.
149
- var scope = parent ? parent.scope : ImmerScope.current;
150
- var state = {
151
- scope: scope,
152
- modified: false,
153
- finalizing: false,
154
- // es5 only
155
- finalized: false,
156
- assigned: {},
157
- parent: parent,
158
- base: base,
159
- draft: draft,
160
- copy: null,
161
- revoke: revoke,
162
- revoked: false // es5 only
163
- };
164
-
165
- createHiddenProperty(draft, DRAFT_STATE, state);
166
- scope.drafts.push(draft);
167
- return draft;
168
- }
169
- function revoke() {
170
- this.revoked = true;
171
- }
172
- function source$1(state) {
173
- return state.copy || state.base;
174
- }
175
-
176
- // Access a property without creating an Immer draft.
177
- function peek$1(draft, prop) {
178
- var state = draft[DRAFT_STATE];
179
- if (state && !state.finalizing) {
180
- state.finalizing = true;
181
- var value = draft[prop];
182
- state.finalizing = false;
183
- return value;
184
- }
185
- return draft[prop];
186
- }
187
- function get$1(state, prop) {
188
- assertUnrevoked(state);
189
- var value = peek$1(source$1(state), prop);
190
- if (state.finalizing) { return value; }
191
- // Create a draft if the value is unmodified.
192
- if (value === peek$1(state.base, prop) && isDraftable(value)) {
193
- prepareCopy(state);
194
- return state.copy[prop] = createProxy$1(value, state);
195
- }
196
- return value;
197
- }
198
- function set$1(state, prop, value) {
199
- assertUnrevoked(state);
200
- state.assigned[prop] = true;
201
- if (!state.modified) {
202
- if (is(value, peek$1(source$1(state), prop))) { return; }
203
- markChanged$1(state);
204
- prepareCopy(state);
205
- }
206
- state.copy[prop] = value;
207
- }
208
- function markChanged$1(state) {
209
- if (!state.modified) {
210
- state.modified = true;
211
- if (state.parent) { markChanged$1(state.parent); }
212
- }
213
- }
214
- function prepareCopy(state) {
215
- if (!state.copy) { state.copy = clonePotentialDraft(state.base); }
216
- }
217
- function clonePotentialDraft(base) {
218
- var state = base && base[DRAFT_STATE];
219
- if (state) {
220
- state.finalizing = true;
221
- var draft = shallowCopy(state.draft, true);
222
- state.finalizing = false;
223
- return draft;
224
- }
225
- return shallowCopy(base);
226
- }
227
- function proxyProperty(draft, prop, enumerable) {
228
- var desc = descriptors[prop];
229
- if (desc) {
230
- desc.enumerable = enumerable;
231
- } else {
232
- descriptors[prop] = desc = {
233
- configurable: true,
234
- enumerable: enumerable,
235
- get: function get$1$1() {
236
- return get$1(this[DRAFT_STATE], prop);
237
- },
238
- set: function set$1$1(value) {
239
- set$1(this[DRAFT_STATE], prop, value);
240
- }
241
- };
242
- }
243
- Object.defineProperty(draft, prop, desc);
244
- }
245
- function assertUnrevoked(state) {
246
- if (state.revoked === true) { throw new Error("Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " + JSON.stringify(source$1(state))); }
247
- }
248
-
249
- // This looks expensive, but only proxies are visited, and only objects without known changes are scanned.
250
- function markChangesSweep(drafts) {
251
- // The natural order of drafts in the `scope` array is based on when they
252
- // were accessed. By processing drafts in reverse natural order, we have a
253
- // better chance of processing leaf nodes first. When a leaf node is known to
254
- // have changed, we can avoid any traversal of its ancestor nodes.
255
- for (var i = drafts.length - 1; i >= 0; i--) {
256
- var state = drafts[i][DRAFT_STATE];
257
- if (!state.modified) {
258
- if (Array.isArray(state.base)) {
259
- if (hasArrayChanges(state)) { markChanged$1(state); }
260
- } else if (hasObjectChanges(state)) { markChanged$1(state); }
261
- }
262
- }
263
- }
264
- function markChangesRecursively(object) {
265
- if (!object || typeof object !== "object") { return; }
266
- var state = object[DRAFT_STATE];
267
- if (!state) { return; }
268
- var base = state.base;
269
- var draft = state.draft;
270
- var assigned = state.assigned;
271
- if (!Array.isArray(object)) {
272
- // Look for added keys.
273
- Object.keys(draft).forEach(function (key) {
274
- // The `undefined` check is a fast path for pre-existing keys.
275
- if (base[key] === undefined && !has(base, key)) {
276
- assigned[key] = true;
277
- markChanged$1(state);
278
- } else if (!assigned[key]) {
279
- // Only untouched properties trigger recursion.
280
- markChangesRecursively(draft[key]);
281
- }
282
- });
283
- // Look for removed keys.
284
- Object.keys(base).forEach(function (key) {
285
- // The `undefined` check is a fast path for pre-existing keys.
286
- if (draft[key] === undefined && !has(draft, key)) {
287
- assigned[key] = false;
288
- markChanged$1(state);
289
- }
290
- });
291
- } else if (hasArrayChanges(state)) {
292
- markChanged$1(state);
293
- assigned.length = true;
294
- if (draft.length < base.length) {
295
- for (var i = draft.length; i < base.length; i++) { assigned[i] = false; }
296
- } else {
297
- for (var i$1 = base.length; i$1 < draft.length; i$1++) { assigned[i$1] = true; }
298
- }
299
- for (var i$2 = 0; i$2 < draft.length; i$2++) {
300
- // Only untouched indices trigger recursion.
301
- if (assigned[i$2] === undefined) { markChangesRecursively(draft[i$2]); }
302
- }
303
- }
304
- }
305
- function hasObjectChanges(state) {
306
- var base = state.base;
307
- var draft = state.draft;
308
-
309
- // Search for added keys and changed keys. Start at the back, because
310
- // non-numeric keys are ordered by time of definition on the object.
311
- var keys = Object.keys(draft);
312
- for (var i = keys.length - 1; i >= 0; i--) {
313
- var key = keys[i];
314
- var baseValue = base[key];
315
- // The `undefined` check is a fast path for pre-existing keys.
316
- if (baseValue === undefined && !has(base, key)) {
317
- return true;
318
- }
319
- // Once a base key is deleted, future changes go undetected, because its
320
- // descriptor is erased. This branch detects any missed changes.
321
- else {
322
- var value = draft[key];
323
- var state$1 = value && value[DRAFT_STATE];
324
- if (state$1 ? state$1.base !== baseValue : !is(value, baseValue)) {
325
- return true;
326
- }
327
- }
328
- }
329
-
330
- // At this point, no keys were added or changed.
331
- // Compare key count to determine if keys were deleted.
332
- return keys.length !== Object.keys(base).length;
333
- }
334
- function hasArrayChanges(state) {
335
- var draft = state.draft;
336
- if (draft.length !== state.base.length) { return true; }
337
- // See #116
338
- // If we first shorten the length, our array interceptors will be removed.
339
- // If after that new items are added, result in the same original length,
340
- // those last items will have no intercepting property.
341
- // So if there is no own descriptor on the last position, we know that items were removed and added
342
- // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check
343
- // the last one
344
- var descriptor = Object.getOwnPropertyDescriptor(draft, draft.length - 1);
345
- // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)
346
- if (descriptor && !descriptor.get) { return true; }
347
- // For all other cases, we don't have to compare, as they would have been picked up by the index setters
348
- return false;
349
- }
350
- function createHiddenProperty(target, prop, value) {
351
- Object.defineProperty(target, prop, {
352
- value: value,
353
- enumerable: false,
354
- writable: true
355
- });
356
- }
357
-
358
- var legacyProxy = /*#__PURE__*/Object.freeze({
359
- __proto__: null,
360
- willFinalize: willFinalize$1,
361
- createProxy: createProxy$1
362
- });
363
-
364
- // Do nothing before being finalized.
365
- function willFinalize() {}
366
- function createProxy(base, parent) {
367
- var scope = parent ? parent.scope : ImmerScope.current;
368
- var state = {
369
- // Track which produce call this is associated with.
370
- scope: scope,
371
- // True for both shallow and deep changes.
372
- modified: false,
373
- // Used during finalization.
374
- finalized: false,
375
- // Track which properties have been assigned (true) or deleted (false).
376
- assigned: {},
377
- // The parent draft state.
378
- parent: parent,
379
- // The base state.
380
- base: base,
381
- // The base proxy.
382
- draft: null,
383
- // Any property proxies.
384
- drafts: {},
385
- // The base copy with any updated values.
386
- copy: null,
387
- // Called by the `produce` function.
388
- revoke: null
389
- };
390
- var ref = Array.isArray(base) ?
391
- // [state] is used for arrays, to make sure the proxy is array-ish and not violate invariants,
392
- // although state itself is an object
393
- Proxy.revocable([state], arrayTraps) : Proxy.revocable(state, objectTraps);
394
- var revoke = ref.revoke;
395
- var proxy = ref.proxy;
396
- state.draft = proxy;
397
- state.revoke = revoke;
398
- scope.drafts.push(proxy);
399
- return proxy;
400
- }
401
- var objectTraps = {
402
- get: get,
403
- has: function has(target, prop) {
404
- return prop in source(target);
405
- },
406
- ownKeys: function ownKeys(target) {
407
- return Reflect.ownKeys(source(target));
408
- },
409
- set: set,
410
- deleteProperty: deleteProperty,
411
- getOwnPropertyDescriptor: getOwnPropertyDescriptor,
412
- defineProperty: function defineProperty() {
413
- throw new Error("Object.defineProperty() cannot be used on an Immer draft"); // prettier-ignore
414
- },
415
-
416
- getPrototypeOf: function getPrototypeOf(target) {
417
- return Object.getPrototypeOf(target.base);
418
- },
419
- setPrototypeOf: function setPrototypeOf() {
420
- throw new Error("Object.setPrototypeOf() cannot be used on an Immer draft"); // prettier-ignore
421
- }
422
- };
423
-
424
- var arrayTraps = {};
425
- each(objectTraps, function (key, fn) {
426
- arrayTraps[key] = function () {
427
- arguments[0] = arguments[0][0];
428
- return fn.apply(this, arguments);
429
- };
430
- });
431
- arrayTraps.deleteProperty = function (state, prop) {
432
- if (isNaN(parseInt(prop))) {
433
- throw new Error("Immer only supports deleting array indices"); // prettier-ignore
434
- }
435
-
436
- return objectTraps.deleteProperty.call(this, state[0], prop);
437
- };
438
- arrayTraps.set = function (state, prop, value) {
439
- if (prop !== "length" && isNaN(parseInt(prop))) {
440
- throw new Error("Immer only supports setting array indices and the 'length' property"); // prettier-ignore
441
- }
442
-
443
- return objectTraps.set.call(this, state[0], prop, value);
444
- };
445
-
446
- // returns the object we should be reading the current value from, which is base, until some change has been made
447
- function source(state) {
448
- return state.copy || state.base;
449
- }
450
-
451
- // Access a property without creating an Immer draft.
452
- function peek(draft, prop) {
453
- var state = draft[DRAFT_STATE];
454
- var desc = Reflect.getOwnPropertyDescriptor(state ? source(state) : draft, prop);
455
- return desc && desc.value;
456
- }
457
- function get(state, prop) {
458
- if (prop === DRAFT_STATE) { return state; }
459
- var drafts = state.drafts;
460
-
461
- // Check for existing draft in unmodified state.
462
- if (!state.modified && has(drafts, prop)) {
463
- return drafts[prop];
464
- }
465
- var value = source(state)[prop];
466
- if (state.finalized || !isDraftable(value)) {
467
- return value;
468
- }
469
-
470
- // Check for existing draft in modified state.
471
- if (state.modified) {
472
- // Assigned values are never drafted. This catches any drafts we created, too.
473
- if (value !== peek(state.base, prop)) { return value; }
474
- // Store drafts on the copy (when one exists).
475
- drafts = state.copy;
476
- }
477
- return drafts[prop] = createProxy(value, state);
478
- }
479
- function set(state, prop, value) {
480
- if (!state.modified) {
481
- var baseValue = peek(state.base, prop);
482
- // Optimize based on value's truthiness. Truthy values are guaranteed to
483
- // never be undefined, so we can avoid the `in` operator. Lastly, truthy
484
- // values may be drafts, but falsy values are never drafts.
485
- var isUnchanged = value ? is(baseValue, value) || value === state.drafts[prop] : is(baseValue, value) && prop in state.base;
486
- if (isUnchanged) { return true; }
487
- markChanged(state);
488
- }
489
- state.assigned[prop] = true;
490
- state.copy[prop] = value;
491
- return true;
492
- }
493
- function deleteProperty(state, prop) {
494
- // The `undefined` check is a fast path for pre-existing keys.
495
- if (peek(state.base, prop) !== undefined || prop in state.base) {
496
- state.assigned[prop] = false;
497
- markChanged(state);
498
- }
499
- if (state.copy) { delete state.copy[prop]; }
500
- return true;
501
- }
502
-
503
- // Note: We never coerce `desc.value` into an Immer draft, because we can't make
504
- // the same guarantee in ES5 mode.
505
- function getOwnPropertyDescriptor(state, prop) {
506
- var owner = source(state);
507
- var desc = Reflect.getOwnPropertyDescriptor(owner, prop);
508
- if (desc) {
509
- desc.writable = true;
510
- desc.configurable = !Array.isArray(owner) || prop !== "length";
511
- }
512
- return desc;
513
- }
514
- function markChanged(state) {
515
- if (!state.modified) {
516
- state.modified = true;
517
- state.copy = assign(shallowCopy(state.base), state.drafts);
518
- state.drafts = null;
519
- if (state.parent) { markChanged(state.parent); }
520
- }
521
- }
522
-
523
- var modernProxy = /*#__PURE__*/Object.freeze({
524
- __proto__: null,
525
- willFinalize: willFinalize,
526
- createProxy: createProxy
527
- });
528
-
529
- function generatePatches(state, basePath, patches, inversePatches) {
530
- Array.isArray(state.base) ? generateArrayPatches(state, basePath, patches, inversePatches) : generateObjectPatches(state, basePath, patches, inversePatches);
531
- }
532
- function generateArrayPatches(state, basePath, patches, inversePatches) {
533
- var assign, assign$1;
534
-
535
- var base = state.base;
536
- var copy = state.copy;
537
- var assigned = state.assigned;
538
-
539
- // Reduce complexity by ensuring `base` is never longer.
540
- if (copy.length < base.length) {
541
- (assign = [copy, base], base = assign[0], copy = assign[1]);
542
- (assign$1 = [inversePatches, patches], patches = assign$1[0], inversePatches = assign$1[1]);
543
- }
544
- var delta = copy.length - base.length;
545
-
546
- // Find the first replaced index.
547
- var start = 0;
548
- while (base[start] === copy[start] && start < base.length) {
549
- ++start;
550
- }
551
-
552
- // Find the last replaced index. Search from the end to optimize splice patches.
553
- var end = base.length;
554
- while (end > start && base[end - 1] === copy[end + delta - 1]) {
555
- --end;
556
- }
557
-
558
- // Process replaced indices.
559
- for (var i = start; i < end; ++i) {
560
- if (assigned[i] && copy[i] !== base[i]) {
561
- var path = basePath.concat([i]);
562
- patches.push({
563
- op: "replace",
564
- path: path,
565
- value: copy[i]
566
- });
567
- inversePatches.push({
568
- op: "replace",
569
- path: path,
570
- value: base[i]
571
- });
572
- }
573
- }
574
- var useRemove = end != base.length;
575
- var replaceCount = patches.length;
576
-
577
- // Process added indices.
578
- for (var i$1 = end + delta - 1; i$1 >= end; --i$1) {
579
- var path$1 = basePath.concat([i$1]);
580
- patches[replaceCount + i$1 - end] = {
581
- op: "add",
582
- path: path$1,
583
- value: copy[i$1]
584
- };
585
- if (useRemove) {
586
- inversePatches.push({
587
- op: "remove",
588
- path: path$1
589
- });
590
- }
591
- }
592
-
593
- // One "replace" patch reverses all non-splicing "add" patches.
594
- if (!useRemove) {
595
- inversePatches.push({
596
- op: "replace",
597
- path: basePath.concat(["length"]),
598
- value: base.length
599
- });
600
- }
601
- }
602
- function generateObjectPatches(state, basePath, patches, inversePatches) {
603
- var base = state.base;
604
- var copy = state.copy;
605
- each(state.assigned, function (key, assignedValue) {
606
- var origValue = base[key];
607
- var value = copy[key];
608
- var op = !assignedValue ? "remove" : key in base ? "replace" : "add";
609
- if (origValue === value && op === "replace") { return; }
610
- var path = basePath.concat(key);
611
- patches.push(op === "remove" ? {
612
- op: op,
613
- path: path
614
- } : {
615
- op: op,
616
- path: path,
617
- value: value
618
- });
619
- inversePatches.push(op === "add" ? {
620
- op: "remove",
621
- path: path
622
- } : op === "remove" ? {
623
- op: "add",
624
- path: path,
625
- value: origValue
626
- } : {
627
- op: "replace",
628
- path: path,
629
- value: origValue
630
- });
631
- });
632
- }
633
- function applyPatches$1(draft, patches) {
634
- for (var i = 0; i < patches.length; i++) {
635
- var patch = patches[i];
636
- var path = patch.path;
637
- if (path.length === 0 && patch.op === "replace") {
638
- draft = patch.value;
639
- } else {
640
- var base = draft;
641
- for (var i$1 = 0; i$1 < path.length - 1; i$1++) {
642
- base = base[path[i$1]];
643
- if (!base || typeof base !== "object") { throw new Error("Cannot apply patch, path doesn't resolve: " + path.join("/")); } // prettier-ignore
644
- }
645
-
646
- var key = path[path.length - 1];
647
- switch (patch.op) {
648
- case "replace":
649
- base[key] = patch.value;
650
- break;
651
- case "add":
652
- if (Array.isArray(base)) {
653
- // TODO: support "foo/-" paths for appending to an array
654
- base.splice(key, 0, patch.value);
655
- } else {
656
- base[key] = patch.value;
657
- }
658
- break;
659
- case "remove":
660
- if (Array.isArray(base)) {
661
- base.splice(key, 1);
662
- } else {
663
- delete base[key];
664
- }
665
- break;
666
- default:
667
- throw new Error("Unsupported patch operation: " + patch.op);
668
- }
669
- }
670
- }
671
- return draft;
672
- }
673
-
674
- function verifyMinified() {}
675
- var configDefaults = {
676
- useProxies: typeof Proxy !== "undefined" && typeof Reflect !== "undefined",
677
- autoFreeze: typeof process !== "undefined" ? process.env.NODE_ENV !== "production" : verifyMinified.name === "verifyMinified",
678
- onAssign: null,
679
- onDelete: null,
680
- onCopy: null
681
- };
682
- var Immer = function Immer(config) {
683
- assign(this, configDefaults, config);
684
- this.setUseProxies(this.useProxies);
685
- this.produce = this.produce.bind(this);
686
- };
687
- Immer.prototype.produce = function produce (base, recipe, patchListener) {
688
- var this$1$1 = this;
689
-
690
- // curried invocation
691
- if (typeof base === "function" && typeof recipe !== "function") {
692
- var defaultBase = recipe;
693
- recipe = base;
694
- var self = this;
695
- return function curriedProduce(base) {
696
- var this$1$1 = this;
697
- if ( base === void 0 ) base = defaultBase;
698
- var args = [], len = arguments.length - 1;
699
- while ( len-- > 0 ) args[ len ] = arguments[ len + 1 ];
700
-
701
- return self.produce(base, function (draft) { return recipe.call.apply(recipe, [ this$1$1, draft ].concat( args )); }); // prettier-ignore
702
- };
703
- }
704
-
705
- // prettier-ignore
706
- {
707
- if (typeof recipe !== "function") {
708
- throw new Error("The first or second argument to `produce` must be a function");
709
- }
710
- if (patchListener !== undefined && typeof patchListener !== "function") {
711
- throw new Error("The third argument to `produce` must be a function or undefined");
712
- }
713
- }
714
- var result;
715
-
716
- // Only plain objects, arrays, and "immerable classes" are drafted.
717
- if (isDraftable(base)) {
718
- var scope = ImmerScope.enter();
719
- var proxy = this.createProxy(base);
720
- var hasError = true;
721
- try {
722
- result = recipe(proxy);
723
- hasError = false;
724
- } finally {
725
- // finally instead of catch + rethrow better preserves original stack
726
- if (hasError) { scope.revoke(); }else { scope.leave(); }
727
- }
728
- if (result instanceof Promise) {
729
- return result.then(function (result) {
730
- scope.usePatches(patchListener);
731
- return this$1$1.processResult(result, scope);
732
- }, function (error) {
733
- scope.revoke();
734
- throw error;
735
- });
736
- }
737
- scope.usePatches(patchListener);
738
- return this.processResult(result, scope);
739
- } else {
740
- result = recipe(base);
741
- if (result === undefined) { return base; }
742
- return result !== NOTHING ? result : undefined;
743
- }
744
- };
745
- Immer.prototype.createDraft = function createDraft (base) {
746
- if (!isDraftable(base)) {
747
- throw new Error("First argument to `createDraft` must be a plain object, an array, or an immerable object"); // prettier-ignore
748
- }
749
-
750
- var scope = ImmerScope.enter();
751
- var proxy = this.createProxy(base, null);
752
- proxy[DRAFT_STATE].isManual = true;
753
- scope.leave();
754
- return proxy;
755
- };
756
- Immer.prototype.finishDraft = function finishDraft (draft, patchListener, keepAlive) {
757
- var state = draft && draft[DRAFT_STATE];
758
- if (!state || !state.isManual) {
759
- throw new Error("First argument to `finishDraft` must be a draft returned by `createDraft`"); // prettier-ignore
760
- }
761
-
762
- if (state.finalized) {
763
- throw new Error("The given draft is already finalized"); // prettier-ignore
764
- }
765
-
766
- var scope = state.scope;
767
- scope.usePatches(patchListener);
768
- return this.processResult(undefined, scope, keepAlive);
769
- };
770
- Immer.prototype.setAutoFreeze = function setAutoFreeze (value) {
771
- this.autoFreeze = value;
772
- };
773
- Immer.prototype.setUseProxies = function setUseProxies (value) {
774
- this.useProxies = value;
775
- assign(this, value ? modernProxy : legacyProxy);
776
- };
777
- Immer.prototype.applyPatches = function applyPatches$1$1 (base, patches) {
778
- // Mutate the base state when a draft is passed.
779
- if (isDraft(base)) {
780
- return applyPatches$1(base, patches);
781
- }
782
- // Otherwise, produce a copy of the base state.
783
- return this.produce(base, function (draft) { return applyPatches$1(draft, patches); });
784
- };
785
- /** @internal */
786
- Immer.prototype.processResult = function processResult (result, scope, keepAlive) {
787
- var baseDraft = scope.drafts[0];
788
- var isReplaced = result !== undefined && result !== baseDraft;
789
- this.willFinalize(scope, result, isReplaced);
790
- if (isReplaced) {
791
- if (baseDraft[DRAFT_STATE].modified) {
792
- scope.revoke(keepAlive);
793
- throw new Error("An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft."); // prettier-ignore
794
- }
795
-
796
- if (isDraftable(result)) {
797
- // Finalize the result in case it contains (or is) a subset of the draft.
798
- result = this.finalize(result, null, scope);
799
- }
800
- if (scope.patches) {
801
- scope.patches.push({
802
- op: "replace",
803
- path: [],
804
- value: result
805
- });
806
- scope.inversePatches.push({
807
- op: "replace",
808
- path: [],
809
- value: baseDraft[DRAFT_STATE].base
810
- });
811
- }
812
- } else {
813
- // Finalize the base draft.
814
- result = this.finalize(baseDraft, [], scope, keepAlive);
815
- }
816
- scope.revoke(keepAlive);
817
- if (scope.patches) {
818
- scope.patchListener(scope.patches, scope.inversePatches);
819
- }
820
- return result !== NOTHING ? result : undefined;
821
- };
822
- /**
823
- * @internal
824
- * Finalize a draft, returning either the unmodified base state or a modified
825
- * copy of the base state.
826
- */
827
- Immer.prototype.finalize = function finalize (draft, path, scope, keepAlive) {
828
- var this$1$1 = this;
829
-
830
- var state = draft[DRAFT_STATE];
831
- if (!state) {
832
- if (Object.isFrozen(draft)) { return draft; }
833
- return this.finalizeTree(draft, null, scope);
834
- }
835
- // Never finalize drafts owned by another scope.
836
- if (state.scope !== scope) {
837
- return draft;
838
- }
839
- if (!state.modified) {
840
- return state.base;
841
- }
842
- if (!state.finalized) {
843
- if (!keepAlive) { state.finalized = true; }
844
- this.finalizeTree(state.draft, path, scope);
845
- if (this.onDelete) {
846
- // The `assigned` object is unreliable with ES5 drafts.
847
- if (this.useProxies) {
848
- var assigned = state.assigned;
849
- for (var prop in assigned) {
850
- if (!assigned[prop]) { this.onDelete(state, prop); }
851
- }
852
- } else {
853
- var base = state.base;
854
- var copy = state.copy;
855
- each(base, function (prop) {
856
- if (!has(copy, prop)) { this$1$1.onDelete(state, prop); }
857
- });
858
- }
859
- }
860
- if (this.onCopy) {
861
- this.onCopy(state);
862
- }
863
-
864
- // At this point, all descendants of `state.copy` have been finalized,
865
- // so we can be sure that `scope.canAutoFreeze` is accurate.
866
- if (this.autoFreeze && scope.canAutoFreeze) {
867
- Object.freeze(state.copy);
868
- }
869
- if (path && scope.patches) {
870
- generatePatches(state, path, scope.patches, scope.inversePatches);
871
- }
872
- }
873
- return state.copy;
874
- };
875
- /**
876
- * @internal
877
- * Finalize all drafts in the given state tree.
878
- */
879
- Immer.prototype.finalizeTree = function finalizeTree (root, rootPath, scope) {
880
- var this$1$1 = this;
881
-
882
- var state = root[DRAFT_STATE];
883
- if (state) {
884
- if (!this.useProxies) {
885
- // Create the final copy, with added keys and without deleted keys.
886
- state.copy = shallowCopy(state.draft, true);
887
- }
888
- root = state.copy;
889
- }
890
- var needPatches = !!rootPath && !!scope.patches;
891
- var finalizeProperty = function (prop, value, parent) {
892
- if (value === parent) {
893
- throw Error("Immer forbids circular references");
894
- }
895
-
896
- // In the `finalizeTree` method, only the `root` object may be a draft.
897
- var isDraftProp = !!state && parent === root;
898
- if (isDraft(value)) {
899
- var path = isDraftProp && needPatches && !state.assigned[prop] ? rootPath.concat(prop) : null;
900
-
901
- // Drafts owned by `scope` are finalized here.
902
- value = this$1$1.finalize(value, path, scope);
903
-
904
- // Drafts from another scope must prevent auto-freezing.
905
- if (isDraft(value)) {
906
- scope.canAutoFreeze = false;
907
- }
908
-
909
- // Preserve non-enumerable properties.
910
- if (Array.isArray(parent) || isEnumerable(parent, prop)) {
911
- parent[prop] = value;
912
- } else {
913
- Object.defineProperty(parent, prop, {
914
- value: value
915
- });
916
- }
917
-
918
- // Unchanged drafts are never passed to the `onAssign` hook.
919
- if (isDraftProp && value === state.base[prop]) { return; }
920
- }
921
- // Unchanged draft properties are ignored.
922
- else if (isDraftProp && is(value, state.base[prop])) {
923
- return;
924
- }
925
- // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.
926
- else if (isDraftable(value) && !Object.isFrozen(value)) {
927
- each(value, finalizeProperty);
928
- }
929
- if (isDraftProp && this$1$1.onAssign) {
930
- this$1$1.onAssign(state, prop, value);
931
- }
932
- };
933
- each(root, finalizeProperty);
934
- return root;
935
- };
936
-
937
- var immer = new Immer();
938
-
939
- /**
940
- * The `produce` function takes a value and a "recipe function" (whose
941
- * return value often depends on the base state). The recipe function is
942
- * free to mutate its first argument however it wants. All mutations are
943
- * only ever applied to a __copy__ of the base state.
944
- *
945
- * Pass only a function to create a "curried producer" which relieves you
946
- * from passing the recipe function every time.
947
- *
948
- * Only plain objects and arrays are made mutable. All other objects are
949
- * considered uncopyable.
950
- *
951
- * Note: This function is __bound__ to its `Immer` instance.
952
- *
953
- * @param {any} base - the initial state
954
- * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified
955
- * @param {Function} patchListener - optional function that will be called with all the patches produced here
956
- * @returns {any} a new state, or the initial state if nothing was modified
957
- */
958
- var produce = immer.produce;
959
-
960
- /**
961
- * Pass true to automatically freeze all copies created by Immer.
962
- *
963
- * By default, auto-freezing is disabled in production.
964
- */
965
- var setAutoFreeze = immer.setAutoFreeze.bind(immer);
966
-
967
- /**
968
- * Pass true to use the ES2015 `Proxy` class when creating drafts, which is
969
- * always faster than using ES5 proxies.
970
- *
971
- * By default, feature detection is used, so calling this is rarely necessary.
972
- */
973
- var setUseProxies = immer.setUseProxies.bind(immer);
974
-
975
- /**
976
- * Apply an array of Immer patches to the first argument.
977
- *
978
- * This function is a producer, which means copy-on-write is in effect.
979
- */
980
- var applyPatches = immer.applyPatches.bind(immer);
981
-
982
- /**
983
- * Create an Immer draft from the given base state, which may be a draft itself.
984
- * The draft can be modified until you finalize it with the `finishDraft` function.
985
- */
986
- var createDraft = immer.createDraft.bind(immer);
987
-
988
- /**
989
- * Finalize an Immer draft from a `createDraft` call, returning the base state
990
- * (if no changes were made) or a modified copy. The draft must *not* be
991
- * mutated afterwards.
992
- *
993
- * Pass a function as the 2nd argument to generate Immer patches based on the
994
- * changes that were made.
995
- */
996
- var finishDraft = immer.finishDraft.bind(immer);
997
-
998
- export { DRAFT_STATE, Immer, applyPatches, createDraft, produce as default, finishDraft, DRAFTABLE as immerable, isDraft, isDraftable, NOTHING as nothing, original, produce, setAutoFreeze, setUseProxies };
999
- //# sourceMappingURL=immer.module.js.map