@likec4/core 1.46.0 → 1.46.3

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.
Files changed (27) hide show
  1. package/dist/builder/index.d.mts +1 -1
  2. package/dist/builder/index.mjs +5 -5
  3. package/dist/compute-view/index.d.mts +1 -1
  4. package/dist/compute-view/index.mjs +4 -4
  5. package/dist/compute-view/relationships-view/index.d.mts +1 -1
  6. package/dist/index.mjs +2 -2
  7. package/dist/model/connection/deployment/index.d.mts +3 -3
  8. package/dist/model/connection/deployment/index.mjs +2 -2
  9. package/dist/model/connection/index.d.mts +4 -4
  10. package/dist/model/connection/index.mjs +3 -3
  11. package/dist/model/connection/model/index.d.mts +3 -3
  12. package/dist/model/connection/model/index.mjs +2 -2
  13. package/dist/model/index.d.mts +5 -5
  14. package/dist/model/index.mjs +4 -4
  15. package/dist/shared/{core.CQPEY2wQ.mjs → core.78IWAHAE.mjs} +1 -1
  16. package/dist/shared/{core.DqlrMlsf.mjs → core.BFd1lU9R.mjs} +1 -1
  17. package/dist/shared/{core.DbQpDoJS.mjs → core.CRhT4Iio.mjs} +9 -6
  18. package/dist/shared/{core.BK-tI3Q3.mjs → core.DEK-BO3_.mjs} +2 -2
  19. package/dist/shared/{core.BLG-ADpi.d.mts → core.DK3pdsTf.d.mts} +1 -0
  20. package/dist/shared/{core.Qv7vZIwZ.mjs → core.DSalPD6v.mjs} +258 -188
  21. package/dist/shared/{core.DU8pv3Q6.mjs → core.DVfPJPJj.mjs} +15 -14
  22. package/dist/shared/{core.Byj60hH6.d.mts → core.DezoCAzf.d.mts} +1 -1
  23. package/dist/shared/{core.D4Y4U8Lp.d.mts → core.I8sCl3WM.d.mts} +2 -2
  24. package/dist/shared/{core.CQYL_Z-_.d.mts → core.e5GJ61_a.d.mts} +2 -2
  25. package/dist/shared/{core.DJpiszim.mjs → core.xgoPk2RG.mjs} +124 -126
  26. package/dist/types/index.mjs +1 -1
  27. package/package.json +9 -9
