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