tutuca 0.15.0 → 1.0.0-beta.2

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 (170) hide show
  1. package/CHANGELOG.md +132 -0
  2. package/LICENSE +1 -1
  3. package/README.md +64 -170
  4. package/{dist → deps}/immer.js +674 -249
  5. package/dist/tutuca.js +2342 -4057
  6. package/dist/tutuca.min.js +3 -3
  7. package/package.json +78 -96
  8. package/src/component.js +66 -0
  9. package/src/css/classes.js +77 -0
  10. package/src/dev/formatters.js +393 -0
  11. package/src/dev/jsonml.js +35 -0
  12. package/src/dev/panel.js +400 -0
  13. package/src/dev/summary.js +239 -0
  14. package/src/dev/vdom-formatters.js +102 -0
  15. package/src/drag.js +23 -0
  16. package/src/embed/element.js +111 -0
  17. package/src/env.js +30 -0
  18. package/src/events.js +85 -0
  19. package/src/history/bar.js +198 -0
  20. package/src/history/index.js +149 -0
  21. package/src/host.js +360 -0
  22. package/src/lint/index.js +470 -0
  23. package/src/meta.js +12 -0
  24. package/src/optimize.js +58 -0
  25. package/src/paths.js +205 -0
  26. package/src/playground/element.js +360 -0
  27. package/src/playground/index.js +5 -0
  28. package/src/playground/sources.js +107 -0
  29. package/src/playground/ui/playground.js +287 -0
  30. package/src/schema.js +460 -0
  31. package/src/scope.js +92 -0
  32. package/src/storybook/index.js +80 -0
  33. package/src/storybook/runner.js +452 -0
  34. package/src/storybook/stories.js +208 -0
  35. package/src/storybook/stress.js +76 -0
  36. package/src/storybook/ui/common.js +37 -0
  37. package/src/storybook/ui/link.js +80 -0
  38. package/src/storybook/ui/panels.js +32 -0
  39. package/src/storybook/ui/story-panel.js +432 -0
  40. package/src/storybook/ui/storybook.js +191 -0
  41. package/src/storybook/ui/stress-panel.js +231 -0
  42. package/src/sync/automerge.js +159 -0
  43. package/src/sync/codec.js +206 -0
  44. package/src/sync/diff.js +153 -0
  45. package/src/sync/index.js +97 -0
  46. package/src/sync/migrate.js +184 -0
  47. package/src/sync/ops.js +160 -0
  48. package/src/sync/remote.js +206 -0
  49. package/src/tag.js +24 -0
  50. package/src/tasks.js +244 -0
  51. package/src/test/driver.js +141 -0
  52. package/src/test/pbt.js +589 -0
  53. package/src/tutuca.js +66 -0
  54. package/src/vdom.js +349 -0
  55. package/src/view.js +354 -0
  56. package/src/walk.js +79 -0
  57. package/dist/chai.js +0 -4210
  58. package/dist/tutuca-cli.js +0 -16315
  59. package/dist/tutuca-components.js +0 -2232
  60. package/dist/tutuca-dev.ext.js +0 -8476
  61. package/dist/tutuca-dev.js +0 -13220
  62. package/dist/tutuca-dev.min.js +0 -5
  63. package/dist/tutuca-extra.ext.js +0 -4261
  64. package/dist/tutuca-extra.js +0 -4964
  65. package/dist/tutuca-extra.min.js +0 -3
  66. package/dist/tutuca-storybook.js +0 -970
  67. package/dist/tutuca.ext.js +0 -4183
  68. package/skill/margaui/SKILL.md +0 -105
  69. package/skill/margaui/components/accordion.md +0 -127
  70. package/skill/margaui/components/alert.md +0 -174
  71. package/skill/margaui/components/aura.md +0 -97
  72. package/skill/margaui/components/avatar.md +0 -220
  73. package/skill/margaui/components/badge.md +0 -193
  74. package/skill/margaui/components/breadcrumbs.md +0 -103
  75. package/skill/margaui/components/button.md +0 -322
  76. package/skill/margaui/components/calendar.md +0 -67
  77. package/skill/margaui/components/card.md +0 -373
  78. package/skill/margaui/components/carousel.md +0 -387
  79. package/skill/margaui/components/chat.md +0 -171
  80. package/skill/margaui/components/checkbox.md +0 -101
  81. package/skill/margaui/components/collapse.md +0 -172
  82. package/skill/margaui/components/countdown.md +0 -165
  83. package/skill/margaui/components/diff.md +0 -53
  84. package/skill/margaui/components/divider.md +0 -107
  85. package/skill/margaui/components/dock.md +0 -173
  86. package/skill/margaui/components/drawer.md +0 -184
  87. package/skill/margaui/components/dropdown.md +0 -388
  88. package/skill/margaui/components/fab.md +0 -346
  89. package/skill/margaui/components/fieldset.md +0 -88
  90. package/skill/margaui/components/file-input.md +0 -84
  91. package/skill/margaui/components/filter.md +0 -52
  92. package/skill/margaui/components/footer.md +0 -583
  93. package/skill/margaui/components/hero.md +0 -135
  94. package/skill/margaui/components/hover-3d.md +0 -129
  95. package/skill/margaui/components/hover-gallery.md +0 -49
  96. package/skill/margaui/components/indicator.md +0 -265
  97. package/skill/margaui/components/input.md +0 -389
  98. package/skill/margaui/components/join.md +0 -100
  99. package/skill/margaui/components/kbd.md +0 -127
  100. package/skill/margaui/components/label.md +0 -102
  101. package/skill/margaui/components/link.md +0 -96
  102. package/skill/margaui/components/list.md +0 -182
  103. package/skill/margaui/components/loading.md +0 -105
  104. package/skill/margaui/components/mask.md +0 -168
  105. package/skill/margaui/components/megamenu.md +0 -131
  106. package/skill/margaui/components/menu.md +0 -887
  107. package/skill/margaui/components/mockup-browser.md +0 -39
  108. package/skill/margaui/components/mockup-code.md +0 -81
  109. package/skill/margaui/components/mockup-phone.md +0 -39
  110. package/skill/margaui/components/mockup-window.md +0 -33
  111. package/skill/margaui/components/modal.md +0 -196
  112. package/skill/margaui/components/navbar.md +0 -282
  113. package/skill/margaui/components/otp.md +0 -171
  114. package/skill/margaui/components/pagination.md +0 -122
  115. package/skill/margaui/components/progress.md +0 -135
  116. package/skill/margaui/components/radial-progress.md +0 -67
  117. package/skill/margaui/components/radio.md +0 -133
  118. package/skill/margaui/components/range.md +0 -134
  119. package/skill/margaui/components/rating.md +0 -170
  120. package/skill/margaui/components/select.md +0 -225
  121. package/skill/margaui/components/skeleton.md +0 -64
  122. package/skill/margaui/components/stack.md +0 -142
  123. package/skill/margaui/components/stat.md +0 -254
  124. package/skill/margaui/components/status.md +0 -73
  125. package/skill/margaui/components/steps.md +0 -138
  126. package/skill/margaui/components/swap.md +0 -152
  127. package/skill/margaui/components/tab.md +0 -248
  128. package/skill/margaui/components/table.md +0 -1018
  129. package/skill/margaui/components/text-rotate.md +0 -91
  130. package/skill/margaui/components/textarea.md +0 -85
  131. package/skill/margaui/components/theme-controller.md +0 -266
  132. package/skill/margaui/components/timeline.md +0 -1356
  133. package/skill/margaui/components/toast.md +0 -165
  134. package/skill/margaui/components/toggle.md +0 -135
  135. package/skill/margaui/components/tooltip.md +0 -181
  136. package/skill/margaui/components/validator.md +0 -163
  137. package/skill/tutuca/SKILL.md +0 -56
  138. package/skill/tutuca/advanced.md +0 -292
  139. package/skill/tutuca/cli.md +0 -239
  140. package/skill/tutuca/component-design.md +0 -168
  141. package/skill/tutuca/core.md +0 -935
  142. package/skill/tutuca/iteration.md +0 -207
  143. package/skill/tutuca/macros.md +0 -86
  144. package/skill/tutuca/margaui.md +0 -175
  145. package/skill/tutuca/messages-and-intents.md +0 -414
  146. package/skill/tutuca/patterns/README.md +0 -48
  147. package/skill/tutuca/patterns/add-a-story.md +0 -26
  148. package/skill/tutuca/patterns/bind-text-and-attributes.md +0 -30
  149. package/skill/tutuca/patterns/conditional-attribute-value.md +0 -29
  150. package/skill/tutuca/patterns/coordinate-components.md +0 -54
  151. package/skill/tutuca/patterns/edit-through-a-dynamic-target.md +0 -32
  152. package/skill/tutuca/patterns/enrich-each-item.md +0 -25
  153. package/skill/tutuca/patterns/file-input.md +0 -39
  154. package/skill/tutuca/patterns/filter-a-list.md +0 -25
  155. package/skill/tutuca/patterns/filter-and-paginate.md +0 -60
  156. package/skill/tutuca/patterns/handle-events.md +0 -45
  157. package/skill/tutuca/patterns/iterate-a-list.md +0 -18
  158. package/skill/tutuca/patterns/paginate-a-list.md +0 -29
  159. package/skill/tutuca/patterns/render-a-child-component.md +0 -21
  160. package/skill/tutuca/patterns/reuse-markup-with-macros.md +0 -36
  161. package/skill/tutuca/patterns/share-state-across-the-tree.md +0 -53
  162. package/skill/tutuca/patterns/show-or-hide-content.md +0 -23
  163. package/skill/tutuca/patterns/switch-between-views.md +0 -30
  164. package/skill/tutuca/patterns/tabbed-interface.md +0 -43
  165. package/skill/tutuca/semantics.md +0 -219
  166. package/skill/tutuca/storybook.md +0 -270
  167. package/skill/tutuca/styles.md +0 -48
  168. package/skill/tutuca/testing.md +0 -345
  169. package/skill/tutuca-source/SKILL.md +0 -33
  170. package/skill/tutuca-source/tutuca.ext.js +0 -4183