@@ -69,7 +69,7 @@ var errors = process.env.NODE_ENV !== "production" ? [
69
69
  function die(error, ...args) {
70
70
  if (process.env.NODE_ENV !== "production") {
71
71
  const e = errors[error];
72
- const msg = typeof e === "function" ? e.apply(null, args) : e;
72
+ const msg = isFunction(e) ? e.apply(null, args) : e;
73
73
  throw new Error(`[Immer] ${msg}`);
74
74
  }
75
75
  throw new Error(
@@ -78,27 +78,32 @@ function die(error, ...args) {
78
78
  }
79
79
 
80
80
  // src/utils/common.ts
81
- var getPrototypeOf = Object.getPrototypeOf;
82
- function isDraft(value) {
83
- return !!value && !!value[DRAFT_STATE];
84
- }
81
+ var O = Object;
82
+ var getPrototypeOf = O.getPrototypeOf;
83
+ var CONSTRUCTOR = "constructor";
84
+ var PROTOTYPE = "prototype";
85
+ var CONFIGURABLE = "configurable";
86
+ var ENUMERABLE = "enumerable";
87
+ var WRITABLE = "writable";
88
+ var VALUE = "value";
89
+ var isDraft = (value) => !!value && !!value[DRAFT_STATE];
85
90
  function isDraftable(value) {
86
91
  if (!value)
87
92
  return false;
88
- return isPlainObject(value) || Array.isArray(value) || !!value[DRAFTABLE] || !!value.constructor?.[DRAFTABLE] || isMap(value) || isSet(value);
93
+ return isPlainObject(value) || isArray(value) || !!value[DRAFTABLE] || !!value[CONSTRUCTOR]?.[DRAFTABLE] || isMap(value) || isSet(value);
89
94
  }
90
- var objectCtorString = Object.prototype.constructor.toString();
95
+ var objectCtorString = O[PROTOTYPE][CONSTRUCTOR].toString();
91
96
  var cachedCtorStrings = /* @__PURE__ */ new WeakMap();
92
97
  function isPlainObject(value) {
93
- if (!value || typeof value !== "object")
98
+ if (!value || !isObjectish(value))
94
99
  return false;
95
- const proto = Object.getPrototypeOf(value);
96
- if (proto === null || proto === Object.prototype)
100
+ const proto = getPrototypeOf(value);
101
+ if (proto === null || proto === O[PROTOTYPE])
97
102
  return true;
98
- const Ctor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor;
103
+ const Ctor = O.hasOwnProperty.call(proto, CONSTRUCTOR) && proto[CONSTRUCTOR];
99
104
  if (Ctor === Object)
100
105
  return true;
101
- if (typeof Ctor !== "function")
106
+ if (!isFunction(Ctor))
102
107
  return false;
103
108
  let ctorString = cachedCtorStrings.get(Ctor);
104
109
  if (ctorString === void 0) {
@@ -109,7 +114,7 @@ function isPlainObject(value) {
109
114
  }
110
115
  function each(obj, iter, strict = true) {
111
116
  if (getArchtype(obj) === 0 /* Object */) {
112
- const keys = strict ? Reflect.ownKeys(obj) : Object.keys(obj);
117
+ const keys = strict ? Reflect.ownKeys(obj) : O.keys(obj);
113
118
  keys.forEach((key) => {
114
119
  iter(key, obj[key], obj);
115
120
  });
@@ -119,20 +124,21 @@ function each(obj, iter, strict = true) {
119
124
  }
120
125
  function getArchtype(thing) {
121
126
  const state = thing[DRAFT_STATE];
122
- return state ? state.type_ : Array.isArray(thing) ? 1 /* Array */ : isMap(thing) ? 2 /* Map */ : isSet(thing) ? 3 /* Set */ : 0 /* Object */;
123
- }
124
- function has(thing, prop) {
125
- return getArchtype(thing) === 2 /* Map */ ? thing.has(prop) : Object.prototype.hasOwnProperty.call(thing, prop);
127
+ return state ? state.type_ : isArray(thing) ? 1 /* Array */ : isMap(thing) ? 2 /* Map */ : isSet(thing) ? 3 /* Set */ : 0 /* Object */;
126
128
  }
127
- function set(thing, propOrOldValue, value) {
128
- const t = getArchtype(thing);
129
- if (t === 2 /* Map */)
129
+ var has = (thing, prop, type = getArchtype(thing)) => type === 2 /* Map */ ? thing.has(prop) : O[PROTOTYPE].hasOwnProperty.call(thing, prop);
130
+ var get = (thing, prop, type = getArchtype(thing)) => (
131
+ // @ts-ignore
132
+ type === 2 /* Map */ ? thing.get(prop) : thing[prop]
133
+ );
134
+ var set = (thing, propOrOldValue, value, type = getArchtype(thing)) => {
135
+ if (type === 2 /* Map */)
130
136
  thing.set(propOrOldValue, value);
131
- else if (t === 3 /* Set */) {
137
+ else if (type === 3 /* Set */) {
132
138
  thing.add(value);
133
139
  } else
134
140
  thing[propOrOldValue] = value;
135
- }
141
+ };
136
142
  function is(x, y) {
137
143
  if (x === y) {
138
144
  return x !== 0 || 1 / x === 1 / y;
@@ -140,15 +146,14 @@ function is(x, y) {
140
146
  return x !== x && y !== y;
141
147
  }
142
148
  }
143
- function isMap(target) {
144
- return target instanceof Map;
145
- }
146
- function isSet(target) {
147
- return target instanceof Set;
148
- }
149
- function latest(state) {
150
- return state.copy_ || state.base_;
151
- }
149
+ var isArray = Array.isArray;
150
+ var isMap = (target) => target instanceof Map;
151
+ var isSet = (target) => target instanceof Set;
152
+ var isObjectish = (target) => typeof target === "object";
153
+ var isFunction = (target) => typeof target === "function";
154
+ var isBoolean = (target) => typeof target === "boolean";
155
+ var latest = (state) => state.copy_ || state.base_;
156
+ var getFinalValue = (state) => state.modified_ ? state.copy_ : state.base_;
152
157
  function shallowCopy(base, strict) {
153
158
  if (isMap(base)) {
154
159
  return new Map(base);
@@ -156,68 +161,76 @@ function shallowCopy(base, strict) {
156
161
  if (isSet(base)) {
157
162
  return new Set(base);
158
163
  }
159
- if (Array.isArray(base))
160
- return Array.prototype.slice.call(base);
164
+ if (isArray(base))
165
+ return Array[PROTOTYPE].slice.call(base);
161
166
  const isPlain = isPlainObject(base);
162
167
  if (strict === true || strict === "class_only" && !isPlain) {
163
- const descriptors = Object.getOwnPropertyDescriptors(base);
168
+ const descriptors = O.getOwnPropertyDescriptors(base);
164
169
  delete descriptors[DRAFT_STATE];
165
170
  let keys = Reflect.ownKeys(descriptors);
166
171
  for (let i = 0; i < keys.length; i++) {
167
172
  const key = keys[i];
168
173
  const desc = descriptors[key];
169
- if (desc.writable === false) {
170
- desc.writable = true;
171
- desc.configurable = true;
174
+ if (desc[WRITABLE] === false) {
175
+ desc[WRITABLE] = true;
176
+ desc[CONFIGURABLE] = true;
172
177
  }
173
178
  if (desc.get || desc.set)
174
179
  descriptors[key] = {
175
- configurable: true,
176
- writable: true,
180
+ [CONFIGURABLE]: true,
181
+ [WRITABLE]: true,
177
182
  // could live with !!desc.set as well here...
178
- enumerable: desc.enumerable,
179
- value: base[key]
183
+ [ENUMERABLE]: desc[ENUMERABLE],
184
+ [VALUE]: base[key]
180
185
  };
181
186
  }
182
- return Object.create(getPrototypeOf(base), descriptors);
187
+ return O.create(getPrototypeOf(base), descriptors);
183
188
  } else {
184
189
  const proto = getPrototypeOf(base);
185
190
  if (proto !== null && isPlain) {
186
191
  return { ...base };
187
192
  }
188
- const obj = Object.create(proto);
189
- return Object.assign(obj, base);
193
+ const obj = O.create(proto);
194
+ return O.assign(obj, base);
190
195
  }
191
196
  }
192
197
  function freeze(obj, deep = false) {
193
198
  if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj))
194
199
  return obj;
195
200
  if (getArchtype(obj) > 1) {
196
- Object.defineProperties(obj, {
201
+ O.defineProperties(obj, {
197
202
  set: dontMutateMethodOverride,
198
203
  add: dontMutateMethodOverride,
199
204
  clear: dontMutateMethodOverride,
200
205
  delete: dontMutateMethodOverride
201
206
  });
202
207
  }
203
- Object.freeze(obj);
208
+ O.freeze(obj);
204
209
  if (deep)
205
- Object.values(obj).forEach((value) => freeze(value, true));
210
+ each(
211
+ obj,
212
+ (_key, value) => {
213
+ freeze(value, true);
214
+ },
215
+ false
216
+ );
206
217
  return obj;
207
218
  }
208
219
  function dontMutateFrozenCollections() {
209
220
  die(2);
210
221
  }
211
222
  var dontMutateMethodOverride = {
212
- value: dontMutateFrozenCollections
223
+ [VALUE]: dontMutateFrozenCollections
213
224
  };
214
225
  function isFrozen(obj) {
215
- if (obj === null || typeof obj !== "object")
226
+ if (obj === null || !isObjectish(obj))
216
227
  return true;
217
- return Object.isFrozen(obj);
228
+ return O.isFrozen(obj);
218
229
  }
219
230
 
220
231
  // src/utils/plugins.ts
232
+ var PluginMapSet = "MapSet";
233
+ var PluginPatches = "Patches";
221
234
  var plugins = {};
222
235
  function getPlugin(pluginKey) {
223
236
  const plugin = plugins[pluginKey];
@@ -226,26 +239,26 @@ function getPlugin(pluginKey) {
226
239
  }
227
240
  return plugin;
228
241
  }
242
+ var isPluginLoaded = (pluginKey) => !!plugins[pluginKey];
229
243
 
230
244
  // src/core/scope.ts
231
245
  var currentScope;
232
- function getCurrentScope() {
233
- return currentScope;
234
- }
235
- function createScope(parent_, immer_) {
236
- return {
237
- drafts_: [],
238
- parent_,
239
- immer_,
240
- // Whenever the modified draft contains a draft from another scope, we
241
- // need to prevent auto-freezing so the unowned draft can be finalized.
242
- canAutoFreeze_: true,
243
- unfinalizedDrafts_: 0
244
- };
245
- }
246
+ var getCurrentScope = () => currentScope;
247
+ var createScope = (parent_, immer_) => ({
248
+ drafts_: [],
249
+ parent_,
250
+ immer_,
251
+ // Whenever the modified draft contains a draft from another scope, we
252
+ // need to prevent auto-freezing so the unowned draft can be finalized.
253
+ canAutoFreeze_: true,
254
+ unfinalizedDrafts_: 0,
255
+ handledSet_: /* @__PURE__ */ new Set(),
256
+ processedForPatches_: /* @__PURE__ */ new Set(),
257
+ mapSetPlugin_: isPluginLoaded(PluginMapSet) ? getPlugin(PluginMapSet) : void 0
258
+ });
246
259
  function usePatchesInScope(scope, patchListener) {
247
260
  if (patchListener) {
248
- getPlugin("Patches");
261
+ scope.patchPlugin_ = getPlugin(PluginPatches);
249
262
  scope.patches_ = [];
250
263
  scope.inversePatches_ = [];
251
264
  scope.patchListener_ = patchListener;
@@ -261,9 +274,7 @@ function leaveScope(scope) {
261
274
  currentScope = scope.parent_;
262
275
  }
263
276
  }
264
- function enterScope(immer2) {
265
- return currentScope = createScope(currentScope, immer2);
266
- }
277
+ var enterScope = (immer2) => currentScope = createScope(currentScope, immer2);
267
278
  function revokeDraft(draft) {
268
279
  const state = draft[DRAFT_STATE];
269
280
  if (state.type_ === 0 /* Object */ || state.type_ === 1 /* Array */)
@@ -284,129 +295,166 @@ function processResult(result, scope) {
284
295
  }
285
296
  if (isDraftable(result)) {
286
297
  result = finalize(scope, result);
287
- if (!scope.parent_)
288
- maybeFreeze(scope, result);
289
298
  }
290
- if (scope.patches_) {
291
- getPlugin("Patches").generateReplacementPatches_(
299
+ const { patchPlugin_ } = scope;
300
+ if (patchPlugin_) {
301
+ patchPlugin_.generateReplacementPatches_(
292
302
  baseDraft[DRAFT_STATE].base_,
293
303
  result,
294
- scope.patches_,
295
- scope.inversePatches_
304
+ scope
296
305
  );
297
306
  }
298
307
  } else {
299
- result = finalize(scope, baseDraft, []);
308
+ result = finalize(scope, baseDraft);
300
309
  }
310
+ maybeFreeze(scope, result, true);
301
311
  revokeScope(scope);
302
312
  if (scope.patches_) {
303
313
  scope.patchListener_(scope.patches_, scope.inversePatches_);
304
314
  }
305
315
  return result !== NOTHING ? result : void 0;
306
316
  }
307
- function finalize(rootScope, value, path) {
317
+ function finalize(rootScope, value) {
308
318
  if (isFrozen(value))
309
319
  return value;
310
- const useStrictIteration = rootScope.immer_.shouldUseStrictIteration();
311
320
  const state = value[DRAFT_STATE];
312
321
  if (!state) {
313
- each(
314
- value,
315
- (key, childValue) => finalizeProperty(rootScope, state, value, key, childValue, path),
316
- useStrictIteration
317
- );
318
- return value;
322
+ const finalValue = handleValue(value, rootScope.handledSet_, rootScope);
323
+ return finalValue;
319
324
  }
320
- if (state.scope_ !== rootScope)
325
+ if (!isSameScope(state, rootScope)) {
321
326
  return value;
327
+ }
322
328
  if (!state.modified_) {
323
- maybeFreeze(rootScope, state.base_, true);
324
329
  return state.base_;
325
330
  }
326
331
  if (!state.finalized_) {
327
- state.finalized_ = true;
328
- state.scope_.unfinalizedDrafts_--;
329
- const result = state.copy_;
330
- let resultEach = result;
331
- let isSet2 = false;
332
- if (state.type_ === 3 /* Set */) {
333
- resultEach = new Set(result);
334
- result.clear();
335
- isSet2 = true;
336
- }
337
- each(
338
- resultEach,
339
- (key, childValue) => finalizeProperty(
340
- rootScope,
341
- state,
342
- result,
343
- key,
344
- childValue,
345
- path,
346
- isSet2
347
- ),
348
- useStrictIteration
349
- );
350
- maybeFreeze(rootScope, result, false);
351
- if (path && rootScope.patches_) {
352
- getPlugin("Patches").generatePatches_(
353
- state,
354
- path,
355
- rootScope.patches_,
356
- rootScope.inversePatches_
357
- );
332
+ const { callbacks_ } = state;
333
+ if (callbacks_) {
334
+ while (callbacks_.length > 0) {
335
+ const callback = callbacks_.pop();
336
+ callback(rootScope);
337
+ }
358
338
  }
339
+ generatePatchesAndFinalize(state, rootScope);
359
340
  }
360
341
  return state.copy_;
361
342
  }
362
- function finalizeProperty(rootScope, parentState, targetObject, prop, childValue, rootPath, targetIsSet) {
363
- if (childValue == null) {
364
- return;
343
+ function maybeFreeze(scope, value, deep = false) {
344
+ if (!scope.parent_ && scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {
345
+ freeze(value, deep);
365
346
  }
366
- if (typeof childValue !== "object" && !targetIsSet) {
367
- return;
347
+ }
348
+ function markStateFinalized(state) {
349
+ state.finalized_ = true;
350
+ state.scope_.unfinalizedDrafts_--;
351
+ }
352
+ var isSameScope = (state, rootScope) => state.scope_ === rootScope;
353
+ var EMPTY_LOCATIONS_RESULT = [];
354
+ function updateDraftInParent(parent, draftValue, finalizedValue, originalKey) {
355
+ const parentCopy = latest(parent);
356
+ const parentType = parent.type_;
357
+ if (originalKey !== void 0) {
358
+ const currentValue = get(parentCopy, originalKey, parentType);
359
+ if (currentValue === draftValue) {
360
+ set(parentCopy, originalKey, finalizedValue, parentType);
361
+ return;
362
+ }
368
363
  }
369
- const childIsFrozen = isFrozen(childValue);
370
- if (childIsFrozen && !targetIsSet) {
371
- return;
364
+ if (!parent.draftLocations_) {
365
+ const draftLocations = parent.draftLocations_ = /* @__PURE__ */ new Map();
366
+ each(parentCopy, (key, value) => {
367
+ if (isDraft(value)) {
368
+ const keys = draftLocations.get(value) || [];
369
+ keys.push(key);
370
+ draftLocations.set(value, keys);
371
+ }
372
+ });
372
373
  }
373
- if (process.env.NODE_ENV !== "production" && childValue === targetObject)
374
- die(5);
375
- if (isDraft(childValue)) {
376
- const path = rootPath && parentState && parentState.type_ !== 3 /* Set */ && // Set objects are atomic since they have no keys.
377
- !has(parentState.assigned_, prop) ? rootPath.concat(prop) : void 0;
378
- const res = finalize(rootScope, childValue, path);
379
- set(targetObject, prop, res);
380
- if (isDraft(res)) {
381
- rootScope.canAutoFreeze_ = false;
382
- } else
383
- return;
384
- } else if (targetIsSet) {
385
- targetObject.add(childValue);
374
+ const locations = parent.draftLocations_.get(draftValue) ?? EMPTY_LOCATIONS_RESULT;
375
+ for (const location of locations) {
376
+ set(parentCopy, location, finalizedValue, parentType);
386
377
  }
387
- if (isDraftable(childValue) && !childIsFrozen) {
388
- if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) {
378
+ }
379
+ function registerChildFinalizationCallback(parent, child, key) {
380
+ parent.callbacks_.push(function childCleanup(rootScope) {
381
+ const state = child;
382
+ if (!state || !isSameScope(state, rootScope)) {
389
383
  return;
390
384
  }
391
- if (parentState && parentState.base_ && parentState.base_[prop] === childValue && childIsFrozen) {
392
- return;
385
+ rootScope.mapSetPlugin_?.fixSetContents(state);
386
+ const finalizedValue = getFinalValue(state);
387
+ updateDraftInParent(parent, state.draft_ ?? state, finalizedValue, key);
388
+ generatePatchesAndFinalize(state, rootScope);
389
+ });
390
+ }
391
+ function generatePatchesAndFinalize(state, rootScope) {
392
+ const shouldFinalize = state.modified_ && !state.finalized_ && (state.type_ === 3 /* Set */ || (state.assigned_?.size ?? 0) > 0);
393
+ if (shouldFinalize) {
394
+ const { patchPlugin_ } = rootScope;
395
+ if (patchPlugin_) {
396
+ const basePath = patchPlugin_.getPath(state);
397
+ if (basePath) {
398
+ patchPlugin_.generatePatches_(state, basePath, rootScope);
399
+ }
393
400
  }
394
- finalize(rootScope, childValue);
395
- if ((!parentState || !parentState.scope_.parent_) && typeof prop !== "symbol" && (isMap(targetObject) ? targetObject.has(prop) : Object.prototype.propertyIsEnumerable.call(targetObject, prop)))
396
- maybeFreeze(rootScope, childValue);
401
+ markStateFinalized(state);
397
402
  }
398
403
  }
399
- function maybeFreeze(scope, value, deep = false) {
400
- if (!scope.parent_ && scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {
401
- freeze(value, deep);
404
+ function handleCrossReference(target, key, value) {
405
+ const { scope_ } = target;
406
+ if (isDraft(value)) {
407
+ const state = value[DRAFT_STATE];
408
+ if (isSameScope(state, scope_)) {
409
+ state.callbacks_.push(function crossReferenceCleanup() {
410
+ prepareCopy(target);
411
+ const finalizedValue = getFinalValue(state);
412
+ updateDraftInParent(target, value, finalizedValue, key);
413
+ });
414
+ }
415
+ } else if (isDraftable(value)) {
416
+ target.callbacks_.push(function nestedDraftCleanup() {
417
+ const targetCopy = latest(target);
418
+ if (get(targetCopy, key, target.type_) === value) {
419
+ if (scope_.drafts_.length > 1 && (target.assigned_.get(key) ?? false) === true && target.copy_) {
420
+ handleValue(
421
+ get(target.copy_, key, target.type_),
422
+ scope_.handledSet_,
423
+ scope_
424
+ );
425
+ }
426
+ }
427
+ });
402
428
  }
403
429
  }
430
+ function handleValue(target, handledSet, rootScope) {
431
+ if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) {
432
+ return target;
433
+ }
434
+ if (isDraft(target) || handledSet.has(target) || !isDraftable(target) || isFrozen(target)) {
435
+ return target;
436
+ }
437
+ handledSet.add(target);
438
+ each(target, (key, value) => {
439
+ if (isDraft(value)) {
440
+ const state = value[DRAFT_STATE];
441
+ if (isSameScope(state, rootScope)) {
442
+ const updatedValue = getFinalValue(state);
443
+ set(target, key, updatedValue, target.type_);
444
+ markStateFinalized(state);
445
+ }
446
+ } else if (isDraftable(value)) {
447
+ handleValue(value, handledSet, rootScope);
448
+ }
449
+ });
450
+ return target;
451
+ }
404
452
 
405
453
  // src/core/proxy.ts
406
454
  function createProxyProxy(base, parent) {
407
- const isArray = Array.isArray(base);
455
+ const baseIsArray = isArray(base);
408
456
  const state = {
409
- type_: isArray ? 1 /* Array */ : 0 /* Object */,
457
+ type_: baseIsArray ? 1 /* Array */ : 0 /* Object */,
410
458
  // Track which produce call this is associated with.
411
459
  scope_: parent ? parent.scope_ : getCurrentScope(),
412
460
  // True for both shallow and deep changes.
@@ -414,7 +462,8 @@ function createProxyProxy(base, parent) {
414
462
  // Used during finalization.
415
463
  finalized_: false,
416
464
  // Track which properties have been assigned (true) or deleted (false).
417
- assigned_: {},
465
+ // actually instantiated in `prepareCopy()`
466
+ assigned_: void 0,
418
467
  // The parent draft state.
419
468
  parent_: parent,
420
469
  // The base state.
@@ -426,25 +475,27 @@ function createProxyProxy(base, parent) {
426
475
  copy_: null,
427
476
  // Called by the `produce` function.
428
477
  revoke_: null,
429
- isManual_: false
478
+ isManual_: false,
479
+ // `callbacks` actually gets assigned in `createProxy`
480
+ callbacks_: void 0
430
481
  };
431
482
  let target = state;
432
483
  let traps = objectTraps;
433
- if (isArray) {
484
+ if (baseIsArray) {
434
485
  target = [state];
435
486
  traps = arrayTraps;
436
487
  }
437
488
  const { revoke, proxy } = Proxy.revocable(target, traps);
438
489
  state.draft_ = proxy;
439
490
  state.revoke_ = revoke;
440
- return proxy;
491
+ return [proxy, state];
441
492
  }
442
493
  var objectTraps = {
443
494
  get(state, prop) {
444
495
  if (prop === DRAFT_STATE)
445
496
  return state;
446
497
  const source = latest(state);
447
- if (!has(source, prop)) {
498
+ if (!has(source, prop, state.type_)) {
448
499
  return readPropFromProto(state, source, prop);
449
500
  }
450
501
  const value = source[prop];
@@ -453,7 +504,9 @@ var objectTraps = {
453
504
  }
454
505
  if (value === peek(state.base_, prop)) {
455
506
  prepareCopy(state);
456
- return state.copy_[prop] = createProxy(value, state);
507
+ const childKey = state.type_ === 1 /* Array */ ? +prop : prop;
508
+ const childDraft = createProxy(state.scope_, value, state, childKey);
509
+ return state.copy_[childKey] = childDraft;
457
510
  }
458
511
  return value;
459
512
  },
@@ -474,10 +527,10 @@ var objectTraps = {
474
527
  const currentState = current2?.[DRAFT_STATE];
475
528
  if (currentState && currentState.base_ === value) {
476
529
  state.copy_[prop] = value;
477
- state.assigned_[prop] = false;
530
+ state.assigned_.set(prop, false);
478
531
  return true;
479
532
  }
480
- if (is(value, current2) && (value !== void 0 || has(state.base_, prop)))
533
+ if (is(value, current2) && (value !== void 0 || has(state.base_, prop, state.type_)))
481
534
  return true;
482
535
  prepareCopy(state);
483
536
  markChanged(state);
@@ -487,16 +540,17 @@ var objectTraps = {
487
540
  Number.isNaN(value) && Number.isNaN(state.copy_[prop]))
488
541
  return true;
489
542
  state.copy_[prop] = value;
490
- state.assigned_[prop] = true;
543
+ state.assigned_.set(prop, true);
544
+ handleCrossReference(state, prop, value);
491
545
  return true;
492
546
  },
493
547
  deleteProperty(state, prop) {
548
+ prepareCopy(state);
494
549
  if (peek(state.base_, prop) !== void 0 || prop in state.base_) {
495
- state.assigned_[prop] = false;
496
- prepareCopy(state);
550
+ state.assigned_.set(prop, false);
497
551
  markChanged(state);
498
552
  } else {
499
- delete state.assigned_[prop];
553
+ state.assigned_.delete(prop);
500
554
  }
501
555
  if (state.copy_) {
502
556
  delete state.copy_[prop];
@@ -511,10 +565,10 @@ var objectTraps = {
511
565
  if (!desc)
512
566
  return desc;
513
567
  return {
514
- writable: true,
515
- configurable: state.type_ !== 1 /* Array */ || prop !== "length",
516
- enumerable: desc.enumerable,
517
- value: owner[prop]
568
+ [WRITABLE]: true,
569
+ [CONFIGURABLE]: state.type_ !== 1 /* Array */ || prop !== "length",
570
+ [ENUMERABLE]: desc[ENUMERABLE],
571
+ [VALUE]: owner[prop]
518
572
  };
519
573
  },
520
574
  defineProperty() {
@@ -530,8 +584,9 @@ var objectTraps = {
530
584
  var arrayTraps = {};
531
585
  each(objectTraps, (key, fn) => {
532
586
  arrayTraps[key] = function() {
533
- arguments[0] = arguments[0][0];
534
- return fn.apply(this, arguments);
587
+ const args = arguments;
588
+ args[0] = args[0][0];
589
+ return fn.apply(this, args);
535
590
  };
536
591
  });
537
592
  arrayTraps.deleteProperty = function(state, prop) {
@@ -551,7 +606,7 @@ function peek(draft, prop) {
551
606
  }
552
607
  function readPropFromProto(state, source, prop) {
553
608
  const desc = getDescriptorFromProto(source, prop);
554
- return desc ? `value` in desc ? desc.value : (
609
+ return desc ? VALUE in desc ? desc[VALUE] : (
555
610
  // This is a very special case, if the prop is a getter defined by the
556
611
  // prototype, we should invoke it with the draft as context!
557
612
  desc.get?.call(state.draft_)
@@ -579,6 +634,7 @@ function markChanged(state) {
579
634
  }
580
635
  function prepareCopy(state) {
581
636
  if (!state.copy_) {
637
+ state.assigned_ = /* @__PURE__ */ new Map();
582
638
  state.copy_ = shallowCopy(
583
639
  state.base_,
584
640
  state.scope_.immer_.useStrictShallowCopy_
@@ -591,7 +647,7 @@ var Immer2 = class {
591
647
  constructor(config) {
592
648
  this.autoFreeze_ = true;
593
649
  this.useStrictShallowCopy_ = false;
594
- this.useStrictIteration_ = true;
650
+ this.useStrictIteration_ = false;
595
651
  /**
596
652
  * The `produce` function takes a value and a "recipe function" (whose
597
653
  * return value often depends on the base state). The recipe function is
@@ -612,7 +668,7 @@ var Immer2 = class {
612
668
  * @returns {any} a new state, or the initial state if nothing was modified
613
669
  */
614
670
  this.produce = (base, recipe, patchListener) => {
615
- if (typeof base === "function" && typeof recipe !== "function") {
671
+ if (isFunction(base) && !isFunction(recipe)) {
616
672
  const defaultBase = recipe;
617
673
  recipe = base;
618
674
  const self = this;
@@ -620,14 +676,14 @@ var Immer2 = class {
620
676
  return self.produce(base2, (draft) => recipe.call(this, draft, ...args));
621
677
  };
622
678
  }
623
- if (typeof recipe !== "function")
679
+ if (!isFunction(recipe))
624
680
  die(6);
625
- if (patchListener !== void 0 && typeof patchListener !== "function")
681
+ if (patchListener !== void 0 && !isFunction(patchListener))
626
682
  die(7);
627
683
  let result;
628
684
  if (isDraftable(base)) {
629
685
  const scope = enterScope(this);
630
- const proxy = createProxy(base, void 0);
686
+ const proxy = createProxy(scope, base, void 0);
631
687
  let hasError = true;
632
688
  try {
633
689
  result = recipe(proxy);
@@ -640,7 +696,7 @@ var Immer2 = class {
640
696
  }
641
697
  usePatchesInScope(scope, patchListener);
642
698
  return processResult(result, scope);
643
- } else if (!base || typeof base !== "object") {
699
+ } else if (!base || !isObjectish(base)) {
644
700
  result = recipe(base);
645
701
  if (result === void 0)
646
702
  result = base;
@@ -651,7 +707,10 @@ var Immer2 = class {
651
707
  if (patchListener) {
652
708
  const p = [];
653
709
  const ip = [];
654
- getPlugin("Patches").generateReplacementPatches_(base, result, p, ip);
710
+ getPlugin(PluginPatches).generateReplacementPatches_(base, result, {
711
+ patches_: p,
712
+ inversePatches_: ip
713
+ });
655
714
  patchListener(p, ip);
656
715
  }
657
716
  return result;
@@ -659,7 +718,7 @@ var Immer2 = class {
659
718
  die(1, base);
660
719
  };
661
720
  this.produceWithPatches = (base, recipe) => {
662
- if (typeof base === "function") {
721
+ if (isFunction(base)) {
663
722
  return (state, ...args) => this.produceWithPatches(state, (draft) => base(draft, ...args));
664
723
  }
665
724
  let patches, inversePatches;
@@ -669,11 +728,11 @@ var Immer2 = class {
669
728
  });
670
729
  return [result, patches, inversePatches];
671
730
  };
672
- if (typeof config?.autoFreeze === "boolean")
731
+ if (isBoolean(config?.autoFreeze))
673
732
  this.setAutoFreeze(config.autoFreeze);
674
- if (typeof config?.useStrictShallowCopy === "boolean")
733
+ if (isBoolean(config?.useStrictShallowCopy))
675
734
  this.setUseStrictShallowCopy(config.useStrictShallowCopy);
676
- if (typeof config?.useStrictIteration === "boolean")
735
+ if (isBoolean(config?.useStrictIteration))
677
736
  this.setUseStrictIteration(config.useStrictIteration);
678
737
  }
679
738
  createDraft(base) {
@@ -682,7 +741,7 @@ var Immer2 = class {
682
741
  if (isDraft(base))
683
742
  base = current(base);
684
743
  const scope = enterScope(this);
685
- const proxy = createProxy(base, void 0);
744
+ const proxy = createProxy(scope, base, void 0);
686
745
  proxy[DRAFT_STATE].isManual_ = true;
687
746
  leaveScope(scope);
688
747
  return proxy;
@@ -735,7 +794,7 @@ var Immer2 = class {
735
794
  if (i > -1) {
736
795
  patches = patches.slice(i + 1);
737
796
  }
738
- const applyPatchesImpl = getPlugin("Patches").applyPatches_;
797
+ const applyPatchesImpl = getPlugin(PluginPatches).applyPatches_;
739
798
  if (isDraft(base)) {
740
799
  return applyPatchesImpl(base, patches);
741
800
  }
@@ -745,10 +804,23 @@ var Immer2 = class {
745
804
  );
746
805
  }
747
806
  };
748
- function createProxy(value, parent) {
749
- const draft = isMap(value) ? getPlugin("MapSet").proxyMap_(value, parent) : isSet(value) ? getPlugin("MapSet").proxySet_(value, parent) : createProxyProxy(value, parent);
750
- const scope = parent ? parent.scope_ : getCurrentScope();
807
+ function createProxy(rootScope, value, parent, key) {
808
+ const [draft, state] = isMap(value) ? getPlugin(PluginMapSet).proxyMap_(value, parent) : isSet(value) ? getPlugin(PluginMapSet).proxySet_(value, parent) : createProxyProxy(value, parent);
809
+ const scope = parent?.scope_ ?? getCurrentScope();
751
810
  scope.drafts_.push(draft);
811
+ state.callbacks_ = parent?.callbacks_ ?? [];
812
+ state.key_ = key;
813
+ if (parent && key !== void 0) {
814
+ registerChildFinalizationCallback(parent, state, key);
815
+ } else {
816
+ state.callbacks_.push(function rootDraftCleanup(rootScope2) {
817
+ rootScope2.mapSetPlugin_?.fixSetContents(state);
818
+ const { patchPlugin_ } = rootScope2;
819
+ if (state.modified_ && patchPlugin_) {
820
+ patchPlugin_.generatePatches_(state, [], rootScope2);
821
+ }
822
+ });
823
+ }
752
824
  return draft;
753
825
  }
754
826
 
@@ -789,9 +861,7 @@ function currentImpl(value) {
789
861
  // src/immer.ts
790
862
  var immer = new Immer2();
791
863
  var produce = immer.produce;
792
- function castDraft(value) {
793
- return value;
794
- }
864
+ var castDraft = (value) => value;
795
865
 
796
866
  function buildElementNotations(nodes) {
797
867
  return t$9(
@@ -1279,4 +1349,4 @@ function calcDriftsFromSnapshot(autoLayouted, snapshot) {
1279
1349
  });
1280
1350
  }
1281
1351
 
1282
- export { applyManualLayout as a, t$1 as b, calcDriftsFromSnapshot as c, t$4 as d, t$5 as e, t$2 as f, buildElementNotations as g, t as h, n$5 as n, t$3 as t };
1352
+ export { applyManualLayout as a, t$2 as b, calcDriftsFromSnapshot as c, t$1 as d, t$4 as e, t$5 as f, buildElementNotations as g, t as h, n$5 as n, t$3 as t };