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