@@ -1,3 +1,27 @@
1
+ /*! immer 11.1.21 (https://github.com/immerjs/immer), vendored by scripts/vendor.js.
2
+ *
3
+ * MIT License
4
+ *
5
+ * Copyright (c) 2017 Michel Weststrate
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
1
25
  // node_modules/immer/dist/immer.mjs
2
26
  var NOTHING = /* @__PURE__ */ Symbol.for("immer-nothing");
3
27
  var DRAFTABLE = /* @__PURE__ */ Symbol.for("immer-draftable");
@@ -36,34 +60,39 @@ var errors = true ? [
36
60
  function die(error, ...args) {
37
61
  if (true) {
38
62
  const e = errors[error];
39
- const msg = typeof e === "function" ? e.apply(null, args) : e;
63
+ const msg = isFunction(e) ? e.apply(null, args) : e;
40
64
  throw new Error(`[Immer] ${msg}`);
41
65
  }
42
66
  throw new Error(
43
67
  `[Immer] minified error nr: ${error}. Full error at: https://bit.ly/3cXEKWf`
44
68
  );
45
69
  }
46
- var getPrototypeOf = Object.getPrototypeOf;
47
- function isDraft(value) {
48
- return !!value && !!value[DRAFT_STATE];
49
- }
70
+ var O = Object;
71
+ var getPrototypeOf = O.getPrototypeOf;
72
+ var CONSTRUCTOR = "constructor";
73
+ var PROTOTYPE = "prototype";
74
+ var CONFIGURABLE = "configurable";
75
+ var ENUMERABLE = "enumerable";
76
+ var WRITABLE = "writable";
77
+ var VALUE = "value";
78
+ var isDraft = (value) => !!value && !!value[DRAFT_STATE];
50
79
  function isDraftable(value) {
51
80
  if (!value)
52
81
  return false;
53
- return isPlainObject(value) || Array.isArray(value) || !!value[DRAFTABLE] || !!value.constructor?.[DRAFTABLE] || isMap(value) || isSet(value);
82
+ return isPlainObject(value) || isArray(value) || !!value[DRAFTABLE] || !!value[CONSTRUCTOR]?.[DRAFTABLE] || isMap(value) || isSet(value);
54
83
  }
55
- var objectCtorString = Object.prototype.constructor.toString();
84
+ var objectCtorString = O[PROTOTYPE][CONSTRUCTOR].toString();
56
85
  var cachedCtorStrings = /* @__PURE__ */ new WeakMap();
57
86
  function isPlainObject(value) {
58
- if (!value || typeof value !== "object")
87
+ if (!value || !isObjectish(value))
59
88
  return false;
60
- const proto = Object.getPrototypeOf(value);
61
- if (proto === null || proto === Object.prototype)
89
+ const proto = getPrototypeOf(value);
90
+ if (proto === null || proto === O[PROTOTYPE])
62
91
  return true;
63
- const Ctor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor;
92
+ const Ctor = O.hasOwnProperty.call(proto, CONSTRUCTOR) && proto[CONSTRUCTOR];
64
93
  if (Ctor === Object)
65
94
  return true;
66
- if (typeof Ctor !== "function")
95
+ if (!isFunction(Ctor))
67
96
  return false;
68
97
  let ctorString = cachedCtorStrings.get(Ctor);
69
98
  if (ctorString === void 0) {
@@ -79,7 +108,7 @@ function original(value) {
79
108
  }
80
109
  function each(obj, iter, strict = true) {
81
110
  if (getArchtype(obj) === 0) {
82
- const keys = strict ? Reflect.ownKeys(obj) : Object.keys(obj);
111
+ const keys = strict ? Reflect.ownKeys(obj) : O.keys(obj);
83
112
  keys.forEach((key) => {
84
113
  iter(key, obj[key], obj);
85
114
  });
@@ -89,23 +118,21 @@ function each(obj, iter, strict = true) {
89
118
  }
90
119
  function getArchtype(thing) {
91
120
  const state = thing[DRAFT_STATE];
92
- return state ? state.type_ : Array.isArray(thing) ? 1 : isMap(thing) ? 2 : isSet(thing) ? 3 : 0;
93
- }
94
- function has(thing, prop) {
95
- return getArchtype(thing) === 2 ? thing.has(prop) : Object.prototype.hasOwnProperty.call(thing, prop);
96
- }
97
- function get(thing, prop) {
98
- return getArchtype(thing) === 2 ? thing.get(prop) : thing[prop];
121
+ return state ? state.type_ : isArray(thing) ? 1 : isMap(thing) ? 2 : isSet(thing) ? 3 : 0;
99
122
  }
100
- function set(thing, propOrOldValue, value) {
101
- const t = getArchtype(thing);
102
- if (t === 2)
123
+ var has = (thing, prop, type = getArchtype(thing)) => type === 2 ? thing.has(prop) : O[PROTOTYPE].hasOwnProperty.call(thing, prop);
124
+ var get = (thing, prop, type = getArchtype(thing)) => (
125
+ // @ts-ignore
126
+ type === 2 ? thing.get(prop) : thing[prop]
127
+ );
128
+ var set = (thing, propOrOldValue, value, type = getArchtype(thing)) => {
129
+ if (type === 2)
103
130
  thing.set(propOrOldValue, value);
104
- else if (t === 3) {
131
+ else if (type === 3) {
105
132
  thing.add(value);
106
133
  } else
107
134
  thing[propOrOldValue] = value;
108
- }
135
+ };
109
136
  function is(x, y) {
110
137
  if (x === y) {
111
138
  return x !== 0 || 1 / x === 1 / y;
@@ -113,15 +140,27 @@ function is(x, y) {
113
140
  return x !== x && y !== y;
114
141
  }
115
142
  }
116
- function isMap(target) {
117
- return target instanceof Map;
118
- }
119
- function isSet(target) {
120
- return target instanceof Set;
121
- }
122
- function latest(state) {
123
- return state.copy_ || state.base_;
143
+ var isArray = Array.isArray;
144
+ var isMap = (target) => target instanceof Map;
145
+ var isSet = (target) => target instanceof Set;
146
+ var isObjectish = (target) => typeof target === "object";
147
+ var isFunction = (target) => typeof target === "function";
148
+ var isBoolean = (target) => typeof target === "boolean";
149
+ function isArrayIndex(value) {
150
+ const n = +value;
151
+ return Number.isInteger(n) && String(n) === value;
124
152
  }
153
+ var getProxyDraft = (value) => {
154
+ if (!isObjectish(value))
155
+ return null;
156
+ return value?.[DRAFT_STATE];
157
+ };
158
+ var latest = (state) => state.copy_ || state.base_;
159
+ var getValue = (value) => {
160
+ const proxyDraft = getProxyDraft(value);
161
+ return proxyDraft ? getFinalValue(proxyDraft) : value;
162
+ };
163
+ var getFinalValue = (state) => state.modified_ ? state.copy_ : state.base_;
125
164
  function shallowCopy(base, strict) {
126
165
  if (isMap(base)) {
127
166
  return new Map(base);
@@ -129,66 +168,75 @@ function shallowCopy(base, strict) {
129
168
  if (isSet(base)) {
130
169
  return new Set(base);
131
170
  }
132
- if (Array.isArray(base))
133
- return Array.prototype.slice.call(base);
171
+ if (isArray(base))
172
+ return Array[PROTOTYPE].slice.call(base);
134
173
  const isPlain = isPlainObject(base);
135
174
  if (strict === true || strict === "class_only" && !isPlain) {
136
- const descriptors = Object.getOwnPropertyDescriptors(base);
175
+ const descriptors = O.getOwnPropertyDescriptors(base);
137
176
  delete descriptors[DRAFT_STATE];
138
177
  let keys = Reflect.ownKeys(descriptors);
139
178
  for (let i = 0; i < keys.length; i++) {
140
179
  const key = keys[i];
141
180
  const desc = descriptors[key];
142
- if (desc.writable === false) {
143
- desc.writable = true;
144
- desc.configurable = true;
181
+ if (desc[WRITABLE] === false) {
182
+ desc[WRITABLE] = true;
183
+ desc[CONFIGURABLE] = true;
145
184
  }
146
185
  if (desc.get || desc.set)
147
186
  descriptors[key] = {
148
- configurable: true,
149
- writable: true,
187
+ [CONFIGURABLE]: true,
188
+ [WRITABLE]: true,
150
189
  // could live with !!desc.set as well here...
151
- enumerable: desc.enumerable,
152
- value: base[key]
190
+ [ENUMERABLE]: desc[ENUMERABLE],
191
+ [VALUE]: base[key]
153
192
  };
154
193
  }
155
- return Object.create(getPrototypeOf(base), descriptors);
194
+ return O.create(getPrototypeOf(base), descriptors);
156
195
  } else {
157
196
  const proto = getPrototypeOf(base);
158
197
  if (proto !== null && isPlain) {
159
198
  return { ...base };
160
199
  }
161
- const obj = Object.create(proto);
162
- return Object.assign(obj, base);
200
+ const obj = O.create(proto);
201
+ return O.assign(obj, base);
163
202
  }
164
203
  }
165
204
  function freeze(obj, deep = false) {
166
205
  if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj))
167
206
  return obj;
168
207
  if (getArchtype(obj) > 1) {
169
- Object.defineProperties(obj, {
208
+ O.defineProperties(obj, {
170
209
  set: dontMutateMethodOverride,
171
210
  add: dontMutateMethodOverride,
172
211
  clear: dontMutateMethodOverride,
173
212
  delete: dontMutateMethodOverride
174
213
  });
175
214
  }
176
- Object.freeze(obj);
215
+ O.freeze(obj);
177
216
  if (deep)
178
- Object.values(obj).forEach((value) => freeze(value, true));
217
+ each(
218
+ obj,
219
+ (_key, value) => {
220
+ freeze(value, true);
221
+ },
222
+ false
223
+ );
179
224
  return obj;
180
225
  }
181
226
  function dontMutateFrozenCollections() {
182
227
  die(2);
183
228
  }
184
229
  var dontMutateMethodOverride = {
185
- value: dontMutateFrozenCollections
230
+ [VALUE]: dontMutateFrozenCollections
186
231
  };
187
232
  function isFrozen(obj) {
188
- if (obj === null || typeof obj !== "object")
233
+ if (obj === null || !isObjectish(obj))
189
234
  return true;
190
- return Object.isFrozen(obj);
235
+ return O.isFrozen(obj);
191
236
  }
237
+ var PluginMapSet = "MapSet";
238
+ var PluginPatches = "Patches";
239
+ var PluginArrayMethods = "ArrayMethods";
192
240
  var plugins = {};
193
241
  function getPlugin(pluginKey) {
194
242
  const plugin = plugins[pluginKey];
@@ -197,28 +245,29 @@ function getPlugin(pluginKey) {
197
245
  }
198
246
  return plugin;
199
247
  }
248
+ var isPluginLoaded = (pluginKey) => !!plugins[pluginKey];
200
249
  function loadPlugin(pluginKey, implementation) {
201
250
  if (!plugins[pluginKey])
202
251
  plugins[pluginKey] = implementation;
203
252
  }
204
253
  var currentScope;
205
- function getCurrentScope() {
206
- return currentScope;
207
- }
208
- function createScope(parent_, immer_) {
209
- return {
210
- drafts_: [],
211
- parent_,
212
- immer_,
213
- // Whenever the modified draft contains a draft from another scope, we
214
- // need to prevent auto-freezing so the unowned draft can be finalized.
215
- canAutoFreeze_: true,
216
- unfinalizedDrafts_: 0
217
- };
218
- }
254
+ var getCurrentScope = () => currentScope;
255
+ var createScope = (parent_, immer_) => ({
256
+ drafts_: [],
257
+ parent_,
258
+ immer_,
259
+ // Whenever the modified draft contains a draft from another scope, we
260
+ // need to prevent auto-freezing so the unowned draft can be finalized.
261
+ canAutoFreeze_: true,
262
+ unfinalizedDrafts_: 0,
263
+ handledSet_: /* @__PURE__ */ new Set(),
264
+ processedForPatches_: /* @__PURE__ */ new Set(),
265
+ mapSetPlugin_: isPluginLoaded(PluginMapSet) ? getPlugin(PluginMapSet) : void 0,
266
+ arrayMethodsPlugin_: isPluginLoaded(PluginArrayMethods) ? getPlugin(PluginArrayMethods) : void 0
267
+ });
219
268
  function usePatchesInScope(scope, patchListener) {
220
269
  if (patchListener) {
221
- getPlugin("Patches");
270
+ scope.patchPlugin_ = getPlugin(PluginPatches);
222
271
  scope.patches_ = [];
223
272
  scope.inversePatches_ = [];
224
273
  scope.patchListener_ = patchListener;
@@ -234,9 +283,7 @@ function leaveScope(scope) {
234
283
  currentScope = scope.parent_;
235
284
  }
236
285
  }
237
- function enterScope(immer2) {
238
- return currentScope = createScope(currentScope, immer2);
239
- }
286
+ var enterScope = (immer2) => currentScope = createScope(currentScope, immer2);
240
287
  function revokeDraft(draft) {
241
288
  const state = draft[DRAFT_STATE];
242
289
  if (state.type_ === 0 || state.type_ === 1)
@@ -255,127 +302,170 @@ function processResult(result, scope) {
255
302
  }
256
303
  if (isDraftable(result)) {
257
304
  result = finalize(scope, result);
258
- if (!scope.parent_)
259
- maybeFreeze(scope, result);
260
305
  }
261
- if (scope.patches_) {
262
- getPlugin("Patches").generateReplacementPatches_(
306
+ const { patchPlugin_ } = scope;
307
+ if (patchPlugin_) {
308
+ patchPlugin_.generateReplacementPatches_(
263
309
  baseDraft[DRAFT_STATE].base_,
264
310
  result,
265
- scope.patches_,
266
- scope.inversePatches_
311
+ scope
267
312
  );
268
313
  }
269
314
  } else {
270
- result = finalize(scope, baseDraft, []);
315
+ result = finalize(scope, baseDraft);
271
316
  }
317
+ maybeFreeze(scope, result, true);
272
318
  revokeScope(scope);
273
319
  if (scope.patches_) {
274
320
  scope.patchListener_(scope.patches_, scope.inversePatches_);
275
321
  }
276
322
  return result !== NOTHING ? result : void 0;
277
323
  }
278
- function finalize(rootScope, value, path) {
324
+ function finalize(rootScope, value) {
279
325
  if (isFrozen(value))
280
326
  return value;
281
- const useStrictIteration = rootScope.immer_.shouldUseStrictIteration();
282
327
  const state = value[DRAFT_STATE];
283
328
  if (!state) {
284
- each(
285
- value,
286
- (key, childValue) => finalizeProperty(rootScope, state, value, key, childValue, path),
287
- useStrictIteration
288
- );
289
- return value;
329
+ const finalValue = handleValue(value, rootScope.handledSet_, rootScope);
330
+ return finalValue;
290
331
  }
291
- if (state.scope_ !== rootScope)
332
+ if (!isSameScope(state, rootScope)) {
292
333
  return value;
334
+ }
293
335
  if (!state.modified_) {
294
- maybeFreeze(rootScope, state.base_, true);
295
336
  return state.base_;
296
337
  }
297
338
  if (!state.finalized_) {
298
- state.finalized_ = true;
299
- state.scope_.unfinalizedDrafts_--;
300
- const result = state.copy_;
301
- let resultEach = result;
302
- let isSet2 = false;
303
- if (state.type_ === 3) {
304
- resultEach = new Set(result);
305
- result.clear();
306
- isSet2 = true;
307
- }
308
- each(
309
- resultEach,
310
- (key, childValue) => finalizeProperty(
311
- rootScope,
312
- state,
313
- result,
314
- key,
315
- childValue,
316
- path,
317
- isSet2
318
- ),
319
- useStrictIteration
320
- );
321
- maybeFreeze(rootScope, result, false);
322
- if (path && rootScope.patches_) {
323
- getPlugin("Patches").generatePatches_(
324
- state,
325
- path,
326
- rootScope.patches_,
327
- rootScope.inversePatches_
328
- );
339
+ const { callbacks_ } = state;
340
+ if (callbacks_) {
341
+ while (callbacks_.length > 0) {
342
+ const callback = callbacks_.pop();
343
+ callback(rootScope);
344
+ }
329
345
  }
346
+ generatePatchesAndFinalize(state, rootScope);
330
347
  }
331
348
  return state.copy_;
332
349
  }
333
- function finalizeProperty(rootScope, parentState, targetObject, prop, childValue, rootPath, targetIsSet) {
334
- if (childValue == null) {
335
- return;
336
- }
337
- if (typeof childValue !== "object" && !targetIsSet) {
338
- return;
339
- }
340
- const childIsFrozen = isFrozen(childValue);
341
- if (childIsFrozen && !targetIsSet) {
342
- return;
343
- }
344
- if (childValue === targetObject)
345
- die(5);
346
- if (isDraft(childValue)) {
347
- const path = rootPath && parentState && parentState.type_ !== 3 && // Set objects are atomic since they have no keys.
348
- !has(parentState.assigned_, prop) ? rootPath.concat(prop) : void 0;
349
- const res = finalize(rootScope, childValue, path);
350
- set(targetObject, prop, res);
351
- if (isDraft(res)) {
352
- rootScope.canAutoFreeze_ = false;
353
- } else
354
- return;
355
- } else if (targetIsSet) {
356
- targetObject.add(childValue);
350
+ function maybeFreeze(scope, value, deep = false) {
351
+ if (!scope.parent_ && scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {
352
+ freeze(value, deep);
357
353
  }
358
- if (isDraftable(childValue) && !childIsFrozen) {
359
- if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) {
354
+ }
355
+ function markStateFinalized(state) {
356
+ state.finalized_ = true;
357
+ state.scope_.unfinalizedDrafts_--;
358
+ }
359
+ var isSameScope = (state, rootScope) => state.scope_ === rootScope;
360
+ var EMPTY_LOCATIONS_RESULT = [];
361
+ function updateDraftInParent(parent, draftValue, finalizedValue, originalKey) {
362
+ const parentCopy = latest(parent);
363
+ const parentType = parent.type_;
364
+ if (originalKey !== void 0) {
365
+ const currentValue = get(parentCopy, originalKey, parentType);
366
+ if (currentValue === draftValue) {
367
+ set(parentCopy, originalKey, finalizedValue, parentType);
360
368
  return;
361
369
  }
362
- if (parentState && parentState.base_ && parentState.base_[prop] === childValue && childIsFrozen) {
370
+ }
371
+ if (!parent.draftLocations_) {
372
+ const draftLocations = parent.draftLocations_ = /* @__PURE__ */ new Map();
373
+ each(parentCopy, (key, value) => {
374
+ if (isDraft(value)) {
375
+ const keys = draftLocations.get(value) || [];
376
+ keys.push(key);
377
+ draftLocations.set(value, keys);
378
+ }
379
+ });
380
+ }
381
+ const locations = parent.draftLocations_.get(draftValue) ?? EMPTY_LOCATIONS_RESULT;
382
+ for (const location of locations) {
383
+ set(parentCopy, location, finalizedValue, parentType);
384
+ }
385
+ }
386
+ function registerChildFinalizationCallback(parent, child, key) {
387
+ parent.callbacks_.push(function childCleanup(rootScope) {
388
+ const state = child;
389
+ if (!state || !isSameScope(state, rootScope)) {
363
390
  return;
364
391
  }
365
- finalize(rootScope, childValue);
366
- if ((!parentState || !parentState.scope_.parent_) && typeof prop !== "symbol" && (isMap(targetObject) ? targetObject.has(prop) : Object.prototype.propertyIsEnumerable.call(targetObject, prop)))
367
- maybeFreeze(rootScope, childValue);
392
+ rootScope.mapSetPlugin_?.fixSetContents(state);
393
+ const finalizedValue = getFinalValue(state);
394
+ updateDraftInParent(parent, state.draft_ ?? state, finalizedValue, key);
395
+ generatePatchesAndFinalize(state, rootScope);
396
+ });
397
+ }
398
+ function generatePatchesAndFinalize(state, rootScope) {
399
+ const shouldFinalize = state.modified_ && !state.finalized_ && (state.type_ === 3 || state.type_ === 1 && state.allIndicesReassigned_ || (state.assigned_?.size ?? 0) > 0);
400
+ if (shouldFinalize) {
401
+ const { patchPlugin_ } = rootScope;
402
+ if (patchPlugin_) {
403
+ const basePath = patchPlugin_.getPath(state);
404
+ if (basePath) {
405
+ patchPlugin_.generatePatches_(state, basePath, rootScope);
406
+ }
407
+ }
408
+ markStateFinalized(state);
368
409
  }
369
410
  }
370
- function maybeFreeze(scope, value, deep = false) {
371
- if (!scope.parent_ && scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {
372
- freeze(value, deep);
411
+ function handleCrossReference(target, key, value) {
412
+ const { scope_ } = target;
413
+ if (isDraft(value)) {
414
+ const state = value[DRAFT_STATE];
415
+ if (isSameScope(state, scope_)) {
416
+ state.callbacks_.push(function crossReferenceCleanup() {
417
+ prepareCopy(target);
418
+ const finalizedValue = getFinalValue(state);
419
+ updateDraftInParent(target, value, finalizedValue, key);
420
+ });
421
+ }
422
+ } else if (isDraftable(value)) {
423
+ target.callbacks_.push(function nestedDraftCleanup() {
424
+ const targetCopy = latest(target);
425
+ if (target.type_ === 3) {
426
+ if (targetCopy.has(value)) {
427
+ handleValue(value, scope_.handledSet_, scope_);
428
+ }
429
+ } else {
430
+ if (get(targetCopy, key, target.type_) === value) {
431
+ if (scope_.drafts_.length > 1 && (target.assigned_.get(key) ?? false) === true && target.copy_) {
432
+ handleValue(
433
+ get(target.copy_, key, target.type_),
434
+ scope_.handledSet_,
435
+ scope_
436
+ );
437
+ }
438
+ }
439
+ }
440
+ });
373
441
  }
374
442
  }
443
+ function handleValue(target, handledSet, rootScope) {
444
+ if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) {
445
+ return target;
446
+ }
447
+ if (isDraft(target) || handledSet.has(target) || !isDraftable(target) || isFrozen(target)) {
448
+ return target;
449
+ }
450
+ handledSet.add(target);
451
+ each(target, (key, value) => {
452
+ if (isDraft(value)) {
453
+ const state = value[DRAFT_STATE];
454
+ if (isSameScope(state, rootScope)) {
455
+ const updatedValue = getFinalValue(state);
456
+ set(target, key, updatedValue, target.type_);
457
+ markStateFinalized(state);
458
+ }
459
+ } else if (isDraftable(value)) {
460
+ handleValue(value, handledSet, rootScope);
461
+ }
462
+ });
463
+ return target;
464
+ }
375
465
  function createProxyProxy(base, parent) {
376
- const isArray = Array.isArray(base);
466
+ const baseIsArray = isArray(base);
377
467
  const state = {
378
- type_: isArray ? 1 : 0,
468
+ type_: baseIsArray ? 1 : 0,
379
469
  // Track which produce call this is associated with.
380
470
  scope_: parent ? parent.scope_ : getCurrentScope(),
381
471
  // True for both shallow and deep changes.
@@ -383,7 +473,8 @@ function createProxyProxy(base, parent) {
383
473
  // Used during finalization.
384
474
  finalized_: false,
385
475
  // Track which properties have been assigned (true) or deleted (false).
386
- assigned_: {},
476
+ // actually instantiated in `prepareCopy()`
477
+ assigned_: void 0,
387
478
  // The parent draft state.
388
479
  parent_: parent,
389
480
  // The base state.
@@ -395,34 +486,50 @@ function createProxyProxy(base, parent) {
395
486
  copy_: null,
396
487
  // Called by the `produce` function.
397
488
  revoke_: null,
398
- isManual_: false
489
+ isManual_: false,
490
+ // `callbacks` actually gets assigned in `createProxy`
491
+ callbacks_: void 0
399
492
  };
400
493
  let target = state;
401
494
  let traps = objectTraps;
402
- if (isArray) {
495
+ if (baseIsArray) {
403
496
  target = [state];
404
497
  traps = arrayTraps;
405
498
  }
406
499
  const { revoke, proxy } = Proxy.revocable(target, traps);
407
500
  state.draft_ = proxy;
408
501
  state.revoke_ = revoke;
409
- return proxy;
502
+ return [proxy, state];
410
503
  }
411
504
  var objectTraps = {
412
505
  get(state, prop) {
413
506
  if (prop === DRAFT_STATE)
414
507
  return state;
508
+ let arrayPlugin = state.scope_.arrayMethodsPlugin_;
509
+ const isArrayWithStringProp = state.type_ === 1 && typeof prop === "string";
510
+ if (isArrayWithStringProp) {
511
+ if (arrayPlugin?.isArrayOperationMethod(prop)) {
512
+ return arrayPlugin.createMethodInterceptor(state, prop);
513
+ }
514
+ }
415
515
  const source = latest(state);
416
- if (!has(source, prop)) {
516
+ if (!has(source, prop, state.type_)) {
417
517
  return readPropFromProto(state, source, prop);
418
518
  }
419
519
  const value = source[prop];
420
520
  if (state.finalized_ || !isDraftable(value)) {
421
521
  return value;
422
522
  }
423
- if (value === peek(state.base_, prop)) {
523
+ if (isArrayWithStringProp && state.operationMethod && arrayPlugin?.isMutatingArrayMethod(
524
+ state.operationMethod
525
+ ) && isArrayIndex(prop)) {
526
+ return value;
527
+ }
528
+ if (value === peek(state.base_, prop) || isRelocatedBaseRef(state, prop, value)) {
424
529
  prepareCopy(state);
425
- return state.copy_[prop] = createProxy(value, state);
530
+ const childKey = state.type_ === 1 ? +prop : prop;
531
+ const childDraft = createProxy(state.scope_, value, state, childKey);
532
+ return state.copy_[childKey] = childDraft;
426
533
  }
427
534
  return value;
428
535
  },
@@ -443,29 +550,30 @@ var objectTraps = {
443
550
  const currentState = current2?.[DRAFT_STATE];
444
551
  if (currentState && currentState.base_ === value) {
445
552
  state.copy_[prop] = value;
446
- state.assigned_[prop] = false;
553
+ state.assigned_.delete(prop);
447
554
  return true;
448
555
  }
449
- if (is(value, current2) && (value !== void 0 || has(state.base_, prop)))
556
+ if (is(value, current2) && (value !== void 0 || has(state.base_, prop, state.type_)))
450
557
  return true;
451
558
  prepareCopy(state);
452
559
  markChanged(state);
453
560
  }
454
561
  if (state.copy_[prop] === value && // special case: handle new props with value 'undefined'
455
- (value !== void 0 || prop in state.copy_) || // special case: NaN
562
+ (value !== void 0 || has(state.copy_, prop, state.type_)) || // special case: NaN
456
563
  Number.isNaN(value) && Number.isNaN(state.copy_[prop]))
457
564
  return true;
458
565
  state.copy_[prop] = value;
459
- state.assigned_[prop] = true;
566
+ state.assigned_.set(prop, true);
567
+ handleCrossReference(state, prop, value);
460
568
  return true;
461
569
  },
462
570
  deleteProperty(state, prop) {
571
+ prepareCopy(state);
463
572
  if (peek(state.base_, prop) !== void 0 || prop in state.base_) {
464
- state.assigned_[prop] = false;
465
- prepareCopy(state);
573
+ state.assigned_.set(prop, false);
466
574
  markChanged(state);
467
575
  } else {
468
- delete state.assigned_[prop];
576
+ state.assigned_.delete(prop);
469
577
  }
470
578
  if (state.copy_) {
471
579
  delete state.copy_[prop];
@@ -480,10 +588,10 @@ var objectTraps = {
480
588
  if (!desc)
481
589
  return desc;
482
590
  return {
483
- writable: true,
484
- configurable: state.type_ !== 1 || prop !== "length",
485
- enumerable: desc.enumerable,
486
- value: owner[prop]
591
+ [WRITABLE]: true,
592
+ [CONFIGURABLE]: state.type_ !== 1 || prop !== "length",
593
+ [ENUMERABLE]: desc[ENUMERABLE],
594
+ [VALUE]: owner[prop]
487
595
  };
488
596
  },
489
597
  defineProperty() {
@@ -497,12 +605,14 @@ var objectTraps = {
497
605
  }
498
606
  };
499
607
  var arrayTraps = {};
500
- each(objectTraps, (key, fn) => {
608
+ for (let key in objectTraps) {
609
+ let fn = objectTraps[key];
501
610
  arrayTraps[key] = function() {
502
- arguments[0] = arguments[0][0];
503
- return fn.apply(this, arguments);
611
+ const args = arguments;
612
+ args[0] = args[0][0];
613
+ return fn.apply(this, args);
504
614
  };
505
- });
615
+ }
506
616
  arrayTraps.deleteProperty = function(state, prop) {
507
617
  if (isNaN(parseInt(prop)))
508
618
  die(13);
@@ -518,9 +628,15 @@ function peek(draft, prop) {
518
628
  const source = state ? latest(state) : draft;
519
629
  return source[prop];
520
630
  }
631
+ function isRelocatedBaseRef(state, prop, value) {
632
+ if (state.type_ !== 1 || !state.allIndicesReassigned_ || state.assigned_?.get(prop) || !isDraftable(value) || value[DRAFT_STATE]) {
633
+ return false;
634
+ }
635
+ return state.baseRefs_.has(value);
636
+ }
521
637
  function readPropFromProto(state, source, prop) {
522
638
  const desc = getDescriptorFromProto(source, prop);
523
- return desc ? `value` in desc ? desc.value : (
639
+ return desc ? VALUE in desc ? desc[VALUE] : (
524
640
  // This is a very special case, if the prop is a getter defined by the
525
641
  // prototype, we should invoke it with the draft as context!
526
642
  desc.get?.call(state.draft_)
@@ -548,6 +664,7 @@ function markChanged(state) {
548
664
  }
549
665
  function prepareCopy(state) {
550
666
  if (!state.copy_) {
667
+ state.assigned_ = /* @__PURE__ */ new Map();
551
668
  state.copy_ = shallowCopy(
552
669
  state.base_,
553
670
  state.scope_.immer_.useStrictShallowCopy_
@@ -558,9 +675,9 @@ var Immer2 = class {
558
675
  constructor(config) {
559
676
  this.autoFreeze_ = true;
560
677
  this.useStrictShallowCopy_ = false;
561
- this.useStrictIteration_ = true;
678
+ this.useStrictIteration_ = false;
562
679
  this.produce = (base, recipe, patchListener) => {
563
- if (typeof base === "function" && typeof recipe !== "function") {
680
+ if (isFunction(base) && !isFunction(recipe)) {
564
681
  const defaultBase = recipe;
565
682
  recipe = base;
566
683
  const self = this;
@@ -568,14 +685,14 @@ var Immer2 = class {
568
685
  return self.produce(base2, (draft) => recipe.call(this, draft, ...args));
569
686
  };
570
687
  }
571
- if (typeof recipe !== "function")
688
+ if (!isFunction(recipe))
572
689
  die(6);
573
- if (patchListener !== void 0 && typeof patchListener !== "function")
690
+ if (patchListener !== void 0 && !isFunction(patchListener))
574
691
  die(7);
575
692
  let result;
576
693
  if (isDraftable(base)) {
577
694
  const scope = enterScope(this);
578
- const proxy = createProxy(base, void 0);
695
+ const proxy = createProxy(scope, base, void 0);
579
696
  let hasError = true;
580
697
  try {
581
698
  result = recipe(proxy);
@@ -588,7 +705,7 @@ var Immer2 = class {
588
705
  }
589
706
  usePatchesInScope(scope, patchListener);
590
707
  return processResult(result, scope);
591
- } else if (!base || typeof base !== "object") {
708
+ } else if (!base || !isObjectish(base)) {
592
709
  result = recipe(base);
593
710
  if (result === void 0)
594
711
  result = base;
@@ -599,7 +716,10 @@ var Immer2 = class {
599
716
  if (patchListener) {
600
717
  const p = [];
601
718
  const ip = [];
602
- getPlugin("Patches").generateReplacementPatches_(base, result, p, ip);
719
+ getPlugin(PluginPatches).generateReplacementPatches_(base, result, {
720
+ patches_: p,
721
+ inversePatches_: ip
722
+ });
603
723
  patchListener(p, ip);
604
724
  }
605
725
  return result;
@@ -607,7 +727,7 @@ var Immer2 = class {
607
727
  die(1, base);
608
728
  };
609
729
  this.produceWithPatches = (base, recipe) => {
610
- if (typeof base === "function") {
730
+ if (isFunction(base)) {
611
731
  return (state, ...args) => this.produceWithPatches(state, (draft) => base(draft, ...args));
612
732
  }
613
733
  let patches, inversePatches;
@@ -617,11 +737,11 @@ var Immer2 = class {
617
737
  });
618
738
  return [result, patches, inversePatches];
619
739
  };
620
- if (typeof config?.autoFreeze === "boolean")
740
+ if (isBoolean(config?.autoFreeze))
621
741
  this.setAutoFreeze(config.autoFreeze);
622
- if (typeof config?.useStrictShallowCopy === "boolean")
742
+ if (isBoolean(config?.useStrictShallowCopy))
623
743
  this.setUseStrictShallowCopy(config.useStrictShallowCopy);
624
- if (typeof config?.useStrictIteration === "boolean")
744
+ if (isBoolean(config?.useStrictIteration))
625
745
  this.setUseStrictIteration(config.useStrictIteration);
626
746
  }
627
747
  createDraft(base) {
@@ -630,7 +750,7 @@ var Immer2 = class {
630
750
  if (isDraft(base))
631
751
  base = current(base);
632
752
  const scope = enterScope(this);
633
- const proxy = createProxy(base, void 0);
753
+ const proxy = createProxy(scope, base, void 0);
634
754
  proxy[DRAFT_STATE].isManual_ = true;
635
755
  leaveScope(scope);
636
756
  return proxy;
@@ -683,7 +803,7 @@ var Immer2 = class {
683
803
  if (i > -1) {
684
804
  patches = patches.slice(i + 1);
685
805
  }
686
- const applyPatchesImpl = getPlugin("Patches").applyPatches_;
806
+ const applyPatchesImpl = getPlugin(PluginPatches).applyPatches_;
687
807
  if (isDraft(base)) {
688
808
  return applyPatchesImpl(base, patches);
689
809
  }
@@ -693,10 +813,23 @@ var Immer2 = class {
693
813
  );
694
814
  }
695
815
  };
696
- function createProxy(value, parent) {
697
- const draft = isMap(value) ? getPlugin("MapSet").proxyMap_(value, parent) : isSet(value) ? getPlugin("MapSet").proxySet_(value, parent) : createProxyProxy(value, parent);
698
- const scope = parent ? parent.scope_ : getCurrentScope();
816
+ function createProxy(rootScope, value, parent, key) {
817
+ const [draft, state] = isMap(value) ? getPlugin(PluginMapSet).proxyMap_(value, parent) : isSet(value) ? getPlugin(PluginMapSet).proxySet_(value, parent) : createProxyProxy(value, parent);
818
+ const scope = parent?.scope_ ?? getCurrentScope();
699
819
  scope.drafts_.push(draft);
820
+ state.callbacks_ = parent?.callbacks_ ?? [];
821
+ state.key_ = key;
822
+ if (parent && key !== void 0) {
823
+ registerChildFinalizationCallback(parent, state, key);
824
+ } else {
825
+ state.callbacks_.push(function rootDraftCleanup(rootScope2) {
826
+ rootScope2.mapSetPlugin_?.fixSetContents(state);
827
+ const { patchPlugin_ } = rootScope2;
828
+ if (state.modified_ && patchPlugin_) {
829
+ patchPlugin_.generatePatches_(state, [], rootScope2);
830
+ }
831
+ });
832
+ }
700
833
  return draft;
701
834
  }
702
835
  function current(value) {
@@ -745,27 +878,86 @@ function enablePatches() {
745
878
  "Patching reserved attributes like __proto__, prototype and constructor is not allowed"
746
879
  );
747
880
  }
881
+ function getPath(state, path = []) {
882
+ if (state.key_ !== void 0) {
883
+ const parentCopy = state.parent_.copy_ ?? state.parent_.base_;
884
+ const proxyDraft = getProxyDraft(get(parentCopy, state.key_));
885
+ const valueAtKey = get(parentCopy, state.key_);
886
+ if (valueAtKey === void 0) {
887
+ return null;
888
+ }
889
+ if (valueAtKey !== state.draft_ && valueAtKey !== state.base_ && valueAtKey !== state.copy_) {
890
+ return null;
891
+ }
892
+ if (proxyDraft != null && proxyDraft.base_ !== state.base_) {
893
+ return null;
894
+ }
895
+ const isSet2 = state.parent_.type_ === 3;
896
+ let key;
897
+ if (isSet2) {
898
+ const setParent = state.parent_;
899
+ key = Array.from(setParent.drafts_.keys()).indexOf(state.key_);
900
+ } else {
901
+ key = state.key_;
902
+ }
903
+ if (!(isSet2 && parentCopy.size > key || has(parentCopy, key))) {
904
+ return null;
905
+ }
906
+ path.push(key);
907
+ }
908
+ if (state.parent_) {
909
+ return getPath(state.parent_, path);
910
+ }
911
+ path.reverse();
912
+ try {
913
+ resolvePath(state.copy_, path);
914
+ } catch (e) {
915
+ return null;
916
+ }
917
+ return path;
918
+ }
919
+ function resolvePath(base, path) {
920
+ let current2 = base;
921
+ for (let i = 0; i < path.length - 1; i++) {
922
+ const key = path[i];
923
+ current2 = get(current2, key);
924
+ if (!isObjectish(current2) || current2 === null) {
925
+ throw new Error(`Cannot resolve path at '${path.join("/")}'`);
926
+ }
927
+ }
928
+ return current2;
929
+ }
748
930
  const REPLACE = "replace";
749
931
  const ADD = "add";
750
932
  const REMOVE = "remove";
751
- function generatePatches_(state, basePath, patches, inversePatches) {
933
+ function generatePatches_(state, basePath, scope) {
934
+ if (state.scope_.processedForPatches_.has(state)) {
935
+ return;
936
+ }
937
+ state.scope_.processedForPatches_.add(state);
938
+ const { patches_, inversePatches_ } = scope;
752
939
  switch (state.type_) {
753
940
  case 0:
754
941
  case 2:
755
942
  return generatePatchesFromAssigned(
756
943
  state,
757
944
  basePath,
758
- patches,
759
- inversePatches
945
+ patches_,
946
+ inversePatches_
760
947
  );
761
948
  case 1:
762
- return generateArrayPatches(state, basePath, patches, inversePatches);
949
+ return generateArrayPatches(
950
+ state,
951
+ basePath,
952
+ patches_,
953
+ inversePatches_
954
+ );
763
955
  case 3:
764
956
  return generateSetPatches(
765
957
  state,
766
958
  basePath,
767
- patches,
768
- inversePatches
959
+ patches_,
960
+ inversePatches_
769
961
  );
770
962
  }
771
963
  }
@@ -777,20 +969,28 @@ function enablePatches() {
777
969
  [base_, copy_] = [copy_, base_];
778
970
  [patches, inversePatches] = [inversePatches, patches];
779
971
  }
972
+ const allReassigned = state.allIndicesReassigned_ === true;
780
973
  for (let i = 0; i < base_.length; i++) {
781
- if (assigned_[i] && copy_[i] !== base_[i]) {
974
+ const copiedItem = copy_[i];
975
+ const baseItem = base_[i];
976
+ const isAssigned = allReassigned || assigned_?.get(i.toString());
977
+ if (isAssigned && copiedItem !== baseItem) {
978
+ const childState = copiedItem?.[DRAFT_STATE];
979
+ if (childState && childState.modified_) {
980
+ continue;
981
+ }
782
982
  const path = basePath.concat([i]);
783
983
  patches.push({
784
984
  op: REPLACE,
785
985
  path,
786
986
  // Need to maybe clone it, as it can in fact be the original value
787
987
  // due to the base/copy inversion at the start of this function
788
- value: clonePatchValueIfNeeded(copy_[i])
988
+ value: clonePatchValueIfNeeded(copiedItem)
789
989
  });
790
990
  inversePatches.push({
791
991
  op: REPLACE,
792
992
  path,
793
- value: clonePatchValueIfNeeded(base_[i])
993
+ value: clonePatchValueIfNeeded(baseItem)
794
994
  });
795
995
  }
796
996
  }
@@ -813,15 +1013,17 @@ function enablePatches() {
813
1013
  }
814
1014
  }
815
1015
  function generatePatchesFromAssigned(state, basePath, patches, inversePatches) {
816
- const { base_, copy_ } = state;
1016
+ const { base_, copy_, type_ } = state;
817
1017
  each(state.assigned_, (key, assignedValue) => {
818
- const origValue = get(base_, key);
819
- const value = get(copy_, key);
1018
+ const origValue = get(base_, key, type_);
1019
+ const value = get(copy_, key, type_);
820
1020
  const op = !assignedValue ? REMOVE : has(base_, key) ? REPLACE : ADD;
821
1021
  if (origValue === value && op === REPLACE)
822
1022
  return;
823
1023
  const path = basePath.concat(key);
824
- patches.push(op === REMOVE ? { op, path } : { op, path, value });
1024
+ patches.push(
1025
+ op === REMOVE ? { op, path } : { op, path, value: clonePatchValueIfNeeded(value) }
1026
+ );
825
1027
  inversePatches.push(
826
1028
  op === ADD ? { op: REMOVE, path } : op === REMOVE ? { op: ADD, path, value: clonePatchValueIfNeeded(origValue) } : { op: REPLACE, path, value: clonePatchValueIfNeeded(origValue) }
827
1029
  );
@@ -864,13 +1066,14 @@ function enablePatches() {
864
1066
  i++;
865
1067
  });
866
1068
  }
867
- function generateReplacementPatches_(baseValue, replacement, patches, inversePatches) {
868
- patches.push({
1069
+ function generateReplacementPatches_(baseValue, replacement, scope) {
1070
+ const { patches_, inversePatches_ } = scope;
1071
+ patches_.push({
869
1072
  op: REPLACE,
870
1073
  path: [],
871
1074
  value: replacement === NOTHING ? void 0 : replacement
872
1075
  });
873
- inversePatches.push({
1076
+ inversePatches_.push({
874
1077
  op: REPLACE,
875
1078
  path: [],
876
1079
  value: baseValue
@@ -886,12 +1089,12 @@ function enablePatches() {
886
1089
  if (typeof p !== "string" && typeof p !== "number") {
887
1090
  p = "" + p;
888
1091
  }
889
- if ((parentType === 0 || parentType === 1) && (p === "__proto__" || p === "constructor"))
1092
+ if ((parentType === 0 || parentType === 1) && (p === "__proto__" || p === CONSTRUCTOR))
890
1093
  die(errorOffset + 3);
891
- if (typeof base === "function" && p === "prototype")
1094
+ if (isFunction(base) && p === PROTOTYPE)
892
1095
  die(errorOffset + 3);
893
1096
  base = get(base, p);
894
- if (typeof base !== "object")
1097
+ if (base === null || !isObjectish(base))
895
1098
  die(errorOffset + 2, path.join("/"));
896
1099
  }
897
1100
  const type = getArchtype(base);
@@ -938,7 +1141,7 @@ function enablePatches() {
938
1141
  function deepClonePatchValue(obj) {
939
1142
  if (!isDraftable(obj))
940
1143
  return obj;
941
- if (Array.isArray(obj))
1144
+ if (isArray(obj))
942
1145
  return obj.map(deepClonePatchValue);
943
1146
  if (isMap(obj))
944
1147
  return new Map(
@@ -959,12 +1162,15 @@ function enablePatches() {
959
1162
  } else
960
1163
  return obj;
961
1164
  }
962
- loadPlugin("Patches", {
1165
+ loadPlugin(PluginPatches, {
963
1166
  applyPatches_,
964
1167
  generatePatches_,
965
- generateReplacementPatches_
1168
+ generateReplacementPatches_,
1169
+ getPath
966
1170
  });
967
1171
  }
1172
+ var _globalIterator = globalThis.Iterator;
1173
+ var hasIteratorFrom = typeof _globalIterator?.from === "function";
968
1174
  function enableMapSet() {
969
1175
  class DraftMap extends Map {
970
1176
  constructor(target, parent) {
@@ -980,14 +1186,19 @@ function enableMapSet() {
980
1186
  base_: target,
981
1187
  draft_: this,
982
1188
  isManual_: false,
983
- revoked_: false
1189
+ revoked_: false,
1190
+ callbacks_: []
984
1191
  };
985
1192
  }
986
1193
  get size() {
987
- return latest(this[DRAFT_STATE]).size;
1194
+ const state = this[DRAFT_STATE];
1195
+ assertUnrevoked(state);
1196
+ return latest(state).size;
988
1197
  }
989
1198
  has(key) {
990
- return latest(this[DRAFT_STATE]).has(key);
1199
+ const state = this[DRAFT_STATE];
1200
+ assertUnrevoked(state);
1201
+ return latest(state).has(key);
991
1202
  }
992
1203
  set(key, value) {
993
1204
  const state = this[DRAFT_STATE];
@@ -998,6 +1209,7 @@ function enableMapSet() {
998
1209
  state.assigned_.set(key, true);
999
1210
  state.copy_.set(key, value);
1000
1211
  state.assigned_.set(key, true);
1212
+ handleCrossReference(state, key, value);
1001
1213
  }
1002
1214
  return this;
1003
1215
  }
@@ -1032,6 +1244,7 @@ function enableMapSet() {
1032
1244
  }
1033
1245
  forEach(cb, thisArg) {
1034
1246
  const state = this[DRAFT_STATE];
1247
+ assertUnrevoked(state);
1035
1248
  latest(state).forEach((_value, key, _map) => {
1036
1249
  cb.call(thisArg, this.get(key), key, this);
1037
1250
  });
@@ -1046,18 +1259,19 @@ function enableMapSet() {
1046
1259
  if (value !== state.base_.get(key)) {
1047
1260
  return value;
1048
1261
  }
1049
- const draft = createProxy(value, state);
1262
+ const draft = createProxy(state.scope_, value, state, key);
1050
1263
  prepareMapCopy(state);
1051
1264
  state.copy_.set(key, draft);
1052
1265
  return draft;
1053
1266
  }
1054
1267
  keys() {
1055
- return latest(this[DRAFT_STATE]).keys();
1268
+ const state = this[DRAFT_STATE];
1269
+ assertUnrevoked(state);
1270
+ return latest(state).keys();
1056
1271
  }
1057
1272
  values() {
1058
1273
  const iterator = this.keys();
1059
- return {
1060
- [Symbol.iterator]: () => this.values(),
1274
+ return iteratorFrom({
1061
1275
  next: () => {
1062
1276
  const r = iterator.next();
1063
1277
  if (r.done)
@@ -1068,12 +1282,11 @@ function enableMapSet() {
1068
1282
  value
1069
1283
  };
1070
1284
  }
1071
- };
1285
+ });
1072
1286
  }
1073
1287
  entries() {
1074
1288
  const iterator = this.keys();
1075
- return {
1076
- [Symbol.iterator]: () => this.entries(),
1289
+ return iteratorFrom({
1077
1290
  next: () => {
1078
1291
  const r = iterator.next();
1079
1292
  if (r.done)
@@ -1084,14 +1297,25 @@ function enableMapSet() {
1084
1297
  value: [r.value, value]
1085
1298
  };
1086
1299
  }
1087
- };
1300
+ });
1088
1301
  }
1089
1302
  [(DRAFT_STATE, Symbol.iterator)]() {
1090
1303
  return this.entries();
1091
1304
  }
1092
1305
  }
1306
+ function iteratorFrom(iterable) {
1307
+ if (hasIteratorFrom) {
1308
+ return _globalIterator.from(iterable);
1309
+ }
1310
+ const iterator = {
1311
+ ...iterable,
1312
+ [Symbol.iterator]: () => iterator
1313
+ };
1314
+ return iterator;
1315
+ }
1093
1316
  function proxyMap_(target, parent) {
1094
- return new DraftMap(target, parent);
1317
+ const map = new DraftMap(target, parent);
1318
+ return [map, map[DRAFT_STATE]];
1095
1319
  }
1096
1320
  function prepareMapCopy(state) {
1097
1321
  if (!state.copy_) {
@@ -1113,11 +1337,15 @@ function enableMapSet() {
1113
1337
  draft_: this,
1114
1338
  drafts_: /* @__PURE__ */ new Map(),
1115
1339
  revoked_: false,
1116
- isManual_: false
1340
+ isManual_: false,
1341
+ assigned_: void 0,
1342
+ callbacks_: []
1117
1343
  };
1118
1344
  }
1119
1345
  get size() {
1120
- return latest(this[DRAFT_STATE]).size;
1346
+ const state = this[DRAFT_STATE];
1347
+ assertUnrevoked(state);
1348
+ return latest(state).size;
1121
1349
  }
1122
1350
  has(value) {
1123
1351
  const state = this[DRAFT_STATE];
@@ -1138,6 +1366,7 @@ function enableMapSet() {
1138
1366
  prepareSetCopy(state);
1139
1367
  markChanged(state);
1140
1368
  state.copy_.add(value);
1369
+ handleCrossReference(state, value, value);
1141
1370
  }
1142
1371
  return this;
1143
1372
  }
@@ -1191,14 +1420,15 @@ function enableMapSet() {
1191
1420
  }
1192
1421
  }
1193
1422
  function proxySet_(target, parent) {
1194
- return new DraftSet(target, parent);
1423
+ const set2 = new DraftSet(target, parent);
1424
+ return [set2, set2[DRAFT_STATE]];
1195
1425
  }
1196
1426
  function prepareSetCopy(state) {
1197
1427
  if (!state.copy_) {
1198
1428
  state.copy_ = /* @__PURE__ */ new Set();
1199
1429
  state.base_.forEach((value) => {
1200
1430
  if (isDraftable(value)) {
1201
- const draft = createProxy(value, state);
1431
+ const draft = createProxy(state.scope_, value, state, value);
1202
1432
  state.drafts_.set(value, draft);
1203
1433
  state.copy_.add(draft);
1204
1434
  } else {
@@ -1211,32 +1441,226 @@ function enableMapSet() {
1211
1441
  if (state.revoked_)
1212
1442
  die(3, JSON.stringify(latest(state)));
1213
1443
  }
1214
- loadPlugin("MapSet", { proxyMap_, proxySet_ });
1444
+ function fixSetContents(target) {
1445
+ if (target.type_ === 3 && target.copy_) {
1446
+ const copy = new Set(target.copy_);
1447
+ target.copy_.clear();
1448
+ copy.forEach((value) => {
1449
+ target.copy_.add(getValue(value));
1450
+ });
1451
+ }
1452
+ }
1453
+ loadPlugin(PluginMapSet, { proxyMap_, proxySet_, fixSetContents });
1454
+ }
1455
+ function enableArrayMethods() {
1456
+ const SHIFTING_METHODS = /* @__PURE__ */ new Set(["shift", "unshift"]);
1457
+ const QUEUE_METHODS = /* @__PURE__ */ new Set(["push", "pop"]);
1458
+ const RESULT_RETURNING_METHODS = /* @__PURE__ */ new Set([
1459
+ ...QUEUE_METHODS,
1460
+ ...SHIFTING_METHODS
1461
+ ]);
1462
+ const REORDERING_METHODS = /* @__PURE__ */ new Set(["reverse", "sort"]);
1463
+ const MUTATING_METHODS = /* @__PURE__ */ new Set([
1464
+ ...RESULT_RETURNING_METHODS,
1465
+ ...REORDERING_METHODS,
1466
+ "splice"
1467
+ ]);
1468
+ const FIND_METHODS = /* @__PURE__ */ new Set(["find", "findLast"]);
1469
+ const NON_MUTATING_METHODS = /* @__PURE__ */ new Set([
1470
+ "filter",
1471
+ "slice",
1472
+ "concat",
1473
+ "flat",
1474
+ ...FIND_METHODS,
1475
+ "findIndex",
1476
+ "findLastIndex",
1477
+ "some",
1478
+ "every",
1479
+ "indexOf",
1480
+ "lastIndexOf",
1481
+ "includes",
1482
+ "join",
1483
+ "toString",
1484
+ "toLocaleString"
1485
+ ]);
1486
+ function isMutatingArrayMethod(method) {
1487
+ return MUTATING_METHODS.has(method);
1488
+ }
1489
+ function isNonMutatingArrayMethod(method) {
1490
+ return NON_MUTATING_METHODS.has(method);
1491
+ }
1492
+ function isArrayOperationMethod(method) {
1493
+ return isMutatingArrayMethod(method) || isNonMutatingArrayMethod(method);
1494
+ }
1495
+ function enterOperation(state, method) {
1496
+ state.operationMethod = method;
1497
+ }
1498
+ function exitOperation(state) {
1499
+ state.operationMethod = void 0;
1500
+ }
1501
+ function executeArrayMethod(state, operation, markLength = true) {
1502
+ prepareCopy(state);
1503
+ const result = operation();
1504
+ markArrayChanged(state, markLength);
1505
+ return result;
1506
+ }
1507
+ function markArrayChanged(state, markLength = true) {
1508
+ markChanged(state);
1509
+ if (markLength)
1510
+ state.assigned_.set("length", true);
1511
+ }
1512
+ function markAllIndicesReassigned(state) {
1513
+ state.allIndicesReassigned_ = true;
1514
+ state.baseRefs_ = new Set(state.base_);
1515
+ }
1516
+ function normalizeSliceIndex(index, length) {
1517
+ if (index < 0) {
1518
+ return Math.max(length + index, 0);
1519
+ }
1520
+ return Math.min(index, length);
1521
+ }
1522
+ function handleInsertedValues(state, startIndex, values) {
1523
+ for (let i = 0; i < values.length; i++) {
1524
+ const index = "" + (startIndex + i);
1525
+ state.assigned_.set(index, true);
1526
+ handleCrossReference(state, index, values[i]);
1527
+ }
1528
+ }
1529
+ function handleSimpleOperation(state, method, args) {
1530
+ const isInsert = method === "push" || method === "unshift";
1531
+ if (isInsert ? args.length === 0 : latest(state).length === 0) {
1532
+ return isInsert ? latest(state).length : void 0;
1533
+ }
1534
+ prepareCopy(state);
1535
+ const lengthBefore = state.copy_.length;
1536
+ const result = state.copy_[method](...args);
1537
+ markArrayChanged(state);
1538
+ if (SHIFTING_METHODS.has(method)) {
1539
+ markAllIndicesReassigned(state);
1540
+ }
1541
+ if (method === "push") {
1542
+ handleInsertedValues(state, lengthBefore, args);
1543
+ } else if (method === "unshift") {
1544
+ handleInsertedValues(state, 0, args);
1545
+ }
1546
+ return RESULT_RETURNING_METHODS.has(method) ? result : state.draft_;
1547
+ }
1548
+ function handleReorderingOperation(state, method, args) {
1549
+ if (latest(state).length <= 1)
1550
+ return state.draft_;
1551
+ return executeArrayMethod(
1552
+ state,
1553
+ () => {
1554
+ ;
1555
+ state.copy_[method](...args);
1556
+ markAllIndicesReassigned(state);
1557
+ return state.draft_;
1558
+ },
1559
+ false
1560
+ );
1561
+ }
1562
+ function createMethodInterceptor(state, originalMethod) {
1563
+ return function interceptedMethod(...args) {
1564
+ const method = originalMethod;
1565
+ enterOperation(state, method);
1566
+ try {
1567
+ if (isMutatingArrayMethod(method)) {
1568
+ if (RESULT_RETURNING_METHODS.has(method)) {
1569
+ return handleSimpleOperation(state, method, args);
1570
+ }
1571
+ if (REORDERING_METHODS.has(method)) {
1572
+ return handleReorderingOperation(state, method, args);
1573
+ }
1574
+ if (method === "splice") {
1575
+ const insertCount = args.length > 2 ? args.length - 2 : 0;
1576
+ if (insertCount === 0) {
1577
+ const length = latest(state).length;
1578
+ if (args.length === 0 || normalizeSliceIndex(args[0] ?? 0, length) === length || args.length > 1 && !(args[1] >= 1)) {
1579
+ return [];
1580
+ }
1581
+ }
1582
+ prepareCopy(state);
1583
+ const res = state.copy_.splice(
1584
+ ...args
1585
+ );
1586
+ if (res.length === 0 && insertCount === 0) {
1587
+ return res;
1588
+ }
1589
+ markArrayChanged(state);
1590
+ markAllIndicesReassigned(state);
1591
+ if (insertCount > 0) {
1592
+ const startIndex = normalizeSliceIndex(
1593
+ args[0] ?? 0,
1594
+ state.copy_.length
1595
+ );
1596
+ handleInsertedValues(state, startIndex, args.slice(2));
1597
+ }
1598
+ return res;
1599
+ }
1600
+ } else {
1601
+ return handleNonMutatingOperation(state, method, args);
1602
+ }
1603
+ } finally {
1604
+ exitOperation(state);
1605
+ }
1606
+ };
1607
+ }
1608
+ function handleNonMutatingOperation(state, method, args) {
1609
+ const source = latest(state);
1610
+ if (method === "filter") {
1611
+ const predicate = args[0];
1612
+ const thisArg = args[1];
1613
+ const result = [];
1614
+ for (let i = 0; i < source.length; i++) {
1615
+ if (predicate.call(thisArg, source[i], i, source)) {
1616
+ result.push(state.draft_[i]);
1617
+ }
1618
+ }
1619
+ return result;
1620
+ }
1621
+ if (FIND_METHODS.has(method)) {
1622
+ const predicate = args[0];
1623
+ const thisArg = args[1];
1624
+ const isForward = method === "find";
1625
+ const step = isForward ? 1 : -1;
1626
+ const start = isForward ? 0 : source.length - 1;
1627
+ for (let i = start; i >= 0 && i < source.length; i += step) {
1628
+ if (predicate.call(thisArg, source[i], i, source)) {
1629
+ return state.draft_[i];
1630
+ }
1631
+ }
1632
+ return void 0;
1633
+ }
1634
+ if (method === "slice") {
1635
+ const rawStart = args[0] ?? 0;
1636
+ const rawEnd = args[1] ?? source.length;
1637
+ const start = normalizeSliceIndex(rawStart, source.length);
1638
+ const end = normalizeSliceIndex(rawEnd, source.length);
1639
+ const result = [];
1640
+ for (let i = start; i < end; i++) {
1641
+ result.push(state.draft_[i]);
1642
+ }
1643
+ return result;
1644
+ }
1645
+ return source[method](...args);
1646
+ }
1647
+ loadPlugin(PluginArrayMethods, {
1648
+ createMethodInterceptor,
1649
+ isArrayOperationMethod,
1650
+ isMutatingArrayMethod
1651
+ });
1215
1652
  }
1216
1653
  var immer = new Immer2();
1217
1654
  var produce = immer.produce;
1218
- var produceWithPatches = /* @__PURE__ */ immer.produceWithPatches.bind(
1219
- immer
1220
- );
1655
+ var produceWithPatches = /* @__PURE__ */ immer.produceWithPatches.bind(immer);
1221
1656
  var setAutoFreeze = /* @__PURE__ */ immer.setAutoFreeze.bind(immer);
1222
- var setUseStrictShallowCopy = /* @__PURE__ */ immer.setUseStrictShallowCopy.bind(
1223
- immer
1224
- );
1225
- var setUseStrictIteration = /* @__PURE__ */ immer.setUseStrictIteration.bind(
1226
- immer
1227
- );
1657
+ var setUseStrictShallowCopy = /* @__PURE__ */ immer.setUseStrictShallowCopy.bind(immer);
1658
+ var setUseStrictIteration = /* @__PURE__ */ immer.setUseStrictIteration.bind(immer);
1228
1659
  var applyPatches = /* @__PURE__ */ immer.applyPatches.bind(immer);
1229
1660
  var createDraft = /* @__PURE__ */ immer.createDraft.bind(immer);
1230
1661
  var finishDraft = /* @__PURE__ */ immer.finishDraft.bind(immer);
1231
- function castDraft(value) {
1232
- return value;
1233
- }
1234
- function castImmutable(value) {
1235
- return value;
1236
- }
1237
-
1238
- // src/immer.js
1239
- enableMapSet();
1662
+ var castDraft = (value) => value;
1663
+ var castImmutable = (value) => value;
1240
1664
  export {
1241
1665
  Immer2 as Immer,
1242
1666
  applyPatches,
@@ -1244,6 +1668,7 @@ export {
1244
1668
  castImmutable,
1245
1669
  createDraft,
1246
1670
  current,
1671
+ enableArrayMethods,
1247
1672
  enableMapSet,
1248
1673
  enablePatches,
1249
1674
  finishDraft,