| 1 | var __defProp = Object.defineProperty;
|
|---|
| 2 | var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|---|
| 3 | var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|---|
| 4 | var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|---|
| 5 | var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|---|
| 6 | var __spreadValues = (a, b) => {
|
|---|
| 7 | for (var prop in b || (b = {}))
|
|---|
| 8 | if (__hasOwnProp.call(b, prop))
|
|---|
| 9 | __defNormalProp(a, prop, b[prop]);
|
|---|
| 10 | if (__getOwnPropSymbols)
|
|---|
| 11 | for (var prop of __getOwnPropSymbols(b)) {
|
|---|
| 12 | if (__propIsEnum.call(b, prop))
|
|---|
| 13 | __defNormalProp(a, prop, b[prop]);
|
|---|
| 14 | }
|
|---|
| 15 | return a;
|
|---|
| 16 | };
|
|---|
| 17 |
|
|---|
| 18 | // src/utils/env.ts
|
|---|
| 19 | var NOTHING = Symbol.for("immer-nothing");
|
|---|
| 20 | var DRAFTABLE = Symbol.for("immer-draftable");
|
|---|
| 21 | var DRAFT_STATE = Symbol.for("immer-state");
|
|---|
| 22 |
|
|---|
| 23 | // src/utils/errors.ts
|
|---|
| 24 | var errors = process.env.NODE_ENV !== "production" ? [
|
|---|
| 25 | // All error codes, starting by 0:
|
|---|
| 26 | function(plugin) {
|
|---|
| 27 | return `The plugin for '${plugin}' has not been loaded into Immer. To enable the plugin, import and call \`enable${plugin}()\` when initializing your application.`;
|
|---|
| 28 | },
|
|---|
| 29 | function(thing) {
|
|---|
| 30 | return `produce can only be called on things that are draftable: plain objects, arrays, Map, Set or classes that are marked with '[immerable]: true'. Got '${thing}'`;
|
|---|
| 31 | },
|
|---|
| 32 | "This object has been frozen and should not be mutated",
|
|---|
| 33 | function(data) {
|
|---|
| 34 | return "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " + data;
|
|---|
| 35 | },
|
|---|
| 36 | "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.",
|
|---|
| 37 | "Immer forbids circular references",
|
|---|
| 38 | "The first or second argument to `produce` must be a function",
|
|---|
| 39 | "The third argument to `produce` must be a function or undefined",
|
|---|
| 40 | "First argument to `createDraft` must be a plain object, an array, or an immerable object",
|
|---|
| 41 | "First argument to `finishDraft` must be a draft returned by `createDraft`",
|
|---|
| 42 | function(thing) {
|
|---|
| 43 | return `'current' expects a draft, got: ${thing}`;
|
|---|
| 44 | },
|
|---|
| 45 | "Object.defineProperty() cannot be used on an Immer draft",
|
|---|
| 46 | "Object.setPrototypeOf() cannot be used on an Immer draft",
|
|---|
| 47 | "Immer only supports deleting array indices",
|
|---|
| 48 | "Immer only supports setting array indices and the 'length' property",
|
|---|
| 49 | function(thing) {
|
|---|
| 50 | return `'original' expects a draft, got: ${thing}`;
|
|---|
| 51 | }
|
|---|
| 52 | // Note: if more errors are added, the errorOffset in Patches.ts should be increased
|
|---|
| 53 | // See Patches.ts for additional errors
|
|---|
| 54 | ] : [];
|
|---|
| 55 | function die(error, ...args) {
|
|---|
| 56 | if (process.env.NODE_ENV !== "production") {
|
|---|
| 57 | const e = errors[error];
|
|---|
| 58 | const msg = typeof e === "function" ? e.apply(null, args) : e;
|
|---|
| 59 | throw new Error(`[Immer] ${msg}`);
|
|---|
| 60 | }
|
|---|
| 61 | throw new Error(
|
|---|
| 62 | `[Immer] minified error nr: ${error}. Full error at: https://bit.ly/3cXEKWf`
|
|---|
| 63 | );
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | // src/utils/common.ts
|
|---|
| 67 | var getPrototypeOf = Object.getPrototypeOf;
|
|---|
| 68 | function isDraft(value) {
|
|---|
| 69 | return !!value && !!value[DRAFT_STATE];
|
|---|
| 70 | }
|
|---|
| 71 | function isDraftable(value) {
|
|---|
| 72 | var _a;
|
|---|
| 73 | if (!value)
|
|---|
| 74 | return false;
|
|---|
| 75 | return isPlainObject(value) || Array.isArray(value) || !!value[DRAFTABLE] || !!((_a = value.constructor) == null ? void 0 : _a[DRAFTABLE]) || isMap(value) || isSet(value);
|
|---|
| 76 | }
|
|---|
| 77 | var objectCtorString = Object.prototype.constructor.toString();
|
|---|
| 78 | var cachedCtorStrings = /* @__PURE__ */ new WeakMap();
|
|---|
| 79 | function isPlainObject(value) {
|
|---|
| 80 | if (!value || typeof value !== "object")
|
|---|
| 81 | return false;
|
|---|
| 82 | const proto = Object.getPrototypeOf(value);
|
|---|
| 83 | if (proto === null || proto === Object.prototype)
|
|---|
| 84 | return true;
|
|---|
| 85 | const Ctor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor;
|
|---|
| 86 | if (Ctor === Object)
|
|---|
| 87 | return true;
|
|---|
| 88 | if (typeof Ctor !== "function")
|
|---|
| 89 | return false;
|
|---|
| 90 | let ctorString = cachedCtorStrings.get(Ctor);
|
|---|
| 91 | if (ctorString === void 0) {
|
|---|
| 92 | ctorString = Function.toString.call(Ctor);
|
|---|
| 93 | cachedCtorStrings.set(Ctor, ctorString);
|
|---|
| 94 | }
|
|---|
| 95 | return ctorString === objectCtorString;
|
|---|
| 96 | }
|
|---|
| 97 | function original(value) {
|
|---|
| 98 | if (!isDraft(value))
|
|---|
| 99 | die(15, value);
|
|---|
| 100 | return value[DRAFT_STATE].base_;
|
|---|
| 101 | }
|
|---|
| 102 | function each(obj, iter, strict = true) {
|
|---|
| 103 | if (getArchtype(obj) === 0 /* Object */) {
|
|---|
| 104 | const keys = strict ? Reflect.ownKeys(obj) : Object.keys(obj);
|
|---|
| 105 | keys.forEach((key) => {
|
|---|
| 106 | iter(key, obj[key], obj);
|
|---|
| 107 | });
|
|---|
| 108 | } else {
|
|---|
| 109 | obj.forEach((entry, index) => iter(index, entry, obj));
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| 112 | function getArchtype(thing) {
|
|---|
| 113 | const state = thing[DRAFT_STATE];
|
|---|
| 114 | return state ? state.type_ : Array.isArray(thing) ? 1 /* Array */ : isMap(thing) ? 2 /* Map */ : isSet(thing) ? 3 /* Set */ : 0 /* Object */;
|
|---|
| 115 | }
|
|---|
| 116 | function has(thing, prop) {
|
|---|
| 117 | return getArchtype(thing) === 2 /* Map */ ? thing.has(prop) : Object.prototype.hasOwnProperty.call(thing, prop);
|
|---|
| 118 | }
|
|---|
| 119 | function get(thing, prop) {
|
|---|
| 120 | return getArchtype(thing) === 2 /* Map */ ? thing.get(prop) : thing[prop];
|
|---|
| 121 | }
|
|---|
| 122 | function set(thing, propOrOldValue, value) {
|
|---|
| 123 | const t = getArchtype(thing);
|
|---|
| 124 | if (t === 2 /* Map */)
|
|---|
| 125 | thing.set(propOrOldValue, value);
|
|---|
| 126 | else if (t === 3 /* Set */) {
|
|---|
| 127 | thing.add(value);
|
|---|
| 128 | } else
|
|---|
| 129 | thing[propOrOldValue] = value;
|
|---|
| 130 | }
|
|---|
| 131 | function is(x, y) {
|
|---|
| 132 | if (x === y) {
|
|---|
| 133 | return x !== 0 || 1 / x === 1 / y;
|
|---|
| 134 | } else {
|
|---|
| 135 | return x !== x && y !== y;
|
|---|
| 136 | }
|
|---|
| 137 | }
|
|---|
| 138 | function isMap(target) {
|
|---|
| 139 | return target instanceof Map;
|
|---|
| 140 | }
|
|---|
| 141 | function isSet(target) {
|
|---|
| 142 | return target instanceof Set;
|
|---|
| 143 | }
|
|---|
| 144 | function latest(state) {
|
|---|
| 145 | return state.copy_ || state.base_;
|
|---|
| 146 | }
|
|---|
| 147 | function shallowCopy(base, strict) {
|
|---|
| 148 | if (isMap(base)) {
|
|---|
| 149 | return new Map(base);
|
|---|
| 150 | }
|
|---|
| 151 | if (isSet(base)) {
|
|---|
| 152 | return new Set(base);
|
|---|
| 153 | }
|
|---|
| 154 | if (Array.isArray(base))
|
|---|
| 155 | return Array.prototype.slice.call(base);
|
|---|
| 156 | const isPlain = isPlainObject(base);
|
|---|
| 157 | if (strict === true || strict === "class_only" && !isPlain) {
|
|---|
| 158 | const descriptors = Object.getOwnPropertyDescriptors(base);
|
|---|
| 159 | delete descriptors[DRAFT_STATE];
|
|---|
| 160 | let keys = Reflect.ownKeys(descriptors);
|
|---|
| 161 | for (let i = 0; i < keys.length; i++) {
|
|---|
| 162 | const key = keys[i];
|
|---|
| 163 | const desc = descriptors[key];
|
|---|
| 164 | if (desc.writable === false) {
|
|---|
| 165 | desc.writable = true;
|
|---|
| 166 | desc.configurable = true;
|
|---|
| 167 | }
|
|---|
| 168 | if (desc.get || desc.set)
|
|---|
| 169 | descriptors[key] = {
|
|---|
| 170 | configurable: true,
|
|---|
| 171 | writable: true,
|
|---|
| 172 | // could live with !!desc.set as well here...
|
|---|
| 173 | enumerable: desc.enumerable,
|
|---|
| 174 | value: base[key]
|
|---|
| 175 | };
|
|---|
| 176 | }
|
|---|
| 177 | return Object.create(getPrototypeOf(base), descriptors);
|
|---|
| 178 | } else {
|
|---|
| 179 | const proto = getPrototypeOf(base);
|
|---|
| 180 | if (proto !== null && isPlain) {
|
|---|
| 181 | return __spreadValues({}, base);
|
|---|
| 182 | }
|
|---|
| 183 | const obj = Object.create(proto);
|
|---|
| 184 | return Object.assign(obj, base);
|
|---|
| 185 | }
|
|---|
| 186 | }
|
|---|
| 187 | function freeze(obj, deep = false) {
|
|---|
| 188 | if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj))
|
|---|
| 189 | return obj;
|
|---|
| 190 | if (getArchtype(obj) > 1) {
|
|---|
| 191 | Object.defineProperties(obj, {
|
|---|
| 192 | set: dontMutateMethodOverride,
|
|---|
| 193 | add: dontMutateMethodOverride,
|
|---|
| 194 | clear: dontMutateMethodOverride,
|
|---|
| 195 | delete: dontMutateMethodOverride
|
|---|
| 196 | });
|
|---|
| 197 | }
|
|---|
| 198 | Object.freeze(obj);
|
|---|
| 199 | if (deep)
|
|---|
| 200 | Object.values(obj).forEach((value) => freeze(value, true));
|
|---|
| 201 | return obj;
|
|---|
| 202 | }
|
|---|
| 203 | function dontMutateFrozenCollections() {
|
|---|
| 204 | die(2);
|
|---|
| 205 | }
|
|---|
| 206 | var dontMutateMethodOverride = {
|
|---|
| 207 | value: dontMutateFrozenCollections
|
|---|
| 208 | };
|
|---|
| 209 | function isFrozen(obj) {
|
|---|
| 210 | if (obj === null || typeof obj !== "object")
|
|---|
| 211 | return true;
|
|---|
| 212 | return Object.isFrozen(obj);
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | // src/utils/plugins.ts
|
|---|
| 216 | var plugins = {};
|
|---|
| 217 | function getPlugin(pluginKey) {
|
|---|
| 218 | const plugin = plugins[pluginKey];
|
|---|
| 219 | if (!plugin) {
|
|---|
| 220 | die(0, pluginKey);
|
|---|
| 221 | }
|
|---|
| 222 | return plugin;
|
|---|
| 223 | }
|
|---|
| 224 | function loadPlugin(pluginKey, implementation) {
|
|---|
| 225 | if (!plugins[pluginKey])
|
|---|
| 226 | plugins[pluginKey] = implementation;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | // src/core/scope.ts
|
|---|
| 230 | var currentScope;
|
|---|
| 231 | function getCurrentScope() {
|
|---|
| 232 | return currentScope;
|
|---|
| 233 | }
|
|---|
| 234 | function createScope(parent_, immer_) {
|
|---|
| 235 | return {
|
|---|
| 236 | drafts_: [],
|
|---|
| 237 | parent_,
|
|---|
| 238 | immer_,
|
|---|
| 239 | // Whenever the modified draft contains a draft from another scope, we
|
|---|
| 240 | // need to prevent auto-freezing so the unowned draft can be finalized.
|
|---|
| 241 | canAutoFreeze_: true,
|
|---|
| 242 | unfinalizedDrafts_: 0
|
|---|
| 243 | };
|
|---|
| 244 | }
|
|---|
| 245 | function usePatchesInScope(scope, patchListener) {
|
|---|
| 246 | if (patchListener) {
|
|---|
| 247 | getPlugin("Patches");
|
|---|
| 248 | scope.patches_ = [];
|
|---|
| 249 | scope.inversePatches_ = [];
|
|---|
| 250 | scope.patchListener_ = patchListener;
|
|---|
| 251 | }
|
|---|
| 252 | }
|
|---|
| 253 | function revokeScope(scope) {
|
|---|
| 254 | leaveScope(scope);
|
|---|
| 255 | scope.drafts_.forEach(revokeDraft);
|
|---|
| 256 | scope.drafts_ = null;
|
|---|
| 257 | }
|
|---|
| 258 | function leaveScope(scope) {
|
|---|
| 259 | if (scope === currentScope) {
|
|---|
| 260 | currentScope = scope.parent_;
|
|---|
| 261 | }
|
|---|
| 262 | }
|
|---|
| 263 | function enterScope(immer2) {
|
|---|
| 264 | return currentScope = createScope(currentScope, immer2);
|
|---|
| 265 | }
|
|---|
| 266 | function revokeDraft(draft) {
|
|---|
| 267 | const state = draft[DRAFT_STATE];
|
|---|
| 268 | if (state.type_ === 0 /* Object */ || state.type_ === 1 /* Array */)
|
|---|
| 269 | state.revoke_();
|
|---|
| 270 | else
|
|---|
| 271 | state.revoked_ = true;
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | // src/core/finalize.ts
|
|---|
| 275 | function processResult(result, scope) {
|
|---|
| 276 | scope.unfinalizedDrafts_ = scope.drafts_.length;
|
|---|
| 277 | const baseDraft = scope.drafts_[0];
|
|---|
| 278 | const isReplaced = result !== void 0 && result !== baseDraft;
|
|---|
| 279 | if (isReplaced) {
|
|---|
| 280 | if (baseDraft[DRAFT_STATE].modified_) {
|
|---|
| 281 | revokeScope(scope);
|
|---|
| 282 | die(4);
|
|---|
| 283 | }
|
|---|
| 284 | if (isDraftable(result)) {
|
|---|
| 285 | result = finalize(scope, result);
|
|---|
| 286 | if (!scope.parent_)
|
|---|
| 287 | maybeFreeze(scope, result);
|
|---|
| 288 | }
|
|---|
| 289 | if (scope.patches_) {
|
|---|
| 290 | getPlugin("Patches").generateReplacementPatches_(
|
|---|
| 291 | baseDraft[DRAFT_STATE].base_,
|
|---|
| 292 | result,
|
|---|
| 293 | scope.patches_,
|
|---|
| 294 | scope.inversePatches_
|
|---|
| 295 | );
|
|---|
| 296 | }
|
|---|
| 297 | } else {
|
|---|
| 298 | result = finalize(scope, baseDraft, []);
|
|---|
| 299 | }
|
|---|
| 300 | revokeScope(scope);
|
|---|
| 301 | if (scope.patches_) {
|
|---|
| 302 | scope.patchListener_(scope.patches_, scope.inversePatches_);
|
|---|
| 303 | }
|
|---|
| 304 | return result !== NOTHING ? result : void 0;
|
|---|
| 305 | }
|
|---|
| 306 | function finalize(rootScope, value, path) {
|
|---|
| 307 | if (isFrozen(value))
|
|---|
| 308 | return value;
|
|---|
| 309 | const useStrictIteration = rootScope.immer_.shouldUseStrictIteration();
|
|---|
| 310 | const state = value[DRAFT_STATE];
|
|---|
| 311 | if (!state) {
|
|---|
| 312 | each(
|
|---|
| 313 | value,
|
|---|
| 314 | (key, childValue) => finalizeProperty(rootScope, state, value, key, childValue, path),
|
|---|
| 315 | useStrictIteration
|
|---|
| 316 | );
|
|---|
| 317 | return value;
|
|---|
| 318 | }
|
|---|
| 319 | if (state.scope_ !== rootScope)
|
|---|
| 320 | return value;
|
|---|
| 321 | if (!state.modified_) {
|
|---|
| 322 | maybeFreeze(rootScope, state.base_, true);
|
|---|
| 323 | return state.base_;
|
|---|
| 324 | }
|
|---|
| 325 | if (!state.finalized_) {
|
|---|
| 326 | state.finalized_ = true;
|
|---|
| 327 | state.scope_.unfinalizedDrafts_--;
|
|---|
| 328 | const result = state.copy_;
|
|---|
| 329 | let resultEach = result;
|
|---|
| 330 | let isSet2 = false;
|
|---|
| 331 | if (state.type_ === 3 /* Set */) {
|
|---|
| 332 | resultEach = new Set(result);
|
|---|
| 333 | result.clear();
|
|---|
| 334 | isSet2 = true;
|
|---|
| 335 | }
|
|---|
| 336 | each(
|
|---|
| 337 | resultEach,
|
|---|
| 338 | (key, childValue) => finalizeProperty(
|
|---|
| 339 | rootScope,
|
|---|
| 340 | state,
|
|---|
| 341 | result,
|
|---|
| 342 | key,
|
|---|
| 343 | childValue,
|
|---|
| 344 | path,
|
|---|
| 345 | isSet2
|
|---|
| 346 | ),
|
|---|
| 347 | useStrictIteration
|
|---|
| 348 | );
|
|---|
| 349 | maybeFreeze(rootScope, result, false);
|
|---|
| 350 | if (path && rootScope.patches_) {
|
|---|
| 351 | getPlugin("Patches").generatePatches_(
|
|---|
| 352 | state,
|
|---|
| 353 | path,
|
|---|
| 354 | rootScope.patches_,
|
|---|
| 355 | rootScope.inversePatches_
|
|---|
| 356 | );
|
|---|
| 357 | }
|
|---|
| 358 | }
|
|---|
| 359 | return state.copy_;
|
|---|
| 360 | }
|
|---|
| 361 | function finalizeProperty(rootScope, parentState, targetObject, prop, childValue, rootPath, targetIsSet) {
|
|---|
| 362 | if (childValue == null) {
|
|---|
| 363 | return;
|
|---|
| 364 | }
|
|---|
| 365 | if (typeof childValue !== "object" && !targetIsSet) {
|
|---|
| 366 | return;
|
|---|
| 367 | }
|
|---|
| 368 | const childIsFrozen = isFrozen(childValue);
|
|---|
| 369 | if (childIsFrozen && !targetIsSet) {
|
|---|
| 370 | return;
|
|---|
| 371 | }
|
|---|
| 372 | if (process.env.NODE_ENV !== "production" && childValue === targetObject)
|
|---|
| 373 | die(5);
|
|---|
| 374 | if (isDraft(childValue)) {
|
|---|
| 375 | const path = rootPath && parentState && parentState.type_ !== 3 /* Set */ && // Set objects are atomic since they have no keys.
|
|---|
| 376 | !has(parentState.assigned_, prop) ? rootPath.concat(prop) : void 0;
|
|---|
| 377 | const res = finalize(rootScope, childValue, path);
|
|---|
| 378 | set(targetObject, prop, res);
|
|---|
| 379 | if (isDraft(res)) {
|
|---|
| 380 | rootScope.canAutoFreeze_ = false;
|
|---|
| 381 | } else
|
|---|
| 382 | return;
|
|---|
| 383 | } else if (targetIsSet) {
|
|---|
| 384 | targetObject.add(childValue);
|
|---|
| 385 | }
|
|---|
| 386 | if (isDraftable(childValue) && !childIsFrozen) {
|
|---|
| 387 | if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) {
|
|---|
| 388 | return;
|
|---|
| 389 | }
|
|---|
| 390 | if (parentState && parentState.base_ && parentState.base_[prop] === childValue && childIsFrozen) {
|
|---|
| 391 | return;
|
|---|
| 392 | }
|
|---|
| 393 | finalize(rootScope, childValue);
|
|---|
| 394 | if ((!parentState || !parentState.scope_.parent_) && typeof prop !== "symbol" && (isMap(targetObject) ? targetObject.has(prop) : Object.prototype.propertyIsEnumerable.call(targetObject, prop)))
|
|---|
| 395 | maybeFreeze(rootScope, childValue);
|
|---|
| 396 | }
|
|---|
| 397 | }
|
|---|
| 398 | function maybeFreeze(scope, value, deep = false) {
|
|---|
| 399 | if (!scope.parent_ && scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {
|
|---|
| 400 | freeze(value, deep);
|
|---|
| 401 | }
|
|---|
| 402 | }
|
|---|
| 403 |
|
|---|
| 404 | // src/core/proxy.ts
|
|---|
| 405 | function createProxyProxy(base, parent) {
|
|---|
| 406 | const isArray = Array.isArray(base);
|
|---|
| 407 | const state = {
|
|---|
| 408 | type_: isArray ? 1 /* Array */ : 0 /* Object */,
|
|---|
| 409 | // Track which produce call this is associated with.
|
|---|
| 410 | scope_: parent ? parent.scope_ : getCurrentScope(),
|
|---|
| 411 | // True for both shallow and deep changes.
|
|---|
| 412 | modified_: false,
|
|---|
| 413 | // Used during finalization.
|
|---|
| 414 | finalized_: false,
|
|---|
| 415 | // Track which properties have been assigned (true) or deleted (false).
|
|---|
| 416 | assigned_: {},
|
|---|
| 417 | // The parent draft state.
|
|---|
| 418 | parent_: parent,
|
|---|
| 419 | // The base state.
|
|---|
| 420 | base_: base,
|
|---|
| 421 | // The base proxy.
|
|---|
| 422 | draft_: null,
|
|---|
| 423 | // set below
|
|---|
| 424 | // The base copy with any updated values.
|
|---|
| 425 | copy_: null,
|
|---|
| 426 | // Called by the `produce` function.
|
|---|
| 427 | revoke_: null,
|
|---|
| 428 | isManual_: false
|
|---|
| 429 | };
|
|---|
| 430 | let target = state;
|
|---|
| 431 | let traps = objectTraps;
|
|---|
| 432 | if (isArray) {
|
|---|
| 433 | target = [state];
|
|---|
| 434 | traps = arrayTraps;
|
|---|
| 435 | }
|
|---|
| 436 | const { revoke, proxy } = Proxy.revocable(target, traps);
|
|---|
| 437 | state.draft_ = proxy;
|
|---|
| 438 | state.revoke_ = revoke;
|
|---|
| 439 | return proxy;
|
|---|
| 440 | }
|
|---|
| 441 | var objectTraps = {
|
|---|
| 442 | get(state, prop) {
|
|---|
| 443 | if (prop === DRAFT_STATE)
|
|---|
| 444 | return state;
|
|---|
| 445 | const source = latest(state);
|
|---|
| 446 | if (!has(source, prop)) {
|
|---|
| 447 | return readPropFromProto(state, source, prop);
|
|---|
| 448 | }
|
|---|
| 449 | const value = source[prop];
|
|---|
| 450 | if (state.finalized_ || !isDraftable(value)) {
|
|---|
| 451 | return value;
|
|---|
| 452 | }
|
|---|
| 453 | if (value === peek(state.base_, prop)) {
|
|---|
| 454 | prepareCopy(state);
|
|---|
| 455 | return state.copy_[prop] = createProxy(value, state);
|
|---|
| 456 | }
|
|---|
| 457 | return value;
|
|---|
| 458 | },
|
|---|
| 459 | has(state, prop) {
|
|---|
| 460 | return prop in latest(state);
|
|---|
| 461 | },
|
|---|
| 462 | ownKeys(state) {
|
|---|
| 463 | return Reflect.ownKeys(latest(state));
|
|---|
| 464 | },
|
|---|
| 465 | set(state, prop, value) {
|
|---|
| 466 | const desc = getDescriptorFromProto(latest(state), prop);
|
|---|
| 467 | if (desc == null ? void 0 : desc.set) {
|
|---|
| 468 | desc.set.call(state.draft_, value);
|
|---|
| 469 | return true;
|
|---|
| 470 | }
|
|---|
| 471 | if (!state.modified_) {
|
|---|
| 472 | const current2 = peek(latest(state), prop);
|
|---|
| 473 | const currentState = current2 == null ? void 0 : current2[DRAFT_STATE];
|
|---|
| 474 | if (currentState && currentState.base_ === value) {
|
|---|
| 475 | state.copy_[prop] = value;
|
|---|
| 476 | state.assigned_[prop] = false;
|
|---|
| 477 | return true;
|
|---|
| 478 | }
|
|---|
| 479 | if (is(value, current2) && (value !== void 0 || has(state.base_, prop)))
|
|---|
| 480 | return true;
|
|---|
| 481 | prepareCopy(state);
|
|---|
| 482 | markChanged(state);
|
|---|
| 483 | }
|
|---|
| 484 | if (state.copy_[prop] === value && // special case: handle new props with value 'undefined'
|
|---|
| 485 | (value !== void 0 || prop in state.copy_) || // special case: NaN
|
|---|
| 486 | Number.isNaN(value) && Number.isNaN(state.copy_[prop]))
|
|---|
| 487 | return true;
|
|---|
| 488 | state.copy_[prop] = value;
|
|---|
| 489 | state.assigned_[prop] = true;
|
|---|
| 490 | return true;
|
|---|
| 491 | },
|
|---|
| 492 | deleteProperty(state, prop) {
|
|---|
| 493 | if (peek(state.base_, prop) !== void 0 || prop in state.base_) {
|
|---|
| 494 | state.assigned_[prop] = false;
|
|---|
| 495 | prepareCopy(state);
|
|---|
| 496 | markChanged(state);
|
|---|
| 497 | } else {
|
|---|
| 498 | delete state.assigned_[prop];
|
|---|
| 499 | }
|
|---|
| 500 | if (state.copy_) {
|
|---|
| 501 | delete state.copy_[prop];
|
|---|
| 502 | }
|
|---|
| 503 | return true;
|
|---|
| 504 | },
|
|---|
| 505 | // Note: We never coerce `desc.value` into an Immer draft, because we can't make
|
|---|
| 506 | // the same guarantee in ES5 mode.
|
|---|
| 507 | getOwnPropertyDescriptor(state, prop) {
|
|---|
| 508 | const owner = latest(state);
|
|---|
| 509 | const desc = Reflect.getOwnPropertyDescriptor(owner, prop);
|
|---|
| 510 | if (!desc)
|
|---|
| 511 | return desc;
|
|---|
| 512 | return {
|
|---|
| 513 | writable: true,
|
|---|
| 514 | configurable: state.type_ !== 1 /* Array */ || prop !== "length",
|
|---|
| 515 | enumerable: desc.enumerable,
|
|---|
| 516 | value: owner[prop]
|
|---|
| 517 | };
|
|---|
| 518 | },
|
|---|
| 519 | defineProperty() {
|
|---|
| 520 | die(11);
|
|---|
| 521 | },
|
|---|
| 522 | getPrototypeOf(state) {
|
|---|
| 523 | return getPrototypeOf(state.base_);
|
|---|
| 524 | },
|
|---|
| 525 | setPrototypeOf() {
|
|---|
| 526 | die(12);
|
|---|
| 527 | }
|
|---|
| 528 | };
|
|---|
| 529 | var arrayTraps = {};
|
|---|
| 530 | each(objectTraps, (key, fn) => {
|
|---|
| 531 | arrayTraps[key] = function() {
|
|---|
| 532 | arguments[0] = arguments[0][0];
|
|---|
| 533 | return fn.apply(this, arguments);
|
|---|
| 534 | };
|
|---|
| 535 | });
|
|---|
| 536 | arrayTraps.deleteProperty = function(state, prop) {
|
|---|
| 537 | if (process.env.NODE_ENV !== "production" && isNaN(parseInt(prop)))
|
|---|
| 538 | die(13);
|
|---|
| 539 | return arrayTraps.set.call(this, state, prop, void 0);
|
|---|
| 540 | };
|
|---|
| 541 | arrayTraps.set = function(state, prop, value) {
|
|---|
| 542 | if (process.env.NODE_ENV !== "production" && prop !== "length" && isNaN(parseInt(prop)))
|
|---|
| 543 | die(14);
|
|---|
| 544 | return objectTraps.set.call(this, state[0], prop, value, state[0]);
|
|---|
| 545 | };
|
|---|
| 546 | function peek(draft, prop) {
|
|---|
| 547 | const state = draft[DRAFT_STATE];
|
|---|
| 548 | const source = state ? latest(state) : draft;
|
|---|
| 549 | return source[prop];
|
|---|
| 550 | }
|
|---|
| 551 | function readPropFromProto(state, source, prop) {
|
|---|
| 552 | var _a;
|
|---|
| 553 | const desc = getDescriptorFromProto(source, prop);
|
|---|
| 554 | return desc ? `value` in desc ? desc.value : (
|
|---|
| 555 | // This is a very special case, if the prop is a getter defined by the
|
|---|
| 556 | // prototype, we should invoke it with the draft as context!
|
|---|
| 557 | (_a = desc.get) == null ? void 0 : _a.call(state.draft_)
|
|---|
| 558 | ) : void 0;
|
|---|
| 559 | }
|
|---|
| 560 | function getDescriptorFromProto(source, prop) {
|
|---|
| 561 | if (!(prop in source))
|
|---|
| 562 | return void 0;
|
|---|
| 563 | let proto = getPrototypeOf(source);
|
|---|
| 564 | while (proto) {
|
|---|
| 565 | const desc = Object.getOwnPropertyDescriptor(proto, prop);
|
|---|
| 566 | if (desc)
|
|---|
| 567 | return desc;
|
|---|
| 568 | proto = getPrototypeOf(proto);
|
|---|
| 569 | }
|
|---|
| 570 | return void 0;
|
|---|
| 571 | }
|
|---|
| 572 | function markChanged(state) {
|
|---|
| 573 | if (!state.modified_) {
|
|---|
| 574 | state.modified_ = true;
|
|---|
| 575 | if (state.parent_) {
|
|---|
| 576 | markChanged(state.parent_);
|
|---|
| 577 | }
|
|---|
| 578 | }
|
|---|
| 579 | }
|
|---|
| 580 | function prepareCopy(state) {
|
|---|
| 581 | if (!state.copy_) {
|
|---|
| 582 | state.copy_ = shallowCopy(
|
|---|
| 583 | state.base_,
|
|---|
| 584 | state.scope_.immer_.useStrictShallowCopy_
|
|---|
| 585 | );
|
|---|
| 586 | }
|
|---|
| 587 | }
|
|---|
| 588 |
|
|---|
| 589 | // src/core/immerClass.ts
|
|---|
| 590 | var Immer2 = class {
|
|---|
| 591 | constructor(config) {
|
|---|
| 592 | this.autoFreeze_ = true;
|
|---|
| 593 | this.useStrictShallowCopy_ = false;
|
|---|
| 594 | this.useStrictIteration_ = true;
|
|---|
| 595 | /**
|
|---|
| 596 | * The `produce` function takes a value and a "recipe function" (whose
|
|---|
| 597 | * return value often depends on the base state). The recipe function is
|
|---|
| 598 | * free to mutate its first argument however it wants. All mutations are
|
|---|
| 599 | * only ever applied to a __copy__ of the base state.
|
|---|
| 600 | *
|
|---|
| 601 | * Pass only a function to create a "curried producer" which relieves you
|
|---|
| 602 | * from passing the recipe function every time.
|
|---|
| 603 | *
|
|---|
| 604 | * Only plain objects and arrays are made mutable. All other objects are
|
|---|
| 605 | * considered uncopyable.
|
|---|
| 606 | *
|
|---|
| 607 | * Note: This function is __bound__ to its `Immer` instance.
|
|---|
| 608 | *
|
|---|
| 609 | * @param {any} base - the initial state
|
|---|
| 610 | * @param {Function} recipe - function that receives a proxy of the base state as first argument and which can be freely modified
|
|---|
| 611 | * @param {Function} patchListener - optional function that will be called with all the patches produced here
|
|---|
| 612 | * @returns {any} a new state, or the initial state if nothing was modified
|
|---|
| 613 | */
|
|---|
| 614 | this.produce = (base, recipe, patchListener) => {
|
|---|
| 615 | if (typeof base === "function" && typeof recipe !== "function") {
|
|---|
| 616 | const defaultBase = recipe;
|
|---|
| 617 | recipe = base;
|
|---|
| 618 | const self = this;
|
|---|
| 619 | return function curriedProduce(base2 = defaultBase, ...args) {
|
|---|
| 620 | return self.produce(base2, (draft) => recipe.call(this, draft, ...args));
|
|---|
| 621 | };
|
|---|
| 622 | }
|
|---|
| 623 | if (typeof recipe !== "function")
|
|---|
| 624 | die(6);
|
|---|
| 625 | if (patchListener !== void 0 && typeof patchListener !== "function")
|
|---|
| 626 | die(7);
|
|---|
| 627 | let result;
|
|---|
| 628 | if (isDraftable(base)) {
|
|---|
| 629 | const scope = enterScope(this);
|
|---|
| 630 | const proxy = createProxy(base, void 0);
|
|---|
| 631 | let hasError = true;
|
|---|
| 632 | try {
|
|---|
| 633 | result = recipe(proxy);
|
|---|
| 634 | hasError = false;
|
|---|
| 635 | } finally {
|
|---|
| 636 | if (hasError)
|
|---|
| 637 | revokeScope(scope);
|
|---|
| 638 | else
|
|---|
| 639 | leaveScope(scope);
|
|---|
| 640 | }
|
|---|
| 641 | usePatchesInScope(scope, patchListener);
|
|---|
| 642 | return processResult(result, scope);
|
|---|
| 643 | } else if (!base || typeof base !== "object") {
|
|---|
| 644 | result = recipe(base);
|
|---|
| 645 | if (result === void 0)
|
|---|
| 646 | result = base;
|
|---|
| 647 | if (result === NOTHING)
|
|---|
| 648 | result = void 0;
|
|---|
| 649 | if (this.autoFreeze_)
|
|---|
| 650 | freeze(result, true);
|
|---|
| 651 | if (patchListener) {
|
|---|
| 652 | const p = [];
|
|---|
| 653 | const ip = [];
|
|---|
| 654 | getPlugin("Patches").generateReplacementPatches_(base, result, p, ip);
|
|---|
| 655 | patchListener(p, ip);
|
|---|
| 656 | }
|
|---|
| 657 | return result;
|
|---|
| 658 | } else
|
|---|
| 659 | die(1, base);
|
|---|
| 660 | };
|
|---|
| 661 | this.produceWithPatches = (base, recipe) => {
|
|---|
| 662 | if (typeof base === "function") {
|
|---|
| 663 | return (state, ...args) => this.produceWithPatches(state, (draft) => base(draft, ...args));
|
|---|
| 664 | }
|
|---|
| 665 | let patches, inversePatches;
|
|---|
| 666 | const result = this.produce(base, recipe, (p, ip) => {
|
|---|
| 667 | patches = p;
|
|---|
| 668 | inversePatches = ip;
|
|---|
| 669 | });
|
|---|
| 670 | return [result, patches, inversePatches];
|
|---|
| 671 | };
|
|---|
| 672 | if (typeof (config == null ? void 0 : config.autoFreeze) === "boolean")
|
|---|
| 673 | this.setAutoFreeze(config.autoFreeze);
|
|---|
| 674 | if (typeof (config == null ? void 0 : config.useStrictShallowCopy) === "boolean")
|
|---|
| 675 | this.setUseStrictShallowCopy(config.useStrictShallowCopy);
|
|---|
| 676 | if (typeof (config == null ? void 0 : config.useStrictIteration) === "boolean")
|
|---|
| 677 | this.setUseStrictIteration(config.useStrictIteration);
|
|---|
| 678 | }
|
|---|
| 679 | createDraft(base) {
|
|---|
| 680 | if (!isDraftable(base))
|
|---|
| 681 | die(8);
|
|---|
| 682 | if (isDraft(base))
|
|---|
| 683 | base = current(base);
|
|---|
| 684 | const scope = enterScope(this);
|
|---|
| 685 | const proxy = createProxy(base, void 0);
|
|---|
| 686 | proxy[DRAFT_STATE].isManual_ = true;
|
|---|
| 687 | leaveScope(scope);
|
|---|
| 688 | return proxy;
|
|---|
| 689 | }
|
|---|
| 690 | finishDraft(draft, patchListener) {
|
|---|
| 691 | const state = draft && draft[DRAFT_STATE];
|
|---|
| 692 | if (!state || !state.isManual_)
|
|---|
| 693 | die(9);
|
|---|
| 694 | const { scope_: scope } = state;
|
|---|
| 695 | usePatchesInScope(scope, patchListener);
|
|---|
| 696 | return processResult(void 0, scope);
|
|---|
| 697 | }
|
|---|
| 698 | /**
|
|---|
| 699 | * Pass true to automatically freeze all copies created by Immer.
|
|---|
| 700 | *
|
|---|
| 701 | * By default, auto-freezing is enabled.
|
|---|
| 702 | */
|
|---|
| 703 | setAutoFreeze(value) {
|
|---|
| 704 | this.autoFreeze_ = value;
|
|---|
| 705 | }
|
|---|
| 706 | /**
|
|---|
| 707 | * Pass true to enable strict shallow copy.
|
|---|
| 708 | *
|
|---|
| 709 | * By default, immer does not copy the object descriptors such as getter, setter and non-enumrable properties.
|
|---|
| 710 | */
|
|---|
| 711 | setUseStrictShallowCopy(value) {
|
|---|
| 712 | this.useStrictShallowCopy_ = value;
|
|---|
| 713 | }
|
|---|
| 714 | /**
|
|---|
| 715 | * Pass false to use faster iteration that skips non-enumerable properties
|
|---|
| 716 | * but still handles symbols for compatibility.
|
|---|
| 717 | *
|
|---|
| 718 | * By default, strict iteration is enabled (includes all own properties).
|
|---|
| 719 | */
|
|---|
| 720 | setUseStrictIteration(value) {
|
|---|
| 721 | this.useStrictIteration_ = value;
|
|---|
| 722 | }
|
|---|
| 723 | shouldUseStrictIteration() {
|
|---|
| 724 | return this.useStrictIteration_;
|
|---|
| 725 | }
|
|---|
| 726 | applyPatches(base, patches) {
|
|---|
| 727 | let i;
|
|---|
| 728 | for (i = patches.length - 1; i >= 0; i--) {
|
|---|
| 729 | const patch = patches[i];
|
|---|
| 730 | if (patch.path.length === 0 && patch.op === "replace") {
|
|---|
| 731 | base = patch.value;
|
|---|
| 732 | break;
|
|---|
| 733 | }
|
|---|
| 734 | }
|
|---|
| 735 | if (i > -1) {
|
|---|
| 736 | patches = patches.slice(i + 1);
|
|---|
| 737 | }
|
|---|
| 738 | const applyPatchesImpl = getPlugin("Patches").applyPatches_;
|
|---|
| 739 | if (isDraft(base)) {
|
|---|
| 740 | return applyPatchesImpl(base, patches);
|
|---|
| 741 | }
|
|---|
| 742 | return this.produce(
|
|---|
| 743 | base,
|
|---|
| 744 | (draft) => applyPatchesImpl(draft, patches)
|
|---|
| 745 | );
|
|---|
| 746 | }
|
|---|
| 747 | };
|
|---|
| 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();
|
|---|
| 751 | scope.drafts_.push(draft);
|
|---|
| 752 | return draft;
|
|---|
| 753 | }
|
|---|
| 754 |
|
|---|
| 755 | // src/core/current.ts
|
|---|
| 756 | function current(value) {
|
|---|
| 757 | if (!isDraft(value))
|
|---|
| 758 | die(10, value);
|
|---|
| 759 | return currentImpl(value);
|
|---|
| 760 | }
|
|---|
| 761 | function currentImpl(value) {
|
|---|
| 762 | if (!isDraftable(value) || isFrozen(value))
|
|---|
| 763 | return value;
|
|---|
| 764 | const state = value[DRAFT_STATE];
|
|---|
| 765 | let copy;
|
|---|
| 766 | let strict = true;
|
|---|
| 767 | if (state) {
|
|---|
| 768 | if (!state.modified_)
|
|---|
| 769 | return state.base_;
|
|---|
| 770 | state.finalized_ = true;
|
|---|
| 771 | copy = shallowCopy(value, state.scope_.immer_.useStrictShallowCopy_);
|
|---|
| 772 | strict = state.scope_.immer_.shouldUseStrictIteration();
|
|---|
| 773 | } else {
|
|---|
| 774 | copy = shallowCopy(value, true);
|
|---|
| 775 | }
|
|---|
| 776 | each(
|
|---|
| 777 | copy,
|
|---|
| 778 | (key, childValue) => {
|
|---|
| 779 | set(copy, key, currentImpl(childValue));
|
|---|
| 780 | },
|
|---|
| 781 | strict
|
|---|
| 782 | );
|
|---|
| 783 | if (state) {
|
|---|
| 784 | state.finalized_ = false;
|
|---|
| 785 | }
|
|---|
| 786 | return copy;
|
|---|
| 787 | }
|
|---|
| 788 |
|
|---|
| 789 | // src/plugins/patches.ts
|
|---|
| 790 | function enablePatches() {
|
|---|
| 791 | const errorOffset = 16;
|
|---|
| 792 | if (process.env.NODE_ENV !== "production") {
|
|---|
| 793 | errors.push(
|
|---|
| 794 | 'Sets cannot have "replace" patches.',
|
|---|
| 795 | function(op) {
|
|---|
| 796 | return "Unsupported patch operation: " + op;
|
|---|
| 797 | },
|
|---|
| 798 | function(path) {
|
|---|
| 799 | return "Cannot apply patch, path doesn't resolve: " + path;
|
|---|
| 800 | },
|
|---|
| 801 | "Patching reserved attributes like __proto__, prototype and constructor is not allowed"
|
|---|
| 802 | );
|
|---|
| 803 | }
|
|---|
| 804 | const REPLACE = "replace";
|
|---|
| 805 | const ADD = "add";
|
|---|
| 806 | const REMOVE = "remove";
|
|---|
| 807 | function generatePatches_(state, basePath, patches, inversePatches) {
|
|---|
| 808 | switch (state.type_) {
|
|---|
| 809 | case 0 /* Object */:
|
|---|
| 810 | case 2 /* Map */:
|
|---|
| 811 | return generatePatchesFromAssigned(
|
|---|
| 812 | state,
|
|---|
| 813 | basePath,
|
|---|
| 814 | patches,
|
|---|
| 815 | inversePatches
|
|---|
| 816 | );
|
|---|
| 817 | case 1 /* Array */:
|
|---|
| 818 | return generateArrayPatches(state, basePath, patches, inversePatches);
|
|---|
| 819 | case 3 /* Set */:
|
|---|
| 820 | return generateSetPatches(
|
|---|
| 821 | state,
|
|---|
| 822 | basePath,
|
|---|
| 823 | patches,
|
|---|
| 824 | inversePatches
|
|---|
| 825 | );
|
|---|
| 826 | }
|
|---|
| 827 | }
|
|---|
| 828 | function generateArrayPatches(state, basePath, patches, inversePatches) {
|
|---|
| 829 | let { base_, assigned_ } = state;
|
|---|
| 830 | let copy_ = state.copy_;
|
|---|
| 831 | if (copy_.length < base_.length) {
|
|---|
| 832 | ;
|
|---|
| 833 | [base_, copy_] = [copy_, base_];
|
|---|
| 834 | [patches, inversePatches] = [inversePatches, patches];
|
|---|
| 835 | }
|
|---|
| 836 | for (let i = 0; i < base_.length; i++) {
|
|---|
| 837 | if (assigned_[i] && copy_[i] !== base_[i]) {
|
|---|
| 838 | const path = basePath.concat([i]);
|
|---|
| 839 | patches.push({
|
|---|
| 840 | op: REPLACE,
|
|---|
| 841 | path,
|
|---|
| 842 | // Need to maybe clone it, as it can in fact be the original value
|
|---|
| 843 | // due to the base/copy inversion at the start of this function
|
|---|
| 844 | value: clonePatchValueIfNeeded(copy_[i])
|
|---|
| 845 | });
|
|---|
| 846 | inversePatches.push({
|
|---|
| 847 | op: REPLACE,
|
|---|
| 848 | path,
|
|---|
| 849 | value: clonePatchValueIfNeeded(base_[i])
|
|---|
| 850 | });
|
|---|
| 851 | }
|
|---|
| 852 | }
|
|---|
| 853 | for (let i = base_.length; i < copy_.length; i++) {
|
|---|
| 854 | const path = basePath.concat([i]);
|
|---|
| 855 | patches.push({
|
|---|
| 856 | op: ADD,
|
|---|
| 857 | path,
|
|---|
| 858 | // Need to maybe clone it, as it can in fact be the original value
|
|---|
| 859 | // due to the base/copy inversion at the start of this function
|
|---|
| 860 | value: clonePatchValueIfNeeded(copy_[i])
|
|---|
| 861 | });
|
|---|
| 862 | }
|
|---|
| 863 | for (let i = copy_.length - 1; base_.length <= i; --i) {
|
|---|
| 864 | const path = basePath.concat([i]);
|
|---|
| 865 | inversePatches.push({
|
|---|
| 866 | op: REMOVE,
|
|---|
| 867 | path
|
|---|
| 868 | });
|
|---|
| 869 | }
|
|---|
| 870 | }
|
|---|
| 871 | function generatePatchesFromAssigned(state, basePath, patches, inversePatches) {
|
|---|
| 872 | const { base_, copy_ } = state;
|
|---|
| 873 | each(state.assigned_, (key, assignedValue) => {
|
|---|
| 874 | const origValue = get(base_, key);
|
|---|
| 875 | const value = get(copy_, key);
|
|---|
| 876 | const op = !assignedValue ? REMOVE : has(base_, key) ? REPLACE : ADD;
|
|---|
| 877 | if (origValue === value && op === REPLACE)
|
|---|
| 878 | return;
|
|---|
| 879 | const path = basePath.concat(key);
|
|---|
| 880 | patches.push(op === REMOVE ? { op, path } : { op, path, value });
|
|---|
| 881 | inversePatches.push(
|
|---|
| 882 | op === ADD ? { op: REMOVE, path } : op === REMOVE ? { op: ADD, path, value: clonePatchValueIfNeeded(origValue) } : { op: REPLACE, path, value: clonePatchValueIfNeeded(origValue) }
|
|---|
| 883 | );
|
|---|
| 884 | });
|
|---|
| 885 | }
|
|---|
| 886 | function generateSetPatches(state, basePath, patches, inversePatches) {
|
|---|
| 887 | let { base_, copy_ } = state;
|
|---|
| 888 | let i = 0;
|
|---|
| 889 | base_.forEach((value) => {
|
|---|
| 890 | if (!copy_.has(value)) {
|
|---|
| 891 | const path = basePath.concat([i]);
|
|---|
| 892 | patches.push({
|
|---|
| 893 | op: REMOVE,
|
|---|
| 894 | path,
|
|---|
| 895 | value
|
|---|
| 896 | });
|
|---|
| 897 | inversePatches.unshift({
|
|---|
| 898 | op: ADD,
|
|---|
| 899 | path,
|
|---|
| 900 | value
|
|---|
| 901 | });
|
|---|
| 902 | }
|
|---|
| 903 | i++;
|
|---|
| 904 | });
|
|---|
| 905 | i = 0;
|
|---|
| 906 | copy_.forEach((value) => {
|
|---|
| 907 | if (!base_.has(value)) {
|
|---|
| 908 | const path = basePath.concat([i]);
|
|---|
| 909 | patches.push({
|
|---|
| 910 | op: ADD,
|
|---|
| 911 | path,
|
|---|
| 912 | value
|
|---|
| 913 | });
|
|---|
| 914 | inversePatches.unshift({
|
|---|
| 915 | op: REMOVE,
|
|---|
| 916 | path,
|
|---|
| 917 | value
|
|---|
| 918 | });
|
|---|
| 919 | }
|
|---|
| 920 | i++;
|
|---|
| 921 | });
|
|---|
| 922 | }
|
|---|
| 923 | function generateReplacementPatches_(baseValue, replacement, patches, inversePatches) {
|
|---|
| 924 | patches.push({
|
|---|
| 925 | op: REPLACE,
|
|---|
| 926 | path: [],
|
|---|
| 927 | value: replacement === NOTHING ? void 0 : replacement
|
|---|
| 928 | });
|
|---|
| 929 | inversePatches.push({
|
|---|
| 930 | op: REPLACE,
|
|---|
| 931 | path: [],
|
|---|
| 932 | value: baseValue
|
|---|
| 933 | });
|
|---|
| 934 | }
|
|---|
| 935 | function applyPatches_(draft, patches) {
|
|---|
| 936 | patches.forEach((patch) => {
|
|---|
| 937 | const { path, op } = patch;
|
|---|
| 938 | let base = draft;
|
|---|
| 939 | for (let i = 0; i < path.length - 1; i++) {
|
|---|
| 940 | const parentType = getArchtype(base);
|
|---|
| 941 | let p = path[i];
|
|---|
| 942 | if (typeof p !== "string" && typeof p !== "number") {
|
|---|
| 943 | p = "" + p;
|
|---|
| 944 | }
|
|---|
| 945 | if ((parentType === 0 /* Object */ || parentType === 1 /* Array */) && (p === "__proto__" || p === "constructor"))
|
|---|
| 946 | die(errorOffset + 3);
|
|---|
| 947 | if (typeof base === "function" && p === "prototype")
|
|---|
| 948 | die(errorOffset + 3);
|
|---|
| 949 | base = get(base, p);
|
|---|
| 950 | if (typeof base !== "object")
|
|---|
| 951 | die(errorOffset + 2, path.join("/"));
|
|---|
| 952 | }
|
|---|
| 953 | const type = getArchtype(base);
|
|---|
| 954 | const value = deepClonePatchValue(patch.value);
|
|---|
| 955 | const key = path[path.length - 1];
|
|---|
| 956 | switch (op) {
|
|---|
| 957 | case REPLACE:
|
|---|
| 958 | switch (type) {
|
|---|
| 959 | case 2 /* Map */:
|
|---|
| 960 | return base.set(key, value);
|
|---|
| 961 | case 3 /* Set */:
|
|---|
| 962 | die(errorOffset);
|
|---|
| 963 | default:
|
|---|
| 964 | return base[key] = value;
|
|---|
| 965 | }
|
|---|
| 966 | case ADD:
|
|---|
| 967 | switch (type) {
|
|---|
| 968 | case 1 /* Array */:
|
|---|
| 969 | return key === "-" ? base.push(value) : base.splice(key, 0, value);
|
|---|
| 970 | case 2 /* Map */:
|
|---|
| 971 | return base.set(key, value);
|
|---|
| 972 | case 3 /* Set */:
|
|---|
| 973 | return base.add(value);
|
|---|
| 974 | default:
|
|---|
| 975 | return base[key] = value;
|
|---|
| 976 | }
|
|---|
| 977 | case REMOVE:
|
|---|
| 978 | switch (type) {
|
|---|
| 979 | case 1 /* Array */:
|
|---|
| 980 | return base.splice(key, 1);
|
|---|
| 981 | case 2 /* Map */:
|
|---|
| 982 | return base.delete(key);
|
|---|
| 983 | case 3 /* Set */:
|
|---|
| 984 | return base.delete(patch.value);
|
|---|
| 985 | default:
|
|---|
| 986 | return delete base[key];
|
|---|
| 987 | }
|
|---|
| 988 | default:
|
|---|
| 989 | die(errorOffset + 1, op);
|
|---|
| 990 | }
|
|---|
| 991 | });
|
|---|
| 992 | return draft;
|
|---|
| 993 | }
|
|---|
| 994 | function deepClonePatchValue(obj) {
|
|---|
| 995 | if (!isDraftable(obj))
|
|---|
| 996 | return obj;
|
|---|
| 997 | if (Array.isArray(obj))
|
|---|
| 998 | return obj.map(deepClonePatchValue);
|
|---|
| 999 | if (isMap(obj))
|
|---|
| 1000 | return new Map(
|
|---|
| 1001 | Array.from(obj.entries()).map(([k, v]) => [k, deepClonePatchValue(v)])
|
|---|
| 1002 | );
|
|---|
| 1003 | if (isSet(obj))
|
|---|
| 1004 | return new Set(Array.from(obj).map(deepClonePatchValue));
|
|---|
| 1005 | const cloned = Object.create(getPrototypeOf(obj));
|
|---|
| 1006 | for (const key in obj)
|
|---|
| 1007 | cloned[key] = deepClonePatchValue(obj[key]);
|
|---|
| 1008 | if (has(obj, DRAFTABLE))
|
|---|
| 1009 | cloned[DRAFTABLE] = obj[DRAFTABLE];
|
|---|
| 1010 | return cloned;
|
|---|
| 1011 | }
|
|---|
| 1012 | function clonePatchValueIfNeeded(obj) {
|
|---|
| 1013 | if (isDraft(obj)) {
|
|---|
| 1014 | return deepClonePatchValue(obj);
|
|---|
| 1015 | } else
|
|---|
| 1016 | return obj;
|
|---|
| 1017 | }
|
|---|
| 1018 | loadPlugin("Patches", {
|
|---|
| 1019 | applyPatches_,
|
|---|
| 1020 | generatePatches_,
|
|---|
| 1021 | generateReplacementPatches_
|
|---|
| 1022 | });
|
|---|
| 1023 | }
|
|---|
| 1024 |
|
|---|
| 1025 | // src/plugins/mapset.ts
|
|---|
| 1026 | function enableMapSet() {
|
|---|
| 1027 | class DraftMap extends Map {
|
|---|
| 1028 | constructor(target, parent) {
|
|---|
| 1029 | super();
|
|---|
| 1030 | this[DRAFT_STATE] = {
|
|---|
| 1031 | type_: 2 /* Map */,
|
|---|
| 1032 | parent_: parent,
|
|---|
| 1033 | scope_: parent ? parent.scope_ : getCurrentScope(),
|
|---|
| 1034 | modified_: false,
|
|---|
| 1035 | finalized_: false,
|
|---|
| 1036 | copy_: void 0,
|
|---|
| 1037 | assigned_: void 0,
|
|---|
| 1038 | base_: target,
|
|---|
| 1039 | draft_: this,
|
|---|
| 1040 | isManual_: false,
|
|---|
| 1041 | revoked_: false
|
|---|
| 1042 | };
|
|---|
| 1043 | }
|
|---|
| 1044 | get size() {
|
|---|
| 1045 | return latest(this[DRAFT_STATE]).size;
|
|---|
| 1046 | }
|
|---|
| 1047 | has(key) {
|
|---|
| 1048 | return latest(this[DRAFT_STATE]).has(key);
|
|---|
| 1049 | }
|
|---|
| 1050 | set(key, value) {
|
|---|
| 1051 | const state = this[DRAFT_STATE];
|
|---|
| 1052 | assertUnrevoked(state);
|
|---|
| 1053 | if (!latest(state).has(key) || latest(state).get(key) !== value) {
|
|---|
| 1054 | prepareMapCopy(state);
|
|---|
| 1055 | markChanged(state);
|
|---|
| 1056 | state.assigned_.set(key, true);
|
|---|
| 1057 | state.copy_.set(key, value);
|
|---|
| 1058 | state.assigned_.set(key, true);
|
|---|
| 1059 | }
|
|---|
| 1060 | return this;
|
|---|
| 1061 | }
|
|---|
| 1062 | delete(key) {
|
|---|
| 1063 | if (!this.has(key)) {
|
|---|
| 1064 | return false;
|
|---|
| 1065 | }
|
|---|
| 1066 | const state = this[DRAFT_STATE];
|
|---|
| 1067 | assertUnrevoked(state);
|
|---|
| 1068 | prepareMapCopy(state);
|
|---|
| 1069 | markChanged(state);
|
|---|
| 1070 | if (state.base_.has(key)) {
|
|---|
| 1071 | state.assigned_.set(key, false);
|
|---|
| 1072 | } else {
|
|---|
| 1073 | state.assigned_.delete(key);
|
|---|
| 1074 | }
|
|---|
| 1075 | state.copy_.delete(key);
|
|---|
| 1076 | return true;
|
|---|
| 1077 | }
|
|---|
| 1078 | clear() {
|
|---|
| 1079 | const state = this[DRAFT_STATE];
|
|---|
| 1080 | assertUnrevoked(state);
|
|---|
| 1081 | if (latest(state).size) {
|
|---|
| 1082 | prepareMapCopy(state);
|
|---|
| 1083 | markChanged(state);
|
|---|
| 1084 | state.assigned_ = /* @__PURE__ */ new Map();
|
|---|
| 1085 | each(state.base_, (key) => {
|
|---|
| 1086 | state.assigned_.set(key, false);
|
|---|
| 1087 | });
|
|---|
| 1088 | state.copy_.clear();
|
|---|
| 1089 | }
|
|---|
| 1090 | }
|
|---|
| 1091 | forEach(cb, thisArg) {
|
|---|
| 1092 | const state = this[DRAFT_STATE];
|
|---|
| 1093 | latest(state).forEach((_value, key, _map) => {
|
|---|
| 1094 | cb.call(thisArg, this.get(key), key, this);
|
|---|
| 1095 | });
|
|---|
| 1096 | }
|
|---|
| 1097 | get(key) {
|
|---|
| 1098 | const state = this[DRAFT_STATE];
|
|---|
| 1099 | assertUnrevoked(state);
|
|---|
| 1100 | const value = latest(state).get(key);
|
|---|
| 1101 | if (state.finalized_ || !isDraftable(value)) {
|
|---|
| 1102 | return value;
|
|---|
| 1103 | }
|
|---|
| 1104 | if (value !== state.base_.get(key)) {
|
|---|
| 1105 | return value;
|
|---|
| 1106 | }
|
|---|
| 1107 | const draft = createProxy(value, state);
|
|---|
| 1108 | prepareMapCopy(state);
|
|---|
| 1109 | state.copy_.set(key, draft);
|
|---|
| 1110 | return draft;
|
|---|
| 1111 | }
|
|---|
| 1112 | keys() {
|
|---|
| 1113 | return latest(this[DRAFT_STATE]).keys();
|
|---|
| 1114 | }
|
|---|
| 1115 | values() {
|
|---|
| 1116 | const iterator = this.keys();
|
|---|
| 1117 | return {
|
|---|
| 1118 | [Symbol.iterator]: () => this.values(),
|
|---|
| 1119 | next: () => {
|
|---|
| 1120 | const r = iterator.next();
|
|---|
| 1121 | if (r.done)
|
|---|
| 1122 | return r;
|
|---|
| 1123 | const value = this.get(r.value);
|
|---|
| 1124 | return {
|
|---|
| 1125 | done: false,
|
|---|
| 1126 | value
|
|---|
| 1127 | };
|
|---|
| 1128 | }
|
|---|
| 1129 | };
|
|---|
| 1130 | }
|
|---|
| 1131 | entries() {
|
|---|
| 1132 | const iterator = this.keys();
|
|---|
| 1133 | return {
|
|---|
| 1134 | [Symbol.iterator]: () => this.entries(),
|
|---|
| 1135 | next: () => {
|
|---|
| 1136 | const r = iterator.next();
|
|---|
| 1137 | if (r.done)
|
|---|
| 1138 | return r;
|
|---|
| 1139 | const value = this.get(r.value);
|
|---|
| 1140 | return {
|
|---|
| 1141 | done: false,
|
|---|
| 1142 | value: [r.value, value]
|
|---|
| 1143 | };
|
|---|
| 1144 | }
|
|---|
| 1145 | };
|
|---|
| 1146 | }
|
|---|
| 1147 | [(DRAFT_STATE, Symbol.iterator)]() {
|
|---|
| 1148 | return this.entries();
|
|---|
| 1149 | }
|
|---|
| 1150 | }
|
|---|
| 1151 | function proxyMap_(target, parent) {
|
|---|
| 1152 | return new DraftMap(target, parent);
|
|---|
| 1153 | }
|
|---|
| 1154 | function prepareMapCopy(state) {
|
|---|
| 1155 | if (!state.copy_) {
|
|---|
| 1156 | state.assigned_ = /* @__PURE__ */ new Map();
|
|---|
| 1157 | state.copy_ = new Map(state.base_);
|
|---|
| 1158 | }
|
|---|
| 1159 | }
|
|---|
| 1160 | class DraftSet extends Set {
|
|---|
| 1161 | constructor(target, parent) {
|
|---|
| 1162 | super();
|
|---|
| 1163 | this[DRAFT_STATE] = {
|
|---|
| 1164 | type_: 3 /* Set */,
|
|---|
| 1165 | parent_: parent,
|
|---|
| 1166 | scope_: parent ? parent.scope_ : getCurrentScope(),
|
|---|
| 1167 | modified_: false,
|
|---|
| 1168 | finalized_: false,
|
|---|
| 1169 | copy_: void 0,
|
|---|
| 1170 | base_: target,
|
|---|
| 1171 | draft_: this,
|
|---|
| 1172 | drafts_: /* @__PURE__ */ new Map(),
|
|---|
| 1173 | revoked_: false,
|
|---|
| 1174 | isManual_: false
|
|---|
| 1175 | };
|
|---|
| 1176 | }
|
|---|
| 1177 | get size() {
|
|---|
| 1178 | return latest(this[DRAFT_STATE]).size;
|
|---|
| 1179 | }
|
|---|
| 1180 | has(value) {
|
|---|
| 1181 | const state = this[DRAFT_STATE];
|
|---|
| 1182 | assertUnrevoked(state);
|
|---|
| 1183 | if (!state.copy_) {
|
|---|
| 1184 | return state.base_.has(value);
|
|---|
| 1185 | }
|
|---|
| 1186 | if (state.copy_.has(value))
|
|---|
| 1187 | return true;
|
|---|
| 1188 | if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value)))
|
|---|
| 1189 | return true;
|
|---|
| 1190 | return false;
|
|---|
| 1191 | }
|
|---|
| 1192 | add(value) {
|
|---|
| 1193 | const state = this[DRAFT_STATE];
|
|---|
| 1194 | assertUnrevoked(state);
|
|---|
| 1195 | if (!this.has(value)) {
|
|---|
| 1196 | prepareSetCopy(state);
|
|---|
| 1197 | markChanged(state);
|
|---|
| 1198 | state.copy_.add(value);
|
|---|
| 1199 | }
|
|---|
| 1200 | return this;
|
|---|
| 1201 | }
|
|---|
| 1202 | delete(value) {
|
|---|
| 1203 | if (!this.has(value)) {
|
|---|
| 1204 | return false;
|
|---|
| 1205 | }
|
|---|
| 1206 | const state = this[DRAFT_STATE];
|
|---|
| 1207 | assertUnrevoked(state);
|
|---|
| 1208 | prepareSetCopy(state);
|
|---|
| 1209 | markChanged(state);
|
|---|
| 1210 | return state.copy_.delete(value) || (state.drafts_.has(value) ? state.copy_.delete(state.drafts_.get(value)) : (
|
|---|
| 1211 | /* istanbul ignore next */
|
|---|
| 1212 | false
|
|---|
| 1213 | ));
|
|---|
| 1214 | }
|
|---|
| 1215 | clear() {
|
|---|
| 1216 | const state = this[DRAFT_STATE];
|
|---|
| 1217 | assertUnrevoked(state);
|
|---|
| 1218 | if (latest(state).size) {
|
|---|
| 1219 | prepareSetCopy(state);
|
|---|
| 1220 | markChanged(state);
|
|---|
| 1221 | state.copy_.clear();
|
|---|
| 1222 | }
|
|---|
| 1223 | }
|
|---|
| 1224 | values() {
|
|---|
| 1225 | const state = this[DRAFT_STATE];
|
|---|
| 1226 | assertUnrevoked(state);
|
|---|
| 1227 | prepareSetCopy(state);
|
|---|
| 1228 | return state.copy_.values();
|
|---|
| 1229 | }
|
|---|
| 1230 | entries() {
|
|---|
| 1231 | const state = this[DRAFT_STATE];
|
|---|
| 1232 | assertUnrevoked(state);
|
|---|
| 1233 | prepareSetCopy(state);
|
|---|
| 1234 | return state.copy_.entries();
|
|---|
| 1235 | }
|
|---|
| 1236 | keys() {
|
|---|
| 1237 | return this.values();
|
|---|
| 1238 | }
|
|---|
| 1239 | [(DRAFT_STATE, Symbol.iterator)]() {
|
|---|
| 1240 | return this.values();
|
|---|
| 1241 | }
|
|---|
| 1242 | forEach(cb, thisArg) {
|
|---|
| 1243 | const iterator = this.values();
|
|---|
| 1244 | let result = iterator.next();
|
|---|
| 1245 | while (!result.done) {
|
|---|
| 1246 | cb.call(thisArg, result.value, result.value, this);
|
|---|
| 1247 | result = iterator.next();
|
|---|
| 1248 | }
|
|---|
| 1249 | }
|
|---|
| 1250 | }
|
|---|
| 1251 | function proxySet_(target, parent) {
|
|---|
| 1252 | return new DraftSet(target, parent);
|
|---|
| 1253 | }
|
|---|
| 1254 | function prepareSetCopy(state) {
|
|---|
| 1255 | if (!state.copy_) {
|
|---|
| 1256 | state.copy_ = /* @__PURE__ */ new Set();
|
|---|
| 1257 | state.base_.forEach((value) => {
|
|---|
| 1258 | if (isDraftable(value)) {
|
|---|
| 1259 | const draft = createProxy(value, state);
|
|---|
| 1260 | state.drafts_.set(value, draft);
|
|---|
| 1261 | state.copy_.add(draft);
|
|---|
| 1262 | } else {
|
|---|
| 1263 | state.copy_.add(value);
|
|---|
| 1264 | }
|
|---|
| 1265 | });
|
|---|
| 1266 | }
|
|---|
| 1267 | }
|
|---|
| 1268 | function assertUnrevoked(state) {
|
|---|
| 1269 | if (state.revoked_)
|
|---|
| 1270 | die(3, JSON.stringify(latest(state)));
|
|---|
| 1271 | }
|
|---|
| 1272 | loadPlugin("MapSet", { proxyMap_, proxySet_ });
|
|---|
| 1273 | }
|
|---|
| 1274 |
|
|---|
| 1275 | // src/immer.ts
|
|---|
| 1276 | var immer = new Immer2();
|
|---|
| 1277 | var produce = immer.produce;
|
|---|
| 1278 | var produceWithPatches = /* @__PURE__ */ immer.produceWithPatches.bind(
|
|---|
| 1279 | immer
|
|---|
| 1280 | );
|
|---|
| 1281 | var setAutoFreeze = /* @__PURE__ */ immer.setAutoFreeze.bind(immer);
|
|---|
| 1282 | var setUseStrictShallowCopy = /* @__PURE__ */ immer.setUseStrictShallowCopy.bind(
|
|---|
| 1283 | immer
|
|---|
| 1284 | );
|
|---|
| 1285 | var setUseStrictIteration = /* @__PURE__ */ immer.setUseStrictIteration.bind(
|
|---|
| 1286 | immer
|
|---|
| 1287 | );
|
|---|
| 1288 | var applyPatches = /* @__PURE__ */ immer.applyPatches.bind(immer);
|
|---|
| 1289 | var createDraft = /* @__PURE__ */ immer.createDraft.bind(immer);
|
|---|
| 1290 | var finishDraft = /* @__PURE__ */ immer.finishDraft.bind(immer);
|
|---|
| 1291 | function castDraft(value) {
|
|---|
| 1292 | return value;
|
|---|
| 1293 | }
|
|---|
| 1294 | function castImmutable(value) {
|
|---|
| 1295 | return value;
|
|---|
| 1296 | }
|
|---|
| 1297 | export {
|
|---|
| 1298 | Immer2 as Immer,
|
|---|
| 1299 | applyPatches,
|
|---|
| 1300 | castDraft,
|
|---|
| 1301 | castImmutable,
|
|---|
| 1302 | createDraft,
|
|---|
| 1303 | current,
|
|---|
| 1304 | enableMapSet,
|
|---|
| 1305 | enablePatches,
|
|---|
| 1306 | finishDraft,
|
|---|
| 1307 | freeze,
|
|---|
| 1308 | DRAFTABLE as immerable,
|
|---|
| 1309 | isDraft,
|
|---|
| 1310 | isDraftable,
|
|---|
| 1311 | NOTHING as nothing,
|
|---|
| 1312 | original,
|
|---|
| 1313 | produce,
|
|---|
| 1314 | produceWithPatches,
|
|---|
| 1315 | setAutoFreeze,
|
|---|
| 1316 | setUseStrictIteration,
|
|---|
| 1317 | setUseStrictShallowCopy
|
|---|
| 1318 | };
|
|---|
| 1319 | //# sourceMappingURL=immer.legacy-esm.js.map |
|---|